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