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.
18 static struct swit switches[] = {
34 static char maildir[BUFSIZ];
44 static void opnfolds(struct st_fold *, int);
45 static void clsfolds(struct st_fold *, int);
46 static int m_file(char *, struct st_fold *, int, int);
50 main(int argc, char **argv)
52 int linkf = 0, filep = 0;
55 char *cp, *folder = NULL, buf[BUFSIZ];
56 char **argp, **arguments;
57 char *filevec[NFOLDERS + 1];
58 char **files = filevec;
59 struct st_fold folders[NFOLDERS + 1];
60 struct msgs_array msgs = { 0, 0, NULL };
63 setlocale(LC_ALL, "");
64 invo_name = mhbasename(argv[0]);
68 arguments = getarguments(invo_name, argc, argv, 1);
74 while ((cp = *argp++)) {
76 switch (smatch(++cp, switches)) {
78 ambigsw(cp, switches);
81 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
84 snprintf(buf, sizeof(buf), "%s [msgs] [switches] +folder ...", invo_name);
85 print_help(buf, switches, 1);
86 exit(argc == 2 ? EX_OK : EX_USAGE);
88 print_version(invo_name);
89 exit(argc == 2 ? EX_OK : EX_USAGE);
100 adios(EX_USAGE, NULL, "only one source folder at a time!");
101 if (!(cp = *argp++) || *cp == '-')
102 adios(EX_USAGE, NULL, "missing argument to %s",
104 folder = getcpy(expandfol(cp));
107 if (filep > NFOLDERS)
108 adios(EX_USAGE, NULL, "only %d files allowed!",
110 if (!(cp = *argp++) || *cp == '-')
111 adios(EX_USAGE, NULL, "missing argument to %s",
113 files[filep++] = getcpy(expanddir(cp));
117 if (*cp == '+' || *cp == '@') {
118 if (foldp > NFOLDERS)
119 adios(EX_USAGE, NULL, "only %d folders allowed!",
121 folders[foldp++].f_name = getcpy(expandfol(cp));
123 app_msgarg(&msgs, cp);
127 adios(EX_USAGE, NULL, "no folder specified");
130 ** We are refiling a file to the folders
133 if (folder || msgs.size)
134 adios(EX_USAGE, NULL, "use -file or msgs, not both");
135 opnfolds(folders, foldp);
136 for (i = 0; i < filep; i++)
137 if (m_file(files[i], folders, foldp, 0))
139 /* If -nolink, then unlink files */
142 char **files = filevec;
144 /* just unlink the files */
145 for (i = 0; i < filep; i++) {
146 if (unlink(files[i]) == NOTOK)
147 admonish(files[i], "unable to unlink");
154 app_msgarg(&msgs, seq_cur);
156 folder = getcurfol();
157 strncpy(maildir, toabsdir(folder), sizeof(maildir));
159 if (chdir(maildir) == NOTOK)
160 adios(EX_OSERR, maildir, "unable to change directory to");
162 /* read source folder and create message structure */
163 if (!(mp = folder_read(folder)))
164 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
166 /* check for empty folder */
168 adios(EX_DATAERR, NULL, "no messages in %s", folder);
170 /* parse the message range/sequence/name and set SELECTED */
171 for (msgnum = 0; msgnum < msgs.size; msgnum++)
172 if (!m_convert(mp, msgs.msgs[msgnum]))
174 seq_setprev(mp); /* set the previous-sequence */
176 /* create folder structures for each destination folder */
177 opnfolds(folders, foldp);
179 /* Link all the selected messages into destination folders.
181 ** This causes the add hook to be run for messages that are
182 ** linked into another folder. The refile hook is run for
183 ** messages that are moved to another folder.
185 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
186 if (is_selected(mp, msgnum)) {
187 cp = getcpy(m_name(msgnum));
188 if (m_file(cp, folders, foldp, !linkf))
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 struct st_fold *fp, *ep;
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, exit);
235 if (chdir(nmaildir) == NOTOK)
236 adios(EX_OSERR, nmaildir, "unable to change directory to");
237 if (!(mp = folder_read(fp->f_name)))
238 adios(EX_IOERR, 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 struct st_fold *fp, *ep;
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)