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