]>
diplodocus.org Git - nmh/blob - uip/refile.c
6daa7f259cac71b4475681485732847fb5a3cc26
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/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/seq_getnum.h"
13 #include "sbr/seq_add.h"
14 #include "sbr/arglist.h"
15 #include "sbr/error.h"
18 #include "sbr/m_maildir.h"
19 #include "sbr/m_mktemp.h"
22 #define REFILE_SWITCHES \
23 X("draft", 0, DRAFTSW) \
24 X("link", 0, LINKSW) \
25 X("nolink", 0, NLINKSW) \
26 X("preserve", 0, PRESSW) \
27 X("nopreserve", 0, NPRESSW) \
28 X("retainsequences", 0, RETAINSEQSSW) \
29 X("noretainsequences", 0, NRETAINSEQSSW) \
30 X("unlink", 0, UNLINKSW) \
31 X("nounlink", 0, NUNLINKSW) \
32 X("src +folder", 0, SRCSW) \
33 X("file file", 0, FILESW) \
34 X("rmmproc program", 0, RPROCSW) \
35 X("normmproc", 0, NRPRCSW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(REFILE
);
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
47 static char maildir
[BUFSIZ
];
57 static void opnfolds (struct msgs
*, struct st_fold
*, int);
58 static void clsfolds (struct st_fold
*, int);
59 static void remove_files (int, char **);
60 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
61 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
65 main (int argc
, char **argv
)
68 bool preserve
= false;
69 bool retainseqs
= false;
71 int foldp
= 0, isdf
= 0;
72 bool unlink_msgs
= false;
74 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
75 char **argp
, **arguments
;
76 char *filevec
[NFOLDERS
+ 2];
77 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
78 struct st_fold folders
[NFOLDERS
+ 1];
79 struct msgs_array msgs
= { 0, 0, NULL
};
82 if (nmh_init(argv
[0], true, true)) { return 1; }
84 arguments
= getarguments (invo_name
, argc
, argv
, 1);
90 while ((cp
= *argp
++)) {
92 switch (smatch (++cp
, switches
)) {
94 ambigsw (cp
, switches
);
97 die("-%s unknown\n", cp
);
100 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
102 print_help (buf
, switches
, 1);
105 print_version(invo_name
);
138 die("only one source folder at a time!");
139 if (!(cp
= *argp
++) || *cp
== '-')
140 die("missing argument to %s", argp
[-2]);
141 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
142 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
145 if (filep
> NFOLDERS
)
146 die("only %d files allowed!", NFOLDERS
);
148 files
[filep
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
151 if (filep
> NFOLDERS
)
152 die("only %d files allowed!", NFOLDERS
);
153 if (!(cp
= *argp
++) || *cp
== '-')
154 die("missing argument to %s", argp
[-2]);
155 files
[filep
++] = path (cp
, TFILE
);
159 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
160 die("missing argument to %s", argp
[-2]);
167 if (*cp
== '+' || *cp
== '@') {
168 if (foldp
> NFOLDERS
)
169 die("only %d folders allowed!", NFOLDERS
);
170 folders
[foldp
++].f_name
=
173 app_msgarg(&msgs
, cp
);
176 if (!context_find ("path"))
177 free (path ("./", TFOLDER
));
179 die("no folder specified");
182 * We are refiling a file to the folders
185 if (folder
|| msgs
.size
)
186 die("use -file or some messages, not both");
187 opnfolds (NULL
, folders
, foldp
);
188 for (i
= 0; i
< filep
; i
++)
189 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
191 /* If -nolink, then "remove" files */
193 remove_files (filep
, filevec
);
198 app_msgarg(&msgs
, "cur");
200 folder
= getfolder (1);
201 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
203 if (chdir (maildir
) == NOTOK
)
204 adios (maildir
, "unable to change directory to");
206 /* read source folder and create message structure */
207 if (!(mp
= folder_read (folder
, 1)))
208 die("unable to read folder %s", folder
);
210 /* check for empty folder */
212 die("no messages in %s", folder
);
214 /* parse the message range/sequence/name and set SELECTED */
215 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
216 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
218 seq_setprev (mp
); /* set the previous-sequence */
220 /* create folder structures for each destination folder */
221 opnfolds (mp
, folders
, foldp
);
223 /* Link all the selected messages into destination folders.
225 * This causes the add hook to be run for messages that are
226 * linked into another folder. The refile hook is run for
227 * messages that are moved to another folder.
229 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
230 if (is_selected (mp
, msgnum
)) {
231 cp
= mh_xstrdup(m_name (msgnum
));
232 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
240 * Update this now, since folder_delmsgs() will save the context
241 * but doesn't have access to the folder name.
244 context_replace (pfolder
, folder
); /* update current folder */
247 * Adjust "cur" if necessary
250 if (mp
->hghsel
!= mp
->curmsg
251 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
252 seq_setcur (mp
, mp
->hghsel
);
255 * Close destination folders now; if we are using private sequences
256 * we need to have all of our calls to seq_save() complete before we
257 * call context_save().
260 clsfolds (folders
, foldp
);
262 /* If -nolink, then "remove" messages from source folder.
264 * Note that folder_delmsgs does not call the delete hook
265 * because the message has already been handled above.
268 folder_delmsgs (mp
, unlink_msgs
, 1);
270 seq_save (mp
); /* synchronize message sequences */
271 context_save (); /* save the context file */
274 folder_free (mp
); /* free folder structure */
281 * Read all the destination folders and
282 * create folder structures for all of them.
286 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
288 char nmaildir
[BUFSIZ
];
289 struct st_fold
*fp
, *ep
;
292 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
293 if (chdir (m_maildir ("")) < 0) {
294 advise (m_maildir (""), "chdir");
296 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
299 * Null src_folder indicates that we are refiling a file to
300 * the folders, in which case we don't want to short-circuit
301 * fp->f_mp to any "source folder".
303 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
304 create_folder (nmaildir
, 0, done
);
306 if (chdir (nmaildir
) == NOTOK
)
307 adios (nmaildir
, "unable to change directory to");
308 if (!(mp
= folder_read (fp
->f_name
, 1)))
309 die("unable to read folder %s", fp
->f_name
);
314 /* Source and destination folders are the same. */
315 fp
->f_mp
= src_folder
;
318 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
319 advise (maildir
, "chdir");
326 * Set the Previous-Sequence and then synchronize the
327 * sequence file, for each destination folder.
331 clsfolds (struct st_fold
*folders
, int nfolders
)
333 struct st_fold
*fp
, *ep
;
336 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
345 * If you have a "rmmproc" defined, we called that
346 * to remove all the specified files. If "rmmproc"
347 * is not defined, then just unlink the files.
351 remove_files (int filep
, char **files
)
354 char **vec
, *program
;
356 /* If rmmproc is defined, we use that */
358 vec
= argsplit(rmmproc
, &program
, &vecp
);
359 files
++; /* Yes, we need to do this */
360 for (i
= 0; i
< filep
; i
++)
361 vec
[vecp
++] = files
[i
];
362 vec
[vecp
] = NULL
; /* NULL terminate list */
365 execvp (program
, vec
);
366 adios (rmmproc
, "unable to exec");
369 /* Else just unlink the files */
370 files
++; /* advance past filevec[0] */
371 for (i
= 0; i
< filep
; i
++) {
372 if (m_unlink (files
[i
]) == NOTOK
)
373 admonish (files
[i
], "unable to unlink");
379 * Link (or copy) the message into each of the destination folders.
380 * If oldmsgnum is not 0, call copy_seqs().
384 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
385 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
388 struct st_fold
*fp
, *ep
;
390 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
392 * With same source and destination folder, don't indicate that
393 * the new message is selected so that 1) folder_delmsgs() doesn't
394 * delete it later and 2) it is not reflected in mp->hghsel, and
395 * therefore won't be assigned to be the current message.
397 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
399 0, preserve
, nfolders
== 1 && refile
,
402 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
409 * Copy sequence information for a refiled message to its
410 * new folder. Skip the cur sequence.
413 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
418 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
419 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
421 if (strcmp (current
, *seq
)) {
422 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
423 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
424 seq_addmsg (newmp
, *seq
, newmsgnum
,
425 !is_seq_private (oldmp
, seqnum
), 0);