]>
diplodocus.org Git - nmh/blob - uip/refile.c
58de202cc8a725a8a92faba7d9895c58752c8ec6
1 /* refile.c -- move or link message(s) from a source folder
2 * -- into one or more destination folders
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
10 #include "sbr/arglist.h"
11 #include "sbr/error.h"
14 #include "sbr/m_maildir.h"
15 #include "sbr/m_mktemp.h"
18 #define REFILE_SWITCHES \
19 X("draft", 0, DRAFTSW) \
20 X("link", 0, LINKSW) \
21 X("nolink", 0, NLINKSW) \
22 X("preserve", 0, PRESSW) \
23 X("nopreserve", 0, NPRESSW) \
24 X("retainsequences", 0, RETAINSEQSSW) \
25 X("noretainsequences", 0, NRETAINSEQSSW) \
26 X("unlink", 0, UNLINKSW) \
27 X("nounlink", 0, NUNLINKSW) \
28 X("src +folder", 0, SRCSW) \
29 X("file file", 0, FILESW) \
30 X("rmmproc program", 0, RPROCSW) \
31 X("normmproc", 0, NRPRCSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(REFILE
);
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
43 static char maildir
[BUFSIZ
];
53 static void opnfolds (struct msgs
*, struct st_fold
*, int);
54 static void clsfolds (struct st_fold
*, int);
55 static void remove_files (int, char **);
56 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
57 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
61 main (int argc
, char **argv
)
64 bool preserve
= false;
65 bool retainseqs
= false;
67 int foldp
= 0, isdf
= 0;
68 bool unlink_msgs
= false;
70 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
71 char **argp
, **arguments
;
72 char *filevec
[NFOLDERS
+ 2];
73 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
74 struct st_fold folders
[NFOLDERS
+ 1];
75 struct msgs_array msgs
= { 0, 0, NULL
};
78 if (nmh_init(argv
[0], true, true)) { return 1; }
80 arguments
= getarguments (invo_name
, argc
, argv
, 1);
86 while ((cp
= *argp
++)) {
88 switch (smatch (++cp
, switches
)) {
90 ambigsw (cp
, switches
);
93 die("-%s unknown\n", cp
);
96 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
98 print_help (buf
, switches
, 1);
101 print_version(invo_name
);
134 die("only one source folder at a time!");
135 if (!(cp
= *argp
++) || *cp
== '-')
136 die("missing argument to %s", argp
[-2]);
137 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
138 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
141 if (filep
> NFOLDERS
)
142 die("only %d files allowed!", NFOLDERS
);
144 files
[filep
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
147 if (filep
> NFOLDERS
)
148 die("only %d files allowed!", NFOLDERS
);
149 if (!(cp
= *argp
++) || *cp
== '-')
150 die("missing argument to %s", argp
[-2]);
151 files
[filep
++] = path (cp
, TFILE
);
155 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
156 die("missing argument to %s", argp
[-2]);
163 if (*cp
== '+' || *cp
== '@') {
164 if (foldp
> NFOLDERS
)
165 die("only %d folders allowed!", NFOLDERS
);
166 folders
[foldp
++].f_name
=
169 app_msgarg(&msgs
, cp
);
172 if (!context_find ("path"))
173 free (path ("./", TFOLDER
));
175 die("no folder specified");
178 * We are refiling a file to the folders
181 if (folder
|| msgs
.size
)
182 die("use -file or some messages, not both");
183 opnfolds (NULL
, folders
, foldp
);
184 for (i
= 0; i
< filep
; i
++)
185 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
187 /* If -nolink, then "remove" files */
189 remove_files (filep
, filevec
);
194 app_msgarg(&msgs
, "cur");
196 folder
= getfolder (1);
197 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
199 if (chdir (maildir
) == NOTOK
)
200 adios (maildir
, "unable to change directory to");
202 /* read source folder and create message structure */
203 if (!(mp
= folder_read (folder
, 1)))
204 die("unable to read folder %s", folder
);
206 /* check for empty folder */
208 die("no messages in %s", folder
);
210 /* parse the message range/sequence/name and set SELECTED */
211 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
212 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
214 seq_setprev (mp
); /* set the previous-sequence */
216 /* create folder structures for each destination folder */
217 opnfolds (mp
, folders
, foldp
);
219 /* Link all the selected messages into destination folders.
221 * This causes the add hook to be run for messages that are
222 * linked into another folder. The refile hook is run for
223 * messages that are moved to another folder.
225 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
226 if (is_selected (mp
, msgnum
)) {
227 cp
= mh_xstrdup(m_name (msgnum
));
228 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
236 * Update this now, since folder_delmsgs() will save the context
237 * but doesn't have access to the folder name.
240 context_replace (pfolder
, folder
); /* update current folder */
243 * Adjust "cur" if necessary
246 if (mp
->hghsel
!= mp
->curmsg
247 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
248 seq_setcur (mp
, mp
->hghsel
);
251 * Close destination folders now; if we are using private sequences
252 * we need to have all of our calls to seq_save() complete before we
253 * call context_save().
256 clsfolds (folders
, foldp
);
258 /* If -nolink, then "remove" messages from source folder.
260 * Note that folder_delmsgs does not call the delete hook
261 * because the message has already been handled above.
264 folder_delmsgs (mp
, unlink_msgs
, 1);
266 seq_save (mp
); /* synchronize message sequences */
267 context_save (); /* save the context file */
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 msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
284 char nmaildir
[BUFSIZ
];
285 struct st_fold
*fp
, *ep
;
288 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
289 if (chdir (m_maildir ("")) < 0) {
290 advise (m_maildir (""), "chdir");
292 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
295 * Null src_folder indicates that we are refiling a file to
296 * the folders, in which case we don't want to short-circuit
297 * fp->f_mp to any "source folder".
299 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
300 create_folder (nmaildir
, 0, done
);
302 if (chdir (nmaildir
) == NOTOK
)
303 adios (nmaildir
, "unable to change directory to");
304 if (!(mp
= folder_read (fp
->f_name
, 1)))
305 die("unable to read folder %s", fp
->f_name
);
310 /* Source and destination folders are the same. */
311 fp
->f_mp
= src_folder
;
314 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
315 advise (maildir
, "chdir");
322 * Set the Previous-Sequence and then synchronize the
323 * sequence file, for each destination folder.
327 clsfolds (struct st_fold
*folders
, int nfolders
)
329 struct st_fold
*fp
, *ep
;
332 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
341 * If you have a "rmmproc" defined, we called that
342 * to remove all the specified files. If "rmmproc"
343 * is not defined, then just unlink the files.
347 remove_files (int filep
, char **files
)
350 char **vec
, *program
;
352 /* If rmmproc is defined, we use that */
354 vec
= argsplit(rmmproc
, &program
, &vecp
);
355 files
++; /* Yes, we need to do this */
356 for (i
= 0; i
< filep
; i
++)
357 vec
[vecp
++] = files
[i
];
358 vec
[vecp
] = NULL
; /* NULL terminate list */
361 execvp (program
, vec
);
362 adios (rmmproc
, "unable to exec");
365 /* Else just unlink the files */
366 files
++; /* advance past filevec[0] */
367 for (i
= 0; i
< filep
; i
++) {
368 if (m_unlink (files
[i
]) == NOTOK
)
369 admonish (files
[i
], "unable to unlink");
375 * Link (or copy) the message into each of the destination folders.
376 * If oldmsgnum is not 0, call copy_seqs().
380 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
381 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
384 struct st_fold
*fp
, *ep
;
386 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
388 * With same source and destination folder, don't indicate that
389 * the new message is selected so that 1) folder_delmsgs() doesn't
390 * delete it later and 2) it is not reflected in mp->hghsel, and
391 * therefore won't be assigned to be the current message.
393 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
395 0, preserve
, nfolders
== 1 && refile
,
398 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
405 * Copy sequence information for a refiled message to its
406 * new folder. Skip the cur sequence.
409 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
414 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
415 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
417 if (strcmp (current
, *seq
)) {
418 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
419 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
420 seq_addmsg (newmp
, *seq
, newmsgnum
,
421 !is_seq_private (oldmp
, seqnum
), 0);