]>
diplodocus.org Git - nmh/blob - uip/refile.c
8beae1b8d6c4a26b71924284be6794653e2424ae
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.
11 #include "../sbr/m_mktemp.h"
14 #define REFILE_SWITCHES \
15 X("draft", 0, DRAFTSW) \
16 X("link", 0, LINKSW) \
17 X("nolink", 0, NLINKSW) \
18 X("preserve", 0, PRESSW) \
19 X("nopreserve", 0, NPRESSW) \
20 X("retainsequences", 0, RETAINSEQSSW) \
21 X("noretainsequences", 0, NRETAINSEQSSW) \
22 X("unlink", 0, UNLINKSW) \
23 X("nounlink", 0, NUNLINKSW) \
24 X("src +folder", 0, SRCSW) \
25 X("file file", 0, FILESW) \
26 X("rmmproc program", 0, RPROCSW) \
27 X("normmproc", 0, NRPRCSW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(REFILE
);
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
39 static char maildir
[BUFSIZ
];
49 static void opnfolds (struct msgs
*, struct st_fold
*, int);
50 static void clsfolds (struct st_fold
*, int);
51 static void remove_files (int, char **);
52 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
53 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
57 main (int argc
, char **argv
)
59 int linkf
= 0, preserve
= 0, retainseqs
= 0, filep
= 0;
60 int foldp
= 0, isdf
= 0, unlink_msgs
= 0;
62 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
63 char **argp
, **arguments
;
64 char *filevec
[NFOLDERS
+ 2];
65 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
66 struct st_fold folders
[NFOLDERS
+ 1];
67 struct msgs_array msgs
= { 0, 0, NULL
};
70 if (nmh_init(argv
[0], 1)) { return 1; }
72 arguments
= getarguments (invo_name
, argc
, argv
, 1);
78 while ((cp
= *argp
++)) {
80 switch (smatch (++cp
, switches
)) {
82 ambigsw (cp
, switches
);
85 adios (NULL
, "-%s unknown\n", cp
);
88 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
90 print_help (buf
, switches
, 1);
93 print_version(invo_name
);
126 adios (NULL
, "only one source folder at a time!");
127 if (!(cp
= *argp
++) || *cp
== '-')
128 adios (NULL
, "missing argument to %s", argp
[-2]);
129 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
130 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
133 if (filep
> NFOLDERS
)
134 adios (NULL
, "only %d files allowed!", NFOLDERS
);
136 files
[filep
++] = getcpy (m_draft (NULL
, NULL
, 1, &isdf
));
139 if (filep
> NFOLDERS
)
140 adios (NULL
, "only %d files allowed!", NFOLDERS
);
141 if (!(cp
= *argp
++) || *cp
== '-')
142 adios (NULL
, "missing argument to %s", argp
[-2]);
143 files
[filep
++] = path (cp
, TFILE
);
147 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
148 adios (NULL
, "missing argument to %s", argp
[-2]);
155 if (*cp
== '+' || *cp
== '@') {
156 if (foldp
> NFOLDERS
)
157 adios (NULL
, "only %d folders allowed!", NFOLDERS
);
158 folders
[foldp
++].f_name
=
161 app_msgarg(&msgs
, cp
);
164 if (!context_find ("path"))
165 free (path ("./", TFOLDER
));
167 adios (NULL
, "no folder specified");
170 * We are refiling a file to the folders
173 if (folder
|| msgs
.size
)
174 adios (NULL
, "use -file or some messages, not both");
175 opnfolds (NULL
, folders
, foldp
);
176 for (i
= 0; i
< filep
; i
++)
177 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
179 /* If -nolink, then "remove" files */
181 remove_files (filep
, filevec
);
186 app_msgarg(&msgs
, "cur");
188 folder
= getfolder (1);
189 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
191 if (chdir (maildir
) == NOTOK
)
192 adios (maildir
, "unable to change directory to");
194 /* read source folder and create message structure */
195 if (!(mp
= folder_read (folder
, 1)))
196 adios (NULL
, "unable to read folder %s", folder
);
198 /* check for empty folder */
200 adios (NULL
, "no messages in %s", folder
);
202 /* parse the message range/sequence/name and set SELECTED */
203 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
204 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
206 seq_setprev (mp
); /* set the previous-sequence */
208 /* create folder structures for each destination folder */
209 opnfolds (mp
, folders
, foldp
);
211 /* Link all the selected messages into destination folders.
213 * This causes the add hook to be run for messages that are
214 * linked into another folder. The refile hook is run for
215 * messages that are moved to another folder.
217 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
218 if (is_selected (mp
, msgnum
)) {
219 cp
= mh_xstrdup(m_name (msgnum
));
220 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
228 * Update this now, since folder_delmsgs() will save the context
229 * but doesn't have access to the folder name.
232 context_replace (pfolder
, folder
); /* update current folder */
235 * Adjust "cur" if necessary
238 if (mp
->hghsel
!= mp
->curmsg
239 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
240 seq_setcur (mp
, mp
->hghsel
);
243 * Close destination folders now; if we are using private sequences
244 * we need to have all of our calls to seq_save() complete before we
245 * call context_save().
248 clsfolds (folders
, foldp
);
250 /* If -nolink, then "remove" messages from source folder.
252 * Note that folder_delmsgs does not call the delete hook
253 * because the message has already been handled above.
256 folder_delmsgs (mp
, unlink_msgs
, 1);
258 seq_save (mp
); /* synchronize message sequences */
259 context_save (); /* save the context file */
262 folder_free (mp
); /* free folder structure */
269 * Read all the destination folders and
270 * create folder structures for all of them.
274 opnfolds (struct msgs
*src_folder
, struct st_fold
*folders
, int nfolders
)
276 char nmaildir
[BUFSIZ
];
277 struct st_fold
*fp
, *ep
;
280 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
281 if (chdir (m_maildir ("")) < 0) {
282 advise (m_maildir (""), "chdir");
284 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
287 * Null src_folder indicates that we are refiling a file to
288 * the folders, in which case we don't want to short-circuit
289 * fp->f_mp to any "source folder".
291 if (! src_folder
|| strcmp (src_folder
->foldpath
, nmaildir
)) {
292 create_folder (nmaildir
, 0, done
);
294 if (chdir (nmaildir
) == NOTOK
)
295 adios (nmaildir
, "unable to change directory to");
296 if (!(mp
= folder_read (fp
->f_name
, 1)))
297 adios (NULL
, "unable to read folder %s", fp
->f_name
);
302 /* Source and destination folders are the same. */
303 fp
->f_mp
= src_folder
;
306 if (maildir
[0] != '\0' && chdir (maildir
) < 0) {
307 advise (maildir
, "chdir");
314 * Set the Previous-Sequence and then synchronize the
315 * sequence file, for each destination folder.
319 clsfolds (struct st_fold
*folders
, int nfolders
)
321 struct st_fold
*fp
, *ep
;
324 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
333 * If you have a "rmmproc" defined, we called that
334 * to remove all the specified files. If "rmmproc"
335 * is not defined, then just unlink the files.
339 remove_files (int filep
, char **files
)
342 char **vec
, *program
;
344 /* If rmmproc is defined, we use that */
346 vec
= argsplit(rmmproc
, &program
, &vecp
);
347 files
++; /* Yes, we need to do this */
348 for (i
= 0; i
< filep
; i
++)
349 vec
[vecp
++] = files
[i
];
350 vec
[vecp
] = NULL
; /* NULL terminate list */
353 execvp (program
, vec
);
354 adios (rmmproc
, "unable to exec");
357 /* Else just unlink the files */
358 files
++; /* advance past filevec[0] */
359 for (i
= 0; i
< filep
; i
++) {
360 if (m_unlink (files
[i
]) == NOTOK
)
361 admonish (files
[i
], "unable to unlink");
367 * Link (or copy) the message into each of the destination folders.
368 * If oldmsgnum is not 0, call copy_seqs().
372 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
373 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
376 struct st_fold
*fp
, *ep
;
378 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
380 * With same source and destination folder, don't indicate that
381 * the new message is selected so that 1) folder_delmsgs() doesn't
382 * delete it later and 2) it is not reflected in mp->hghsel, and
383 * therefore won't be assigned to be the current message.
385 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
,
387 0, preserve
, nfolders
== 1 && refile
,
390 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
397 * Copy sequence information for a refiled message to its
398 * new folder. Skip the cur sequence.
401 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
406 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
407 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
409 if (strcmp (current
, *seq
)) {
410 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
411 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
412 seq_addmsg (newmp
, *seq
, newmsgnum
,
413 !is_seq_private (oldmp
, seqnum
), 0);