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