* Changed repl defaults to partly revert to MH behaviour:
[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 = 0;         /* global for replsbr */
112 short cccc = 0;
113 short ccme = 0;
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                     ccto = cccc = ccme = 1;
181                     continue;
182                 case NGROUPSW:
183                     groupreply = 0;
184                     ccto = cccc = ccme = 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 = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
320         } else {
321             if (msg)
322                 adios (NULL, "only one message at a time!");
323             else
324                 msg = cp;
325         }
326     }
327
328     cwd = getcpy (pwd ());
329
330     if (!context_find ("path"))
331         free (path ("./", TFOLDER));
332     if (file && (msg || folder))
333         adios (NULL, "can't mix files and folders/msgs");
334
335 try_it_again:
336
337 #ifdef MHE
338     strncpy (drft, buildsw ? m_maildir ("reply")
339                           : m_draft (dfolder, NULL, NOUSE, &isdf), sizeof(drft));
340
341     /* Check if a draft exists */
342     if (!buildsw && stat (drft, &st) != NOTOK) {
343 #else
344     strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
345
346     /* Check if a draft exists */
347     if (stat (drft, &st) != NOTOK) {
348 #endif /* MHE */
349         printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
350         for (i = LISTDSW; i != YESW;) {
351             if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
352                 done (1);
353             switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
354                 case NOSW: 
355                     done (0);
356                 case NEWSW: 
357                     dmsg = NULL;
358                     goto try_it_again;
359                 case YESW: 
360                     break;
361                 case LISTDSW: 
362                     showfile (++argp, drft);
363                     break;
364                 case REFILSW: 
365                     if (refile (++argp, drft) == 0)
366                         i = YESW;
367                     break;
368                 default: 
369                     advise (NULL, "say what?");
370                     break;
371             }
372         }
373     }
374
375     if (file) {
376         /*
377          * We are replying to a file.
378          */
379         anot = 0;       /* we don't want to annotate a file */
380     } else {
381         /*
382          * We are replying to a message.
383          */
384         if (!msg)
385             msg = "cur";
386         if (!folder)
387             folder = getfolder (1);
388         maildir = m_maildir (folder);
389
390         if (chdir (maildir) == NOTOK)
391             adios (maildir, "unable to change directory to");
392
393         /* read folder and create message structure */
394         if (!(mp = folder_read (folder)))
395             adios (NULL, "unable to read folder %s", folder);
396
397         /* check for empty folder */
398         if (mp->nummsg == 0)
399             adios (NULL, "no messages in %s", folder);
400
401         /* parse the message range/sequence/name and set SELECTED */
402         if (!m_convert (mp, msg))
403             done (1);
404         seq_setprev (mp);       /* set the previous-sequence */
405
406         if (mp->numsel > 1)
407             adios (NULL, "only one message at a time!");
408
409         context_replace (pfolder, folder);      /* update current folder   */
410         seq_setcur (mp, mp->lowsel);    /* update current message  */
411         seq_save (mp);                  /* synchronize sequences   */
412         context_save ();                        /* save the context file   */
413     }
414
415     msg = file ? file : getcpy (m_name (mp->lowsel));
416
417     if ((in = fopen (msg, "r")) == NULL)
418         adios (msg, "unable to open");
419
420     /* find form (components) file */
421     if (!form) {
422         if (groupreply)
423             form = etcpath (replgroupcomps);
424         else
425             form = etcpath (replcomps);
426     }
427
428     replout (in, msg, drft, mp, outputlinelen, mime, form, filter, fcc);
429     fclose (in);
430
431     if (nwhat)
432         done (0);
433     what_now (ed, nedit, NOUSE, drft, msg, 0, mp,
434             anot ? "Replied" : NULL, inplace, cwd);
435     return done (1);
436 }
437
438 void
439 docc (char *cp, int ccflag)
440 {
441     switch (smatch (cp, ccswitches)) {
442         case AMBIGSW: 
443             ambigsw (cp, ccswitches);
444             done (1);
445         case UNKWNSW: 
446             adios (NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
447
448         case CTOSW: 
449             ccto = ccflag;
450             break;
451
452         case CCCSW: 
453             cccc = ccflag;
454             break;
455
456         case CMESW: 
457             ccme = ccflag;
458             break;
459
460         case CALSW: 
461             ccto = cccc = ccme = ccflag;
462             break;
463     }
464 }