]>
diplodocus.org Git - nmh/blob - uip/refile.c
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/m_name.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_setcur.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/smatch.h"
16 #include "sbr/m_draft.h"
17 #include "sbr/m_convert.h"
18 #include "sbr/getfolder.h"
19 #include "sbr/folder_read.h"
20 #include "sbr/folder_free.h"
21 #include "sbr/folder_delmsgs.h"
22 #include "sbr/folder_addmsg.h"
23 #include "sbr/context_save.h"
24 #include "sbr/context_replace.h"
25 #include "sbr/context_find.h"
26 #include "sbr/ambigsw.h"
28 #include "sbr/print_version.h"
29 #include "sbr/print_help.h"
30 #include "sbr/seq_getnum.h"
31 #include "sbr/seq_add.h"
32 #include "sbr/arglist.h"
33 #include "sbr/error.h"
36 #include "sbr/m_maildir.h"
37 #include "sbr/m_mktemp.h"
40 #define REFILE_SWITCHES \
41 X("draft", 0, DRAFTSW) \
42 X("link", 0, LINKSW) \
43 X("nolink", 0, NLINKSW) \
44 X("preserve", 0, PRESSW) \
45 X("nopreserve", 0, NPRESSW) \
46 X("retainsequences", 0, RETAINSEQSSW) \
47 X("noretainsequences", 0, NRETAINSEQSSW) \
48 X("unlink", 0, UNLINKSW) \
49 X("nounlink", 0, NUNLINKSW) \
50 X("src +folder", 0, SRCSW) \
51 X("file file", 0, FILESW) \
52 X("rmmproc program", 0, RPROCSW) \
53 X("normmproc", 0, NRPRCSW) \
54 X("version", 0, VERSIONSW) \
55 X("help", 0, HELPSW) \
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(REFILE
);
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
65 static char maildir
[BUFSIZ
];
75 static void opnfolds (struct msgs
*, struct st_fold
*, int);
76 static void clsfolds (struct st_fold
*, int);
77 static void remove_files (int, char **);
78 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
79 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
83 main (int argc
, char **argv
)
86 bool preserve
= false;
87 bool retainseqs
= false;
89 int foldp
= 0, isdf
= 0;
90 bool unlink_msgs
= false;
92 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
93 char **argp
, **arguments
;
94 char *filevec
[NFOLDERS
+ 2];
95 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
96 struct st_fold folders
[NFOLDERS
+ 1];
97 struct msgs_array msgs
= { 0, 0, NULL
};
100 if (nmh_init(argv
[0], true, true)) { return 1; }
102 arguments
= getarguments (invo_name
, argc
, argv
, 1);
108 while ((cp
= *argp
++)) {
110 switch (smatch (++cp
, switches
)) {
112 ambigsw (cp
, switches
);
115 die("-%s unknown\n", cp
);
118 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
120 print_help (buf
, switches
, 1);
123 print_version(invo_name
);
156 die("only one source folder at a time!");
157 if (!(cp
= *argp
++) || *cp
== '-')
158 die("missing argument to %s", argp
[-2]);
159 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
160 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
163 if (filep
> NFOLDERS
)
164 die("only %d files allowed!", NFOLDERS
);
166 files
[filep
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
169 if (filep
> NFOLDERS
)
170 die("only %d files allowed!", NFOLDERS
);
171 if (!(cp
= *argp
++) || *cp
== '-')
172 die("missing argument to %s", argp
[-2]);
173 files
[filep
++] = path (cp
, TFILE
);
177 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
178 die("missing argument to %s", argp
[-2]);
185 if (*cp
== '+' || *cp
== '@') {
186 if (foldp
> NFOLDERS
)
187 die("only %d folders allowed!", NFOLDERS
);
188 folders
[foldp
++].f_name
=
191 app_msgarg(&msgs
, cp
);
194 if (!context_find ("path"))
195 free (path ("./", TFOLDER
));
197 die("no folder specified");
200 * We are refiling a file to the folders
203 if (folder
|| msgs
.size
)
204 die("use -file or some messages, not both");
205 opnfolds (NULL
, folders
, foldp
);
206 for (i
= 0; i
< filep
; i
++)
207 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
209 /* If -nolink, then "remove" files */
211 remove_files (filep
, filevec
);
216 app_msgarg(&msgs
, "cur");
218 folder
= getfolder (1);
219 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
221 if (chdir (maildir
) == NOTOK
)
222 adios (maildir
, "unable to change directory to");
224 /* read source folder and create message structure */
225 if (!(mp
= folder_read (folder
, 1)))
226 die("unable to read folder %s", folder
);
228 /* check for empty folder */
230 die("no messages in %s", folder
);
232 /* parse the message range/sequence/name and set SELECTED */
233 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
234 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
236 seq_setprev (mp
); /* set the previous-sequence */
238 /* create folder structures for each destination folder */
239 opnfolds (mp
, folders
, foldp
);
241 /* Link all the selected messages into destination folders.
243 * This causes the add hook to be run for messages that are
244 * linked into another folder. The refile hook is run for
245 * messages that are moved to another folder.
247 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
248 if (is_selected (mp
, msgnum
)) {
249 cp
= mh_xstrdup(m_name (msgnum
));
250 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
258 * Update this now, since folder_delmsgs() will save the context
259 * but doesn't have access to the folder name.
262 context_replace (pfolder
, folder
); /* update current folder */
265 * Adjust "cur" if necessary
268 if (mp
->hghsel
!= mp
->curmsg
269 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
270 seq_setcur (mp
, mp
->hghsel
);
273 * Close destination folders now; if we are using private sequences
274 * we need to have all of our calls to seq_save() complete before we
275 * call context_save().
278 clsfolds (folders
, foldp
);
280 /* If -nolink, then "remove" messages from source folder.
282 * Note that folder_delmsgs does not call the delete hook
283 * because the message has already been handled above.
286 folder_delmsgs (mp
, unlink_msgs
, 1);
288 seq_save (mp
); /* synchronize message sequences */
289 context_save (); /* save the context file */
292 folder_free (mp
); /* free folder structure */
299 * Read all the destination folders and
300 * create folder structures for all of them.
304 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
306 char nmaildir
[BUFSIZ
];
307 struct st_fold
*fp
, *ep
;
310 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
311 if (chdir (m_maildir ("")) < 0) {
312 advise (m_maildir (""), "chdir");
314 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
317 * Null src_folder indicates that we are refiling a file to
318 * the folders, in which case we don't want to short-circuit
319 * fp->f_mp to any "source folder".
321 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
322 create_folder (nmaildir
, 0, done
);
324 if (chdir (nmaildir
) == NOTOK
)
325 adios (nmaildir
, "unable to change directory to");
326 if (!(mp
= folder_read (fp
->f_name
, 1)))
327 die("unable to read folder %s", fp
->f_name
);
332 /* Source and destination folders are the same. */
333 fp
->f_mp
= src_folder
;
336 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
337 advise (maildir
, "chdir");
344 * Set the Previous-Sequence and then synchronize the
345 * sequence file, for each destination folder.
349 clsfolds (struct st_fold
*folders
, int nfolders
)
351 struct st_fold
*fp
, *ep
;
354 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
363 * If you have a "rmmproc" defined, we called that
364 * to remove all the specified files. If "rmmproc"
365 * is not defined, then just unlink the files.
369 remove_files (int filep
, char **files
)
372 char **vec
, *program
;
374 /* If rmmproc is defined, we use that */
376 vec
= argsplit(rmmproc
, &program
, &vecp
);
377 files
++; /* Yes, we need to do this */
378 for (i
= 0; i
< filep
; i
++)
379 vec
[vecp
++] = files
[i
];
380 vec
[vecp
] = NULL
; /* NULL terminate list */
383 execvp (program
, vec
);
384 adios (rmmproc
, "unable to exec");
387 /* Else just unlink the files */
388 files
++; /* advance past filevec[0] */
389 for (i
= 0; i
< filep
; i
++) {
390 if (m_unlink (files
[i
]) == NOTOK
)
391 admonish (files
[i
], "unable to unlink");
397 * Link (or copy) the message into each of the destination folders.
398 * If oldmsgnum is not 0, call copy_seqs().
402 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
403 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
406 struct st_fold
*fp
, *ep
;
408 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
410 * With same source and destination folder, don't indicate that
411 * the new message is selected so that 1) folder_delmsgs() doesn't
412 * delete it later and 2) it is not reflected in mp->hghsel, and
413 * therefore won't be assigned to be the current message.
415 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
417 0, preserve
, nfolders
== 1 && refile
,
420 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
427 * Copy sequence information for a refiled message to its
428 * new folder. Skip the cur sequence.
431 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
436 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
437 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
439 if (strcmp (current
, *seq
)) {
440 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
441 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
442 seq_addmsg (newmp
, *seq
, newmsgnum
,
443 !is_seq_private (oldmp
, seqnum
), 0);