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