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