Move #include from h/mh.h to source files
[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
17 static struct swit switches[] = {
18 #define LINKSW  0
19         { "link", 0 },
20 #define NLINKSW  1
21         { "nolink", 2 },
22 #define SRCSW  2
23         { "src +folder", 0 },
24 #define FILESW  3
25         { "file file", 0 },
26 #define VERSIONSW  4
27         { "Version", 0 },
28 #define HELPSW  5
29         { "help", 0 },
30         { NULL, 0 }
31 };
32
33 static char maildir[BUFSIZ];
34
35 struct st_fold {
36         char *f_name;
37         struct msgs *f_mp;
38 };
39
40 /*
41 ** static prototypes
42 */
43 static void opnfolds(struct st_fold *, int);
44 static void clsfolds(struct st_fold *, int);
45 static int m_file(char *, struct st_fold *, int, int);
46
47
48 int
49 main(int argc, char **argv)
50 {
51         int linkf = 0, filep = 0;
52         int foldp = 0;
53         int i, msgnum;
54         char *cp, *folder = NULL, buf[BUFSIZ];
55         char **argp, **arguments;
56         char *filevec[NFOLDERS + 1];
57         char **files = filevec;
58         struct st_fold folders[NFOLDERS + 1];
59         struct msgs_array msgs = { 0, 0, NULL };
60         struct msgs *mp;
61
62         setlocale(LC_ALL, "");
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                                 exit(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                                 exit(0);
86                         case VERSIONSW:
87                                 print_version(invo_name);
88                                 exit(0);
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         /*
129         ** We are refiling a file to the folders
130         */
131         if (filep > 0) {
132                 if (folder || msgs.size)
133                         adios(NULL, "use -file or msgs, not both");
134                 opnfolds(folders, foldp);
135                 for (i = 0; i < filep; i++)
136                         if (m_file(files[i], folders, foldp, 0))
137                                 /* sysexits.h EX_IOERR */
138                                 exit(1);
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(0);
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(maildir, "unable to change directory to");
161
162         /* read source folder and create message structure */
163         if (!(mp = folder_read(folder)))
164                 adios(NULL, "unable to read folder %s", folder);
165
166         /* check for empty folder */
167         if (mp->nummsg == 0)
168                 adios(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                         /* sysexits.h EX_USAGE */
174                         exit(1);
175         seq_setprev(mp);  /* set the previous-sequence */
176
177         /* create folder structures for each destination folder */
178         opnfolds(folders, foldp);
179
180         /* Link all the selected messages into destination folders.
181         **
182         ** This causes the add hook to be run for messages that are
183         ** linked into another folder.  The refile hook is run for
184         ** messages that are moved to another folder.
185         */
186         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
187                 if (is_selected(mp, msgnum)) {
188                         cp = getcpy(m_name(msgnum));
189                         if (m_file(cp, folders, foldp, !linkf))
190                                 /* sysexits.h EX_IOERR */
191                                 exit(1);
192                         free(cp);
193                 }
194         }
195
196         /*
197         ** If -nolink, then remove (= unlink) messages from source folder.
198         **
199         ** Note that folder_delmsgs does not call the delete hook
200         ** because the message has already been handled above.
201         */
202         if (!linkf) {
203                 folder_delmsgs(mp, 0);
204         }
205
206         clsfolds(folders, foldp);
207
208         if (linkf) {
209                 seq_setcur(mp, mp->hghsel);
210         }
211         seq_save(mp);  /* synchronize message sequences */
212
213         context_replace(curfolder, folder);
214         context_save();
215         folder_free(mp);
216         return 0;
217 }
218
219
220 /*
221 ** Read all the destination folders and
222 ** create folder structures for all of them.
223 */
224 static void
225 opnfolds(struct st_fold *folders, int nfolders)
226 {
227         char nmaildir[BUFSIZ];
228         register struct st_fold *fp, *ep;
229         register struct msgs *mp;
230
231         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
232                 chdir(toabsdir("+"));
233                 strncpy(nmaildir, toabsdir(fp->f_name), sizeof(nmaildir));
234
235                 create_folder(nmaildir, 0, exit);
236
237                 if (chdir(nmaildir) == NOTOK)
238                         adios(nmaildir, "unable to change directory to");
239                 if (!(mp = folder_read(fp->f_name)))
240                         adios(NULL, "unable to read folder %s", fp->f_name);
241                 mp->curmsg = 0;
242
243                 fp->f_mp = mp;
244
245                 chdir(maildir);
246         }
247 }
248
249
250 /*
251 ** Set the Previous-Sequence and then sychronize the
252 ** sequence file, for each destination folder.
253 */
254 static void
255 clsfolds(struct st_fold *folders, int nfolders)
256 {
257         register struct st_fold *fp, *ep;
258         register struct msgs *mp;
259
260         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
261                 mp = fp->f_mp;
262                 seq_setprev(mp);
263                 seq_save(mp);
264         }
265 }
266
267
268 /*
269 ** Link (or copy) the message into each of
270 ** the destination folders.
271 */
272 static int
273 m_file(char *msgfile, struct st_fold *folders, int nfolders, int refile)
274 {
275         int msgnum;
276         struct st_fold *fp, *ep;
277
278         for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
279                 if ((msgnum = folder_addmsg(&fp->f_mp, msgfile, 1, 0,
280                                 0, nfolders == 1 && refile, maildir)) == -1)
281                         return 1;
282         }
283         return 0;
284 }