]> diplodocus.org Git - nmh/blob - uip/repl.c
sbr/vector.c: Replace Nbby with <limits.h>'s CHAR_BIT.
[nmh] / uip / repl.c
1 /* repl.c -- reply to a message
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/mime.h>
10 #include <h/utils.h>
11
12 #define REPL_SWITCHES \
13 X("group", 0, GROUPSW) \
14 X("nogroup", 0, NGROUPSW) \
15 X("annotate", 0, ANNOSW) \
16 X("noannotate", 0, NANNOSW) \
17 X("cc all|to|cc|me", 0, CCSW) \
18 X("nocc all|to|cc|me", 0, NCCSW) \
19 X("draftfolder +folder", 0, DFOLDSW) \
20 X("draftmessage msg", 0, DMSGSW) \
21 X("nodraftfolder", 0, NDFLDSW) \
22 X("editor editor", 0, EDITRSW) \
23 X("noedit", 0, NEDITSW) \
24 X("convertargs type argstring", 0, CONVERTARGSW) \
25 X("fcc folder", 0, FCCSW) \
26 X("filter filterfile", 0, FILTSW) \
27 X("form formfile", 0, FORMSW) \
28 X("format", 5, FRMTSW) \
29 X("noformat", 7, NFRMTSW) \
30 X("inplace", 0, INPLSW) \
31 X("noinplace", 0, NINPLSW) \
32 X("mime", 0, MIMESW) \
33 X("nomime", 0, NMIMESW) \
34 X("query", 0, QURYSW) \
35 X("noquery", 0, NQURYSW) \
36 X("whatnowproc program", 0, WHATSW) \
37 X("nowhatnowproc", 0, NWHATSW) \
38 X("width columns", 0, WIDTHSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
41 X("file file", 4, FILESW) /* interface from msh */ \
42 X("build", 5, BILDSW) /* interface from mhe */ \
43 X("atfile", 0, ATFILESW) \
44 X("noatfile", 0, NOATFILESW) \
45 X("fmtproc program", 0, FMTPROCSW) \
46 X("nofmtproc", 0, NFMTPROCSW) \
47
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(REPL);
50 #undef X
51
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(REPL, switches);
54 #undef X
55
56 #define CC_SWITCHES \
57 X("to", 0, CTOSW) \
58 X("cc", 0, CCCSW) \
59 X("me", 0, CMESW) \
60 X("all", 0, CALSW) \
61
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(CC);
64 #undef X
65
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(CC, ccswitches);
68 #undef X
69
70 #define DISPO_SWITCHES \
71 X("quit", 0, NOSW) \
72 X("replace", 0, YESW) \
73 X("list", 0, LISTDSW) \
74 X("refile +folder", 0, REFILSW) \
75 X("new", 0, NEWSW) \
76
77 #define X(sw, minchars, id) id,
78 DEFINE_SWITCH_ENUM(DISPO);
79 #undef X
80
81 #define X(sw, minchars, id) { sw, minchars, id },
82 DEFINE_SWITCH_ARRAY(DISPO, aqrnl);
83 #undef X
84
85 static struct swit aqrl[] = {
86 { "quit", 0, NOSW },
87 { "replace", 0, YESW },
88 { "list", 0, LISTDSW },
89 { "refile +folder", 0, REFILSW },
90 { NULL, 0, 0 }
91 };
92
93 short ccto = -1; /* global for replsbr */
94 short cccc = -1;
95 short ccme = -1;
96 short querysw = 0;
97
98 static short outputlinelen = OUTPUTLINELEN;
99 static short groupreply = 0; /* Is this a group reply? */
100
101 static int mime = 0; /* include original as MIME part */
102 static char *form = NULL; /* form (components) file */
103 static char *filter = NULL; /* message filter file */
104 static char *fcc = NULL; /* folders to add to Fcc: header */
105
106
107 /*
108 * prototypes
109 */
110 static void docc (char *, int);
111 static void add_convert_header (const char *, char *, char *, char *);
112
113
114 int
115 main (int argc, char **argv)
116 {
117 int i, isdf = 0;
118 int anot = 0, inplace = 1;
119 int nedit = 0, nwhat = 0;
120 int atfile = 0;
121 int fmtproc = -1;
122 char *cp, *cwd, *dp, *maildir, *file = NULL;
123 char *folder = NULL, *msg = NULL, *dfolder = NULL;
124 char *dmsg = NULL, *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
125 char **argp, **arguments;
126 svector_t convert_types = svector_create (10);
127 svector_t convert_args = svector_create (10);
128 size_t n;
129 struct msgs *mp = NULL;
130 struct stat st;
131 FILE *in;
132
133 int buildsw = 0;
134
135 if (nmh_init(argv[0], 1)) { return 1; }
136
137 arguments = getarguments (invo_name, argc, argv, 1);
138 argp = arguments;
139
140 while ((cp = *argp++)) {
141 if (*cp == '-') {
142 switch (smatch (++cp, switches)) {
143 case AMBIGSW:
144 ambigsw (cp, switches);
145 done (1);
146 case UNKWNSW:
147 adios (NULL, "-%s unknown", cp);
148
149 case HELPSW:
150 snprintf (buf, sizeof(buf), "%s: [+folder] [msg] [switches]",
151 invo_name);
152 print_help (buf, switches, 1);
153 done (0);
154 case VERSIONSW:
155 print_version(invo_name);
156 done (0);
157
158 case GROUPSW:
159 groupreply++;
160 continue;
161 case NGROUPSW:
162 groupreply = 0;
163 continue;
164
165 case ANNOSW:
166 anot++;
167 continue;
168 case NANNOSW:
169 anot = 0;
170 continue;
171
172 case CCSW:
173 if (!(cp = *argp++) || *cp == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 docc (cp, 1);
176 continue;
177 case NCCSW:
178 if (!(cp = *argp++) || *cp == '-')
179 adios (NULL, "missing argument to %s", argp[-2]);
180 docc (cp, 0);
181 continue;
182
183 case EDITRSW:
184 if (!(ed = *argp++) || *ed == '-')
185 adios (NULL, "missing argument to %s", argp[-2]);
186 nedit = 0;
187 continue;
188 case NEDITSW:
189 nedit++;
190 continue;
191
192 case CONVERTARGSW: {
193 char *type;
194 size_t i;
195
196 if (!(type = *argp++)) {
197 adios (NULL, "missing type argument to %s", argp[-2]);
198 }
199 if (!(cp = *argp++)) {
200 adios (NULL, "missing argstring argument to %s",
201 argp[-3]);
202 }
203
204 for (i = 0; i < svector_size (convert_types); ++i) {
205 if (! strcmp (svector_at (convert_types, i), type)) {
206 /* Already saw this type, so just update
207 its args. */
208 svector_strs (convert_args)[i] = cp;
209 break;
210 }
211 }
212
213 if (i == svector_size (convert_types)) {
214 svector_push_back (convert_types, type);
215 svector_push_back (convert_args, cp);
216 }
217 continue;
218 }
219
220 case WHATSW:
221 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
222 adios (NULL, "missing argument to %s", argp[-2]);
223 nwhat = 0;
224 continue;
225 case BILDSW:
226 buildsw++;
227 /* FALLTHRU */
228 case NWHATSW:
229 nwhat++;
230 continue;
231
232 case FCCSW:
233 if (!(cp = *argp++) || *cp == '-')
234 adios (NULL, "missing argument to %s", argp[-2]);
235 dp = NULL;
236 if (*cp == '@')
237 cp = dp = path (cp + 1, TSUBCWF);
238 if (fcc)
239 fcc = add (", ", fcc);
240 fcc = add (cp, fcc);
241 mh_xfree(dp);
242 continue;
243
244 case FILESW:
245 if (file)
246 adios (NULL, "only one file at a time!");
247 if (!(cp = *argp++) || *cp == '-')
248 adios (NULL, "missing argument to %s", argp[-2]);
249 file = path (cp, TFILE);
250 continue;
251 case FILTSW:
252 if (!(cp = *argp++) || *cp == '-')
253 adios (NULL, "missing argument to %s", argp[-2]);
254 filter = getcpy (etcpath (cp));
255 mime = 0;
256 continue;
257 case FORMSW:
258 if (!(form = *argp++) || *form == '-')
259 adios (NULL, "missing argument to %s", argp[-2]);
260 continue;
261
262 case FRMTSW:
263 filter = getcpy (etcpath (mhlreply));
264 mime = 0;
265 continue;
266 case NFRMTSW:
267 filter = NULL;
268 continue;
269
270 case INPLSW:
271 inplace++;
272 continue;
273 case NINPLSW:
274 inplace = 0;
275 continue;
276
277 case MIMESW:
278 mime++;
279 filter = NULL;
280 continue;
281 case NMIMESW:
282 mime = 0;
283 continue;
284
285 case QURYSW:
286 querysw++;
287 continue;
288 case NQURYSW:
289 querysw = 0;
290 continue;
291
292 case WIDTHSW:
293 if (!(cp = *argp++) || *cp == '-')
294 adios (NULL, "missing argument to %s", argp[-2]);
295 if ((outputlinelen = atoi (cp)) < 10)
296 adios (NULL, "impossible width %d", outputlinelen);
297 continue;
298
299 case DFOLDSW:
300 if (dfolder)
301 adios (NULL, "only one draft folder at a time!");
302 if (!(cp = *argp++) || *cp == '-')
303 adios (NULL, "missing argument to %s", argp[-2]);
304 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
305 *cp != '@' ? TFOLDER : TSUBCWF);
306 continue;
307 case DMSGSW:
308 if (dmsg)
309 adios (NULL, "only one draft message at a time!");
310 if (!(dmsg = *argp++) || *dmsg == '-')
311 adios (NULL, "missing argument to %s", argp[-2]);
312 continue;
313 case NDFLDSW:
314 dfolder = NULL;
315 isdf = NOTOK;
316 continue;
317
318 case ATFILESW:
319 atfile++;
320 continue;
321 case NOATFILESW:
322 atfile = 0;
323 continue;
324
325 case FMTPROCSW:
326 if (!(formatproc = *argp++) || *formatproc == '-')
327 adios (NULL, "missing argument to %s", argp[-2]);
328 fmtproc = 1;
329 continue;
330 case NFMTPROCSW:
331 fmtproc = 0;
332 continue;
333 }
334 }
335 if (*cp == '+' || *cp == '@') {
336 if (folder)
337 adios (NULL, "only one folder at a time!");
338 else
339 folder = pluspath (cp);
340 } else {
341 if (msg)
342 adios (NULL, "only one message at a time!");
343 else
344 msg = cp;
345 }
346 }
347
348 if (ccto == -1)
349 ccto = groupreply;
350 if (cccc == -1)
351 cccc = groupreply;
352 if (ccme == -1)
353 ccme = groupreply;
354
355 cwd = mh_xstrdup(pwd ());
356
357 if (!context_find ("path"))
358 free (path ("./", TFOLDER));
359 if (file && (msg || folder))
360 adios (NULL, "can't mix files and folders/msgs");
361
362 try_it_again:
363
364 strncpy (drft, buildsw ? m_maildir ("reply")
365 : m_draft (dfolder, NULL, NOUSE, &isdf), sizeof(drft));
366
367 /* Check if a draft exists */
368 if (!buildsw && stat (drft, &st) != NOTOK) {
369 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
370 for (i = LISTDSW; i != YESW;) {
371 if (!(argp = read_switch_multiword ("\nDisposition? ",
372 isdf ? aqrnl : aqrl)))
373 done (1);
374 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
375 case NOSW:
376 done (0);
377 case NEWSW:
378 dmsg = NULL;
379 goto try_it_again;
380 case YESW:
381 break;
382 case LISTDSW:
383 showfile (++argp, drft);
384 break;
385 case REFILSW:
386 if (refile (++argp, drft) == 0)
387 i = YESW;
388 break;
389 default:
390 inform("say what?");
391 break;
392 }
393 }
394 }
395
396 if (file) {
397 /*
398 * We are replying to a file.
399 */
400 anot = 0; /* we don't want to annotate a file */
401 } else {
402 /*
403 * We are replying to a message.
404 */
405 if (!msg)
406 msg = "cur";
407 if (!folder)
408 folder = getfolder (1);
409 maildir = m_maildir (folder);
410
411 if (chdir (maildir) == NOTOK)
412 adios (maildir, "unable to change directory to");
413
414 /* read folder and create message structure */
415 if (!(mp = folder_read (folder, 1)))
416 adios (NULL, "unable to read folder %s", folder);
417
418 /* check for empty folder */
419 if (mp->nummsg == 0)
420 adios (NULL, "no messages in %s", folder);
421
422 /* parse the message range/sequence/name and set SELECTED */
423 if (!m_convert (mp, msg))
424 done (1);
425 seq_setprev (mp); /* set the previous-sequence */
426
427 if (mp->numsel > 1)
428 adios (NULL, "only one message at a time!");
429
430 context_replace (pfolder, folder); /* update current folder */
431 seq_setcur (mp, mp->lowsel); /* update current message */
432 seq_save (mp); /* synchronize sequences */
433 context_save (); /* save the context file */
434 }
435
436 msg = file ? file : mh_xstrdup(m_name (mp->lowsel));
437
438 if ((in = fopen (msg, "r")) == NULL)
439 adios (msg, "unable to open");
440
441 /* find form (components) file */
442 if (!form) {
443 if (groupreply)
444 form = etcpath (replgroupcomps);
445 else
446 form = etcpath (replcomps);
447 }
448
449 replout (in, msg, drft, mp, outputlinelen, mime, form, filter,
450 fcc, fmtproc);
451 fclose (in);
452
453 {
454 char *filename = file ? file : concat (mp->foldpath, "/", msg, NULL);
455
456 for (n = 0; n < svector_size (convert_types); ++n) {
457 add_convert_header (svector_at (convert_types, n),
458 svector_at (convert_args, n),
459 filename, drft);
460 }
461 if (! file) {
462 free (filename);
463 }
464 }
465
466 if (nwhat)
467 done (0);
468 what_now (ed, nedit, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL,
469 inplace, cwd, atfile);
470
471 svector_free (convert_args);
472 svector_free (convert_types);
473
474 done (1);
475 return 1;
476 }
477
478 void
479 docc (char *cp, int ccflag)
480 {
481 switch (smatch (cp, ccswitches)) {
482 case AMBIGSW:
483 ambigsw (cp, ccswitches);
484 done (1);
485 case UNKWNSW:
486 adios (NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
487
488 case CTOSW:
489 ccto = ccflag;
490 break;
491
492 case CCCSW:
493 cccc = ccflag;
494 break;
495
496 case CMESW:
497 ccme = ccflag;
498 break;
499
500 case CALSW:
501 ccto = cccc = ccme = ccflag;
502 break;
503 }
504 }
505
506 /*
507 * Add pseudoheaders that will pass the convert arguments to
508 * mhbuild. They have the form:
509 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
510 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
511 * The ARGS pseudoheader is optional, but we always add it when
512 * -convertargs is used.
513 */
514 void
515 add_convert_header (const char *convert_type, char *convert_arg,
516 char *filename, char *drft) {
517 char *field_name;
518
519 field_name = concat (MHBUILD_FILE_PSEUDOHEADER, convert_type, NULL);
520 annotate (drft, field_name, filename, 1, 0, -2, 1);
521 free (field_name);
522
523 field_name = concat (MHBUILD_ARGS_PSEUDOHEADER, convert_type, NULL);
524 annotate (drft, field_name, convert_arg, 1, 0, -2, 1);
525 free (field_name);
526 }