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