]>
diplodocus.org Git - nmh/blob - uip/refile.c
cb9cac373fa27472bb4455dd59bcdc8eb02ee516
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_free.h"
11 #include "sbr/folder_delmsgs.h"
12 #include "sbr/folder_addmsg.h"
13 #include "sbr/context_save.h"
14 #include "sbr/context_replace.h"
15 #include "sbr/context_find.h"
16 #include "sbr/ambigsw.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/seq_getnum.h"
21 #include "sbr/seq_add.h"
22 #include "sbr/arglist.h"
23 #include "sbr/error.h"
26 #include "sbr/m_maildir.h"
27 #include "sbr/m_mktemp.h"
30 #define REFILE_SWITCHES \
31 X("draft", 0, DRAFTSW) \
32 X("link", 0, LINKSW) \
33 X("nolink", 0, NLINKSW) \
34 X("preserve", 0, PRESSW) \
35 X("nopreserve", 0, NPRESSW) \
36 X("retainsequences", 0, RETAINSEQSSW) \
37 X("noretainsequences", 0, NRETAINSEQSSW) \
38 X("unlink", 0, UNLINKSW) \
39 X("nounlink", 0, NUNLINKSW) \
40 X("src +folder", 0, SRCSW) \
41 X("file file", 0, FILESW) \
42 X("rmmproc program", 0, RPROCSW) \
43 X("normmproc", 0, NRPRCSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(REFILE
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
55 static char maildir
[BUFSIZ
];
65 static void opnfolds (struct msgs
*, struct st_fold
*, int);
66 static void clsfolds (struct st_fold
*, int);
67 static void remove_files (int, char **);
68 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
69 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
73 main (int argc
, char **argv
)
76 bool preserve
= false;
77 bool retainseqs
= false;
79 int foldp
= 0, isdf
= 0;
80 bool unlink_msgs
= false;
82 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
83 char **argp
, **arguments
;
84 char *filevec
[NFOLDERS
+ 2];
85 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
86 struct st_fold folders
[NFOLDERS
+ 1];
87 struct msgs_array msgs
= { 0, 0, NULL
};
90 if (nmh_init(argv
[0], true, true)) { return 1; }
92 arguments
= getarguments (invo_name
, argc
, argv
, 1);
98 while ((cp
= *argp
++)) {
100 switch (smatch (++cp
, switches
)) {
102 ambigsw (cp
, switches
);
105 die("-%s unknown\n", cp
);
108 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
110 print_help (buf
, switches
, 1);
113 print_version(invo_name
);
146 die("only one source folder at a time!");
147 if (!(cp
= *argp
++) || *cp
== '-')
148 die("missing argument to %s", argp
[-2]);
149 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
150 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
153 if (filep
> NFOLDERS
)
154 die("only %d files allowed!", NFOLDERS
);
156 files
[filep
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
159 if (filep
> NFOLDERS
)
160 die("only %d files allowed!", NFOLDERS
);
161 if (!(cp
= *argp
++) || *cp
== '-')
162 die("missing argument to %s", argp
[-2]);
163 files
[filep
++] = path (cp
, TFILE
);
167 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
168 die("missing argument to %s", argp
[-2]);
175 if (*cp
== '+' || *cp
== '@') {
176 if (foldp
> NFOLDERS
)
177 die("only %d folders allowed!", NFOLDERS
);
178 folders
[foldp
++].f_name
=
181 app_msgarg(&msgs
, cp
);
184 if (!context_find ("path"))
185 free (path ("./", TFOLDER
));
187 die("no folder specified");
190 * We are refiling a file to the folders
193 if (folder
|| msgs
.size
)
194 die("use -file or some messages, not both");
195 opnfolds (NULL
, folders
, foldp
);
196 for (i
= 0; i
< filep
; i
++)
197 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
199 /* If -nolink, then "remove" files */
201 remove_files (filep
, filevec
);
206 app_msgarg(&msgs
, "cur");
208 folder
= getfolder (1);
209 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
211 if (chdir (maildir
) == NOTOK
)
212 adios (maildir
, "unable to change directory to");
214 /* read source folder and create message structure */
215 if (!(mp
= folder_read (folder
, 1)))
216 die("unable to read folder %s", folder
);
218 /* check for empty folder */
220 die("no messages in %s", folder
);
222 /* parse the message range/sequence/name and set SELECTED */
223 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
224 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
226 seq_setprev (mp
); /* set the previous-sequence */
228 /* create folder structures for each destination folder */
229 opnfolds (mp
, folders
, foldp
);
231 /* Link all the selected messages into destination folders.
233 * This causes the add hook to be run for messages that are
234 * linked into another folder. The refile hook is run for
235 * messages that are moved to another folder.
237 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
238 if (is_selected (mp
, msgnum
)) {
239 cp
= mh_xstrdup(m_name (msgnum
));
240 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
248 * Update this now, since folder_delmsgs() will save the context
249 * but doesn't have access to the folder name.
252 context_replace (pfolder
, folder
); /* update current folder */
255 * Adjust "cur" if necessary
258 if (mp
->hghsel
!= mp
->curmsg
259 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
260 seq_setcur (mp
, mp
->hghsel
);
263 * Close destination folders now; if we are using private sequences
264 * we need to have all of our calls to seq_save() complete before we
265 * call context_save().
268 clsfolds (folders
, foldp
);
270 /* If -nolink, then "remove" messages from source folder.
272 * Note that folder_delmsgs does not call the delete hook
273 * because the message has already been handled above.
276 folder_delmsgs (mp
, unlink_msgs
, 1);
278 seq_save (mp
); /* synchronize message sequences */
279 context_save (); /* save the context file */
282 folder_free (mp
); /* free folder structure */
289 * Read all the destination folders and
290 * create folder structures for all of them.
294 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
296 char nmaildir
[BUFSIZ
];
297 struct st_fold
*fp
, *ep
;
300 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
301 if (chdir (m_maildir ("")) < 0) {
302 advise (m_maildir (""), "chdir");
304 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
307 * Null src_folder indicates that we are refiling a file to
308 * the folders, in which case we don't want to short-circuit
309 * fp->f_mp to any "source folder".
311 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
312 create_folder (nmaildir
, 0, done
);
314 if (chdir (nmaildir
) == NOTOK
)
315 adios (nmaildir
, "unable to change directory to");
316 if (!(mp
= folder_read (fp
->f_name
, 1)))
317 die("unable to read folder %s", fp
->f_name
);
322 /* Source and destination folders are the same. */
323 fp
->f_mp
= src_folder
;
326 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
327 advise (maildir
, "chdir");
334 * Set the Previous-Sequence and then synchronize the
335 * sequence file, for each destination folder.
339 clsfolds (struct st_fold
*folders
, int nfolders
)
341 struct st_fold
*fp
, *ep
;
344 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
353 * If you have a "rmmproc" defined, we called that
354 * to remove all the specified files. If "rmmproc"
355 * is not defined, then just unlink the files.
359 remove_files (int filep
, char **files
)
362 char **vec
, *program
;
364 /* If rmmproc is defined, we use that */
366 vec
= argsplit(rmmproc
, &program
, &vecp
);
367 files
++; /* Yes, we need to do this */
368 for (i
= 0; i
< filep
; i
++)
369 vec
[vecp
++] = files
[i
];
370 vec
[vecp
] = NULL
; /* NULL terminate list */
373 execvp (program
, vec
);
374 adios (rmmproc
, "unable to exec");
377 /* Else just unlink the files */
378 files
++; /* advance past filevec[0] */
379 for (i
= 0; i
< filep
; i
++) {
380 if (m_unlink (files
[i
]) == NOTOK
)
381 admonish (files
[i
], "unable to unlink");
387 * Link (or copy) the message into each of the destination folders.
388 * If oldmsgnum is not 0, call copy_seqs().
392 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
393 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
396 struct st_fold
*fp
, *ep
;
398 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
400 * With same source and destination folder, don't indicate that
401 * the new message is selected so that 1) folder_delmsgs() doesn't
402 * delete it later and 2) it is not reflected in mp->hghsel, and
403 * therefore won't be assigned to be the current message.
405 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
407 0, preserve
, nfolders
== 1 && refile
,
410 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
417 * Copy sequence information for a refiled message to its
418 * new folder. Skip the cur sequence.
421 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
426 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
427 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
429 if (strcmp (current
, *seq
)) {
430 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
431 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
432 seq_addmsg (newmp
, *seq
, newmsgnum
,
433 !is_seq_private (oldmp
, seqnum
), 0);