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