01f205beac7025aec2c16ae654d40388a54092c7
[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 = getcpy(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++] = getcpy(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 = getcpy(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         /*
130         ** We are refiling a file to the folders
131         */
132         if (filep > 0) {
133                 if (folder || msgs.size)
134                         adios(EX_USAGE, NULL, "use -file or msgs, not both");
135                 opnfolds(folders, foldp);
136                 for (i = 0; i < filep; i++)
137                         if (m_file(files[i], folders, foldp, 0))
138                                 exit(EX_IOERR);
139                 /* If -nolink, then unlink files */
140                 if (!linkf) {
141                         int i;
142                         char **files = filevec;
143
144                         /* just unlink the files */
145                         for (i = 0; i < filep; i++) {
146                                 if (unlink(files[i]) == NOTOK)
147                                         admonish(files[i], "unable to unlink");
148                         }
149                 }
150                 exit(EX_OK);
151         }
152
153         if (!msgs.size)
154                 app_msgarg(&msgs, seq_cur);
155         if (!folder)
156                 folder = getcurfol();
157         strncpy(maildir, toabsdir(folder), sizeof(maildir));
158
159         if (chdir(maildir) == NOTOK)
160                 adios(EX_OSERR, maildir, "unable to change directory to");
161
162         /* read source folder and create message structure */
163         if (!(mp = folder_read(folder)))
164                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
165
166         /* check for empty folder */
167         if (mp->nummsg == 0)
168                 adios(EX_DATAERR, NULL, "no messages in %s", folder);
169
170         /* parse the message range/sequence/name and set SELECTED */
171         for (msgnum = 0; msgnum < msgs.size; msgnum++)
172                 if (!m_convert(mp, msgs.msgs[msgnum]))
173                         exit(EX_SOFTWARE);
174         seq_setprev(mp);  /* set the previous-sequence */
175
176         /* create folder structures for each destination folder */
177         opnfolds(folders, foldp);
178
179         /* Link all the selected messages into destination folders.
180         **
181         ** This causes the add hook to be run for messages that are
182         ** linked into another folder.  The refile hook is run for
183         ** messages that are moved to another folder.
184         */
185         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
186                 if (is_selected(mp, msgnum)) {
187                         cp = getcpy(m_name(msgnum));
188                         if (m_file(cp, folders, foldp, !linkf))
189                                 exit(EX_IOERR);
190                         free(cp);
191                 }
192         }
193
194         /*
195         ** If -nolink, then remove (= unlink) messages from source folder.
196         **
197         ** Note that folder_delmsgs does not call the delete hook
198         ** because the message has already been handled above.
199         */
200         if (!linkf) {
201                 folder_delmsgs(mp, 0);
202         }
203
204         clsfolds(folders, foldp);
205
206         if (linkf) {
207                 seq_setcur(mp, mp->hghsel);
208         }
209         seq_save(mp);  /* synchronize message sequences */
210
211         context_replace(curfolder, folder);
212         context_save();
213         folder_free(mp);
214         return 0;
215 }
216
217
218 /*
219 ** Read all the destination folders and
220 ** create folder structures for all of them.
221 */
222 static void
223 opnfolds(struct st_fold *folders, int nfolders)
224 {
225         char nmaildir[BUFSIZ];
226         register struct st_fold *fp, *ep;
227         register struct msgs *mp;
228
229         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
230                 chdir(toabsdir("+"));
231                 strncpy(nmaildir, toabsdir(fp->f_name), sizeof(nmaildir));
232
233                 create_folder(nmaildir, 0, exit);
234
235                 if (chdir(nmaildir) == NOTOK)
236                         adios(EX_OSERR, nmaildir, "unable to change directory to");
237                 if (!(mp = folder_read(fp->f_name)))
238                         adios(EX_IOERR, NULL, "unable to read folder %s", fp->f_name);
239                 mp->curmsg = 0;
240
241                 fp->f_mp = mp;
242
243                 chdir(maildir);
244         }
245 }
246
247
248 /*
249 ** Set the Previous-Sequence and then sychronize the
250 ** sequence file, for each destination folder.
251 */
252 static void
253 clsfolds(struct st_fold *folders, int nfolders)
254 {
255         register struct st_fold *fp, *ep;
256         register struct msgs *mp;
257
258         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
259                 mp = fp->f_mp;
260                 seq_setprev(mp);
261                 seq_save(mp);
262         }
263 }
264
265
266 /*
267 ** Link (or copy) the message into each of
268 ** the destination folders.
269 */
270 static int
271 m_file(char *msgfile, struct st_fold *folders, int nfolders, int refile)
272 {
273         int msgnum;
274         struct st_fold *fp, *ep;
275
276         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
277                 if ((msgnum = folder_addmsg(&fp->f_mp, msgfile, 1, 0,
278                                 0, nfolders == 1 && refile, maildir)) == -1)
279                         return 1;
280         }
281         return 0;
282 }