]>
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.
13 #define REFILE_SWITCHES \
14 X("draft", 0, DRAFTSW) \
15 X("link", 0, LINKSW) \
16 X("nolink", 0, NLINKSW) \
17 X("preserve", 0, PRESSW) \
18 X("nopreserve", 0, NPRESSW) \
19 X("retainsequences", 0, RETAINSEQSSW) \
20 X("noretainsequences", 0, NRETAINSEQSSW) \
21 X("unlink", 0, UNLINKSW) \
22 X("nounlink", 0, NUNLINKSW) \
23 X("src +folder", 0, SRCSW) \
24 X("file file", 0, FILESW) \
25 X("rmmproc program", 0, RPROCSW) \
26 X("normmproc", 0, NRPRCSW) \
27 X("version", 0, VERSIONSW) \
28 X("help", 0, HELPSW) \
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(REFILE
);
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
38 static char maildir
[BUFSIZ
];
48 static void opnfolds (struct msgs
*, struct st_fold
*, int);
49 static void clsfolds (struct st_fold
*, int);
50 static void remove_files (int, char **);
51 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
52 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
56 main (int argc
, char **argv
)
58 int linkf
= 0, preserve
= 0, retainseqs
= 0, filep
= 0;
59 int foldp
= 0, isdf
= 0, unlink_msgs
= 0;
61 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
62 char **argp
, **arguments
;
63 char *filevec
[NFOLDERS
+ 2];
64 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
65 struct st_fold folders
[NFOLDERS
+ 1];
66 struct msgs_array msgs
= { 0, 0, NULL
};
69 if (nmh_init(argv
[0], 1)) { return 1; }
71 arguments
= getarguments (invo_name
, argc
, argv
, 1);
77 while ((cp
= *argp
++)) {
79 switch (smatch (++cp
, switches
)) {
81 ambigsw (cp
, switches
);
84 adios (NULL
, "-%s unknown\n", cp
);
87 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
89 print_help (buf
, switches
, 1);
92 print_version(invo_name
);
125 adios (NULL
, "only one source folder at a time!");
126 if (!(cp
= *argp
++) || *cp
== '-')
127 adios (NULL
, "missing argument to %s", argp
[-2]);
128 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
129 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
132 if (filep
> NFOLDERS
)
133 adios (NULL
, "only %d files allowed!", NFOLDERS
);
135 files
[filep
++] = getcpy (m_draft (NULL
, NULL
, 1, &isdf
));
138 if (filep
> NFOLDERS
)
139 adios (NULL
, "only %d files allowed!", NFOLDERS
);
140 if (!(cp
= *argp
++) || *cp
== '-')
141 adios (NULL
, "missing argument to %s", argp
[-2]);
142 files
[filep
++] = path (cp
, TFILE
);
146 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
147 adios (NULL
, "missing argument to %s", argp
[-2]);
154 if (*cp
== '+' || *cp
== '@') {
155 if (foldp
> NFOLDERS
)
156 adios (NULL
, "only %d folders allowed!", NFOLDERS
);
157 folders
[foldp
++].f_name
=
160 app_msgarg(&msgs
, cp
);
163 if (!context_find ("path"))
164 free (path ("./", TFOLDER
));
166 adios (NULL
, "no folder specified");
169 * We are refiling a file to the folders
172 if (folder
|| msgs
.size
)
173 adios (NULL
, "use -file or some messages, not both");
174 opnfolds (NULL
, folders
, foldp
);
175 for (i
= 0; i
< filep
; i
++)
176 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
178 /* If -nolink, then "remove" files */
180 remove_files (filep
, filevec
);
185 app_msgarg(&msgs
, "cur");
187 folder
= getfolder (1);
188 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
190 if (chdir (maildir
) == NOTOK
)
191 adios (maildir
, "unable to change directory to");
193 /* read source folder and create message structure */
194 if (!(mp
= folder_read (folder
, 1)))
195 adios (NULL
, "unable to read folder %s", folder
);
197 /* check for empty folder */
199 adios (NULL
, "no messages in %s", folder
);
201 /* parse the message range/sequence/name and set SELECTED */
202 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
203 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
205 seq_setprev (mp
); /* set the previous-sequence */
207 /* create folder structures for each destination folder */
208 opnfolds (mp
, folders
, foldp
);
210 /* Link all the selected messages into destination folders.
212 * This causes the add hook to be run for messages that are
213 * linked into another folder. The refile hook is run for
214 * messages that are moved to another folder.
216 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
217 if (is_selected (mp
, msgnum
)) {
218 cp
= mh_xstrdup(m_name (msgnum
));
219 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
227 * Update this now, since folder_delmsgs() will save the context
228 * but doesn't have access to the folder name.
231 context_replace (pfolder
, folder
); /* update current folder */
234 * Adjust "cur" if necessary
237 if (mp
->hghsel
!= mp
->curmsg
238 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
239 seq_setcur (mp
, mp
->hghsel
);
242 * Close destination folders now; if we are using private sequences
243 * we need to have all of our calls to seq_save() complete before we
244 * call context_save().
247 clsfolds (folders
, foldp
);
249 /* If -nolink, then "remove" messages from source folder.
251 * Note that folder_delmsgs does not call the delete hook
252 * because the message has already been handled above.
255 folder_delmsgs (mp
, unlink_msgs
, 1);
257 seq_save (mp
); /* synchronize message sequences */
258 context_save (); /* save the context file */
261 folder_free (mp
); /* free folder structure */
268 * Read all the destination folders and
269 * create folder structures for all of them.
273 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
275 char nmaildir
[BUFSIZ
];
276 struct st_fold
*fp
, *ep
;
279 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
280 if (chdir (m_maildir ("")) < 0) {
281 advise (m_maildir (""), "chdir");
283 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
286 * Null src_folder indicates that we are refiling a file to
287 * the folders, in which case we don't want to short-circuit
288 * fp->f_mp to any "source folder".
290 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
291 create_folder (nmaildir
, 0, done
);
293 if (chdir (nmaildir
) == NOTOK
)
294 adios (nmaildir
, "unable to change directory to");
295 if (!(mp
= folder_read (fp
->f_name
, 1)))
296 adios (NULL
, "unable to read folder %s", fp
->f_name
);
301 /* Source and destination folders are the same. */
302 fp
->f_mp
= src_folder
;
305 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
306 advise (maildir
, "chdir");
313 * Set the Previous-Sequence and then sychronize the
314 * sequence file, for each destination folder.
318 clsfolds (struct st_fold
*folders
, int nfolders
)
320 struct st_fold
*fp
, *ep
;
323 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
332 * If you have a "rmmproc" defined, we called that
333 * to remove all the specified files. If "rmmproc"
334 * is not defined, then just unlink the files.
338 remove_files (int filep
, char **files
)
341 char **vec
, *program
;
343 /* If rmmproc is defined, we use that */
345 vec
= argsplit(rmmproc
, &program
, &vecp
);
346 files
++; /* Yes, we need to do this */
347 for (i
= 0; i
< filep
; i
++)
348 vec
[vecp
++] = files
[i
];
349 vec
[vecp
] = NULL
; /* NULL terminate list */
352 execvp (program
, vec
);
353 adios (rmmproc
, "unable to exec");
356 /* Else just unlink the files */
357 files
++; /* advance past filevec[0] */
358 for (i
= 0; i
< filep
; i
++) {
359 if (m_unlink (files
[i
]) == NOTOK
)
360 admonish (files
[i
], "unable to unlink");
366 * Link (or copy) the message into each of the destination folders.
367 * If oldmsgnum is not 0, call copy_seqs().
371 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
372 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
375 struct st_fold
*fp
, *ep
;
377 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
379 * With same source and destination folder, don't indicate that
380 * the new message is selected so that 1) folder_delmsgs() doesn't
381 * delete it later and 2) it is not reflected in mp->hghsel, and
382 * therefore won't be assigned to be the current message.
384 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
385 mp
== fp
->f_mp
? 0 : 1,
386 0, preserve
, nfolders
== 1 && refile
,
389 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
396 * Copy sequence information for a refiled message to its
397 * new folder. Skip the cur sequence.
400 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
405 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
406 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
408 if (strcmp (current
, *seq
)) {
409 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
410 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
411 seq_addmsg (newmp
, *seq
, newmsgnum
,
412 is_seq_private (oldmp
, seqnum
) ? 0 : 1, 0);