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 = mh_xstrdup(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++] = mh_xstrdup(expanddir(cp));
117 if (*cp == '+' || *cp == '@') {
118 if (foldp > NFOLDERS)
119 adios(EX_USAGE, NULL, "only %d folders allowed!",
121 folders[foldp++].f_name = mh_xstrdup(expandfol(cp));
123 app_msgarg(&msgs, cp);
127 adios(EX_USAGE, NULL, "no folder specified");
131 ** We are refiling one or more files (-file) to the folders
134 adios(EX_USAGE, NULL, "use -file or msgs, not both");
137 adios(EX_USAGE, NULL, "use -file or -src, not both");
139 opnfolds(folders, foldp);
140 for (i = 0; i < filep; i++) {
141 if (m_file(files[i], folders, foldp, 0)) {
145 /* If -nolink, then unlink files */
148 char **files = filevec;
150 for (i = 0; i < filep; i++) {
151 if (unlink(files[i]) == NOTOK) {
152 admonish(files[i], "unable to unlink");
160 ** We are refiling messages to the folders
163 app_msgarg(&msgs, seq_cur);
166 folder = getcurfol();
169 strncpy(maildir, toabsdir(folder), sizeof(maildir));
170 if (chdir(maildir) == NOTOK) {
171 adios(EX_OSERR, maildir, "unable to change directory to");
174 /* read source folder and create message structure */
175 if (!(mp = folder_read(folder))) {
176 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
179 if (mp->nummsg == 0) {
180 adios(EX_DATAERR, NULL, "no messages in %s", folder);
183 /* parse the message range/sequence/name and set SELECTED */
184 for (msgnum = 0; msgnum < msgs.size; msgnum++) {
185 if (!m_convert(mp, msgs.msgs[msgnum])) {
191 /* create folder structures for each destination folder */
192 opnfolds(folders, foldp);
195 ** Link all the selected messages into destination folders.
197 ** This causes the add hook to be run for messages that are
198 ** linked into another folder. The refile hook is run for
199 ** messages that are moved to another folder.
201 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
202 if (is_selected(mp, msgnum)) {
203 cp = mh_xstrdup(m_name(msgnum));
204 if (m_file(cp, folders, foldp, !linkf)) {
212 ** If -nolink, then remove (= unlink) messages from source folder.
214 ** Note that folder_delmsgs does not call the delete hook
215 ** because the message has already been handled above.
218 folder_delmsgs(mp, 0);
221 clsfolds(folders, foldp);
224 seq_setcur(mp, mp->hghsel);
235 ** Read all the destination folders and
236 ** create folder structures for all of them.
239 opnfolds(struct st_fold *folders, int nfolders)
241 char nmaildir[BUFSIZ];
242 struct st_fold *fp, *ep;
245 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
246 chdir(toabsdir("+"));
247 strncpy(nmaildir, toabsdir(fp->f_name), sizeof(nmaildir));
249 create_folder(nmaildir, 0, exit);
251 if (chdir(nmaildir) == NOTOK) {
252 adios(EX_OSERR, nmaildir, "unable to change directory to");
254 if (!(mp = folder_read(fp->f_name))) {
255 adios(EX_IOERR, NULL, "unable to read folder %s", fp->f_name);
267 ** Set the Previous-Sequence and then sychronize the
268 ** sequence file, for each destination folder.
271 clsfolds(struct st_fold *folders, int nfolders)
273 struct st_fold *fp, *ep;
276 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) {