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 char *version=VERSION;
36 static char maildir[BUFSIZ];
46 static void opnfolds(struct st_fold *, int);
47 static void clsfolds(struct st_fold *, int);
48 static int m_file(char *, struct st_fold *, int, int);
52 main(int argc, char **argv)
54 int linkf = 0, filep = 0;
57 char *cp, *folder = NULL, buf[BUFSIZ];
58 char **argp, **arguments;
59 char *filevec[NFOLDERS + 1];
60 char **files = filevec;
61 struct st_fold folders[NFOLDERS + 1];
62 struct msgs_array msgs = { 0, 0, NULL };
65 setlocale(LC_ALL, "");
66 invo_name = mhbasename(argv[0]);
70 arguments = getarguments(invo_name, argc, argv, 1);
76 while ((cp = *argp++)) {
78 switch (smatch(++cp, switches)) {
80 ambigsw(cp, switches);
83 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
86 snprintf(buf, sizeof(buf), "%s [msgs] [switches] +folder ...", invo_name);
87 print_help(buf, switches, 1);
88 exit(argc == 2 ? EX_OK : EX_USAGE);
90 print_version(invo_name);
91 exit(argc == 2 ? EX_OK : EX_USAGE);
102 adios(EX_USAGE, NULL, "only one source folder at a time!");
103 if (!(cp = *argp++) || *cp == '-')
104 adios(EX_USAGE, NULL, "missing argument to %s",
106 folder = mh_xstrdup(expandfol(cp));
109 if (filep > NFOLDERS)
110 adios(EX_USAGE, NULL, "only %d files allowed!",
112 if (!(cp = *argp++) || *cp == '-')
113 adios(EX_USAGE, NULL, "missing argument to %s",
115 files[filep++] = mh_xstrdup(expanddir(cp));
119 if (*cp == '+' || *cp == '@') {
120 if (foldp > NFOLDERS)
121 adios(EX_USAGE, NULL, "only %d folders allowed!",
123 folders[foldp++].f_name = mh_xstrdup(expandfol(cp));
125 app_msgarg(&msgs, cp);
129 adios(EX_USAGE, NULL, "no folder specified");
133 ** We are refiling one or more files (-file) to the folders
136 adios(EX_USAGE, NULL, "use -file or msgs, not both");
139 adios(EX_USAGE, NULL, "use -file or -src, not both");
141 opnfolds(folders, foldp);
142 for (i = 0; i < filep; i++) {
143 if (m_file(files[i], folders, foldp, 0)) {
147 /* If -nolink, then unlink files */
150 char **files = filevec;
152 for (i = 0; i < filep; i++) {
153 if (unlink(files[i]) == NOTOK) {
154 admonish(files[i], "unable to unlink");
162 ** We are refiling messages to the folders
165 app_msgarg(&msgs, seq_cur);
168 folder = getcurfol();
171 strncpy(maildir, toabsdir(folder), sizeof(maildir));
172 if (chdir(maildir) == NOTOK) {
173 adios(EX_OSERR, maildir, "unable to change directory to");
176 /* read source folder and create message structure */
177 if (!(mp = folder_read(folder))) {
178 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
181 if (mp->nummsg == 0) {
182 adios(EX_DATAERR, NULL, "no messages in %s", folder);
185 /* parse the message range/sequence/name and set SELECTED */
186 for (msgnum = 0; msgnum < msgs.size; msgnum++) {
187 if (!m_convert(mp, msgs.msgs[msgnum])) {
193 /* create folder structures for each destination folder */
194 opnfolds(folders, foldp);
197 ** Link all the selected messages into destination folders.
199 ** This causes the add hook to be run for messages that are
200 ** linked into another folder. The refile hook is run for
201 ** messages that are moved to another folder.
203 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
204 if (is_selected(mp, msgnum)) {
205 cp = mh_xstrdup(m_name(msgnum));
206 if (m_file(cp, folders, foldp, !linkf)) {
214 ** If -nolink, then remove (= unlink) messages from source folder.
216 ** Note that folder_delmsgs does not call the delete hook
217 ** because the message has already been handled above.
220 folder_delmsgs(mp, 0);
223 clsfolds(folders, foldp);
226 seq_setcur(mp, mp->hghsel);
237 ** Read all the destination folders and
238 ** create folder structures for all of them.
241 opnfolds(struct st_fold *folders, int nfolders)
243 char nmaildir[BUFSIZ];
244 struct st_fold *fp, *ep;
247 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
248 chdir(toabsdir("+"));
249 strncpy(nmaildir, toabsdir(fp->f_name), sizeof(nmaildir));
251 create_folder(nmaildir, 0, exit);
253 if (chdir(nmaildir) == NOTOK) {
254 adios(EX_OSERR, nmaildir, "unable to change directory to");
256 if (!(mp = folder_read(fp->f_name))) {
257 adios(EX_IOERR, NULL, "unable to read folder %s", fp->f_name);
269 ** Set the Previous-Sequence and then sychronize the
270 ** sequence file, for each destination folder.
273 clsfolds(struct st_fold *folders, int nfolders)
275 struct st_fold *fp, *ep;
278 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
287 ** Link (or copy) the message into each of
288 ** the destination folders.
291 m_file(char *msgfile, struct st_fold *folders, int nfolders, int refile)
294 struct st_fold *fp, *ep;
296 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
297 if ((msgnum = folder_addmsg(&fp->f_mp, msgfile, 1, 0, 0,
298 nfolders == 1 && refile, maildir)) == -1) {