Trash folder replaces rmmproc. Rework of rmm(1) and refile(1).
[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 SRCSW  2
21         { "src +folder", 0 },
22 #define FILESW  3
23         { "file file", 0 },
24 #define VERSIONSW  4
25         { "version", 0 },
26 #define HELPSW  5
27         { "help", 0 },
28         { NULL, 0 }
29 };
30
31 static char maildir[BUFSIZ];
32
33 struct st_fold {
34         char *f_name;
35         struct msgs *f_mp;
36 };
37
38 /*
39 ** static prototypes
40 */
41 static void opnfolds(struct st_fold *, int);
42 static void clsfolds(struct st_fold *, int);
43 static int m_file(char *, struct st_fold *, int, int);
44
45
46 int
47 main(int argc, char **argv)
48 {
49         int linkf = 0, filep = 0;
50         int foldp = 0;
51         int i, msgnum;
52         char *cp, *folder = NULL, buf[BUFSIZ];
53         char **argp, **arguments;
54         char *filevec[NFOLDERS + 1];
55         char **files = filevec;
56         struct st_fold folders[NFOLDERS + 1];
57         struct msgs_array msgs = { 0, 0, NULL };
58         struct msgs *mp;
59
60 #ifdef LOCALE
61         setlocale(LC_ALL, "");
62 #endif
63         invo_name = mhbasename(argv[0]);
64
65         context_read();
66
67         arguments = getarguments(invo_name, argc, argv, 1);
68         argp = arguments;
69
70         /*
71         ** Parse arguments
72         */
73         while ((cp = *argp++)) {
74                 if (*cp == '-') {
75                         switch (smatch(++cp, switches)) {
76                         case AMBIGSW:
77                                 ambigsw(cp, switches);
78                                 done(1);
79                         case UNKWNSW:
80                                 adios(NULL, "-%s unknown\n", cp);
81
82                         case HELPSW:
83                                 snprintf(buf, sizeof(buf), "%s [msgs] [switches] +folder ...", invo_name);
84                                 print_help(buf, switches, 1);
85                                 done(1);
86                         case VERSIONSW:
87                                 print_version(invo_name);
88                                 done(1);
89
90                         case LINKSW:
91                                 linkf++;
92                                 continue;
93                         case NLINKSW:
94                                 linkf = 0;
95                                 continue;
96
97                         case SRCSW:
98                                 if (folder)
99                                         adios(NULL, "only one source folder at a time!");
100                                 if (!(cp = *argp++) || *cp == '-')
101                                         adios(NULL, "missing argument to %s",
102                                                         argp[-2]);
103                                 folder = getcpy(expandfol(cp));
104                                 continue;
105                         case FILESW:
106                                 if (filep > NFOLDERS)
107                                         adios(NULL, "only %d files allowed!",
108                                                         NFOLDERS);
109                                 if (!(cp = *argp++) || *cp == '-')
110                                         adios(NULL, "missing argument to %s",
111                                                         argp[-2]);
112                                 files[filep++] = getcpy(expanddir(cp));
113                                 continue;
114                         }
115                 }
116                 if (*cp == '+' || *cp == '@') {
117                         if (foldp > NFOLDERS)
118                                 adios(NULL, "only %d folders allowed!",
119                                                 NFOLDERS);
120                         folders[foldp++].f_name = getcpy(expandfol(cp));
121                 } else
122                         app_msgarg(&msgs, cp);
123         }
124
125         if (foldp == 0)
126                 adios(NULL, "no folder specified");
127
128 #ifdef WHATNOW
129         if (!msgs.size && !foldp && !filep && (cp = getenv("mhdraft")) && *cp)
130                 files[filep++] = cp;
131 #endif /* WHATNOW */
132
133         /*
134         ** We are refiling a file to the folders
135         */
136         if (filep > 0) {
137                 if (folder || msgs.size)
138                         adios(NULL, "use -file or some messages, not both");
139                 opnfolds(folders, foldp);
140                 for (i = 0; i < filep; i++)
141                         if (m_file(files[i], folders, foldp, 0))
142                                 done(1);
143                 /* If -nolink, then unlink files */
144                 if (!linkf) {
145                         int i;
146                         char **files = filevec;
147
148                         /* just unlink the files */
149                         for (i = 0; i < filep; i++) {
150                                 if (unlink(files[i]) == NOTOK)
151                                         admonish(files[i], "unable to unlink");
152                         }
153                 }
154                 done(0);
155         }
156
157         if (!msgs.size)
158                 app_msgarg(&msgs, seq_cur);
159         if (!folder)
160                 folder = getcurfol();
161         strncpy(maildir, toabsdir(folder), sizeof(maildir));
162
163         if (chdir(maildir) == NOTOK)
164                 adios(maildir, "unable to change directory to");
165
166         /* read source folder and create message structure */
167         if (!(mp = folder_read(folder)))
168                 adios(NULL, "unable to read folder %s", folder);
169
170         /* check for empty folder */
171         if (mp->nummsg == 0)
172                 adios(NULL, "no messages in %s", folder);
173
174         /* parse the message range/sequence/name and set SELECTED */
175         for (msgnum = 0; msgnum < msgs.size; msgnum++)
176                 if (!m_convert(mp, msgs.msgs[msgnum]))
177                         done(1);
178         seq_setprev(mp);  /* set the previous-sequence */
179
180         /* create folder structures for each destination folder */
181         opnfolds(folders, foldp);
182
183         /* Link all the selected messages into destination folders.
184         **
185         ** This causes the add hook to be run for messages that are
186         ** linked into another folder.  The refile hook is run for
187         ** messages that are moved to another folder.
188         */
189         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
190                 if (is_selected(mp, msgnum)) {
191                         cp = getcpy(m_name(msgnum));
192                         if (m_file(cp, folders, foldp, !linkf))
193                                 done(1);
194                         free(cp);
195                 }
196         }
197
198         /*
199         ** If -nolink, then remove (= unlink) messages from source folder.
200         **
201         ** Note that folder_delmsgs does not call the delete hook
202         ** because the message has already been handled above.
203         */
204         if (!linkf) {
205                 folder_delmsgs(mp, 0);
206         }
207
208         clsfolds(folders, foldp);
209
210         if (linkf) {
211                 seq_setcur(mp, mp->hghsel);
212         }
213         seq_save(mp);  /* synchronize message sequences */
214
215         context_replace(curfolder, folder);
216         context_save();
217         folder_free(mp);
218         done(0);
219         return 1;
220 }
221
222
223 /*
224 ** Read all the destination folders and
225 ** create folder structures for all of them.
226 */
227 static void
228 opnfolds(struct st_fold *folders, int nfolders)
229 {
230         char nmaildir[BUFSIZ];
231         register struct st_fold *fp, *ep;
232         register struct msgs *mp;
233
234         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
235                 chdir(toabsdir("+"));
236                 strncpy(nmaildir, toabsdir(fp->f_name), sizeof(nmaildir));
237
238                 create_folder(nmaildir, 0, done);
239
240                 if (chdir(nmaildir) == NOTOK)
241                         adios(nmaildir, "unable to change directory to");
242                 if (!(mp = folder_read(fp->f_name)))
243                         adios(NULL, "unable to read folder %s", fp->f_name);
244                 mp->curmsg = 0;
245
246                 fp->f_mp = mp;
247
248                 chdir(maildir);
249         }
250 }
251
252
253 /*
254 ** Set the Previous-Sequence and then sychronize the
255 ** sequence file, for each destination folder.
256 */
257 static void
258 clsfolds(struct st_fold *folders, int nfolders)
259 {
260         register struct st_fold *fp, *ep;
261         register struct msgs *mp;
262
263         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
264                 mp = fp->f_mp;
265                 seq_setprev(mp);
266                 seq_save(mp);
267         }
268 }
269
270
271 /*
272 ** Link (or copy) the message into each of
273 ** the destination folders.
274 */
275 static int
276 m_file(char *msgfile, struct st_fold *folders, int nfolders, int refile)
277 {
278         int msgnum;
279         struct st_fold *fp, *ep;
280
281         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
282                 if ((msgnum = folder_addmsg(&fp->f_mp, msgfile, 1, 0,
283                                 0, nfolders == 1 && refile, maildir)) == -1)
284                         return 1;
285         }
286         return 0;
287 }