]>
diplodocus.org Git - nmh/blob - uip/refile.c
3 * refile.c -- move or link message(s) from a source folder
4 * -- into one or more destination folders
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
17 #define REFILE_SWITCHES \
18 X("draft", 0, DRAFTSW) \
19 X("link", 0, LINKSW) \
20 X("nolink", 0, NLINKSW) \
21 X("preserve", 0, PRESSW) \
22 X("nopreserve", 0, NPRESSW) \
23 X("retainsequences", 0, RETAINSEQSSW) \
24 X("noretainsequences", 0, NRETAINSEQSSW) \
25 X("unlink", 0, UNLINKSW) \
26 X("nounlink", 0, NUNLINKSW) \
27 X("src +folder", 0, SRCSW) \
28 X("file file", 0, FILESW) \
29 X("rmmproc program", 0, RPROCSW) \
30 X("normmproc", 0, NRPRCSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
34 #define X(sw, minchars, id) id,
35 DEFINE_SWITCH_ENUM(REFILE
);
38 #define X(sw, minchars, id) { sw, minchars, id },
39 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
42 static char maildir
[BUFSIZ
];
52 static void opnfolds (struct st_fold
*, int);
53 static void clsfolds (struct st_fold
*, int);
54 static void remove_files (int, char **);
55 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
56 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
60 main (int argc
, char **argv
)
62 int linkf
= 0, preserve
= 0, retainseqs
= 0, filep
= 0;
63 int foldp
= 0, isdf
= 0, unlink_msgs
= 0;
65 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
66 char **argp
, **arguments
;
67 char *filevec
[NFOLDERS
+ 2];
68 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
69 struct st_fold folders
[NFOLDERS
+ 1];
70 struct msgs_array msgs
= { 0, 0, NULL
};
74 setlocale(LC_ALL
, "");
76 invo_name
= r1bindex (argv
[0], '/');
78 /* read user profile/context */
81 arguments
= getarguments (invo_name
, argc
, argv
, 1);
87 while ((cp
= *argp
++)) {
89 switch (smatch (++cp
, switches
)) {
91 ambigsw (cp
, switches
);
94 adios (NULL
, "-%s unknown\n", cp
);
97 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
99 print_help (buf
, switches
, 1);
102 print_version(invo_name
);
135 adios (NULL
, "only one source folder at a time!");
136 if (!(cp
= *argp
++) || *cp
== '-')
137 adios (NULL
, "missing argument to %s", argp
[-2]);
138 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
139 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
142 if (filep
> NFOLDERS
)
143 adios (NULL
, "only %d files allowed!", NFOLDERS
);
145 files
[filep
++] = getcpy (m_draft (NULL
, NULL
, 1, &isdf
));
148 if (filep
> NFOLDERS
)
149 adios (NULL
, "only %d files allowed!", NFOLDERS
);
150 if (!(cp
= *argp
++) || *cp
== '-')
151 adios (NULL
, "missing argument to %s", argp
[-2]);
152 files
[filep
++] = path (cp
, TFILE
);
156 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
157 adios (NULL
, "missing argument to %s", argp
[-2]);
164 if (*cp
== '+' || *cp
== '@') {
165 if (foldp
> NFOLDERS
)
166 adios (NULL
, "only %d folders allowed!", NFOLDERS
);
167 folders
[foldp
++].f_name
=
170 app_msgarg(&msgs
, cp
);
173 if (!context_find ("path"))
174 free (path ("./", TFOLDER
));
176 adios (NULL
, "no folder specified");
179 if (!msgs
.size
&& !foldp
&& !filep
&& (cp
= getenv ("mhdraft")) && *cp
)
184 * We are refiling a file to the folders
187 if (folder
|| msgs
.size
)
188 adios (NULL
, "use -file or some messages, not both");
189 opnfolds (folders
, foldp
);
190 for (i
= 0; i
< filep
; i
++)
191 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
193 /* If -nolink, then "remove" files */
195 remove_files (filep
, filevec
);
200 app_msgarg(&msgs
, "cur");
202 folder
= getfolder (1);
203 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
205 if (chdir (maildir
) == NOTOK
)
206 adios (maildir
, "unable to change directory to");
208 /* read source folder and create message structure */
209 if (!(mp
= folder_read (folder
, 1)))
210 adios (NULL
, "unable to read folder %s", folder
);
212 /* check for empty folder */
214 adios (NULL
, "no messages in %s", folder
);
216 /* parse the message range/sequence/name and set SELECTED */
217 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
218 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
220 seq_setprev (mp
); /* set the previous-sequence */
222 /* create folder structures for each destination folder */
223 opnfolds (folders
, foldp
);
225 /* Link all the selected messages into destination folders.
227 * This causes the add hook to be run for messages that are
228 * linked into another folder. The refile hook is run for
229 * messages that are moved to another folder.
231 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
232 if (is_selected (mp
, msgnum
)) {
233 cp
= getcpy (m_name (msgnum
));
234 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
242 * Update this now, since folder_delmsgs() will save the context
243 * but doesn't have access to the folder name.
246 context_replace (pfolder
, folder
); /* update current folder */
249 * Adjust "cur" if necessary
252 if (mp
->hghsel
!= mp
->curmsg
253 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
254 seq_setcur (mp
, mp
->hghsel
);
256 /* If -nolink, then "remove" messages from source folder.
258 * Note that folder_delmsgs does not call the delete hook
259 * because the message has already been handled above.
262 folder_delmsgs (mp
, unlink_msgs
, 1);
264 seq_save (mp
); /* synchronize message sequences */
265 context_save (); /* save the context file */
268 clsfolds (folders
, foldp
);
270 folder_free (mp
); /* free folder structure */
277 * Read all the destination folders and
278 * create folder structures for all of them.
282 opnfolds (struct st_fold
*folders
, int nfolders
)
284 char nmaildir
[BUFSIZ
];
285 register struct st_fold
*fp
, *ep
;
286 register struct msgs
*mp
;
288 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
289 chdir (m_maildir (""));
290 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
292 create_folder (nmaildir
, 0, done
);
294 if (chdir (nmaildir
) == NOTOK
)
295 adios (nmaildir
, "unable to change directory to");
296 if (!(mp
= folder_read (fp
->f_name
, 1)))
297 adios (NULL
, "unable to read folder %s", fp
->f_name
);
308 * Set the Previous-Sequence and then sychronize the
309 * sequence file, for each destination folder.
313 clsfolds (struct st_fold
*folders
, int nfolders
)
315 register struct st_fold
*fp
, *ep
;
316 register struct msgs
*mp
;
318 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
327 * If you have a "rmmproc" defined, we called that
328 * to remove all the specified files. If "rmmproc"
329 * is not defined, then just unlink the files.
333 remove_files (int filep
, char **files
)
336 char **vec
, *program
;
338 /* If rmmproc is defined, we use that */
340 vec
= argsplit(rmmproc
, &program
, &vecp
);
341 files
++; /* Yes, we need to do this */
342 for (i
= 0; i
< filep
; i
++)
343 vec
[vecp
++] = files
[i
];
344 vec
[vecp
] = NULL
; /* NULL terminate list */
347 execvp (program
, vec
);
348 adios (rmmproc
, "unable to exec");
351 /* Else just unlink the files */
352 files
++; /* advance past filevec[0] */
353 for (i
= 0; i
< filep
; i
++) {
354 if (unlink (files
[i
]) == NOTOK
)
355 admonish (files
[i
], "unable to unlink");
361 * Link (or copy) the message into each of the destination folders.
362 * If oldmsgnum is not 0, call copy_seqs().
366 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
367 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
370 struct st_fold
*fp
, *ep
;
372 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
373 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
, 1, 0, preserve
, nfolders
== 1 && refile
, maildir
)) == -1)
375 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
382 * Copy sequence information for a refiled message to its
383 * new folder. Skip the cur sequence.
386 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
391 for (seq
= oldmp
->msgattrs
, seqnum
= 0; *seq
; ++seq
, ++seqnum
) {
392 if (strcmp (current
, *seq
)) {
393 assert (seqnum
== seq_getnum (oldmp
, *seq
));
394 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
395 seq_addmsg (newmp
, *seq
, newmsgnum
,
396 is_seq_private (oldmp
, seqnum
) ? 0 : 1, 0);