]> diplodocus.org Git - nmh/blob - uip/refile.c
Make the test suite work on systems other than Linux. Still needs work.
[nmh] / uip / refile.c
1
2 /*
3 * refile.c -- move or link message(s) from a source folder
4 * -- into one or more destination folders
5 *
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
9 */
10
11 #include <h/mh.h>
12 #include <h/utils.h>
13 #include <fcntl.h>
14 #include <errno.h>
15
16 static struct swit switches[] = {
17 #define DRAFTSW 0
18 { "draft", 0 },
19 #define LINKSW 1
20 { "link", 0 },
21 #define NLINKSW 2
22 { "nolink", 0 },
23 #define PRESSW 3
24 { "preserve", 0 },
25 #define NPRESSW 4
26 { "nopreserve", 0 },
27 #define UNLINKSW 5
28 { "unlink", 0 },
29 #define NUNLINKSW 6
30 { "nounlink", 0 },
31 #define SRCSW 7
32 { "src +folder", 0 },
33 #define FILESW 8
34 { "file file", 0 },
35 #define RPROCSW 9
36 { "rmmproc program", 0 },
37 #define NRPRCSW 10
38 { "normmproc", 0 },
39 #define VERSIONSW 11
40 { "version", 0 },
41 #define HELPSW 12
42 { "help", 0 },
43 { NULL, 0 }
44 };
45
46 static char maildir[BUFSIZ];
47
48 struct st_fold {
49 char *f_name;
50 struct msgs *f_mp;
51 };
52
53 /*
54 * static prototypes
55 */
56 static void opnfolds (struct st_fold *, int);
57 static void clsfolds (struct st_fold *, int);
58 static void remove_files (int, char **);
59 static int m_file (char *, struct st_fold *, int, int, int);
60
61
62 int
63 main (int argc, char **argv)
64 {
65 int linkf = 0, preserve = 0, filep = 0;
66 int foldp = 0, isdf = 0, unlink_msgs = 0;
67 int i, msgnum;
68 char *cp, *folder = NULL, buf[BUFSIZ];
69 char **argp, **arguments;
70 char *filevec[NFOLDERS + 2];
71 char **files = &filevec[1]; /* leave room for remove_files:vec[0] */
72 struct st_fold folders[NFOLDERS + 1];
73 struct msgs_array msgs = { 0, 0, NULL };
74 struct msgs *mp;
75
76 #ifdef LOCALE
77 setlocale(LC_ALL, "");
78 #endif
79 invo_name = r1bindex (argv[0], '/');
80
81 /* read user profile/context */
82 context_read();
83
84 arguments = getarguments (invo_name, argc, argv, 1);
85 argp = arguments;
86
87 /*
88 * Parse arguments
89 */
90 while ((cp = *argp++)) {
91 if (*cp == '-') {
92 switch (smatch (++cp, switches)) {
93 case AMBIGSW:
94 ambigsw (cp, switches);
95 done (1);
96 case UNKWNSW:
97 adios (NULL, "-%s unknown\n", cp);
98
99 case HELPSW:
100 snprintf (buf, sizeof(buf), "%s [msgs] [switches] +folder ...",
101 invo_name);
102 print_help (buf, switches, 1);
103 done (1);
104 case VERSIONSW:
105 print_version(invo_name);
106 done (1);
107
108 case LINKSW:
109 linkf++;
110 continue;
111 case NLINKSW:
112 linkf = 0;
113 continue;
114
115 case PRESSW:
116 preserve++;
117 continue;
118 case NPRESSW:
119 preserve = 0;
120 continue;
121
122 case UNLINKSW:
123 unlink_msgs++;
124 continue;
125 case NUNLINKSW:
126 unlink_msgs = 0;
127 continue;
128
129 case SRCSW:
130 if (folder)
131 adios (NULL, "only one source folder at a time!");
132 if (!(cp = *argp++) || *cp == '-')
133 adios (NULL, "missing argument to %s", argp[-2]);
134 folder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
135 *cp != '@' ? TFOLDER : TSUBCWF);
136 continue;
137 case DRAFTSW:
138 if (filep > NFOLDERS)
139 adios (NULL, "only %d files allowed!", NFOLDERS);
140 isdf = 0;
141 files[filep++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
142 continue;
143 case FILESW:
144 if (filep > NFOLDERS)
145 adios (NULL, "only %d files allowed!", NFOLDERS);
146 if (!(cp = *argp++) || *cp == '-')
147 adios (NULL, "missing argument to %s", argp[-2]);
148 files[filep++] = path (cp, TFILE);
149 continue;
150
151 case RPROCSW:
152 if (!(rmmproc = *argp++) || *rmmproc == '-')
153 adios (NULL, "missing argument to %s", argp[-2]);
154 continue;
155 case NRPRCSW:
156 rmmproc = NULL;
157 continue;
158 }
159 }
160 if (*cp == '+' || *cp == '@') {
161 if (foldp > NFOLDERS)
162 adios (NULL, "only %d folders allowed!", NFOLDERS);
163 folders[foldp++].f_name =
164 pluspath (cp);
165 } else
166 app_msgarg(&msgs, cp);
167 }
168
169 if (!context_find ("path"))
170 free (path ("./", TFOLDER));
171 if (foldp == 0)
172 adios (NULL, "no folder specified");
173
174 #ifdef WHATNOW
175 if (!msgs.size && !foldp && !filep && (cp = getenv ("mhdraft")) && *cp)
176 files[filep++] = cp;
177 #endif /* WHATNOW */
178
179 /*
180 * We are refiling a file to the folders
181 */
182 if (filep > 0) {
183 if (folder || msgs.size)
184 adios (NULL, "use -file or some messages, not both");
185 opnfolds (folders, foldp);
186 for (i = 0; i < filep; i++)
187 if (m_file (files[i], folders, foldp, preserve, 0))
188 done (1);
189 /* If -nolink, then "remove" files */
190 if (!linkf)
191 remove_files (filep, filevec);
192 done (0);
193 }
194
195 if (!msgs.size)
196 app_msgarg(&msgs, "cur");
197 if (!folder)
198 folder = getfolder (1);
199 strncpy (maildir, m_maildir (folder), sizeof(maildir));
200
201 if (chdir (maildir) == NOTOK)
202 adios (maildir, "unable to change directory to");
203
204 /* read source folder and create message structure */
205 if (!(mp = folder_read (folder)))
206 adios (NULL, "unable to read folder %s", folder);
207
208 /* check for empty folder */
209 if (mp->nummsg == 0)
210 adios (NULL, "no messages in %s", folder);
211
212 /* parse the message range/sequence/name and set SELECTED */
213 for (msgnum = 0; msgnum < msgs.size; msgnum++)
214 if (!m_convert (mp, msgs.msgs[msgnum]))
215 done (1);
216 seq_setprev (mp); /* set the previous-sequence */
217
218 /* create folder structures for each destination folder */
219 opnfolds (folders, foldp);
220
221 /* Link all the selected messages into destination folders.
222 *
223 * This causes the add hook to be run for messages that are
224 * linked into another folder. The refile hook is run for
225 * messages that are moved to another folder.
226 */
227 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
228 if (is_selected (mp, msgnum)) {
229 cp = getcpy (m_name (msgnum));
230 if (m_file (cp, folders, foldp, preserve, !linkf))
231 done (1);
232 free (cp);
233 }
234 }
235
236 /*
237 * This is a hack. If we are using an external rmmproc,
238 * then save the current folder to the context file,
239 * so the external rmmproc will remove files from the correct
240 * directory. This should be moved to folder_delmsgs().
241 */
242 if (rmmproc) {
243 context_replace (pfolder, folder);
244 context_save ();
245 fflush (stdout);
246 }
247
248 /* If -nolink, then "remove" messages from source folder.
249 *
250 * Note that folder_delmsgs does not call the delete hook
251 * because the message has already been handled above.
252 */
253 if (!linkf) {
254 folder_delmsgs (mp, unlink_msgs, 1);
255 }
256
257 clsfolds (folders, foldp);
258
259 if (mp->hghsel != mp->curmsg
260 && (mp->numsel != mp->nummsg || linkf))
261 seq_setcur (mp, mp->hghsel);
262 seq_save (mp); /* synchronize message sequences */
263
264 context_replace (pfolder, folder); /* update current folder */
265 context_save (); /* save the context file */
266 folder_free (mp); /* free folder structure */
267 done (0);
268 return 1;
269 }
270
271
272 /*
273 * Read all the destination folders and
274 * create folder structures for all of them.
275 */
276
277 static void
278 opnfolds (struct st_fold *folders, int nfolders)
279 {
280 char nmaildir[BUFSIZ];
281 register struct st_fold *fp, *ep;
282 register struct msgs *mp;
283
284 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
285 chdir (m_maildir (""));
286 strncpy (nmaildir, m_maildir (fp->f_name), sizeof(nmaildir));
287
288 create_folder (nmaildir, 0, done);
289
290 if (chdir (nmaildir) == NOTOK)
291 adios (nmaildir, "unable to change directory to");
292 if (!(mp = folder_read (fp->f_name)))
293 adios (NULL, "unable to read folder %s", fp->f_name);
294 mp->curmsg = 0;
295
296 fp->f_mp = mp;
297
298 chdir (maildir);
299 }
300 }
301
302
303 /*
304 * Set the Previous-Sequence and then sychronize the
305 * sequence file, for each destination folder.
306 */
307
308 static void
309 clsfolds (struct st_fold *folders, int nfolders)
310 {
311 register struct st_fold *fp, *ep;
312 register struct msgs *mp;
313
314 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
315 mp = fp->f_mp;
316 seq_setprev (mp);
317 seq_save (mp);
318 }
319 }
320
321
322 /*
323 * If you have a "rmmproc" defined, we called that
324 * to remove all the specified files. If "rmmproc"
325 * is not defined, then just unlink the files.
326 */
327
328 static void
329 remove_files (int filep, char **files)
330 {
331 int i;
332 char **vec;
333
334 /* If rmmproc is defined, we use that */
335 if (rmmproc) {
336 vec = files++; /* vec[0] = filevec[0] */
337 files[filep] = NULL; /* NULL terminate list */
338
339 fflush (stdout);
340 vec[0] = r1bindex (rmmproc, '/');
341 execvp (rmmproc, vec);
342 adios (rmmproc, "unable to exec");
343 }
344
345 /* Else just unlink the files */
346 files++; /* advance past filevec[0] */
347 for (i = 0; i < filep; i++) {
348 if (unlink (files[i]) == NOTOK)
349 admonish (files[i], "unable to unlink");
350 }
351 }
352
353
354 /*
355 * Link (or copy) the message into each of
356 * the destination folders.
357 */
358
359 static int
360 m_file (char *msgfile, struct st_fold *folders, int nfolders, int preserve, int refile)
361 {
362 int msgnum;
363 struct st_fold *fp, *ep;
364
365 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
366 if ((msgnum = folder_addmsg (&fp->f_mp, msgfile, 1, 0, preserve, nfolders == 1 && refile, maildir)) == -1)
367 return 1;
368 }
369 return 0;
370 }