]>
diplodocus.org Git - nmh/blob - uip/refile.c
3 * refile.c -- move or link message(s) from a source folder
4 * -- into one or more destination folders
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
15 #define REFILE_SWITCHES \
16 X("draft", 0, DRAFTSW) \
17 X("link", 0, LINKSW) \
18 X("nolink", 0, NLINKSW) \
19 X("preserve", 0, PRESSW) \
20 X("nopreserve", 0, NPRESSW) \
21 X("retainsequences", 0, RETAINSEQSSW) \
22 X("noretainsequences", 0, NRETAINSEQSSW) \
23 X("unlink", 0, UNLINKSW) \
24 X("nounlink", 0, NUNLINKSW) \
25 X("src +folder", 0, SRCSW) \
26 X("file file", 0, FILESW) \
27 X("rmmproc program", 0, RPROCSW) \
28 X("normmproc", 0, NRPRCSW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
32 #define X(sw, minchars, id) id,
33 DEFINE_SWITCH_ENUM(REFILE
);
36 #define X(sw, minchars, id) { sw, minchars, id },
37 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
40 static char maildir
[BUFSIZ
];
50 static void opnfolds (struct msgs
*, struct st_fold
*, int);
51 static void clsfolds (struct st_fold
*, int);
52 static void remove_files (int, char **);
53 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
54 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
58 main (int argc
, char **argv
)
60 int linkf
= 0, preserve
= 0, retainseqs
= 0, filep
= 0;
61 int foldp
= 0, isdf
= 0, unlink_msgs
= 0;
63 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
64 char **argp
, **arguments
;
65 char *filevec
[NFOLDERS
+ 2];
66 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
67 struct st_fold folders
[NFOLDERS
+ 1];
68 struct msgs_array msgs
= { 0, 0, NULL
};
71 if (nmh_init(argv
[0], 1)) { return 1; }
73 arguments
= getarguments (invo_name
, argc
, argv
, 1);
79 while ((cp
= *argp
++)) {
81 switch (smatch (++cp
, switches
)) {
83 ambigsw (cp
, switches
);
86 adios (NULL
, "-%s unknown\n", cp
);
89 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
91 print_help (buf
, switches
, 1);
94 print_version(invo_name
);
127 adios (NULL
, "only one source folder at a time!");
128 if (!(cp
= *argp
++) || *cp
== '-')
129 adios (NULL
, "missing argument to %s", argp
[-2]);
130 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
131 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
134 if (filep
> NFOLDERS
)
135 adios (NULL
, "only %d files allowed!", NFOLDERS
);
137 files
[filep
++] = getcpy (m_draft (NULL
, NULL
, 1, &isdf
));
140 if (filep
> NFOLDERS
)
141 adios (NULL
, "only %d files allowed!", NFOLDERS
);
142 if (!(cp
= *argp
++) || *cp
== '-')
143 adios (NULL
, "missing argument to %s", argp
[-2]);
144 files
[filep
++] = path (cp
, TFILE
);
148 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
149 adios (NULL
, "missing argument to %s", argp
[-2]);
156 if (*cp
== '+' || *cp
== '@') {
157 if (foldp
> NFOLDERS
)
158 adios (NULL
, "only %d folders allowed!", NFOLDERS
);
159 folders
[foldp
++].f_name
=
162 app_msgarg(&msgs
, cp
);
165 if (!context_find ("path"))
166 free (path ("./", TFOLDER
));
168 adios (NULL
, "no folder specified");
171 * We are refiling a file to the folders
174 if (folder
|| msgs
.size
)
175 adios (NULL
, "use -file or some messages, not both");
176 opnfolds (NULL
, folders
, foldp
);
177 for (i
= 0; i
< filep
; i
++)
178 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
180 /* If -nolink, then "remove" files */
182 remove_files (filep
, filevec
);
187 app_msgarg(&msgs
, "cur");
189 folder
= getfolder (1);
190 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
192 if (chdir (maildir
) == NOTOK
)
193 adios (maildir
, "unable to change directory to");
195 /* read source folder and create message structure */
196 if (!(mp
= folder_read (folder
, 1)))
197 adios (NULL
, "unable to read folder %s", folder
);
199 /* check for empty folder */
201 adios (NULL
, "no messages in %s", folder
);
203 /* parse the message range/sequence/name and set SELECTED */
204 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
205 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
207 seq_setprev (mp
); /* set the previous-sequence */
209 /* create folder structures for each destination folder */
210 opnfolds (mp
, folders
, foldp
);
212 /* Link all the selected messages into destination folders.
214 * This causes the add hook to be run for messages that are
215 * linked into another folder. The refile hook is run for
216 * messages that are moved to another folder.
218 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
219 if (is_selected (mp
, msgnum
)) {
220 cp
= getcpy (m_name (msgnum
));
221 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
229 * Update this now, since folder_delmsgs() will save the context
230 * but doesn't have access to the folder name.
233 context_replace (pfolder
, folder
); /* update current folder */
236 * Adjust "cur" if necessary
239 if (mp
->hghsel
!= mp
->curmsg
240 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
241 seq_setcur (mp
, mp
->hghsel
);
244 * Close destination folders now; if we are using private sequences
245 * we need to have all of our calls to seq_save() complete before we
246 * call context_save().
249 clsfolds (folders
, foldp
);
251 /* If -nolink, then "remove" messages from source folder.
253 * Note that folder_delmsgs does not call the delete hook
254 * because the message has already been handled above.
257 folder_delmsgs (mp
, unlink_msgs
, 1);
259 seq_save (mp
); /* synchronize message sequences */
260 context_save (); /* save the context file */
263 folder_free (mp
); /* free folder structure */
270 * Read all the destination folders and
271 * create folder structures for all of them.
275 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
277 char nmaildir
[BUFSIZ
];
278 register struct st_fold
*fp
, *ep
;
279 register struct msgs
*mp
;
281 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
282 if (chdir (m_maildir ("")) < 0) {
283 advise (m_maildir (""), "chdir");
285 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
288 * Null src_folder indicates that we are refiling a file to
289 * the folders, in which case we don't want to short-circuit
290 * fp->f_mp to any "source folder".
292 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
293 create_folder (nmaildir
, 0, done
);
295 if (chdir (nmaildir
) == NOTOK
)
296 adios (nmaildir
, "unable to change directory to");
297 if (!(mp
= folder_read (fp
->f_name
, 1)))
298 adios (NULL
, "unable to read folder %s", fp
->f_name
);
303 /* Source and destination folders are the same. */
304 fp
->f_mp
= src_folder
;
307 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
308 advise (maildir
, "chdir");
315 * Set the Previous-Sequence and then sychronize the
316 * sequence file, for each destination folder.
320 clsfolds (struct st_fold
*folders
, int nfolders
)
322 register struct st_fold
*fp
, *ep
;
323 register struct msgs
*mp
;
325 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
334 * If you have a "rmmproc" defined, we called that
335 * to remove all the specified files. If "rmmproc"
336 * is not defined, then just unlink the files.
340 remove_files (int filep
, char **files
)
343 char **vec
, *program
;
345 /* If rmmproc is defined, we use that */
347 vec
= argsplit(rmmproc
, &program
, &vecp
);
348 files
++; /* Yes, we need to do this */
349 for (i
= 0; i
< filep
; i
++)
350 vec
[vecp
++] = files
[i
];
351 vec
[vecp
] = NULL
; /* NULL terminate list */
354 execvp (program
, vec
);
355 adios (rmmproc
, "unable to exec");
358 /* Else just unlink the files */
359 files
++; /* advance past filevec[0] */
360 for (i
= 0; i
< filep
; i
++) {
361 if (m_unlink (files
[i
]) == NOTOK
)
362 admonish (files
[i
], "unable to unlink");
368 * Link (or copy) the message into each of the destination folders.
369 * If oldmsgnum is not 0, call copy_seqs().
373 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
374 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
377 struct st_fold
*fp
, *ep
;
379 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
381 * With same source and destination folder, don't indicate that
382 * the new message is selected so that 1) folder_delmsgs() doesn't
383 * delete it later and 2) it is not reflected in mp->hghsel, and
384 * therefore won't be assigned to be the current message.
386 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
387 mp
== fp
->f_mp
? 0 : 1,
388 0, preserve
, nfolders
== 1 && refile
,
391 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
398 * Copy sequence information for a refiled message to its
399 * new folder. Skip the cur sequence.
402 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
407 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
408 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
410 if (strcmp (current
, *seq
)) {
411 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
412 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
413 seq_addmsg (newmp
, *seq
, newmsgnum
,
414 is_seq_private (oldmp
, seqnum
) ? 0 : 1, 0);