]>
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.
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/seq_getnum.h"
14 #include "sbr/seq_add.h"
15 #include "sbr/arglist.h"
16 #include "sbr/error.h"
19 #include "sbr/m_maildir.h"
20 #include "sbr/m_mktemp.h"
23 #define REFILE_SWITCHES \
24 X("draft", 0, DRAFTSW) \
25 X("link", 0, LINKSW) \
26 X("nolink", 0, NLINKSW) \
27 X("preserve", 0, PRESSW) \
28 X("nopreserve", 0, NPRESSW) \
29 X("retainsequences", 0, RETAINSEQSSW) \
30 X("noretainsequences", 0, NRETAINSEQSSW) \
31 X("unlink", 0, UNLINKSW) \
32 X("nounlink", 0, NUNLINKSW) \
33 X("src +folder", 0, SRCSW) \
34 X("file file", 0, FILESW) \
35 X("rmmproc program", 0, RPROCSW) \
36 X("normmproc", 0, NRPRCSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
40 #define X(sw, minchars, id) id,
41 DEFINE_SWITCH_ENUM(REFILE
);
44 #define X(sw, minchars, id) { sw, minchars, id },
45 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
48 static char maildir
[BUFSIZ
];
58 static void opnfolds (struct msgs
*, struct st_fold
*, int);
59 static void clsfolds (struct st_fold
*, int);
60 static void remove_files (int, char **);
61 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
62 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
66 main (int argc
, char **argv
)
69 bool preserve
= false;
70 bool retainseqs
= false;
72 int foldp
= 0, isdf
= 0;
73 bool unlink_msgs
= false;
75 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
76 char **argp
, **arguments
;
77 char *filevec
[NFOLDERS
+ 2];
78 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
79 struct st_fold folders
[NFOLDERS
+ 1];
80 struct msgs_array msgs
= { 0, 0, NULL
};
83 if (nmh_init(argv
[0], true, true)) { return 1; }
85 arguments
= getarguments (invo_name
, argc
, argv
, 1);
91 while ((cp
= *argp
++)) {
93 switch (smatch (++cp
, switches
)) {
95 ambigsw (cp
, switches
);
98 die("-%s unknown\n", cp
);
101 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
103 print_help (buf
, switches
, 1);
106 print_version(invo_name
);
139 die("only one source folder at a time!");
140 if (!(cp
= *argp
++) || *cp
== '-')
141 die("missing argument to %s", argp
[-2]);
142 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
143 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
146 if (filep
> NFOLDERS
)
147 die("only %d files allowed!", NFOLDERS
);
149 files
[filep
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
152 if (filep
> NFOLDERS
)
153 die("only %d files allowed!", NFOLDERS
);
154 if (!(cp
= *argp
++) || *cp
== '-')
155 die("missing argument to %s", argp
[-2]);
156 files
[filep
++] = path (cp
, TFILE
);
160 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
161 die("missing argument to %s", argp
[-2]);
168 if (*cp
== '+' || *cp
== '@') {
169 if (foldp
> NFOLDERS
)
170 die("only %d folders allowed!", NFOLDERS
);
171 folders
[foldp
++].f_name
=
174 app_msgarg(&msgs
, cp
);
177 if (!context_find ("path"))
178 free (path ("./", TFOLDER
));
180 die("no folder specified");
183 * We are refiling a file to the folders
186 if (folder
|| msgs
.size
)
187 die("use -file or some messages, not both");
188 opnfolds (NULL
, folders
, foldp
);
189 for (i
= 0; i
< filep
; i
++)
190 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
192 /* If -nolink, then "remove" files */
194 remove_files (filep
, filevec
);
199 app_msgarg(&msgs
, "cur");
201 folder
= getfolder (1);
202 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
204 if (chdir (maildir
) == NOTOK
)
205 adios (maildir
, "unable to change directory to");
207 /* read source folder and create message structure */
208 if (!(mp
= folder_read (folder
, 1)))
209 die("unable to read folder %s", folder
);
211 /* check for empty folder */
213 die("no messages in %s", folder
);
215 /* parse the message range/sequence/name and set SELECTED */
216 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
217 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
219 seq_setprev (mp
); /* set the previous-sequence */
221 /* create folder structures for each destination folder */
222 opnfolds (mp
, folders
, foldp
);
224 /* Link all the selected messages into destination folders.
226 * This causes the add hook to be run for messages that are
227 * linked into another folder. The refile hook is run for
228 * messages that are moved to another folder.
230 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
231 if (is_selected (mp
, msgnum
)) {
232 cp
= mh_xstrdup(m_name (msgnum
));
233 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
241 * Update this now, since folder_delmsgs() will save the context
242 * but doesn't have access to the folder name.
245 context_replace (pfolder
, folder
); /* update current folder */
248 * Adjust "cur" if necessary
251 if (mp
->hghsel
!= mp
->curmsg
252 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
253 seq_setcur (mp
, mp
->hghsel
);
256 * Close destination folders now; if we are using private sequences
257 * we need to have all of our calls to seq_save() complete before we
258 * call context_save().
261 clsfolds (folders
, foldp
);
263 /* If -nolink, then "remove" messages from source folder.
265 * Note that folder_delmsgs does not call the delete hook
266 * because the message has already been handled above.
269 folder_delmsgs (mp
, unlink_msgs
, 1);
271 seq_save (mp
); /* synchronize message sequences */
272 context_save (); /* save the context file */
275 folder_free (mp
); /* free folder structure */
282 * Read all the destination folders and
283 * create folder structures for all of them.
287 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
289 char nmaildir
[BUFSIZ
];
290 struct st_fold
*fp
, *ep
;
293 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
294 if (chdir (m_maildir ("")) < 0) {
295 advise (m_maildir (""), "chdir");
297 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
300 * Null src_folder indicates that we are refiling a file to
301 * the folders, in which case we don't want to short-circuit
302 * fp->f_mp to any "source folder".
304 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
305 create_folder (nmaildir
, 0, done
);
307 if (chdir (nmaildir
) == NOTOK
)
308 adios (nmaildir
, "unable to change directory to");
309 if (!(mp
= folder_read (fp
->f_name
, 1)))
310 die("unable to read folder %s", fp
->f_name
);
315 /* Source and destination folders are the same. */
316 fp
->f_mp
= src_folder
;
319 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
320 advise (maildir
, "chdir");
327 * Set the Previous-Sequence and then synchronize the
328 * sequence file, for each destination folder.
332 clsfolds (struct st_fold
*folders
, int nfolders
)
334 struct st_fold
*fp
, *ep
;
337 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
346 * If you have a "rmmproc" defined, we called that
347 * to remove all the specified files. If "rmmproc"
348 * is not defined, then just unlink the files.
352 remove_files (int filep
, char **files
)
355 char **vec
, *program
;
357 /* If rmmproc is defined, we use that */
359 vec
= argsplit(rmmproc
, &program
, &vecp
);
360 files
++; /* Yes, we need to do this */
361 for (i
= 0; i
< filep
; i
++)
362 vec
[vecp
++] = files
[i
];
363 vec
[vecp
] = NULL
; /* NULL terminate list */
366 execvp (program
, vec
);
367 adios (rmmproc
, "unable to exec");
370 /* Else just unlink the files */
371 files
++; /* advance past filevec[0] */
372 for (i
= 0; i
< filep
; i
++) {
373 if (m_unlink (files
[i
]) == NOTOK
)
374 admonish (files
[i
], "unable to unlink");
380 * Link (or copy) the message into each of the destination folders.
381 * If oldmsgnum is not 0, call copy_seqs().
385 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
386 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
389 struct st_fold
*fp
, *ep
;
391 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
393 * With same source and destination folder, don't indicate that
394 * the new message is selected so that 1) folder_delmsgs() doesn't
395 * delete it later and 2) it is not reflected in mp->hghsel, and
396 * therefore won't be assigned to be the current message.
398 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
400 0, preserve
, nfolders
== 1 && refile
,
403 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
410 * Copy sequence information for a refiled message to its
411 * new folder. Skip the cur sequence.
414 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
419 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
420 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
422 if (strcmp (current
, *seq
)) {
423 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
424 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
425 seq_addmsg (newmp
, *seq
, newmsgnum
,
426 !is_seq_private (oldmp
, seqnum
), 0);