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