]> diplodocus.org Git - nmh/blob - uip/repl.c
Update manpages to use .TP for tagged paragraphs (part I).
[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 type", 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 short outputlinelen = OUTPUTLINELEN;
100 short groupreply = 0; /* Is this a group reply? */
101
102 int mime = 0; /* include original as MIME part */
103 char *form = NULL; /* form (components) file */
104 char *filter = NULL; /* message filter file */
105 char *fcc = NULL; /* folders to add to Fcc: header */
106
107
108 /*
109 * prototypes
110 */
111 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 #ifdef LOCALE
133 setlocale(LC_ALL, "");
134 #endif
135 invo_name = r1bindex (argv[0], '/');
136
137 /* read user profile/context */
138 context_read();
139
140 arguments = getarguments (invo_name, argc, argv, 1);
141 argp = arguments;
142
143 while ((cp = *argp++)) {
144 if (*cp == '-') {
145 switch (smatch (++cp, switches)) {
146 case AMBIGSW:
147 ambigsw (cp, switches);
148 done (1);
149 case UNKWNSW:
150 adios (NULL, "-%s unknown", cp);
151
152 case HELPSW:
153 snprintf (buf, sizeof(buf), "%s: [+folder] [msg] [switches]",
154 invo_name);
155 print_help (buf, switches, 1);
156 done (0);
157 case VERSIONSW:
158 print_version(invo_name);
159 done (0);
160
161 case GROUPSW:
162 groupreply++;
163 continue;
164 case NGROUPSW:
165 groupreply = 0;
166 continue;
167
168 case ANNOSW:
169 anot++;
170 continue;
171 case NANNOSW:
172 anot = 0;
173 continue;
174
175 case CCSW:
176 if (!(cp = *argp++) || *cp == '-')
177 adios (NULL, "missing argument to %s", argp[-2]);
178 docc (cp, 1);
179 continue;
180 case NCCSW:
181 if (!(cp = *argp++) || *cp == '-')
182 adios (NULL, "missing argument to %s", argp[-2]);
183 docc (cp, 0);
184 continue;
185
186 case EDITRSW:
187 if (!(ed = *argp++) || *ed == '-')
188 adios (NULL, "missing argument to %s", argp[-2]);
189 nedit = 0;
190 continue;
191 case NEDITSW:
192 nedit++;
193 continue;
194
195 case WHATSW:
196 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
197 adios (NULL, "missing argument to %s", argp[-2]);
198 nwhat = 0;
199 continue;
200 case BILDSW:
201 buildsw++; /* fall... */
202 case NWHATSW:
203 nwhat++;
204 continue;
205
206 case FCCSW:
207 if (!(cp = *argp++) || *cp == '-')
208 adios (NULL, "missing argument to %s", argp[-2]);
209 dp = NULL;
210 if (*cp == '@')
211 cp = dp = path (cp + 1, TSUBCWF);
212 if (fcc)
213 fcc = add (", ", fcc);
214 fcc = add (cp, fcc);
215 if (dp)
216 free (dp);
217 continue;
218
219 case FILESW:
220 if (file)
221 adios (NULL, "only one file at a time!");
222 if (!(cp = *argp++) || *cp == '-')
223 adios (NULL, "missing argument to %s", argp[-2]);
224 file = path (cp, TFILE);
225 continue;
226 case FILTSW:
227 if (!(cp = *argp++) || *cp == '-')
228 adios (NULL, "missing argument to %s", argp[-2]);
229 filter = getcpy (etcpath (cp));
230 mime = 0;
231 continue;
232 case FORMSW:
233 if (!(form = *argp++) || *form == '-')
234 adios (NULL, "missing argument to %s", argp[-2]);
235 continue;
236
237 case FRMTSW:
238 filter = getcpy (etcpath (mhlreply));
239 mime = 0;
240 continue;
241 case NFRMTSW:
242 filter = NULL;
243 continue;
244
245 case INPLSW:
246 inplace++;
247 continue;
248 case NINPLSW:
249 inplace = 0;
250 continue;
251
252 case MIMESW:
253 mime++;
254 filter = NULL;
255 continue;
256 case NMIMESW:
257 mime = 0;
258 continue;
259
260 case QURYSW:
261 querysw++;
262 continue;
263 case NQURYSW:
264 querysw = 0;
265 continue;
266
267 case WIDTHSW:
268 if (!(cp = *argp++) || *cp == '-')
269 adios (NULL, "missing argument to %s", argp[-2]);
270 if ((outputlinelen = atoi (cp)) < 10)
271 adios (NULL, "impossible width %d", outputlinelen);
272 continue;
273
274 case DFOLDSW:
275 if (dfolder)
276 adios (NULL, "only one draft folder at a time!");
277 if (!(cp = *argp++) || *cp == '-')
278 adios (NULL, "missing argument to %s", argp[-2]);
279 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
280 *cp != '@' ? TFOLDER : TSUBCWF);
281 continue;
282 case DMSGSW:
283 if (dmsg)
284 adios (NULL, "only one draft message at a time!");
285 if (!(dmsg = *argp++) || *dmsg == '-')
286 adios (NULL, "missing argument to %s", argp[-2]);
287 continue;
288 case NDFLDSW:
289 dfolder = NULL;
290 isdf = NOTOK;
291 continue;
292
293 case ATFILESW:
294 atfile++;
295 continue;
296 case NOATFILESW:
297 atfile = 0;
298 continue;
299
300 case FMTPROCSW:
301 if (!(formatproc = *argp++) || *formatproc == '-')
302 adios (NULL, "missing argument to %s", argp[-2]);
303 fmtproc = 1;
304 continue;
305 case NFMTPROCSW:
306 fmtproc = 0;
307 continue;
308 }
309 }
310 if (*cp == '+' || *cp == '@') {
311 if (folder)
312 adios (NULL, "only one folder at a time!");
313 else
314 folder = pluspath (cp);
315 } else {
316 if (msg)
317 adios (NULL, "only one message at a time!");
318 else
319 msg = cp;
320 }
321 }
322
323 if (ccto == -1)
324 ccto = groupreply;
325 if (cccc == -1)
326 cccc = groupreply;
327 if (ccme == -1)
328 ccme = groupreply;
329
330 cwd = getcpy (pwd ());
331
332 if (!context_find ("path"))
333 free (path ("./", TFOLDER));
334 if (file && (msg || folder))
335 adios (NULL, "can't mix files and folders/msgs");
336
337 try_it_again:
338
339 strncpy (drft, buildsw ? m_maildir ("reply")
340 : m_draft (dfolder, NULL, NOUSE, &isdf), sizeof(drft));
341
342 /* Check if a draft exists */
343 if (!buildsw && stat (drft, &st) != NOTOK) {
344 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
345 for (i = LISTDSW; i != YESW;) {
346 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
347 done (1);
348 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
349 case NOSW:
350 done (0);
351 case NEWSW:
352 dmsg = NULL;
353 goto try_it_again;
354 case YESW:
355 break;
356 case LISTDSW:
357 showfile (++argp, drft);
358 break;
359 case REFILSW:
360 if (refile (++argp, drft) == 0)
361 i = YESW;
362 break;
363 default:
364 advise (NULL, "say what?");
365 break;
366 }
367 }
368 }
369
370 if (file) {
371 /*
372 * We are replying to a file.
373 */
374 anot = 0; /* we don't want to annotate a file */
375 } else {
376 /*
377 * We are replying to a message.
378 */
379 if (!msg)
380 msg = "cur";
381 if (!folder)
382 folder = getfolder (1);
383 maildir = m_maildir (folder);
384
385 if (chdir (maildir) == NOTOK)
386 adios (maildir, "unable to change directory to");
387
388 /* read folder and create message structure */
389 if (!(mp = folder_read (folder)))
390 adios (NULL, "unable to read folder %s", folder);
391
392 /* check for empty folder */
393 if (mp->nummsg == 0)
394 adios (NULL, "no messages in %s", folder);
395
396 /* parse the message range/sequence/name and set SELECTED */
397 if (!m_convert (mp, msg))
398 done (1);
399 seq_setprev (mp); /* set the previous-sequence */
400
401 if (mp->numsel > 1)
402 adios (NULL, "only one message at a time!");
403
404 context_replace (pfolder, folder); /* update current folder */
405 seq_setcur (mp, mp->lowsel); /* update current message */
406 seq_save (mp); /* synchronize sequences */
407 context_save (); /* save the context file */
408 }
409
410 msg = file ? file : getcpy (m_name (mp->lowsel));
411
412 if ((in = fopen (msg, "r")) == NULL)
413 adios (msg, "unable to open");
414
415 /* find form (components) file */
416 if (!form) {
417 if (groupreply)
418 form = etcpath (replgroupcomps);
419 else
420 form = etcpath (replcomps);
421 }
422
423 replout (in, msg, drft, mp, outputlinelen, mime, form, filter,
424 fcc, fmtproc);
425 fclose (in);
426
427 if (nwhat)
428 done (0);
429 what_now (ed, nedit, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL,
430 inplace, cwd, atfile);
431 done (1);
432 return 1;
433 }
434
435 void
436 docc (char *cp, int ccflag)
437 {
438 switch (smatch (cp, ccswitches)) {
439 case AMBIGSW:
440 ambigsw (cp, ccswitches);
441 done (1);
442 case UNKWNSW:
443 adios (NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
444
445 case CTOSW:
446 ccto = ccflag;
447 break;
448
449 case CCCSW:
450 cccc = ccflag;
451 break;
452
453 case CMESW:
454 ccme = ccflag;
455 break;
456
457 case CALSW:
458 ccto = cccc = ccme = ccflag;
459 break;
460 }
461 }