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