]>
diplodocus.org Git - nmh/blob - uip/refile.c
b0deb9ee34019ed2e1d23e55f1446180f9aa3691
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/print_help.h"
11 #include "sbr/seq_getnum.h"
12 #include "sbr/seq_add.h"
13 #include "sbr/arglist.h"
14 #include "sbr/error.h"
17 #include "sbr/m_maildir.h"
18 #include "sbr/m_mktemp.h"
21 #define REFILE_SWITCHES \
22 X("draft", 0, DRAFTSW) \
23 X("link", 0, LINKSW) \
24 X("nolink", 0, NLINKSW) \
25 X("preserve", 0, PRESSW) \
26 X("nopreserve", 0, NPRESSW) \
27 X("retainsequences", 0, RETAINSEQSSW) \
28 X("noretainsequences", 0, NRETAINSEQSSW) \
29 X("unlink", 0, UNLINKSW) \
30 X("nounlink", 0, NUNLINKSW) \
31 X("src +folder", 0, SRCSW) \
32 X("file file", 0, FILESW) \
33 X("rmmproc program", 0, RPROCSW) \
34 X("normmproc", 0, NRPRCSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(REFILE
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
46 static char maildir
[BUFSIZ
];
56 static void opnfolds (struct msgs
*, struct st_fold
*, int);
57 static void clsfolds (struct st_fold
*, int);
58 static void remove_files (int, char **);
59 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
60 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
64 main (int argc
, char **argv
)
67 bool preserve
= false;
68 bool retainseqs
= false;
70 int foldp
= 0, isdf
= 0;
71 bool unlink_msgs
= false;
73 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
74 char **argp
, **arguments
;
75 char *filevec
[NFOLDERS
+ 2];
76 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
77 struct st_fold folders
[NFOLDERS
+ 1];
78 struct msgs_array msgs
= { 0, 0, NULL
};
81 if (nmh_init(argv
[0], true, true)) { return 1; }
83 arguments
= getarguments (invo_name
, argc
, argv
, 1);
89 while ((cp
= *argp
++)) {
91 switch (smatch (++cp
, switches
)) {
93 ambigsw (cp
, switches
);
96 die("-%s unknown\n", cp
);
99 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
101 print_help (buf
, switches
, 1);
104 print_version(invo_name
);
137 die("only one source folder at a time!");
138 if (!(cp
= *argp
++) || *cp
== '-')
139 die("missing argument to %s", argp
[-2]);
140 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
141 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
144 if (filep
> NFOLDERS
)
145 die("only %d files allowed!", NFOLDERS
);
147 files
[filep
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
150 if (filep
> NFOLDERS
)
151 die("only %d files allowed!", NFOLDERS
);
152 if (!(cp
= *argp
++) || *cp
== '-')
153 die("missing argument to %s", argp
[-2]);
154 files
[filep
++] = path (cp
, TFILE
);
158 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
159 die("missing argument to %s", argp
[-2]);
166 if (*cp
== '+' || *cp
== '@') {
167 if (foldp
> NFOLDERS
)
168 die("only %d folders allowed!", NFOLDERS
);
169 folders
[foldp
++].f_name
=
172 app_msgarg(&msgs
, cp
);
175 if (!context_find ("path"))
176 free (path ("./", TFOLDER
));
178 die("no folder specified");
181 * We are refiling a file to the folders
184 if (folder
|| msgs
.size
)
185 die("use -file or some messages, not both");
186 opnfolds (NULL
, folders
, foldp
);
187 for (i
= 0; i
< filep
; i
++)
188 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
190 /* If -nolink, then "remove" files */
192 remove_files (filep
, filevec
);
197 app_msgarg(&msgs
, "cur");
199 folder
= getfolder (1);
200 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
202 if (chdir (maildir
) == NOTOK
)
203 adios (maildir
, "unable to change directory to");
205 /* read source folder and create message structure */
206 if (!(mp
= folder_read (folder
, 1)))
207 die("unable to read folder %s", folder
);
209 /* check for empty folder */
211 die("no messages in %s", folder
);
213 /* parse the message range/sequence/name and set SELECTED */
214 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
215 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
217 seq_setprev (mp
); /* set the previous-sequence */
219 /* create folder structures for each destination folder */
220 opnfolds (mp
, folders
, foldp
);
222 /* Link all the selected messages into destination folders.
224 * This causes the add hook to be run for messages that are
225 * linked into another folder. The refile hook is run for
226 * messages that are moved to another folder.
228 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
229 if (is_selected (mp
, msgnum
)) {
230 cp
= mh_xstrdup(m_name (msgnum
));
231 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
239 * Update this now, since folder_delmsgs() will save the context
240 * but doesn't have access to the folder name.
243 context_replace (pfolder
, folder
); /* update current folder */
246 * Adjust "cur" if necessary
249 if (mp
->hghsel
!= mp
->curmsg
250 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
251 seq_setcur (mp
, mp
->hghsel
);
254 * Close destination folders now; if we are using private sequences
255 * we need to have all of our calls to seq_save() complete before we
256 * call context_save().
259 clsfolds (folders
, foldp
);
261 /* If -nolink, then "remove" messages from source folder.
263 * Note that folder_delmsgs does not call the delete hook
264 * because the message has already been handled above.
267 folder_delmsgs (mp
, unlink_msgs
, 1);
269 seq_save (mp
); /* synchronize message sequences */
270 context_save (); /* save the context file */
273 folder_free (mp
); /* free folder structure */
280 * Read all the destination folders and
281 * create folder structures for all of them.
285 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
287 char nmaildir
[BUFSIZ
];
288 struct st_fold
*fp
, *ep
;
291 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
292 if (chdir (m_maildir ("")) < 0) {
293 advise (m_maildir (""), "chdir");
295 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
298 * Null src_folder indicates that we are refiling a file to
299 * the folders, in which case we don't want to short-circuit
300 * fp->f_mp to any "source folder".
302 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
303 create_folder (nmaildir
, 0, done
);
305 if (chdir (nmaildir
) == NOTOK
)
306 adios (nmaildir
, "unable to change directory to");
307 if (!(mp
= folder_read (fp
->f_name
, 1)))
308 die("unable to read folder %s", fp
->f_name
);
313 /* Source and destination folders are the same. */
314 fp
->f_mp
= src_folder
;
317 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
318 advise (maildir
, "chdir");
325 * Set the Previous-Sequence and then synchronize the
326 * sequence file, for each destination folder.
330 clsfolds (struct st_fold
*folders
, int nfolders
)
332 struct st_fold
*fp
, *ep
;
335 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
344 * If you have a "rmmproc" defined, we called that
345 * to remove all the specified files. If "rmmproc"
346 * is not defined, then just unlink the files.
350 remove_files (int filep
, char **files
)
353 char **vec
, *program
;
355 /* If rmmproc is defined, we use that */
357 vec
= argsplit(rmmproc
, &program
, &vecp
);
358 files
++; /* Yes, we need to do this */
359 for (i
= 0; i
< filep
; i
++)
360 vec
[vecp
++] = files
[i
];
361 vec
[vecp
] = NULL
; /* NULL terminate list */
364 execvp (program
, vec
);
365 adios (rmmproc
, "unable to exec");
368 /* Else just unlink the files */
369 files
++; /* advance past filevec[0] */
370 for (i
= 0; i
< filep
; i
++) {
371 if (m_unlink (files
[i
]) == NOTOK
)
372 admonish (files
[i
], "unable to unlink");
378 * Link (or copy) the message into each of the destination folders.
379 * If oldmsgnum is not 0, call copy_seqs().
383 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
384 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
387 struct st_fold
*fp
, *ep
;
389 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
391 * With same source and destination folder, don't indicate that
392 * the new message is selected so that 1) folder_delmsgs() doesn't
393 * delete it later and 2) it is not reflected in mp->hghsel, and
394 * therefore won't be assigned to be the current message.
396 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
398 0, preserve
, nfolders
== 1 && refile
,
401 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
408 * Copy sequence information for a refiled message to its
409 * new folder. Skip the cur sequence.
412 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
417 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
418 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
420 if (strcmp (current
, *seq
)) {
421 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
422 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
423 seq_addmsg (newmp
, *seq
, newmsgnum
,
424 !is_seq_private (oldmp
, seqnum
), 0);