]>
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/folder_read.h"
11 #include "sbr/folder_free.h"
12 #include "sbr/folder_delmsgs.h"
13 #include "sbr/folder_addmsg.h"
14 #include "sbr/context_save.h"
15 #include "sbr/context_replace.h"
16 #include "sbr/context_find.h"
17 #include "sbr/ambigsw.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/seq_getnum.h"
22 #include "sbr/seq_add.h"
23 #include "sbr/arglist.h"
24 #include "sbr/error.h"
27 #include "sbr/m_maildir.h"
28 #include "sbr/m_mktemp.h"
31 #define REFILE_SWITCHES \
32 X("draft", 0, DRAFTSW) \
33 X("link", 0, LINKSW) \
34 X("nolink", 0, NLINKSW) \
35 X("preserve", 0, PRESSW) \
36 X("nopreserve", 0, NPRESSW) \
37 X("retainsequences", 0, RETAINSEQSSW) \
38 X("noretainsequences", 0, NRETAINSEQSSW) \
39 X("unlink", 0, UNLINKSW) \
40 X("nounlink", 0, NUNLINKSW) \
41 X("src +folder", 0, SRCSW) \
42 X("file file", 0, FILESW) \
43 X("rmmproc program", 0, RPROCSW) \
44 X("normmproc", 0, NRPRCSW) \
45 X("version", 0, VERSIONSW) \
46 X("help", 0, HELPSW) \
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(REFILE
);
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
56 static char maildir
[BUFSIZ
];
66 static void opnfolds (struct msgs
*, struct st_fold
*, int);
67 static void clsfolds (struct st_fold
*, int);
68 static void remove_files (int, char **);
69 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
70 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
74 main (int argc
, char **argv
)
77 bool preserve
= false;
78 bool retainseqs
= false;
80 int foldp
= 0, isdf
= 0;
81 bool unlink_msgs
= false;
83 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
84 char **argp
, **arguments
;
85 char *filevec
[NFOLDERS
+ 2];
86 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
87 struct st_fold folders
[NFOLDERS
+ 1];
88 struct msgs_array msgs
= { 0, 0, NULL
};
91 if (nmh_init(argv
[0], true, true)) { return 1; }
93 arguments
= getarguments (invo_name
, argc
, argv
, 1);
99 while ((cp
= *argp
++)) {
101 switch (smatch (++cp
, switches
)) {
103 ambigsw (cp
, switches
);
106 die("-%s unknown\n", cp
);
109 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
111 print_help (buf
, switches
, 1);
114 print_version(invo_name
);
147 die("only one source folder at a time!");
148 if (!(cp
= *argp
++) || *cp
== '-')
149 die("missing argument to %s", argp
[-2]);
150 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
151 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
154 if (filep
> NFOLDERS
)
155 die("only %d files allowed!", NFOLDERS
);
157 files
[filep
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
160 if (filep
> NFOLDERS
)
161 die("only %d files allowed!", NFOLDERS
);
162 if (!(cp
= *argp
++) || *cp
== '-')
163 die("missing argument to %s", argp
[-2]);
164 files
[filep
++] = path (cp
, TFILE
);
168 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
169 die("missing argument to %s", argp
[-2]);
176 if (*cp
== '+' || *cp
== '@') {
177 if (foldp
> NFOLDERS
)
178 die("only %d folders allowed!", NFOLDERS
);
179 folders
[foldp
++].f_name
=
182 app_msgarg(&msgs
, cp
);
185 if (!context_find ("path"))
186 free (path ("./", TFOLDER
));
188 die("no folder specified");
191 * We are refiling a file to the folders
194 if (folder
|| msgs
.size
)
195 die("use -file or some messages, not both");
196 opnfolds (NULL
, folders
, foldp
);
197 for (i
= 0; i
< filep
; i
++)
198 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
200 /* If -nolink, then "remove" files */
202 remove_files (filep
, filevec
);
207 app_msgarg(&msgs
, "cur");
209 folder
= getfolder (1);
210 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
212 if (chdir (maildir
) == NOTOK
)
213 adios (maildir
, "unable to change directory to");
215 /* read source folder and create message structure */
216 if (!(mp
= folder_read (folder
, 1)))
217 die("unable to read folder %s", folder
);
219 /* check for empty folder */
221 die("no messages in %s", folder
);
223 /* parse the message range/sequence/name and set SELECTED */
224 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
225 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
227 seq_setprev (mp
); /* set the previous-sequence */
229 /* create folder structures for each destination folder */
230 opnfolds (mp
, folders
, foldp
);
232 /* Link all the selected messages into destination folders.
234 * This causes the add hook to be run for messages that are
235 * linked into another folder. The refile hook is run for
236 * messages that are moved to another folder.
238 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
239 if (is_selected (mp
, msgnum
)) {
240 cp
= mh_xstrdup(m_name (msgnum
));
241 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
249 * Update this now, since folder_delmsgs() will save the context
250 * but doesn't have access to the folder name.
253 context_replace (pfolder
, folder
); /* update current folder */
256 * Adjust "cur" if necessary
259 if (mp
->hghsel
!= mp
->curmsg
260 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
261 seq_setcur (mp
, mp
->hghsel
);
264 * Close destination folders now; if we are using private sequences
265 * we need to have all of our calls to seq_save() complete before we
266 * call context_save().
269 clsfolds (folders
, foldp
);
271 /* If -nolink, then "remove" messages from source folder.
273 * Note that folder_delmsgs does not call the delete hook
274 * because the message has already been handled above.
277 folder_delmsgs (mp
, unlink_msgs
, 1);
279 seq_save (mp
); /* synchronize message sequences */
280 context_save (); /* save the context file */
283 folder_free (mp
); /* free folder structure */
290 * Read all the destination folders and
291 * create folder structures for all of them.
295 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
297 char nmaildir
[BUFSIZ
];
298 struct st_fold
*fp
, *ep
;
301 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
302 if (chdir (m_maildir ("")) < 0) {
303 advise (m_maildir (""), "chdir");
305 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
308 * Null src_folder indicates that we are refiling a file to
309 * the folders, in which case we don't want to short-circuit
310 * fp->f_mp to any "source folder".
312 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
313 create_folder (nmaildir
, 0, done
);
315 if (chdir (nmaildir
) == NOTOK
)
316 adios (nmaildir
, "unable to change directory to");
317 if (!(mp
= folder_read (fp
->f_name
, 1)))
318 die("unable to read folder %s", fp
->f_name
);
323 /* Source and destination folders are the same. */
324 fp
->f_mp
= src_folder
;
327 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
328 advise (maildir
, "chdir");
335 * Set the Previous-Sequence and then synchronize the
336 * sequence file, for each destination folder.
340 clsfolds (struct st_fold
*folders
, int nfolders
)
342 struct st_fold
*fp
, *ep
;
345 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
354 * If you have a "rmmproc" defined, we called that
355 * to remove all the specified files. If "rmmproc"
356 * is not defined, then just unlink the files.
360 remove_files (int filep
, char **files
)
363 char **vec
, *program
;
365 /* If rmmproc is defined, we use that */
367 vec
= argsplit(rmmproc
, &program
, &vecp
);
368 files
++; /* Yes, we need to do this */
369 for (i
= 0; i
< filep
; i
++)
370 vec
[vecp
++] = files
[i
];
371 vec
[vecp
] = NULL
; /* NULL terminate list */
374 execvp (program
, vec
);
375 adios (rmmproc
, "unable to exec");
378 /* Else just unlink the files */
379 files
++; /* advance past filevec[0] */
380 for (i
= 0; i
< filep
; i
++) {
381 if (m_unlink (files
[i
]) == NOTOK
)
382 admonish (files
[i
], "unable to unlink");
388 * Link (or copy) the message into each of the destination folders.
389 * If oldmsgnum is not 0, call copy_seqs().
393 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
394 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
397 struct st_fold
*fp
, *ep
;
399 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
401 * With same source and destination folder, don't indicate that
402 * the new message is selected so that 1) folder_delmsgs() doesn't
403 * delete it later and 2) it is not reflected in mp->hghsel, and
404 * therefore won't be assigned to be the current message.
406 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
408 0, preserve
, nfolders
== 1 && refile
,
411 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
418 * Copy sequence information for a refiled message to its
419 * new folder. Skip the cur sequence.
422 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
427 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
428 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
430 if (strcmp (current
, *seq
)) {
431 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
432 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
433 seq_addmsg (newmp
, *seq
, newmsgnum
,
434 !is_seq_private (oldmp
, seqnum
), 0);