]>
diplodocus.org Git - nmh/blob - uip/refile.c
28d7ab1fd961a4182e15ce6770f46765e8c94e11
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/folder_addmsg.h"
11 #include "sbr/context_save.h"
12 #include "sbr/context_replace.h"
13 #include "sbr/context_find.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/seq_getnum.h"
19 #include "sbr/seq_add.h"
20 #include "sbr/arglist.h"
21 #include "sbr/error.h"
24 #include "sbr/m_maildir.h"
25 #include "sbr/m_mktemp.h"
28 #define REFILE_SWITCHES \
29 X("draft", 0, DRAFTSW) \
30 X("link", 0, LINKSW) \
31 X("nolink", 0, NLINKSW) \
32 X("preserve", 0, PRESSW) \
33 X("nopreserve", 0, NPRESSW) \
34 X("retainsequences", 0, RETAINSEQSSW) \
35 X("noretainsequences", 0, NRETAINSEQSSW) \
36 X("unlink", 0, UNLINKSW) \
37 X("nounlink", 0, NUNLINKSW) \
38 X("src +folder", 0, SRCSW) \
39 X("file file", 0, FILESW) \
40 X("rmmproc program", 0, RPROCSW) \
41 X("normmproc", 0, NRPRCSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(REFILE
);
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
53 static char maildir
[BUFSIZ
];
63 static void opnfolds (struct msgs
*, struct st_fold
*, int);
64 static void clsfolds (struct st_fold
*, int);
65 static void remove_files (int, char **);
66 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
67 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
71 main (int argc
, char **argv
)
74 bool preserve
= false;
75 bool retainseqs
= false;
77 int foldp
= 0, isdf
= 0;
78 bool unlink_msgs
= false;
80 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
81 char **argp
, **arguments
;
82 char *filevec
[NFOLDERS
+ 2];
83 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
84 struct st_fold folders
[NFOLDERS
+ 1];
85 struct msgs_array msgs
= { 0, 0, NULL
};
88 if (nmh_init(argv
[0], true, true)) { return 1; }
90 arguments
= getarguments (invo_name
, argc
, argv
, 1);
96 while ((cp
= *argp
++)) {
98 switch (smatch (++cp
, switches
)) {
100 ambigsw (cp
, switches
);
103 die("-%s unknown\n", cp
);
106 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
108 print_help (buf
, switches
, 1);
111 print_version(invo_name
);
144 die("only one source folder at a time!");
145 if (!(cp
= *argp
++) || *cp
== '-')
146 die("missing argument to %s", argp
[-2]);
147 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
148 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
151 if (filep
> NFOLDERS
)
152 die("only %d files allowed!", NFOLDERS
);
154 files
[filep
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
157 if (filep
> NFOLDERS
)
158 die("only %d files allowed!", NFOLDERS
);
159 if (!(cp
= *argp
++) || *cp
== '-')
160 die("missing argument to %s", argp
[-2]);
161 files
[filep
++] = path (cp
, TFILE
);
165 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
166 die("missing argument to %s", argp
[-2]);
173 if (*cp
== '+' || *cp
== '@') {
174 if (foldp
> NFOLDERS
)
175 die("only %d folders allowed!", NFOLDERS
);
176 folders
[foldp
++].f_name
=
179 app_msgarg(&msgs
, cp
);
182 if (!context_find ("path"))
183 free (path ("./", TFOLDER
));
185 die("no folder specified");
188 * We are refiling a file to the folders
191 if (folder
|| msgs
.size
)
192 die("use -file or some messages, not both");
193 opnfolds (NULL
, folders
, foldp
);
194 for (i
= 0; i
< filep
; i
++)
195 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
197 /* If -nolink, then "remove" files */
199 remove_files (filep
, filevec
);
204 app_msgarg(&msgs
, "cur");
206 folder
= getfolder (1);
207 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
209 if (chdir (maildir
) == NOTOK
)
210 adios (maildir
, "unable to change directory to");
212 /* read source folder and create message structure */
213 if (!(mp
= folder_read (folder
, 1)))
214 die("unable to read folder %s", folder
);
216 /* check for empty folder */
218 die("no messages in %s", folder
);
220 /* parse the message range/sequence/name and set SELECTED */
221 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
222 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
224 seq_setprev (mp
); /* set the previous-sequence */
226 /* create folder structures for each destination folder */
227 opnfolds (mp
, folders
, foldp
);
229 /* Link all the selected messages into destination folders.
231 * This causes the add hook to be run for messages that are
232 * linked into another folder. The refile hook is run for
233 * messages that are moved to another folder.
235 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
236 if (is_selected (mp
, msgnum
)) {
237 cp
= mh_xstrdup(m_name (msgnum
));
238 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
246 * Update this now, since folder_delmsgs() will save the context
247 * but doesn't have access to the folder name.
250 context_replace (pfolder
, folder
); /* update current folder */
253 * Adjust "cur" if necessary
256 if (mp
->hghsel
!= mp
->curmsg
257 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
258 seq_setcur (mp
, mp
->hghsel
);
261 * Close destination folders now; if we are using private sequences
262 * we need to have all of our calls to seq_save() complete before we
263 * call context_save().
266 clsfolds (folders
, foldp
);
268 /* If -nolink, then "remove" messages from source folder.
270 * Note that folder_delmsgs does not call the delete hook
271 * because the message has already been handled above.
274 folder_delmsgs (mp
, unlink_msgs
, 1);
276 seq_save (mp
); /* synchronize message sequences */
277 context_save (); /* save the context file */
280 folder_free (mp
); /* free folder structure */
287 * Read all the destination folders and
288 * create folder structures for all of them.
292 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
294 char nmaildir
[BUFSIZ
];
295 struct st_fold
*fp
, *ep
;
298 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
299 if (chdir (m_maildir ("")) < 0) {
300 advise (m_maildir (""), "chdir");
302 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
305 * Null src_folder indicates that we are refiling a file to
306 * the folders, in which case we don't want to short-circuit
307 * fp->f_mp to any "source folder".
309 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
310 create_folder (nmaildir
, 0, done
);
312 if (chdir (nmaildir
) == NOTOK
)
313 adios (nmaildir
, "unable to change directory to");
314 if (!(mp
= folder_read (fp
->f_name
, 1)))
315 die("unable to read folder %s", fp
->f_name
);
320 /* Source and destination folders are the same. */
321 fp
->f_mp
= src_folder
;
324 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
325 advise (maildir
, "chdir");
332 * Set the Previous-Sequence and then synchronize the
333 * sequence file, for each destination folder.
337 clsfolds (struct st_fold
*folders
, int nfolders
)
339 struct st_fold
*fp
, *ep
;
342 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
351 * If you have a "rmmproc" defined, we called that
352 * to remove all the specified files. If "rmmproc"
353 * is not defined, then just unlink the files.
357 remove_files (int filep
, char **files
)
360 char **vec
, *program
;
362 /* If rmmproc is defined, we use that */
364 vec
= argsplit(rmmproc
, &program
, &vecp
);
365 files
++; /* Yes, we need to do this */
366 for (i
= 0; i
< filep
; i
++)
367 vec
[vecp
++] = files
[i
];
368 vec
[vecp
] = NULL
; /* NULL terminate list */
371 execvp (program
, vec
);
372 adios (rmmproc
, "unable to exec");
375 /* Else just unlink the files */
376 files
++; /* advance past filevec[0] */
377 for (i
= 0; i
< filep
; i
++) {
378 if (m_unlink (files
[i
]) == NOTOK
)
379 admonish (files
[i
], "unable to unlink");
385 * Link (or copy) the message into each of the destination folders.
386 * If oldmsgnum is not 0, call copy_seqs().
390 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
391 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
394 struct st_fold
*fp
, *ep
;
396 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
398 * With same source and destination folder, don't indicate that
399 * the new message is selected so that 1) folder_delmsgs() doesn't
400 * delete it later and 2) it is not reflected in mp->hghsel, and
401 * therefore won't be assigned to be the current message.
403 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
405 0, preserve
, nfolders
== 1 && refile
,
408 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
415 * Copy sequence information for a refiled message to its
416 * new folder. Skip the cur sequence.
419 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
424 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
425 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
427 if (strcmp (current
, *seq
)) {
428 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
429 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
430 seq_addmsg (newmp
, *seq
, newmsgnum
,
431 !is_seq_private (oldmp
, seqnum
), 0);