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