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