s/pfolder/curfolder/g
[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 = mhbasename(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]", invo_name);
143                                         print_help(buf, switches, 1);
144                                         done(0);
145                                 case VERSIONSW:
146                                         print_version(invo_name);
147                                         done(1);
148
149                                 case GROUPSW:
150                                         groupreply++;
151                                         continue;
152                                 case NGROUPSW:
153                                         groupreply = 0;
154                                         continue;
155
156                                 case ANNOSW:
157                                         anot++;
158                                         continue;
159                                 case NANNOSW:
160                                         anot = 0;
161                                         continue;
162
163                                 case CCSW:
164                                         if (!(cp = *argp++) || *cp == '-')
165                                                 adios(NULL, "missing argument to %s", argp[-2]);
166                                         docc(cp, 1);
167                                         continue;
168                                 case NCCSW:
169                                         if (!(cp = *argp++) || *cp == '-')
170                                                 adios(NULL, "missing argument to %s", argp[-2]);
171                                         docc(cp, 0);
172                                         continue;
173
174                                 case EDITRSW:
175                                         if (!(ed = *argp++) || *ed == '-')
176                                                 adios(NULL, "missing argument to %s", argp[-2]);
177                                         nedit = 0;
178                                         continue;
179                                 case NEDITSW:
180                                         nedit++;
181                                         continue;
182
183                                 case WHATSW:
184                                         if (!(whatnowproc = *argp++) ||
185                                                         *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 = getcpy(expandfol(cp));
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 = getcpy(expanddir(cp));
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 = getcpy(expandfol(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 (file && (msg || folder))
289                 adios(NULL, "can't mix files and folders/msgs");
290
291 #ifdef MHE
292         strncpy(drft, buildsw ? toabsdir("reply")
293                 : m_draft("new"), sizeof(drft));
294 #else
295         strncpy(drft, m_draft("new"), sizeof(drft));
296 #endif /* MHE */
297
298         if (file) {
299                 /*
300                 ** We are replying to a file.
301                 */
302                 anot = 0;  /* we don't want to annotate a file */
303         } else {
304                 /*
305                 ** We are replying to a message.
306                 */
307                 if (!msg)
308                         msg = "cur";
309                 if (!folder)
310                         folder = getcurfol();
311                 maildir = toabsdir(folder);
312
313                 if (chdir(maildir) == NOTOK)
314                         adios(maildir, "unable to change directory to");
315
316                 /* read folder and create message structure */
317                 if (!(mp = folder_read(folder)))
318                         adios(NULL, "unable to read folder %s", folder);
319
320                 /* check for empty folder */
321                 if (mp->nummsg == 0)
322                         adios(NULL, "no messages in %s", folder);
323
324                 /* parse the message range/sequence/name and set SELECTED */
325                 if (!m_convert(mp, msg))
326                         done(1);
327                 seq_setprev(mp);  /* set the previous-sequence */
328
329                 if (mp->numsel > 1)
330                         adios(NULL, "only one message at a time!");
331
332                 context_replace(curfolder, folder); /* update current folder */
333                 seq_setcur(mp, mp->lowsel);  /* update current message  */
334                 seq_save(mp);  /* synchronize sequences   */
335                 context_save();  /* save the context file   */
336         }
337
338         msg = file ? file : getcpy(m_name(mp->lowsel));
339
340         if ((in = fopen(msg, "r")) == NULL)
341                 adios(msg, "unable to open");
342
343         /* find form (components) file */
344         if (!form) {
345                 if (groupreply)
346                         form = etcpath(replgroupcomps);
347                 else
348                         form = etcpath(replcomps);
349         }
350
351         replout(in, msg, drft, mp, outputlinelen, mime, form, filter, fcc);
352         fclose(in);
353
354         if (nwhat)
355                 done(0);
356         what_now(ed, nedit, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL,
357                         inplace, cwd);
358         done(1);
359         return 1;
360 }
361
362 void
363 docc(char *cp, int ccflag)
364 {
365         switch (smatch(cp, ccswitches)) {
366                 case AMBIGSW:
367                         ambigsw(cp, ccswitches);
368                         done(1);
369                 case UNKWNSW:
370                         adios(NULL, "-%scc %s unknown", ccflag ? "" : "no",
371                                         cp);
372
373                 case CTOSW:
374                         ccto = ccflag;
375                         break;
376
377                 case CCCSW:
378                         cccc = ccflag;
379                         break;
380
381                 case CMESW:
382                         ccme = ccflag;
383                         break;
384
385                 case CALSW:
386                         ccto = cccc = ccme = ccflag;
387                         break;
388         }
389 }