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