]>
diplodocus.org Git - nmh/blob - uip/refile.c
7d08195b337b144c9aff10d1ab2ae081323a323d
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/context_find.h"
11 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/seq_getnum.h"
16 #include "sbr/seq_add.h"
17 #include "sbr/arglist.h"
18 #include "sbr/error.h"
21 #include "sbr/m_maildir.h"
22 #include "sbr/m_mktemp.h"
25 #define REFILE_SWITCHES \
26 X("draft", 0, DRAFTSW) \
27 X("link", 0, LINKSW) \
28 X("nolink", 0, NLINKSW) \
29 X("preserve", 0, PRESSW) \
30 X("nopreserve", 0, NPRESSW) \
31 X("retainsequences", 0, RETAINSEQSSW) \
32 X("noretainsequences", 0, NRETAINSEQSSW) \
33 X("unlink", 0, UNLINKSW) \
34 X("nounlink", 0, NUNLINKSW) \
35 X("src +folder", 0, SRCSW) \
36 X("file file", 0, FILESW) \
37 X("rmmproc program", 0, RPROCSW) \
38 X("normmproc", 0, NRPRCSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(REFILE
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
50 static char maildir
[BUFSIZ
];
60 static void opnfolds (struct msgs
*, struct st_fold
*, int);
61 static void clsfolds (struct st_fold
*, int);
62 static void remove_files (int, char **);
63 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
64 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
68 main (int argc
, char **argv
)
71 bool preserve
= false;
72 bool retainseqs
= false;
74 int foldp
= 0, isdf
= 0;
75 bool unlink_msgs
= false;
77 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
78 char **argp
, **arguments
;
79 char *filevec
[NFOLDERS
+ 2];
80 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
81 struct st_fold folders
[NFOLDERS
+ 1];
82 struct msgs_array msgs
= { 0, 0, NULL
};
85 if (nmh_init(argv
[0], true, true)) { return 1; }
87 arguments
= getarguments (invo_name
, argc
, argv
, 1);
93 while ((cp
= *argp
++)) {
95 switch (smatch (++cp
, switches
)) {
97 ambigsw (cp
, switches
);
100 die("-%s unknown\n", cp
);
103 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
105 print_help (buf
, switches
, 1);
108 print_version(invo_name
);
141 die("only one source folder at a time!");
142 if (!(cp
= *argp
++) || *cp
== '-')
143 die("missing argument to %s", argp
[-2]);
144 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
145 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
148 if (filep
> NFOLDERS
)
149 die("only %d files allowed!", NFOLDERS
);
151 files
[filep
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
154 if (filep
> NFOLDERS
)
155 die("only %d files allowed!", NFOLDERS
);
156 if (!(cp
= *argp
++) || *cp
== '-')
157 die("missing argument to %s", argp
[-2]);
158 files
[filep
++] = path (cp
, TFILE
);
162 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
163 die("missing argument to %s", argp
[-2]);
170 if (*cp
== '+' || *cp
== '@') {
171 if (foldp
> NFOLDERS
)
172 die("only %d folders allowed!", NFOLDERS
);
173 folders
[foldp
++].f_name
=
176 app_msgarg(&msgs
, cp
);
179 if (!context_find ("path"))
180 free (path ("./", TFOLDER
));
182 die("no folder specified");
185 * We are refiling a file to the folders
188 if (folder
|| msgs
.size
)
189 die("use -file or some messages, not both");
190 opnfolds (NULL
, folders
, foldp
);
191 for (i
= 0; i
< filep
; i
++)
192 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
194 /* If -nolink, then "remove" files */
196 remove_files (filep
, filevec
);
201 app_msgarg(&msgs
, "cur");
203 folder
= getfolder (1);
204 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
206 if (chdir (maildir
) == NOTOK
)
207 adios (maildir
, "unable to change directory to");
209 /* read source folder and create message structure */
210 if (!(mp
= folder_read (folder
, 1)))
211 die("unable to read folder %s", folder
);
213 /* check for empty folder */
215 die("no messages in %s", folder
);
217 /* parse the message range/sequence/name and set SELECTED */
218 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
219 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
221 seq_setprev (mp
); /* set the previous-sequence */
223 /* create folder structures for each destination folder */
224 opnfolds (mp
, folders
, foldp
);
226 /* Link all the selected messages into destination folders.
228 * This causes the add hook to be run for messages that are
229 * linked into another folder. The refile hook is run for
230 * messages that are moved to another folder.
232 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
233 if (is_selected (mp
, msgnum
)) {
234 cp
= mh_xstrdup(m_name (msgnum
));
235 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
243 * Update this now, since folder_delmsgs() will save the context
244 * but doesn't have access to the folder name.
247 context_replace (pfolder
, folder
); /* update current folder */
250 * Adjust "cur" if necessary
253 if (mp
->hghsel
!= mp
->curmsg
254 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
255 seq_setcur (mp
, mp
->hghsel
);
258 * Close destination folders now; if we are using private sequences
259 * we need to have all of our calls to seq_save() complete before we
260 * call context_save().
263 clsfolds (folders
, foldp
);
265 /* If -nolink, then "remove" messages from source folder.
267 * Note that folder_delmsgs does not call the delete hook
268 * because the message has already been handled above.
271 folder_delmsgs (mp
, unlink_msgs
, 1);
273 seq_save (mp
); /* synchronize message sequences */
274 context_save (); /* save the context file */
277 folder_free (mp
); /* free folder structure */
284 * Read all the destination folders and
285 * create folder structures for all of them.
289 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
291 char nmaildir
[BUFSIZ
];
292 struct st_fold
*fp
, *ep
;
295 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
296 if (chdir (m_maildir ("")) < 0) {
297 advise (m_maildir (""), "chdir");
299 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
302 * Null src_folder indicates that we are refiling a file to
303 * the folders, in which case we don't want to short-circuit
304 * fp->f_mp to any "source folder".
306 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
307 create_folder (nmaildir
, 0, done
);
309 if (chdir (nmaildir
) == NOTOK
)
310 adios (nmaildir
, "unable to change directory to");
311 if (!(mp
= folder_read (fp
->f_name
, 1)))
312 die("unable to read folder %s", fp
->f_name
);
317 /* Source and destination folders are the same. */
318 fp
->f_mp
= src_folder
;
321 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
322 advise (maildir
, "chdir");
329 * Set the Previous-Sequence and then synchronize the
330 * sequence file, for each destination folder.
334 clsfolds (struct st_fold
*folders
, int nfolders
)
336 struct st_fold
*fp
, *ep
;
339 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
348 * If you have a "rmmproc" defined, we called that
349 * to remove all the specified files. If "rmmproc"
350 * is not defined, then just unlink the files.
354 remove_files (int filep
, char **files
)
357 char **vec
, *program
;
359 /* If rmmproc is defined, we use that */
361 vec
= argsplit(rmmproc
, &program
, &vecp
);
362 files
++; /* Yes, we need to do this */
363 for (i
= 0; i
< filep
; i
++)
364 vec
[vecp
++] = files
[i
];
365 vec
[vecp
] = NULL
; /* NULL terminate list */
368 execvp (program
, vec
);
369 adios (rmmproc
, "unable to exec");
372 /* Else just unlink the files */
373 files
++; /* advance past filevec[0] */
374 for (i
= 0; i
< filep
; i
++) {
375 if (m_unlink (files
[i
]) == NOTOK
)
376 admonish (files
[i
], "unable to unlink");
382 * Link (or copy) the message into each of the destination folders.
383 * If oldmsgnum is not 0, call copy_seqs().
387 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
388 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
391 struct st_fold
*fp
, *ep
;
393 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
395 * With same source and destination folder, don't indicate that
396 * the new message is selected so that 1) folder_delmsgs() doesn't
397 * delete it later and 2) it is not reflected in mp->hghsel, and
398 * therefore won't be assigned to be the current message.
400 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
402 0, preserve
, nfolders
== 1 && refile
,
405 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
412 * Copy sequence information for a refiled message to its
413 * new folder. Skip the cur sequence.
416 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
421 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
422 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
424 if (strcmp (current
, *seq
)) {
425 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
426 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
427 seq_addmsg (newmp
, *seq
, newmsgnum
,
428 !is_seq_private (oldmp
, seqnum
), 0);