simplify whatnow.c/main() function
[mmh] / uip / forw.c
1 /*
2 ** forw.c -- forward a message, or group of messages.
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 <fcntl.h>
11 #include <h/fmt_scan.h>
12 #include <h/tws.h>
13 #include <h/utils.h>
14 #include <unistd.h>
15 #include <locale.h>
16 #include <sysexits.h>
17
18 #define IFORMAT  "digest-issue-%s"
19 #define VFORMAT  "digest-volume-%s"
20
21 static struct swit switches[] = {
22 #define ANNOSW  0
23         { "annotate", 0 },
24 #define NANNOSW  1
25         { "noannotate", 2 },
26 #define EDITRSW  2
27         { "editor editor", 0 },
28 #define FORMSW  3
29         { "form formfile", 0 },
30 #define DGSTSW  4
31         { "digest list", 0 },
32 #define ISSUESW  5
33         { "issue number", 0 },
34 #define VOLUMSW  6
35         { "volume number", 0 },
36 #define WHATSW  7
37         { "whatnowproc program", 0 },
38 #define VERSIONSW  8
39         { "Version", 0 },
40 #define HELPSW  9
41         { "help", 0 },
42 #define BILDSW  10
43         { "build", 5 },  /* interface from mhe */
44         { NULL, 0 }
45 };
46
47 static char drft[BUFSIZ];
48 static struct msgs *mp = NULL;
49
50
51 /*
52 ** static prototypes
53 */
54 static void add_forw_hdr(char *);
55 static int build_form(char *, char *, int, int);
56
57
58 int
59 main(int argc, char **argv)
60 {
61         int msgp = 0, anot = 0;
62         int issue = 0, volume = 0;
63         int in;
64         int out, msgnum;
65         char *cp, *cwd, *maildir;
66         char *digest = NULL, *ed = NULL;
67         char *folder = NULL;
68         char *form = NULL, buf[BUFSIZ], value[10];
69         char **argp, **arguments, *msgs[MAXARGS];
70         char *fmtstr;
71         int buildsw = 0;
72
73         setlocale(LC_ALL, "");
74         invo_name = mhbasename(argv[0]);
75
76         /* read user profile/context */
77         context_read();
78
79         arguments = getarguments(invo_name, argc, argv, 1);
80         argp = arguments;
81
82         while ((cp = *argp++)) {
83                 if (*cp == '-') {
84                         switch (smatch(++cp, switches)) {
85                         case AMBIGSW:
86                                 ambigsw(cp, switches);
87                                 exit(EX_USAGE);
88                         case UNKWNSW:
89                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
90
91                         case HELPSW:
92                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
93                                 print_help(buf, switches, 1);
94                                 exit(argc == 2 ? EX_OK : EX_USAGE);
95                         case VERSIONSW:
96                                 print_version(invo_name);
97                                 exit(argc == 2 ? EX_OK : EX_USAGE);
98
99                         case ANNOSW:
100                                 anot++;
101                                 continue;
102                         case NANNOSW:
103                                 anot = 0;
104                                 continue;
105
106                         case EDITRSW:
107                                 if (!(ed = *argp++) || *ed == '-')
108                                         adios(EX_USAGE, NULL, "missing argument to %s",
109                                                         argp[-2]);
110                                 continue;
111
112                         case WHATSW:
113                                 if (!(whatnowproc = *argp++) ||
114                                                 *whatnowproc == '-')
115                                         adios(EX_USAGE, NULL, "missing argument to %s",
116                                                         argp[-2]);
117                                 continue;
118
119                         case BILDSW:
120                                 buildsw++;
121                                 continue;
122
123                         case FORMSW:
124                                 if (!(form = *argp++) || *form == '-')
125                                         adios(EX_USAGE, NULL, "missing argument to %s",
126                                                         argp[-2]);
127                                 continue;
128
129                         case DGSTSW:
130                                 if (!(digest = *argp++) || *digest == '-')
131                                         adios(EX_USAGE, NULL, "missing argument to %s",
132                                                         argp[-2]);
133                                 continue;
134                         case ISSUESW:
135                                 if (!(cp = *argp++) || *cp == '-')
136                                         adios(EX_USAGE, NULL, "missing argument to %s",
137                                                         argp[-2]);
138                                 if ((issue = atoi(cp)) < 1)
139                                         adios(EX_USAGE, NULL, "bad argument %s %s",
140                                                         argp[-2], cp);
141                                 continue;
142                         case VOLUMSW:
143                                 if (!(cp = *argp++) || *cp == '-')
144                                         adios(EX_USAGE, NULL, "missing argument to %s",
145                                                         argp[-2]);
146                                 if ((volume = atoi(cp)) < 1)
147                                         adios(EX_USAGE, NULL, "bad argument %s %s",
148                                                         argp[-2], cp);
149                                 continue;
150                         }
151                 }
152                 if (*cp == '+' || *cp == '@') {
153                         if (folder)
154                                 adios(EX_USAGE, NULL, "only one folder at a time!");
155                         else
156                                 folder = mh_xstrdup(expandfol(cp));
157                 } else {
158                         msgs[msgp++] = cp;
159                 }
160         }
161
162         cwd = mh_xstrdup(pwd());
163         strncpy(drft, buildsw ? toabsdir("draft") : m_draft(seq_beyond),
164                         sizeof(drft));
165         /*
166         ** FIXME: (concerning MHE support (buildsw) only)
167         ** There's no check if the draft already exists. mmh has removed
168         ** this case by having the draft folder. I won't add code only to
169         ** handle this legacy issue for MHE. -- meillo@marmaro.de 2012-05
170         */
171
172         /*
173         ** Forwarding a message.
174         */
175         if (!msgp)
176                 msgs[msgp++] = seq_cur;
177         if (!folder)
178                 folder = getcurfol();
179         maildir = toabsdir(folder);
180
181         if (chdir(maildir) == NOTOK)
182                 adios(EX_OSERR, maildir, "unable to change directory to");
183
184         /* read folder and create message structure */
185         if (!(mp = folder_read(folder)))
186                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
187
188         /* check for empty folder */
189         if (mp->nummsg == 0)
190                 adios(EX_DATAERR, NULL, "no messages in %s", folder);
191
192         /* parse all the message ranges/sequences and set SELECTED */
193         for (msgnum = 0; msgnum < msgp; msgnum++) {
194                 if (!m_convert(mp, msgs[msgnum])) {
195                         exit(EX_SOFTWARE);
196                 }
197         }
198         seq_setprev(mp);  /* set the previous sequence */
199
200         if ((out = creat(drft, m_gmprot())) == NOTOK)
201                 adios(EX_CANTCREAT, drft, "unable to create");
202
203         /* Open form (component) file. */
204         if (digest) {
205                 if (issue == 0) {
206                         snprintf(buf, sizeof(buf), IFORMAT, digest);
207                         if (volume == 0 && (cp = context_find(buf))
208                                         && ((issue = atoi(cp)) < 0))
209                                 issue = 0;
210                         issue++;
211                 }
212                 if (volume == 0) {
213                         snprintf(buf, sizeof(buf), VFORMAT, digest);
214                         if ((cp = context_find(buf)) == NULL ||
215                                         (volume = atoi(cp)) <= 0)
216                                 volume = 1;
217                 }
218                 if (!form)
219                         form = digestcomps;
220                 in = build_form(form, digest, volume, issue);
221                 cpydata(in, out, form, drft);
222                 close(in);
223         } else {
224                 fmtstr = new_fs(form, forwcomps);
225                 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
226                         adios(EX_IOERR, drft, "error writing");
227                 }
228         }
229         close(out);
230
231         add_forw_hdr(drft);
232
233         if (digest) {
234                 snprintf(buf, sizeof(buf), IFORMAT, digest);
235                 snprintf(value, sizeof(value), "%d", issue);
236                 context_replace(buf, mh_xstrdup(value));
237                 snprintf(buf, sizeof(buf), VFORMAT, digest);
238                 snprintf(value, sizeof(value), "%d", volume);
239                 context_replace(buf, mh_xstrdup(value));
240         }
241
242         context_replace(curfolder, folder); /* update current folder */
243         seq_setcur(mp, mp->lowsel);  /* update current message */
244         seq_save(mp);  /* synchronize sequences */
245         context_save();  /* save the context file */
246
247         if (buildsw)
248                 exit(EX_OK);
249         what_now(ed, NOUSE, drft, NULL, 0, mp,
250                 anot ? "Forwarded" : NULL, cwd);
251         return EX_OSERR;
252 }
253
254
255 /*
256 ** Create an attachment header for the to be forward messages.
257 */
258 static void
259 add_forw_hdr(char *draft)
260 {
261         int msgnum;
262         char buf[BUFSIZ];
263         char *vec[MAXARGS];
264         int vecp = 0;
265
266         vec[vecp++] = "anno";
267         vec[vecp++] = "-append";
268         vec[vecp++] = "-nodate";
269         vec[vecp++] = draft;
270         vec[vecp++] = "-comp";
271         vec[vecp++] = attach_hdr;
272         vec[vecp++] = "-text";
273         snprintf(buf, sizeof buf, "+%s", mp->foldpath);
274         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
275                 if (!is_selected(mp, msgnum)) {
276                         continue;
277                 }
278                 if (strlen(buf) + 1 + strlen(m_name(msgnum)) + 1
279                                 > sizeof buf) {
280                         adios(EX_DATAERR, NULL, "Attachment header line too long. "
281                                         "Forward less messages.");
282                 }
283                 strcat(buf, " ");
284                 strcat(buf, m_name(msgnum));
285         }
286         vec[vecp++] = buf;
287         vec[vecp] = NULL;
288         if (execprog(*vec, vec) != 0) {
289                 advise(NULL, "unable to add attachment header");
290         }
291 }
292
293
294 static int
295 build_form(char *form, char *digest, int volume, int issue)
296 {
297         int in;
298         int fmtsize;
299         char *fmtstr;
300         char *line, tmpfil[BUFSIZ];
301         FILE *tmp;
302         struct comp *cptr;
303         struct format *fmt;
304         int dat[5];
305         char *cp = NULL;
306
307         /* Get new format string */
308         fmtstr = new_fs(form, NULL);
309         fmtsize = strlen(fmtstr) + 256;
310
311         /* Compile format string */
312         fmt_compile(fmtstr, &fmt);
313
314         FINDCOMP(cptr, "digest");
315         if (cptr)
316                 cptr->c_text = digest;
317         FINDCOMP(cptr, "date");
318         if (cptr)
319                 cptr->c_text = mh_xstrdup(dtimenow());
320
321         dat[0] = issue;
322         dat[1] = volume;
323         dat[2] = 0;
324         dat[3] = fmtsize;
325         dat[4] = 0;
326
327         cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
328         if (cp == NULL) {
329                 adios(EX_CANTCREAT, "forw", "unable to create temporary file");
330         }
331         strncpy(tmpfil, cp, sizeof(tmpfil));
332         unlink(tmpfil);
333         if ((in = dup(fileno(tmp))) == NOTOK)
334                 adios(EX_OSERR, "dup", "unable to");
335
336         line = mh_xcalloc(fmtsize, sizeof(char));
337         fmt_scan(fmt, line, fmtsize, dat);
338         fputs(line, tmp);
339         mh_free0(&line);
340         if (fclose(tmp))
341                 adios(EX_IOERR, tmpfil, "error writing");
342
343         lseek(in, (off_t) 0, SEEK_SET);
344         return in;
345 }