Improved pluspath() and used it instead of path() where appropriate.
[mmh] / uip / refile.c
1 /*
2 ** refile.c -- move or link message(s) from a source folder
3 **          -- into one or more destination folders
4 **
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.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12 #include <fcntl.h>
13 #include <errno.h>
14
15 static struct swit switches[] = {
16 #define LINKSW  0
17         { "link", 0 },
18 #define NLINKSW  1
19         { "nolink", 0 },
20 #define PRESSW  2
21         { "preserve", 0 },
22 #define NPRESSW  3
23         { "nopreserve", 0 },
24 #define UNLINKSW  4
25         { "unlink", 0 },
26 #define NUNLINKSW  5
27         { "nounlink", 0 },
28 #define SRCSW  6
29         { "src +folder", 0 },
30 #define FILESW  7
31         { "file file", 0 },
32 #define RPROCSW  8
33         { "rmmproc program", 0 },
34 #define NRPRCSW  9
35         { "normmproc", 0 },
36 #define VERSIONSW  10
37         { "version", 0 },
38 #define HELPSW  11
39         { "help", 0 },
40         { NULL, 0 }
41 };
42
43 static char maildir[BUFSIZ];
44
45 struct st_fold {
46         char *f_name;
47         struct msgs *f_mp;
48 };
49
50 /*
51 ** static prototypes
52 */
53 static void opnfolds(struct st_fold *, int);
54 static void clsfolds(struct st_fold *, int);
55 static void remove_files(int, char **);
56 static int m_file(char *, struct st_fold *, int, int, int);
57
58
59 int
60 main(int argc, char **argv)
61 {
62         int linkf = 0, preserve = 0, filep = 0;
63         int foldp = 0, unlink_msgs = 0;
64         int i, msgnum;
65         char *cp, *folder = NULL, buf[BUFSIZ];
66         char **argp, **arguments;
67         char *filevec[NFOLDERS + 2];
68         char **files = &filevec[1];  /* leave room for remove_files:vec[0] */
69         struct st_fold folders[NFOLDERS + 1];
70         struct msgs_array msgs = { 0, 0, NULL };
71         struct msgs *mp;
72
73 #ifdef LOCALE
74         setlocale(LC_ALL, "");
75 #endif
76         invo_name = mhbasename(argv[0]);
77
78         /* read user profile/context */
79         context_read();
80
81         arguments = getarguments(invo_name, argc, argv, 1);
82         argp = arguments;
83
84         /*
85         ** Parse arguments
86         */
87         while ((cp = *argp++)) {
88                 if (*cp == '-') {
89                         switch (smatch(++cp, switches)) {
90                         case AMBIGSW:
91                                 ambigsw(cp, switches);
92                                 done(1);
93                         case UNKWNSW:
94                                 adios(NULL, "-%s unknown\n", cp);
95
96                         case HELPSW:
97                                 snprintf(buf, sizeof(buf), "%s [msgs] [switches] +folder ...", invo_name);
98                                 print_help(buf, switches, 1);
99                                 done(1);
100                         case VERSIONSW:
101                                 print_version(invo_name);
102                                 done(1);
103
104                         case LINKSW:
105                                 linkf++;
106                                 continue;
107                         case NLINKSW:
108                                 linkf = 0;
109                                 continue;
110
111                         case PRESSW:
112                                 preserve++;
113                                 continue;
114                         case NPRESSW:
115                                 preserve = 0;
116                                 continue;
117
118                         case UNLINKSW:
119                                 unlink_msgs++;
120                                 continue;
121                         case NUNLINKSW:
122                                 unlink_msgs = 0;
123                                 continue;
124
125                         case SRCSW:
126                                 if (folder)
127                                         adios(NULL, "only one source folder at a time!");
128                                 if (!(cp = *argp++) || *cp == '-')
129                                         adios(NULL, "missing argument to %s",
130                                                         argp[-2]);
131                                 folder = pluspath(cp);
132                                 continue;
133                         case FILESW:
134                                 if (filep > NFOLDERS)
135                                         adios(NULL, "only %d files allowed!",
136                                                         NFOLDERS);
137                                 if (!(cp = *argp++) || *cp == '-')
138                                         adios(NULL, "missing argument to %s",
139                                                         argp[-2]);
140                                 files[filep++] = path(cp, TFILE);
141                                 continue;
142
143                         case RPROCSW:
144                                 if (!(rmmproc = *argp++) || *rmmproc == '-')
145                                         adios(NULL, "missing argument to %s",
146                                                         argp[-2]);
147                                 continue;
148                         case NRPRCSW:
149                                 rmmproc = NULL;
150                                 continue;
151                         }
152                 }
153                 if (*cp == '+' || *cp == '@') {
154                         if (foldp > NFOLDERS)
155                                 adios(NULL, "only %d folders allowed!",
156                                                 NFOLDERS);
157                         folders[foldp++].f_name =
158                                 pluspath(cp);
159                 } else
160                                 app_msgarg(&msgs, cp);
161         }
162
163         if (!context_find("path"))
164                 free(path("./", TFOLDER));
165         if (foldp == 0)
166                 adios(NULL, "no folder specified");
167
168 #ifdef WHATNOW
169         if (!msgs.size && !foldp && !filep && (cp = getenv("mhdraft")) && *cp)
170                 files[filep++] = cp;
171 #endif /* WHATNOW */
172
173         /*
174         ** We are refiling a file to the folders
175         */
176         if (filep > 0) {
177                 if (folder || msgs.size)
178                         adios(NULL, "use -file or some messages, not both");
179                 opnfolds(folders, foldp);
180                 for (i = 0; i < filep; i++)
181                         if (m_file(files[i], folders, foldp, preserve, 0))
182                                 done(1);
183                 /* If -nolink, then "remove" files */
184                 if (!linkf)
185                         remove_files(filep, filevec);
186                 done(0);
187         }
188
189         if (!msgs.size)
190                 app_msgarg(&msgs, "cur");
191         if (!folder)
192                 folder = getfolder(FCUR);
193         strncpy(maildir, m_maildir(folder), sizeof(maildir));
194
195         if (chdir(maildir) == NOTOK)
196                 adios(maildir, "unable to change directory to");
197
198         /* read source folder and create message structure */
199         if (!(mp = folder_read(folder)))
200                 adios(NULL, "unable to read folder %s", folder);
201
202         /* check for empty folder */
203         if (mp->nummsg == 0)
204                 adios(NULL, "no messages in %s", folder);
205
206         /* parse the message range/sequence/name and set SELECTED */
207         for (msgnum = 0; msgnum < msgs.size; msgnum++)
208                 if (!m_convert(mp, msgs.msgs[msgnum]))
209                         done(1);
210         seq_setprev(mp);  /* set the previous-sequence */
211
212         /* create folder structures for each destination folder */
213         opnfolds(folders, foldp);
214
215         /* Link all the selected messages into destination folders.
216         **
217         ** This causes the add hook to be run for messages that are
218         ** linked into another folder.  The refile hook is run for
219         ** messages that are moved to another folder.
220         */
221         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
222                 if (is_selected(mp, msgnum)) {
223                         cp = getcpy(m_name(msgnum));
224                         if (m_file(cp, folders, foldp, preserve, !linkf))
225                                 done(1);
226                         free(cp);
227                 }
228         }
229
230         /*
231         ** This is a hack.  If we are using an external rmmproc,
232         ** then save the current folder to the context file,
233         ** so the external rmmproc will remove files from the correct
234         ** directory.  This should be moved to folder_delmsgs().
235         */
236         if (rmmproc) {
237                 context_replace(pfolder, folder);
238                 context_save();
239                 fflush(stdout);
240         }
241
242         /*
243         ** If -nolink, then "remove" messages from source folder.
244         **
245         ** Note that folder_delmsgs does not call the delete hook
246         ** because the message has already been handled above.
247         */
248         if (!linkf) {
249                 folder_delmsgs(mp, unlink_msgs, 1);
250         }
251
252         clsfolds(folders, foldp);
253
254         if (mp->hghsel != mp->curmsg &&
255                                 (mp->numsel != mp->nummsg || linkf))
256                 seq_setcur(mp, mp->hghsel);
257         seq_save(mp);  /* synchronize message sequences */
258
259         context_replace(pfolder, folder);  /* update current folder   */
260         context_save();  /* save the context file   */
261         folder_free(mp);  /* free folder structure   */
262         done(0);
263         return 1;
264 }
265
266
267 /*
268 ** Read all the destination folders and
269 ** create folder structures for all of them.
270 */
271
272 static void
273 opnfolds(struct st_fold *folders, int nfolders)
274 {
275         char nmaildir[BUFSIZ];
276         register struct st_fold *fp, *ep;
277         register struct msgs *mp;
278
279         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
280                 chdir(m_maildir(""));
281                 strncpy(nmaildir, m_maildir(fp->f_name), sizeof(nmaildir));
282
283                 create_folder(nmaildir, 0, done);
284
285                 if (chdir(nmaildir) == NOTOK)
286                         adios(nmaildir, "unable to change directory to");
287                 if (!(mp = folder_read(fp->f_name)))
288                         adios(NULL, "unable to read folder %s", fp->f_name);
289                 mp->curmsg = 0;
290
291                 fp->f_mp = mp;
292
293                 chdir(maildir);
294         }
295 }
296
297
298 /*
299 ** Set the Previous-Sequence and then sychronize the
300 ** sequence file, for each destination folder.
301 */
302
303 static void
304 clsfolds(struct st_fold *folders, int nfolders)
305 {
306         register struct st_fold *fp, *ep;
307         register struct msgs *mp;
308
309         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
310                 mp = fp->f_mp;
311                 seq_setprev(mp);
312                 seq_save(mp);
313         }
314 }
315
316
317 /*
318 ** If you have a "rmmproc" defined, we called that
319 ** to remove all the specified files.  If "rmmproc"
320 ** is not defined, then just unlink the files.
321 */
322
323 static void
324 remove_files(int filep, char **files)
325 {
326         int i;
327         char **vec;
328
329         /* If rmmproc is defined, we use that */
330         if (rmmproc) {
331                 vec = files++;  /* vec[0] = filevec[0] */
332                 files[filep] = NULL;  /* NULL terminate list */
333
334                 fflush(stdout);
335                 vec[0] = mhbasename(rmmproc);
336                 execvp(rmmproc, vec);
337                 adios(rmmproc, "unable to exec");
338         }
339
340         /* Else just unlink the files */
341         files++;  /* advance past filevec[0] */
342         for (i = 0; i < filep; i++) {
343                 if (unlink(files[i]) == NOTOK)
344                         admonish(files[i], "unable to unlink");
345         }
346 }
347
348
349 /*
350 ** Link (or copy) the message into each of
351 ** the destination folders.
352 */
353
354 static int
355 m_file(char *msgfile, struct st_fold *folders, int nfolders, int preserve,
356                 int refile)
357 {
358         int msgnum;
359         struct st_fold *fp, *ep;
360
361         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
362                 if ((msgnum = folder_addmsg(&fp->f_mp, msgfile, 1, 0,
363                                 preserve, nfolders == 1 && refile, maildir))
364                                 == -1)
365                         return 1;
366         }
367         return 0;
368 }