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