2 ** refile.c -- move or link message(s) from a source folder
3 ** -- into one or more destination folders
5 ** This code is Copyright (c) 2002, by the authors of nmh. See the
6 ** COPYRIGHT file in the root directory of the nmh distribution for
7 ** complete copyright information.
15 static struct swit switches[] = {
31 static char maildir[BUFSIZ];
41 static void opnfolds(struct st_fold *, int);
42 static void clsfolds(struct st_fold *, int);
43 static int m_file(char *, struct st_fold *, int, int);
47 main(int argc, char **argv)
49 int linkf = 0, filep = 0;
52 char *cp, *folder = NULL, buf[BUFSIZ];
53 char **argp, **arguments;
54 char *filevec[NFOLDERS + 1];
55 char **files = filevec;
56 struct st_fold folders[NFOLDERS + 1];
57 struct msgs_array msgs = { 0, 0, NULL };
60 setlocale(LC_ALL, "");
61 invo_name = mhbasename(argv[0]);
65 arguments = getarguments(invo_name, argc, argv, 1);
71 while ((cp = *argp++)) {
73 switch (smatch(++cp, switches)) {
75 ambigsw(cp, switches);
78 adios(NULL, "-%s unknown\n", cp);
81 snprintf(buf, sizeof(buf), "%s [msgs] [switches] +folder ...", invo_name);
82 print_help(buf, switches, 1);
85 print_version(invo_name);
97 adios(NULL, "only one source folder at a time!");
98 if (!(cp = *argp++) || *cp == '-')
99 adios(NULL, "missing argument to %s",
101 folder = getcpy(expandfol(cp));
104 if (filep > NFOLDERS)
105 adios(NULL, "only %d files allowed!",
107 if (!(cp = *argp++) || *cp == '-')
108 adios(NULL, "missing argument to %s",
110 files[filep++] = getcpy(expanddir(cp));
114 if (*cp == '+' || *cp == '@') {
115 if (foldp > NFOLDERS)
116 adios(NULL, "only %d folders allowed!",
118 folders[foldp++].f_name = getcpy(expandfol(cp));
120 app_msgarg(&msgs, cp);
124 adios(NULL, "no folder specified");
127 ** We are refiling a file to the folders
130 if (folder || msgs.size)
131 adios(NULL, "use -file or msgs, not both");
132 opnfolds(folders, foldp);
133 for (i = 0; i < filep; i++)
134 if (m_file(files[i], folders, foldp, 0))
135 /* sysexits.h EX_IOERR */
137 /* If -nolink, then unlink files */
140 char **files = filevec;
142 /* just unlink the files */
143 for (i = 0; i < filep; i++) {
144 if (unlink(files[i]) == NOTOK)
145 admonish(files[i], "unable to unlink");
152 app_msgarg(&msgs, seq_cur);
154 folder = getcurfol();
155 strncpy(maildir, toabsdir(folder), sizeof(maildir));
157 if (chdir(maildir) == NOTOK)
158 adios(maildir, "unable to change directory to");
160 /* read source folder and create message structure */
161 if (!(mp = folder_read(folder)))
162 adios(NULL, "unable to read folder %s", folder);
164 /* check for empty folder */
166 adios(NULL, "no messages in %s", folder);
168 /* parse the message range/sequence/name and set SELECTED */
169 for (msgnum = 0; msgnum < msgs.size; msgnum++)
170 if (!m_convert(mp, msgs.msgs[msgnum]))
171 /* sysexits.h EX_USAGE */
173 seq_setprev(mp); /* set the previous-sequence */
175 /* create folder structures for each destination folder */
176 opnfolds(folders, foldp);
178 /* Link all the selected messages into destination folders.
180 ** This causes the add hook to be run for messages that are
181 ** linked into another folder. The refile hook is run for
182 ** messages that are moved to another folder.
184 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
185 if (is_selected(mp, msgnum)) {
186 cp = getcpy(m_name(msgnum));
187 if (m_file(cp, folders, foldp, !linkf))
188 /* sysexits.h EX_IOERR */
195 ** If -nolink, then remove (= unlink) messages from source folder.
197 ** Note that folder_delmsgs does not call the delete hook
198 ** because the message has already been handled above.
201 folder_delmsgs(mp, 0);
204 clsfolds(folders, foldp);
207 seq_setcur(mp, mp->hghsel);
209 seq_save(mp); /* synchronize message sequences */
211 context_replace(curfolder, folder);
219 ** Read all the destination folders and
220 ** create folder structures for all of them.
223 opnfolds(struct st_fold *folders, int nfolders)
225 char nmaildir[BUFSIZ];
226 register struct st_fold *fp, *ep;
227 register struct msgs *mp;
229 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
230 chdir(toabsdir("+"));
231 strncpy(nmaildir, toabsdir(fp->f_name), sizeof(nmaildir));
233 create_folder(nmaildir, 0, done);
235 if (chdir(nmaildir) == NOTOK)
236 adios(nmaildir, "unable to change directory to");
237 if (!(mp = folder_read(fp->f_name)))
238 adios(NULL, "unable to read folder %s", fp->f_name);
249 ** Set the Previous-Sequence and then sychronize the
250 ** sequence file, for each destination folder.
253 clsfolds(struct st_fold *folders, int nfolders)
255 register struct st_fold *fp, *ep;
256 register struct msgs *mp;
258 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
267 ** Link (or copy) the message into each of
268 ** the destination folders.
271 m_file(char *msgfile, struct st_fold *folders, int nfolders, int refile)
274 struct st_fold *fp, *ep;
276 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
277 if ((msgnum = folder_addmsg(&fp->f_mp, msgfile, 1, 0,
278 0, nfolders == 1 && refile, maildir)) == -1)