Removed the space between function names and the opening parenthesis.
[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 = r1bindex(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 = path(*cp == '+' || *cp == '@' ?
132                                                 cp + 1 : cp,
133                                                 *cp != '@' ?
134                                                 TFOLDER : TSUBCWF);
135                                 continue;
136                         case FILESW:
137                                 if (filep > NFOLDERS)
138                                         adios(NULL, "only %d files allowed!",
139                                                         NFOLDERS);
140                                 if (!(cp = *argp++) || *cp == '-')
141                                         adios(NULL, "missing argument to %s",
142                                                         argp[-2]);
143                                 files[filep++] = path(cp, TFILE);
144                                 continue;
145
146                         case RPROCSW:
147                                 if (!(rmmproc = *argp++) || *rmmproc == '-')
148                                         adios(NULL, "missing argument to %s",
149                                                         argp[-2]);
150                                 continue;
151                         case NRPRCSW:
152                                 rmmproc = NULL;
153                                 continue;
154                         }
155                 }
156                 if (*cp == '+' || *cp == '@') {
157                         if (foldp > NFOLDERS)
158                                 adios(NULL, "only %d folders allowed!",
159                                                 NFOLDERS);
160                         folders[foldp++].f_name =
161                                 pluspath(cp);
162                 } else
163                                 app_msgarg(&msgs, cp);
164         }
165
166         if (!context_find("path"))
167                 free(path("./", TFOLDER));
168         if (foldp == 0)
169                 adios(NULL, "no folder specified");
170
171 #ifdef WHATNOW
172         if (!msgs.size && !foldp && !filep && (cp = getenv("mhdraft")) && *cp)
173                 files[filep++] = cp;
174 #endif /* WHATNOW */
175
176         /*
177         ** We are refiling a file to the folders
178         */
179         if (filep > 0) {
180                 if (folder || msgs.size)
181                         adios(NULL, "use -file or some messages, not both");
182                 opnfolds(folders, foldp);
183                 for (i = 0; i < filep; i++)
184                         if (m_file(files[i], folders, foldp, preserve, 0))
185                                 done(1);
186                 /* If -nolink, then "remove" files */
187                 if (!linkf)
188                         remove_files(filep, filevec);
189                 done(0);
190         }
191
192         if (!msgs.size)
193                 app_msgarg(&msgs, "cur");
194         if (!folder)
195                 folder = getfolder(1);
196         strncpy(maildir, m_maildir(folder), sizeof(maildir));
197
198         if (chdir(maildir) == NOTOK)
199                 adios(maildir, "unable to change directory to");
200
201         /* read source folder and create message structure */
202         if (!(mp = folder_read(folder)))
203                 adios(NULL, "unable to read folder %s", folder);
204
205         /* check for empty folder */
206         if (mp->nummsg == 0)
207                 adios(NULL, "no messages in %s", folder);
208
209         /* parse the message range/sequence/name and set SELECTED */
210         for (msgnum = 0; msgnum < msgs.size; msgnum++)
211                 if (!m_convert(mp, msgs.msgs[msgnum]))
212                         done(1);
213         seq_setprev(mp);  /* set the previous-sequence */
214
215         /* create folder structures for each destination folder */
216         opnfolds(folders, foldp);
217
218         /* Link all the selected messages into destination folders.
219         **
220         ** This causes the add hook to be run for messages that are
221         ** linked into another folder.  The refile hook is run for
222         ** messages that are moved to another folder.
223         */
224         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
225                 if (is_selected(mp, msgnum)) {
226                         cp = getcpy(m_name(msgnum));
227                         if (m_file(cp, folders, foldp, preserve, !linkf))
228                                 done(1);
229                         free(cp);
230                 }
231         }
232
233         /*
234         ** This is a hack.  If we are using an external rmmproc,
235         ** then save the current folder to the context file,
236         ** so the external rmmproc will remove files from the correct
237         ** directory.  This should be moved to folder_delmsgs().
238         */
239         if (rmmproc) {
240                 context_replace(pfolder, folder);
241                 context_save();
242                 fflush(stdout);
243         }
244
245         /*
246         ** If -nolink, then "remove" messages from source folder.
247         **
248         ** Note that folder_delmsgs does not call the delete hook
249         ** because the message has already been handled above.
250         */
251         if (!linkf) {
252                 folder_delmsgs(mp, unlink_msgs, 1);
253         }
254
255         clsfolds(folders, foldp);
256
257         if (mp->hghsel != mp->curmsg &&
258                                 (mp->numsel != mp->nummsg || linkf))
259                 seq_setcur(mp, mp->hghsel);
260         seq_save(mp);  /* synchronize message sequences */
261
262         context_replace(pfolder, folder);  /* update current folder   */
263         context_save();  /* save the context file   */
264         folder_free(mp);  /* free folder structure   */
265         done(0);
266         return 1;
267 }
268
269
270 /*
271 ** Read all the destination folders and
272 ** create folder structures for all of them.
273 */
274
275 static void
276 opnfolds(struct st_fold *folders, int nfolders)
277 {
278         char nmaildir[BUFSIZ];
279         register struct st_fold *fp, *ep;
280         register struct msgs *mp;
281
282         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
283                 chdir(m_maildir(""));
284                 strncpy(nmaildir, m_maildir(fp->f_name), sizeof(nmaildir));
285
286                 create_folder(nmaildir, 0, done);
287
288                 if (chdir(nmaildir) == NOTOK)
289                         adios(nmaildir, "unable to change directory to");
290                 if (!(mp = folder_read(fp->f_name)))
291                         adios(NULL, "unable to read folder %s", fp->f_name);
292                 mp->curmsg = 0;
293
294                 fp->f_mp = mp;
295
296                 chdir(maildir);
297         }
298 }
299
300
301 /*
302 ** Set the Previous-Sequence and then sychronize the
303 ** sequence file, for each destination folder.
304 */
305
306 static void
307 clsfolds(struct st_fold *folders, int nfolders)
308 {
309         register struct st_fold *fp, *ep;
310         register struct msgs *mp;
311
312         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
313                 mp = fp->f_mp;
314                 seq_setprev(mp);
315                 seq_save(mp);
316         }
317 }
318
319
320 /*
321 ** If you have a "rmmproc" defined, we called that
322 ** to remove all the specified files.  If "rmmproc"
323 ** is not defined, then just unlink the files.
324 */
325
326 static void
327 remove_files(int filep, char **files)
328 {
329         int i;
330         char **vec;
331
332         /* If rmmproc is defined, we use that */
333         if (rmmproc) {
334                 vec = files++;  /* vec[0] = filevec[0] */
335                 files[filep] = NULL;  /* NULL terminate list */
336
337                 fflush(stdout);
338                 vec[0] = r1bindex(rmmproc, '/');
339                 execvp(rmmproc, vec);
340                 adios(rmmproc, "unable to exec");
341         }
342
343         /* Else just unlink the files */
344         files++;  /* advance past filevec[0] */
345         for (i = 0; i < filep; i++) {
346                 if (unlink(files[i]) == NOTOK)
347                         admonish(files[i], "unable to unlink");
348         }
349 }
350
351
352 /*
353 ** Link (or copy) the message into each of
354 ** the destination folders.
355 */
356
357 static int
358 m_file(char *msgfile, struct st_fold *folders, int nfolders, int preserve,
359                 int refile)
360 {
361         int msgnum;
362         struct st_fold *fp, *ep;
363
364         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
365                 if ((msgnum = folder_addmsg(&fp->f_mp, msgfile, 1, 0,
366                                 preserve, nfolders == 1 && refile, maildir))
367                                 == -1)
368                         return 1;
369         }
370         return 0;
371 }