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