]>
diplodocus.org Git - nmh/blob - uip/refile.c
660003e07387423e0c42c963b990518abc780363
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_replace.h"
11 #include "sbr/context_find.h"
12 #include "sbr/ambigsw.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/seq_getnum.h"
17 #include "sbr/seq_add.h"
18 #include "sbr/arglist.h"
19 #include "sbr/error.h"
22 #include "sbr/m_maildir.h"
23 #include "sbr/m_mktemp.h"
26 #define REFILE_SWITCHES \
27 X("draft", 0, DRAFTSW) \
28 X("link", 0, LINKSW) \
29 X("nolink", 0, NLINKSW) \
30 X("preserve", 0, PRESSW) \
31 X("nopreserve", 0, NPRESSW) \
32 X("retainsequences", 0, RETAINSEQSSW) \
33 X("noretainsequences", 0, NRETAINSEQSSW) \
34 X("unlink", 0, UNLINKSW) \
35 X("nounlink", 0, NUNLINKSW) \
36 X("src +folder", 0, SRCSW) \
37 X("file file", 0, FILESW) \
38 X("rmmproc program", 0, RPROCSW) \
39 X("normmproc", 0, NRPRCSW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(REFILE
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
51 static char maildir
[BUFSIZ
];
61 static void opnfolds (struct msgs
*, struct st_fold
*, int);
62 static void clsfolds (struct st_fold
*, int);
63 static void remove_files (int, char **);
64 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
65 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
69 main (int argc
, char **argv
)
72 bool preserve
= false;
73 bool retainseqs
= false;
75 int foldp
= 0, isdf
= 0;
76 bool unlink_msgs
= false;
78 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
79 char **argp
, **arguments
;
80 char *filevec
[NFOLDERS
+ 2];
81 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
82 struct st_fold folders
[NFOLDERS
+ 1];
83 struct msgs_array msgs
= { 0, 0, NULL
};
86 if (nmh_init(argv
[0], true, true)) { return 1; }
88 arguments
= getarguments (invo_name
, argc
, argv
, 1);
94 while ((cp
= *argp
++)) {
96 switch (smatch (++cp
, switches
)) {
98 ambigsw (cp
, switches
);
101 die("-%s unknown\n", cp
);
104 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
106 print_help (buf
, switches
, 1);
109 print_version(invo_name
);
142 die("only one source folder at a time!");
143 if (!(cp
= *argp
++) || *cp
== '-')
144 die("missing argument to %s", argp
[-2]);
145 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
146 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
149 if (filep
> NFOLDERS
)
150 die("only %d files allowed!", NFOLDERS
);
152 files
[filep
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
155 if (filep
> NFOLDERS
)
156 die("only %d files allowed!", NFOLDERS
);
157 if (!(cp
= *argp
++) || *cp
== '-')
158 die("missing argument to %s", argp
[-2]);
159 files
[filep
++] = path (cp
, TFILE
);
163 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
164 die("missing argument to %s", argp
[-2]);
171 if (*cp
== '+' || *cp
== '@') {
172 if (foldp
> NFOLDERS
)
173 die("only %d folders allowed!", NFOLDERS
);
174 folders
[foldp
++].f_name
=
177 app_msgarg(&msgs
, cp
);
180 if (!context_find ("path"))
181 free (path ("./", TFOLDER
));
183 die("no folder specified");
186 * We are refiling a file to the folders
189 if (folder
|| msgs
.size
)
190 die("use -file or some messages, not both");
191 opnfolds (NULL
, folders
, foldp
);
192 for (i
= 0; i
< filep
; i
++)
193 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
195 /* If -nolink, then "remove" files */
197 remove_files (filep
, filevec
);
202 app_msgarg(&msgs
, "cur");
204 folder
= getfolder (1);
205 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
207 if (chdir (maildir
) == NOTOK
)
208 adios (maildir
, "unable to change directory to");
210 /* read source folder and create message structure */
211 if (!(mp
= folder_read (folder
, 1)))
212 die("unable to read folder %s", folder
);
214 /* check for empty folder */
216 die("no messages in %s", folder
);
218 /* parse the message range/sequence/name and set SELECTED */
219 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
220 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
222 seq_setprev (mp
); /* set the previous-sequence */
224 /* create folder structures for each destination folder */
225 opnfolds (mp
, folders
, foldp
);
227 /* Link all the selected messages into destination folders.
229 * This causes the add hook to be run for messages that are
230 * linked into another folder. The refile hook is run for
231 * messages that are moved to another folder.
233 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
234 if (is_selected (mp
, msgnum
)) {
235 cp
= mh_xstrdup(m_name (msgnum
));
236 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
244 * Update this now, since folder_delmsgs() will save the context
245 * but doesn't have access to the folder name.
248 context_replace (pfolder
, folder
); /* update current folder */
251 * Adjust "cur" if necessary
254 if (mp
->hghsel
!= mp
->curmsg
255 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
256 seq_setcur (mp
, mp
->hghsel
);
259 * Close destination folders now; if we are using private sequences
260 * we need to have all of our calls to seq_save() complete before we
261 * call context_save().
264 clsfolds (folders
, foldp
);
266 /* If -nolink, then "remove" messages from source folder.
268 * Note that folder_delmsgs does not call the delete hook
269 * because the message has already been handled above.
272 folder_delmsgs (mp
, unlink_msgs
, 1);
274 seq_save (mp
); /* synchronize message sequences */
275 context_save (); /* save the context file */
278 folder_free (mp
); /* free folder structure */
285 * Read all the destination folders and
286 * create folder structures for all of them.
290 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
292 char nmaildir
[BUFSIZ
];
293 struct st_fold
*fp
, *ep
;
296 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
297 if (chdir (m_maildir ("")) < 0) {
298 advise (m_maildir (""), "chdir");
300 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
303 * Null src_folder indicates that we are refiling a file to
304 * the folders, in which case we don't want to short-circuit
305 * fp->f_mp to any "source folder".
307 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
308 create_folder (nmaildir
, 0, done
);
310 if (chdir (nmaildir
) == NOTOK
)
311 adios (nmaildir
, "unable to change directory to");
312 if (!(mp
= folder_read (fp
->f_name
, 1)))
313 die("unable to read folder %s", fp
->f_name
);
318 /* Source and destination folders are the same. */
319 fp
->f_mp
= src_folder
;
322 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
323 advise (maildir
, "chdir");
330 * Set the Previous-Sequence and then synchronize the
331 * sequence file, for each destination folder.
335 clsfolds (struct st_fold
*folders
, int nfolders
)
337 struct st_fold
*fp
, *ep
;
340 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
349 * If you have a "rmmproc" defined, we called that
350 * to remove all the specified files. If "rmmproc"
351 * is not defined, then just unlink the files.
355 remove_files (int filep
, char **files
)
358 char **vec
, *program
;
360 /* If rmmproc is defined, we use that */
362 vec
= argsplit(rmmproc
, &program
, &vecp
);
363 files
++; /* Yes, we need to do this */
364 for (i
= 0; i
< filep
; i
++)
365 vec
[vecp
++] = files
[i
];
366 vec
[vecp
] = NULL
; /* NULL terminate list */
369 execvp (program
, vec
);
370 adios (rmmproc
, "unable to exec");
373 /* Else just unlink the files */
374 files
++; /* advance past filevec[0] */
375 for (i
= 0; i
< filep
; i
++) {
376 if (m_unlink (files
[i
]) == NOTOK
)
377 admonish (files
[i
], "unable to unlink");
383 * Link (or copy) the message into each of the destination folders.
384 * If oldmsgnum is not 0, call copy_seqs().
388 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
389 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
392 struct st_fold
*fp
, *ep
;
394 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
396 * With same source and destination folder, don't indicate that
397 * the new message is selected so that 1) folder_delmsgs() doesn't
398 * delete it later and 2) it is not reflected in mp->hghsel, and
399 * therefore won't be assigned to be the current message.
401 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
403 0, preserve
, nfolders
== 1 && refile
,
406 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
413 * Copy sequence information for a refiled message to its
414 * new folder. Skip the cur sequence.
417 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
422 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
423 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
425 if (strcmp (current
, *seq
)) {
426 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
427 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
428 seq_addmsg (newmp
, *seq
, newmsgnum
,
429 !is_seq_private (oldmp
, seqnum
), 0);