2 ** forw.c -- forward a message, or group of messages.
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.
11 #include <h/fmt_scan.h>
16 #define IFORMAT "digest-issue-%s"
17 #define VFORMAT "digest-volume-%s"
19 static struct swit switches[] = {
25 { "editor editor", 0 },
29 { "filter filterfile", 0 },
31 { "form formfile", 0 },
43 { "issue number", 0 },
45 { "volume number", 0 },
47 { "whatnowproc program", 0 },
49 { "nowhatnowproc", 0 },
57 { "build", 5 }, /* interface from mhe */
63 static char drft[BUFSIZ];
65 static char delim3[] =
66 "\n------------------------------------------------------------\n\n";
67 static char delim4[] = "\n------------------------------\n\n";
70 static struct msgs *mp = NULL; /* used a lot */
76 static void mhl_draft(int, char *, int, int, char *, char *);
77 static void copy_draft(int, char *, char *, int, int);
78 static void add_forw_hdr(char *);
79 static int build_form(char *, char *, int, int);
83 main(int argc, char **argv)
85 int msgp = 0, anot = 0, mime = 0;
86 int issue = 0, volume = 0;
87 int nedit = 0, nwhat = 0, in;
89 char *cp, *cwd, *maildir;
90 char *digest = NULL, *ed = NULL;
91 char *filter = NULL, *folder = NULL;
92 char *form = NULL, buf[BUFSIZ], value[10];
93 char **argp, **arguments, *msgs[MAXARGS];
100 setlocale(LC_ALL, "");
102 invo_name = mhbasename(argv[0]);
104 /* read user profile/context */
107 arguments = getarguments(invo_name, argc, argv, 1);
110 while ((cp = *argp++)) {
112 switch (smatch(++cp, switches)) {
114 ambigsw(cp, switches);
117 adios(NULL, "-%s unknown", cp);
120 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
121 print_help(buf, switches, 1);
124 print_version(invo_name);
135 if (!(ed = *argp++) || *ed == '-')
136 adios(NULL, "missing argument to %s",
145 if (!(whatnowproc = *argp++) ||
147 adios(NULL, "missing argument to %s",
153 buildsw++; /* fall... */
160 if (!(cp = *argp++) || *cp == '-')
161 adios(NULL, "missing argument to %s",
163 filter = getcpy(etcpath(cp));
167 if (!(form = *argp++) || *form == '-')
168 adios(NULL, "missing argument to %s",
173 filter = getcpy(etcpath(mhlforward));
188 if (!(digest = *argp++) || *digest == '-')
189 adios(NULL, "missing argument to %s",
194 if (!(cp = *argp++) || *cp == '-')
195 adios(NULL, "missing argument to %s",
197 if ((issue = atoi(cp)) < 1)
198 adios(NULL, "bad argument %s %s",
202 if (!(cp = *argp++) || *cp == '-')
203 adios(NULL, "missing argument to %s",
205 if ((volume = atoi(cp)) < 1)
206 adios(NULL, "bad argument %s %s",
211 if (*cp == '+' || *cp == '@') {
213 adios(NULL, "only one folder at a time!");
215 folder = getcpy(expandfol(cp));
224 strncpy(drft, buildsw ? toabsdir("draft")
225 : m_draft(seq_beyond), sizeof(drft));
227 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
231 ** Forwarding a message.
234 msgs[msgp++] = seq_cur;
236 folder = getcurfol();
237 maildir = toabsdir(folder);
239 if (chdir(maildir) == NOTOK)
240 adios(maildir, "unable to change directory to");
242 /* read folder and create message structure */
243 if (!(mp = folder_read(folder)))
244 adios(NULL, "unable to read folder %s", folder);
246 /* check for empty folder */
248 adios(NULL, "no messages in %s", folder);
250 /* parse all the message ranges/sequences and set SELECTED */
251 for (msgnum = 0; msgnum < msgp; msgnum++)
252 if (!m_convert(mp, msgs[msgnum]))
254 seq_setprev(mp); /* set the previous sequence */
256 if (filter && access(filter, R_OK) == NOTOK)
257 adios(filter, "unable to read");
259 /* Open form (component) file. */
262 snprintf(buf, sizeof(buf), IFORMAT, digest);
263 if (volume == 0 && (cp = context_find(buf))
264 && ((issue = atoi(cp)) < 0))
269 snprintf(buf, sizeof(buf), VFORMAT, digest);
270 if ((cp = context_find(buf)) == NULL ||
271 (volume = atoi(cp)) <= 0)
275 in = build_form(form, digest, volume, issue);
277 in = open_form(&form, forwcomps);
279 if ((out = creat(drft, m_gmprot())) == NOTOK)
280 adios(drft, "unable to create");
283 ** copy the components into the draft
285 cpydata(in, out, form, drft);
289 ** If filter file is defined, then format the
290 ** messages into the draft using mhlproc.
293 mhl_draft(out, digest, volume, issue, drft, filter);
299 copy_draft(out, digest, drft, volume, issue);
304 snprintf(buf, sizeof(buf), IFORMAT, digest);
305 snprintf(value, sizeof(value), "%d", issue);
306 context_replace(buf, getcpy(value));
307 snprintf(buf, sizeof(buf), VFORMAT, digest);
308 snprintf(value, sizeof(value), "%d", volume);
309 context_replace(buf, getcpy(value));
312 context_replace(curfolder, folder); /* update current folder */
313 seq_setcur(mp, mp->lowsel); /* update current message */
314 seq_save(mp); /* synchronize sequences */
315 context_save(); /* save the context file */
319 what_now(ed, nedit, NOUSE, drft, NULL, 0, mp,
320 anot ? "Forwarded" : NULL, cwd);
327 ** Filter the messages you are forwarding, into the
328 ** draft calling the mhlproc, and reading its output
332 mhl_draft(int out, char *digest, int volume, int issue,
333 char *file, char *filter)
336 int i, msgnum, pd[2];
341 if (pipe(pd) == NOTOK)
342 adios("pipe", "unable to create");
344 vec[0] = mhbasename(mhlproc);
346 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
350 adios("fork", "unable to");
358 vec[i++] = "-forwall";
363 vec[i++] = "-digest";
366 snprintf(buf1, sizeof(buf1), "%d", issue);
368 vec[i++] = "-volume";
369 snprintf(buf2, sizeof(buf2), "%d", volume);
373 if (mp->numsel >= MAXARGS - i)
374 adios(NULL, "more than %d messages for %s exec",
375 MAXARGS - i, vec[0]);
378 ** Now add the message names to filter. We can only
379 ** handle about 995 messages (because vec is fixed
380 ** size), but that should be plenty.
382 for (msgnum = mp->lowsel;
383 msgnum <= mp->hghsel && i < sizeof(vec) - 1;
385 if (is_selected(mp, msgnum))
386 vec[i++] = getcpy(m_name(msgnum));
389 execvp(mhlproc, vec);
390 fprintf(stderr, "unable to exec ");
396 cpydata(pd[0], out, vec[0], file);
398 pidXwait(child_id, mhlproc);
405 ** Copy the messages into the draft. The messages are
406 ** not filtered through the mhlproc. Do dashstuffing.
409 copy_draft(int out, char *digest, char *file, int volume, int issue)
411 int fd,i, msgcnt, msgnum;
413 register char *bp, *msgnam;
417 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
418 if (is_selected(mp, msgnum)) {
420 strncpy(buffer, msgnum == mp->lowsel ?
421 delim3 : delim4, sizeof(buffer));
423 /* Get buffer ready to go */
425 buflen = sizeof(buffer);
427 strncpy(bp, "\n-------", buflen);
432 if (msgnum == mp->lowsel) {
433 snprintf(bp, buflen, " Forwarded Message%s", mp->numsel > 1 ? "s" : "");
435 snprintf(bp, buflen, " Message %d", msgcnt);
441 strncpy(bp, "\n\n", buflen);
443 write(out, buffer, strlen(buffer));
445 if ((fd = open(msgnam = m_name (msgnum), O_RDONLY))
447 admonish(msgnam, "unable to read message");
452 ** Copy the message. Add RFC934 quoting (dashstuffing)
454 cpydgst(fd, out, msgnam, file);
462 strncpy(buffer, delim4, sizeof(buffer));
464 snprintf(buffer, sizeof(buffer),
465 "\n------- End of Forwarded Message%s\n\n",
466 mp->numsel > 1 ? "s" : "");
468 write(out, buffer, strlen(buffer));
471 snprintf(buffer, sizeof(buffer),
472 "End of %s Digest [Volume %d Issue %d]\n",
473 digest, volume, issue);
475 for (bp = buffer + i; i > 1; i--)
479 write(out, buffer, strlen(buffer));
485 ** Create an attachment header for the to be forward messages.
488 add_forw_hdr(char *draft)
493 snprintf(buffer, sizeof(buffer), "+%s", mp->foldpath);
494 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
495 if (!is_selected(mp, msgnum)) {
498 /* TODO: improve the code */
499 strncat(buffer, " ", sizeof(buffer)-strlen(buffer)-1);
500 strncat(buffer, m_name(msgnum),
501 sizeof(buffer)-strlen(buffer)-1);
503 annotate(draft, attach_hdr, buffer, 0, -2, 1);
508 build_form(char *form, char *digest, int volume, int issue)
513 char *line, tmpfil[BUFSIZ];
515 register struct comp *cptr;
520 /* Get new format string */
521 nfs = new_fs(form, NULL, NULL);
522 fmtsize = strlen(nfs) + 256;
524 /* Compile format string */
525 fmt_compile(nfs, &fmt);
527 FINDCOMP(cptr, "digest");
529 cptr->c_text = digest;
530 FINDCOMP(cptr, "date");
532 cptr->c_text = getcpy(dtimenow(0));
540 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
541 if (cp == NULL) adios("forw", "unable to create temporary file");
542 strncpy(tmpfil, cp, sizeof(tmpfil));
544 if ((in = dup(fileno(tmp))) == NOTOK)
545 adios("dup", "unable to");
547 line = mh_xmalloc((unsigned) fmtsize);
548 fmt_scan(fmt, line, fmtsize, dat);
552 adios(tmpfil, "error writing");
554 lseek(in, (off_t) 0, SEEK_SET);