]>
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/seq_setprev.h"
11 #include "sbr/seq_setcur.h"
12 #include "sbr/seq_save.h"
13 #include "sbr/smatch.h"
14 #include "sbr/m_draft.h"
15 #include "sbr/m_convert.h"
16 #include "sbr/getfolder.h"
17 #include "sbr/folder_read.h"
18 #include "sbr/folder_free.h"
19 #include "sbr/folder_delmsgs.h"
20 #include "sbr/folder_addmsg.h"
21 #include "sbr/context_save.h"
22 #include "sbr/context_replace.h"
23 #include "sbr/context_find.h"
24 #include "sbr/ambigsw.h"
26 #include "sbr/print_version.h"
27 #include "sbr/print_help.h"
28 #include "sbr/seq_getnum.h"
29 #include "sbr/seq_add.h"
30 #include "sbr/arglist.h"
31 #include "sbr/error.h"
34 #include "sbr/m_maildir.h"
35 #include "sbr/m_mktemp.h"
38 #define REFILE_SWITCHES \
39 X("draft", 0, DRAFTSW) \
40 X("link", 0, LINKSW) \
41 X("nolink", 0, NLINKSW) \
42 X("preserve", 0, PRESSW) \
43 X("nopreserve", 0, NPRESSW) \
44 X("retainsequences", 0, RETAINSEQSSW) \
45 X("noretainsequences", 0, NRETAINSEQSSW) \
46 X("unlink", 0, UNLINKSW) \
47 X("nounlink", 0, NUNLINKSW) \
48 X("src +folder", 0, SRCSW) \
49 X("file file", 0, FILESW) \
50 X("rmmproc program", 0, RPROCSW) \
51 X("normmproc", 0, NRPRCSW) \
52 X("version", 0, VERSIONSW) \
53 X("help", 0, HELPSW) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(REFILE
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
63 static char maildir
[BUFSIZ
];
73 static void opnfolds (struct msgs
*, struct st_fold
*, int);
74 static void clsfolds (struct st_fold
*, int);
75 static void remove_files (int, char **);
76 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
77 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
81 main (int argc
, char **argv
)
84 bool preserve
= false;
85 bool retainseqs
= false;
87 int foldp
= 0, isdf
= 0;
88 bool unlink_msgs
= false;
90 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
91 char **argp
, **arguments
;
92 char *filevec
[NFOLDERS
+ 2];
93 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
94 struct st_fold folders
[NFOLDERS
+ 1];
95 struct msgs_array msgs
= { 0, 0, NULL
};
98 if (nmh_init(argv
[0], true, true)) { return 1; }
100 arguments
= getarguments (invo_name
, argc
, argv
, 1);
106 while ((cp
= *argp
++)) {
108 switch (smatch (++cp
, switches
)) {
110 ambigsw (cp
, switches
);
113 die("-%s unknown\n", cp
);
116 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
118 print_help (buf
, switches
, 1);
121 print_version(invo_name
);
154 die("only one source folder at a time!");
155 if (!(cp
= *argp
++) || *cp
== '-')
156 die("missing argument to %s", argp
[-2]);
157 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
158 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
161 if (filep
> NFOLDERS
)
162 die("only %d files allowed!", NFOLDERS
);
164 files
[filep
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
167 if (filep
> NFOLDERS
)
168 die("only %d files allowed!", NFOLDERS
);
169 if (!(cp
= *argp
++) || *cp
== '-')
170 die("missing argument to %s", argp
[-2]);
171 files
[filep
++] = path (cp
, TFILE
);
175 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
176 die("missing argument to %s", argp
[-2]);
183 if (*cp
== '+' || *cp
== '@') {
184 if (foldp
> NFOLDERS
)
185 die("only %d folders allowed!", NFOLDERS
);
186 folders
[foldp
++].f_name
=
189 app_msgarg(&msgs
, cp
);
192 if (!context_find ("path"))
193 free (path ("./", TFOLDER
));
195 die("no folder specified");
198 * We are refiling a file to the folders
201 if (folder
|| msgs
.size
)
202 die("use -file or some messages, not both");
203 opnfolds (NULL
, folders
, foldp
);
204 for (i
= 0; i
< filep
; i
++)
205 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
207 /* If -nolink, then "remove" files */
209 remove_files (filep
, filevec
);
214 app_msgarg(&msgs
, "cur");
216 folder
= getfolder (1);
217 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
219 if (chdir (maildir
) == NOTOK
)
220 adios (maildir
, "unable to change directory to");
222 /* read source folder and create message structure */
223 if (!(mp
= folder_read (folder
, 1)))
224 die("unable to read folder %s", folder
);
226 /* check for empty folder */
228 die("no messages in %s", folder
);
230 /* parse the message range/sequence/name and set SELECTED */
231 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
232 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
234 seq_setprev (mp
); /* set the previous-sequence */
236 /* create folder structures for each destination folder */
237 opnfolds (mp
, folders
, foldp
);
239 /* Link all the selected messages into destination folders.
241 * This causes the add hook to be run for messages that are
242 * linked into another folder. The refile hook is run for
243 * messages that are moved to another folder.
245 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
246 if (is_selected (mp
, msgnum
)) {
247 cp
= mh_xstrdup(m_name (msgnum
));
248 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
256 * Update this now, since folder_delmsgs() will save the context
257 * but doesn't have access to the folder name.
260 context_replace (pfolder
, folder
); /* update current folder */
263 * Adjust "cur" if necessary
266 if (mp
->hghsel
!= mp
->curmsg
267 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
268 seq_setcur (mp
, mp
->hghsel
);
271 * Close destination folders now; if we are using private sequences
272 * we need to have all of our calls to seq_save() complete before we
273 * call context_save().
276 clsfolds (folders
, foldp
);
278 /* If -nolink, then "remove" messages from source folder.
280 * Note that folder_delmsgs does not call the delete hook
281 * because the message has already been handled above.
284 folder_delmsgs (mp
, unlink_msgs
, 1);
286 seq_save (mp
); /* synchronize message sequences */
287 context_save (); /* save the context file */
290 folder_free (mp
); /* free folder structure */
297 * Read all the destination folders and
298 * create folder structures for all of them.
302 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
304 char nmaildir
[BUFSIZ
];
305 struct st_fold
*fp
, *ep
;
308 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
309 if (chdir (m_maildir ("")) < 0) {
310 advise (m_maildir (""), "chdir");
312 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
315 * Null src_folder indicates that we are refiling a file to
316 * the folders, in which case we don't want to short-circuit
317 * fp->f_mp to any "source folder".
319 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
320 create_folder (nmaildir
, 0, done
);
322 if (chdir (nmaildir
) == NOTOK
)
323 adios (nmaildir
, "unable to change directory to");
324 if (!(mp
= folder_read (fp
->f_name
, 1)))
325 die("unable to read folder %s", fp
->f_name
);
330 /* Source and destination folders are the same. */
331 fp
->f_mp
= src_folder
;
334 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
335 advise (maildir
, "chdir");
342 * Set the Previous-Sequence and then synchronize the
343 * sequence file, for each destination folder.
347 clsfolds (struct st_fold
*folders
, int nfolders
)
349 struct st_fold
*fp
, *ep
;
352 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
361 * If you have a "rmmproc" defined, we called that
362 * to remove all the specified files. If "rmmproc"
363 * is not defined, then just unlink the files.
367 remove_files (int filep
, char **files
)
370 char **vec
, *program
;
372 /* If rmmproc is defined, we use that */
374 vec
= argsplit(rmmproc
, &program
, &vecp
);
375 files
++; /* Yes, we need to do this */
376 for (i
= 0; i
< filep
; i
++)
377 vec
[vecp
++] = files
[i
];
378 vec
[vecp
] = NULL
; /* NULL terminate list */
381 execvp (program
, vec
);
382 adios (rmmproc
, "unable to exec");
385 /* Else just unlink the files */
386 files
++; /* advance past filevec[0] */
387 for (i
= 0; i
< filep
; i
++) {
388 if (m_unlink (files
[i
]) == NOTOK
)
389 admonish (files
[i
], "unable to unlink");
395 * Link (or copy) the message into each of the destination folders.
396 * If oldmsgnum is not 0, call copy_seqs().
400 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
401 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
404 struct st_fold
*fp
, *ep
;
406 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
408 * With same source and destination folder, don't indicate that
409 * the new message is selected so that 1) folder_delmsgs() doesn't
410 * delete it later and 2) it is not reflected in mp->hghsel, and
411 * therefore won't be assigned to be the current message.
413 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
415 0, preserve
, nfolders
== 1 && refile
,
418 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
425 * Copy sequence information for a refiled message to its
426 * new folder. Skip the cur sequence.
429 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
434 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
435 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
437 if (strcmp (current
, *seq
)) {
438 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
439 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
440 seq_addmsg (newmp
, *seq
, newmsgnum
,
441 !is_seq_private (oldmp
, seqnum
), 0);