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