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