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])) {
192 /* create folder structures for each destination folder */
193 opnfolds(folders, foldp);
196 ** Link all the selected messages into destination folders.
198 ** This causes the add hook to be run for messages that are
199 ** linked into another folder. The refile hook is run for
200 ** messages that are moved to another folder.
202 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
203 if (is_selected(mp, msgnum)) {
204 cp = mh_xstrdup(m_name(msgnum));
205 if (m_file(cp, folders, foldp, !linkf)) {
213 ** If -nolink, then remove (= unlink) messages from source folder.
215 ** Note that folder_delmsgs does not call the delete hook
216 ** because the message has already been handled above.
219 folder_delmsgs(mp, 0);
222 clsfolds(folders, foldp);
225 seq_setcur(mp, mp->hghsel);
236 ** Read all the destination folders and
237 ** create folder structures for all of them.
240 opnfolds(struct st_fold *folders, int nfolders)
242 char nmaildir[BUFSIZ];
243 struct st_fold *fp, *ep;
246 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
247 chdir(toabsdir("+"));
248 strncpy(nmaildir, toabsdir(fp->f_name), sizeof(nmaildir));
250 create_folder(nmaildir, 0, exit);
252 if (chdir(nmaildir) == NOTOK) {
253 adios(EX_OSERR, nmaildir, "unable to change directory to");
255 if (!(mp = folder_read(fp->f_name))) {
256 adios(EX_IOERR, NULL, "unable to read folder %s", fp->f_name);
268 ** Set the Previous-Sequence and then sychronize the
269 ** sequence file, for each destination folder.
272 clsfolds(struct st_fold *folders, int nfolders)
274 struct st_fold *fp, *ep;
277 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
285 ** Link (or copy) the message into each of
286 ** the destination folders.
289 m_file(char *msgfile, struct st_fold *folders, int nfolders, int refile)
292 struct st_fold *fp, *ep;
294 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
295 if ((msgnum = folder_addmsg(&fp->f_mp, msgfile, 1, 0, 0,
296 nfolders == 1 && refile, maildir)) == -1) {