Renamed -version switch to -Version to remove the conflict with -verbose.
[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
15
16 #define IFORMAT  "digest-issue-%s"
17 #define VFORMAT  "digest-volume-%s"
18
19 static struct swit switches[] = {
20 #define ANNOSW  0
21         { "annotate", 0 },
22 #define NANNOSW  1
23         { "noannotate", 0 },
24 #define EDITRSW  2
25         { "editor editor", 0 },
26 #define FORMSW  3
27         { "form formfile", 0 },
28 #define DGSTSW  4
29         { "digest list", 0 },
30 #define ISSUESW  5
31         { "issue number", 0 },
32 #define VOLUMSW  6
33         { "volume number", 0 },
34 #define WHATSW  7
35         { "whatnowproc program", 0 },
36 #define VERSIONSW  8
37         { "Version", 0 },
38 #define HELPSW  9
39         { "help", 0 },
40 #define BILDSW  12
41         { "build", 5 },  /* interface from mhe */
42         { NULL, 0 }
43 };
44
45 static char drft[BUFSIZ];
46 static struct msgs *mp = NULL;
47
48
49 /*
50 ** static prototypes
51 */
52 static void add_forw_hdr(char *);
53 static int build_form(char *, char *, int, int);
54
55
56 int
57 main(int argc, char **argv)
58 {
59         int msgp = 0, anot = 0;
60         int issue = 0, volume = 0;
61         int in;
62         int out, msgnum;
63         char *cp, *cwd, *maildir;
64         char *digest = NULL, *ed = NULL;
65         char *folder = NULL;
66         char *form = NULL, buf[BUFSIZ], value[10];
67         char **argp, **arguments, *msgs[MAXARGS];
68         char *fmtstr;
69         int buildsw = 0;
70
71 #ifdef LOCALE
72         setlocale(LC_ALL, "");
73 #endif
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                                 done(1);
88                         case UNKWNSW:
89                                 adios(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                                 done(1);
95                         case VERSIONSW:
96                                 print_version(invo_name);
97                                 done(1);
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(NULL, "missing argument to %s",
109                                                         argp[-2]);
110                                 continue;
111
112                         case WHATSW:
113                                 if (!(whatnowproc = *argp++) ||
114                                                 *whatnowproc == '-')
115                                         adios(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(NULL, "missing argument to %s",
126                                                         argp[-2]);
127                                 continue;
128
129                         case DGSTSW:
130                                 if (!(digest = *argp++) || *digest == '-')
131                                         adios(NULL, "missing argument to %s",
132                                                         argp[-2]);
133                                 //mime = 0;
134                                 continue;
135                         case ISSUESW:
136                                 if (!(cp = *argp++) || *cp == '-')
137                                         adios(NULL, "missing argument to %s",
138                                                         argp[-2]);
139                                 if ((issue = atoi(cp)) < 1)
140                                         adios(NULL, "bad argument %s %s",
141                                                         argp[-2], cp);
142                                 continue;
143                         case VOLUMSW:
144                                 if (!(cp = *argp++) || *cp == '-')
145                                         adios(NULL, "missing argument to %s",
146                                                         argp[-2]);
147                                 if ((volume = atoi(cp)) < 1)
148                                         adios(NULL, "bad argument %s %s",
149                                                         argp[-2], cp);
150                                 continue;
151                         }
152                 }
153                 if (*cp == '+' || *cp == '@') {
154                         if (folder)
155                                 adios(NULL, "only one folder at a time!");
156                         else
157                                 folder = getcpy(expandfol(cp));
158                 } else {
159                         msgs[msgp++] = cp;
160                 }
161         }
162
163         cwd = getcpy(pwd());
164         strncpy(drft, buildsw ? toabsdir("draft") : m_draft(seq_beyond),
165                         sizeof(drft));
166         /*
167         ** FIXME: (concerning MHE support (buildsw) only)
168         ** There's no check if the draft already exists. mmh has removed
169         ** this case by having the draft folder. I won't add code only to
170         ** handle this legacy issue for MHE. -- meillo@marmaro.de 2012-05
171         */
172
173         /*
174         ** Forwarding a message.
175         */
176         if (!msgp)
177                 msgs[msgp++] = seq_cur;
178         if (!folder)
179                 folder = getcurfol();
180         maildir = toabsdir(folder);
181
182         if (chdir(maildir) == NOTOK)
183                 adios(maildir, "unable to change directory to");
184
185         /* read folder and create message structure */
186         if (!(mp = folder_read(folder)))
187                 adios(NULL, "unable to read folder %s", folder);
188
189         /* check for empty folder */
190         if (mp->nummsg == 0)
191                 adios(NULL, "no messages in %s", folder);
192
193         /* parse all the message ranges/sequences and set SELECTED */
194         for (msgnum = 0; msgnum < msgp; msgnum++)
195                 if (!m_convert(mp, msgs[msgnum]))
196                         done(1);
197         seq_setprev(mp);  /* set the previous sequence */
198
199         if ((out = creat(drft, m_gmprot())) == NOTOK)
200                 adios(drft, "unable to create");
201
202         /* Open form (component) file. */
203         if (digest) {
204                 if (issue == 0) {
205                         snprintf(buf, sizeof(buf), IFORMAT, digest);
206                         if (volume == 0 && (cp = context_find(buf))
207                                         && ((issue = atoi(cp)) < 0))
208                                 issue = 0;
209                         issue++;
210                 }
211                 if (volume == 0) {
212                         snprintf(buf, sizeof(buf), VFORMAT, digest);
213                         if ((cp = context_find(buf)) == NULL ||
214                                         (volume = atoi(cp)) <= 0)
215                                 volume = 1;
216                 }
217                 if (!form)
218                         form = digestcomps;
219                 in = build_form(form, digest, volume, issue);
220                 cpydata(in, out, form, drft);
221                 close(in);
222         } else {
223                 fmtstr = new_fs(form, forwcomps);
224                 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
225                         adios(drft, "error writing");
226                 }
227         }
228         close(out);
229
230         add_forw_hdr(drft);
231
232         if (digest) {
233                 snprintf(buf, sizeof(buf), IFORMAT, digest);
234                 snprintf(value, sizeof(value), "%d", issue);
235                 context_replace(buf, getcpy(value));
236                 snprintf(buf, sizeof(buf), VFORMAT, digest);
237                 snprintf(value, sizeof(value), "%d", volume);
238                 context_replace(buf, getcpy(value));
239         }
240
241         context_replace(curfolder, folder); /* update current folder */
242         seq_setcur(mp, mp->lowsel);  /* update current message */
243         seq_save(mp);  /* synchronize sequences */
244         context_save();  /* save the context file */
245
246         if (buildsw)
247                 done(0);
248         what_now(ed, NOUSE, drft, NULL, 0, mp,
249                 anot ? "Forwarded" : NULL, cwd);
250         done(1);
251         return 1;
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 buffer[BUFSIZ];
263
264         snprintf(buffer, sizeof buffer, "anno -append -nodate '%s' "
265                         "-comp '%s' -text '+%s",
266                         draft, attach_hdr, mp->foldpath);
267         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
268                 if (!is_selected(mp, msgnum)) {
269                         continue;
270                 }
271                 /* TODO: Check for buffer length! */
272                 strcat(buffer, " ");
273                 strcat(buffer, m_name(msgnum));
274         }
275         strcat(buffer, "'");
276         /* TODO: This check is bad, but better than nothing */
277         if (strlen(buffer) > BUFSIZ) {
278                 adios(NULL, "Too long attachment header line. Forward less messages.");
279         }
280         if (system(buffer) != 0) {
281                 advise(NULL, "unable to add attachment header");
282         }
283 }
284
285
286 static int
287 build_form(char *form, char *digest, int volume, int issue)
288 {
289         int in;
290         int fmtsize;
291         register char *fmtstr;
292         char *line, tmpfil[BUFSIZ];
293         FILE *tmp;
294         register struct comp *cptr;
295         struct format *fmt;
296         int dat[5];
297         char *cp = NULL;
298
299         /* Get new format string */
300         fmtstr = new_fs(form, NULL);
301         fmtsize = strlen(fmtstr) + 256;
302
303         /* Compile format string */
304         fmt_compile(fmtstr, &fmt);
305
306         FINDCOMP(cptr, "digest");
307         if (cptr)
308                 cptr->c_text = digest;
309         FINDCOMP(cptr, "date");
310         if (cptr)
311                 cptr->c_text = getcpy(dtimenow());
312
313         dat[0] = issue;
314         dat[1] = volume;
315         dat[2] = 0;
316         dat[3] = fmtsize;
317         dat[4] = 0;
318
319         cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
320         if (cp == NULL) adios("forw", "unable to create temporary file");
321         strncpy(tmpfil, cp, sizeof(tmpfil));
322         unlink(tmpfil);
323         if ((in = dup(fileno(tmp))) == NOTOK)
324                 adios("dup", "unable to");
325
326         line = mh_xmalloc((unsigned) fmtsize);
327         fmt_scan(fmt, line, fmtsize, dat);
328         fputs(line, tmp);
329         free(line);
330         if (fclose(tmp))
331                 adios(tmpfil, "error writing");
332
333         lseek(in, (off_t) 0, SEEK_SET);
334         return in;
335 }