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