]>
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.
12 #include "sbr/m_maildir.h"
13 #include "sbr/m_mktemp.h"
16 #define REFILE_SWITCHES \
17 X("draft", 0, DRAFTSW) \
18 X("link", 0, LINKSW) \
19 X("nolink", 0, NLINKSW) \
20 X("preserve", 0, PRESSW) \
21 X("nopreserve", 0, NPRESSW) \
22 X("retainsequences", 0, RETAINSEQSSW) \
23 X("noretainsequences", 0, NRETAINSEQSSW) \
24 X("unlink", 0, UNLINKSW) \
25 X("nounlink", 0, NUNLINKSW) \
26 X("src +folder", 0, SRCSW) \
27 X("file file", 0, FILESW) \
28 X("rmmproc program", 0, RPROCSW) \
29 X("normmproc", 0, NRPRCSW) \
30 X("version", 0, VERSIONSW) \
31 X("help", 0, HELPSW) \
33 #define X(sw, minchars, id) id,
34 DEFINE_SWITCH_ENUM(REFILE
);
37 #define X(sw, minchars, id) { sw, minchars, id },
38 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
41 static char maildir
[BUFSIZ
];
51 static void opnfolds (struct msgs
*, struct st_fold
*, int);
52 static void clsfolds (struct st_fold
*, int);
53 static void remove_files (int, char **);
54 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
55 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
59 main (int argc
, char **argv
)
62 bool preserve
= false;
63 bool retainseqs
= false;
65 int foldp
= 0, isdf
= 0;
66 bool unlink_msgs
= false;
68 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
69 char **argp
, **arguments
;
70 char *filevec
[NFOLDERS
+ 2];
71 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
72 struct st_fold folders
[NFOLDERS
+ 1];
73 struct msgs_array msgs
= { 0, 0, NULL
};
76 if (nmh_init(argv
[0], true, true)) { return 1; }
78 arguments
= getarguments (invo_name
, argc
, argv
, 1);
84 while ((cp
= *argp
++)) {
86 switch (smatch (++cp
, switches
)) {
88 ambigsw (cp
, switches
);
91 die("-%s unknown\n", cp
);
94 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
96 print_help (buf
, switches
, 1);
99 print_version(invo_name
);
132 die("only one source folder at a time!");
133 if (!(cp
= *argp
++) || *cp
== '-')
134 die("missing argument to %s", argp
[-2]);
135 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
136 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
139 if (filep
> NFOLDERS
)
140 die("only %d files allowed!", NFOLDERS
);
142 files
[filep
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
145 if (filep
> NFOLDERS
)
146 die("only %d files allowed!", NFOLDERS
);
147 if (!(cp
= *argp
++) || *cp
== '-')
148 die("missing argument to %s", argp
[-2]);
149 files
[filep
++] = path (cp
, TFILE
);
153 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
154 die("missing argument to %s", argp
[-2]);
161 if (*cp
== '+' || *cp
== '@') {
162 if (foldp
> NFOLDERS
)
163 die("only %d folders allowed!", NFOLDERS
);
164 folders
[foldp
++].f_name
=
167 app_msgarg(&msgs
, cp
);
170 if (!context_find ("path"))
171 free (path ("./", TFOLDER
));
173 die("no folder specified");
176 * We are refiling a file to the folders
179 if (folder
|| msgs
.size
)
180 die("use -file or some messages, not both");
181 opnfolds (NULL
, folders
, foldp
);
182 for (i
= 0; i
< filep
; i
++)
183 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
185 /* If -nolink, then "remove" files */
187 remove_files (filep
, filevec
);
192 app_msgarg(&msgs
, "cur");
194 folder
= getfolder (1);
195 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
197 if (chdir (maildir
) == NOTOK
)
198 adios (maildir
, "unable to change directory to");
200 /* read source folder and create message structure */
201 if (!(mp
= folder_read (folder
, 1)))
202 die("unable to read folder %s", folder
);
204 /* check for empty folder */
206 die("no messages in %s", folder
);
208 /* parse the message range/sequence/name and set SELECTED */
209 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
210 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
212 seq_setprev (mp
); /* set the previous-sequence */
214 /* create folder structures for each destination folder */
215 opnfolds (mp
, folders
, foldp
);
217 /* Link all the selected messages into destination folders.
219 * This causes the add hook to be run for messages that are
220 * linked into another folder. The refile hook is run for
221 * messages that are moved to another folder.
223 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
224 if (is_selected (mp
, msgnum
)) {
225 cp
= mh_xstrdup(m_name (msgnum
));
226 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
234 * Update this now, since folder_delmsgs() will save the context
235 * but doesn't have access to the folder name.
238 context_replace (pfolder
, folder
); /* update current folder */
241 * Adjust "cur" if necessary
244 if (mp
->hghsel
!= mp
->curmsg
245 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
246 seq_setcur (mp
, mp
->hghsel
);
249 * Close destination folders now; if we are using private sequences
250 * we need to have all of our calls to seq_save() complete before we
251 * call context_save().
254 clsfolds (folders
, foldp
);
256 /* If -nolink, then "remove" messages from source folder.
258 * Note that folder_delmsgs does not call the delete hook
259 * because the message has already been handled above.
262 folder_delmsgs (mp
, unlink_msgs
, 1);
264 seq_save (mp
); /* synchronize message sequences */
265 context_save (); /* save the context file */
268 folder_free (mp
); /* free folder structure */
275 * Read all the destination folders and
276 * create folder structures for all of them.
280 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
282 char nmaildir
[BUFSIZ
];
283 struct st_fold
*fp
, *ep
;
286 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
287 if (chdir (m_maildir ("")) < 0) {
288 advise (m_maildir (""), "chdir");
290 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
293 * Null src_folder indicates that we are refiling a file to
294 * the folders, in which case we don't want to short-circuit
295 * fp->f_mp to any "source folder".
297 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
298 create_folder (nmaildir
, 0, done
);
300 if (chdir (nmaildir
) == NOTOK
)
301 adios (nmaildir
, "unable to change directory to");
302 if (!(mp
= folder_read (fp
->f_name
, 1)))
303 die("unable to read folder %s", fp
->f_name
);
308 /* Source and destination folders are the same. */
309 fp
->f_mp
= src_folder
;
312 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
313 advise (maildir
, "chdir");
320 * Set the Previous-Sequence and then synchronize the
321 * sequence file, for each destination folder.
325 clsfolds (struct st_fold
*folders
, int nfolders
)
327 struct st_fold
*fp
, *ep
;
330 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
339 * If you have a "rmmproc" defined, we called that
340 * to remove all the specified files. If "rmmproc"
341 * is not defined, then just unlink the files.
345 remove_files (int filep
, char **files
)
348 char **vec
, *program
;
350 /* If rmmproc is defined, we use that */
352 vec
= argsplit(rmmproc
, &program
, &vecp
);
353 files
++; /* Yes, we need to do this */
354 for (i
= 0; i
< filep
; i
++)
355 vec
[vecp
++] = files
[i
];
356 vec
[vecp
] = NULL
; /* NULL terminate list */
359 execvp (program
, vec
);
360 adios (rmmproc
, "unable to exec");
363 /* Else just unlink the files */
364 files
++; /* advance past filevec[0] */
365 for (i
= 0; i
< filep
; i
++) {
366 if (m_unlink (files
[i
]) == NOTOK
)
367 admonish (files
[i
], "unable to unlink");
373 * Link (or copy) the message into each of the destination folders.
374 * If oldmsgnum is not 0, call copy_seqs().
378 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
379 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
382 struct st_fold
*fp
, *ep
;
384 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
386 * With same source and destination folder, don't indicate that
387 * the new message is selected so that 1) folder_delmsgs() doesn't
388 * delete it later and 2) it is not reflected in mp->hghsel, and
389 * therefore won't be assigned to be the current message.
391 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
393 0, preserve
, nfolders
== 1 && refile
,
396 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
403 * Copy sequence information for a refiled message to its
404 * new folder. Skip the cur sequence.
407 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
412 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
413 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
415 if (strcmp (current
, *seq
)) {
416 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
417 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
418 seq_addmsg (newmp
, *seq
, newmsgnum
,
419 !is_seq_private (oldmp
, seqnum
), 0);