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 },
55 { "file file", 4 }, /* interface from msh */
59 { "build", 5 }, /* interface from mhe */
65 static char drft[BUFSIZ];
67 static char delim3[] =
68 "\n------------------------------------------------------------\n\n";
69 static char delim4[] = "\n------------------------------\n\n";
72 static struct msgs *mp = NULL; /* used a lot */
78 static void mhl_draft(int, char *, int, int, char *, char *);
79 static void copy_draft(int, char *, char *, int, int);
80 static void add_forw_hdr(char *);
81 static int build_form(char *, char *, int, int);
85 main(int argc, char **argv)
87 int msgp = 0, anot = 0, mime = 0;
88 int issue = 0, volume = 0;
89 int nedit = 0, nwhat = 0, in;
91 char *cp, *cwd, *maildir;
92 char *digest = NULL, *ed = NULL;
93 char *file = NULL, *filter = NULL, *folder = NULL;
94 char *form = NULL, buf[BUFSIZ], value[10];
95 char **argp, **arguments, *msgs[MAXARGS];
102 setlocale(LC_ALL, "");
104 invo_name = mhbasename(argv[0]);
106 /* read user profile/context */
109 arguments = getarguments(invo_name, argc, argv, 1);
112 while ((cp = *argp++)) {
114 switch (smatch(++cp, switches)) {
116 ambigsw(cp, switches);
119 adios(NULL, "-%s unknown", cp);
122 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
123 print_help(buf, switches, 1);
126 print_version(invo_name);
137 if (!(ed = *argp++) || *ed == '-')
138 adios(NULL, "missing argument to %s",
147 if (!(whatnowproc = *argp++) ||
149 adios(NULL, "missing argument to %s",
155 buildsw++; /* fall... */
163 adios(NULL, "only one file at a time!");
164 if (!(cp = *argp++) || *cp == '-')
165 adios(NULL, "missing argument to %s",
167 file = getcpy(expanddir(cp));
170 if (!(cp = *argp++) || *cp == '-')
171 adios(NULL, "missing argument to %s",
173 filter = getcpy(etcpath(cp));
177 if (!(form = *argp++) || *form == '-')
178 adios(NULL, "missing argument to %s",
183 filter = getcpy(etcpath(mhlforward));
198 if (!(digest = *argp++) || *digest == '-')
199 adios(NULL, "missing argument to %s",
204 if (!(cp = *argp++) || *cp == '-')
205 adios(NULL, "missing argument to %s",
207 if ((issue = atoi(cp)) < 1)
208 adios(NULL, "bad argument %s %s",
212 if (!(cp = *argp++) || *cp == '-')
213 adios(NULL, "missing argument to %s",
215 if ((volume = atoi(cp)) < 1)
216 adios(NULL, "bad argument %s %s",
221 if (*cp == '+' || *cp == '@') {
223 adios(NULL, "only one folder at a time!");
225 folder = getcpy(expandfol(cp));
233 if (file && (msgp || folder))
234 adios(NULL, "can't mix files and folders/msgs");
237 strncpy(drft, buildsw ? toabsdir("draft")
238 : m_draft(seq_beyond), sizeof(drft));
240 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
245 ** Forwarding a file.
247 anot = 0; /* don't want to annotate a file */
250 ** Forwarding a message.
253 msgs[msgp++] = seq_cur;
255 folder = getcurfol();
256 maildir = toabsdir(folder);
258 if (chdir(maildir) == NOTOK)
259 adios(maildir, "unable to change directory to");
261 /* read folder and create message structure */
262 if (!(mp = folder_read(folder)))
263 adios(NULL, "unable to read folder %s", folder);
265 /* check for empty folder */
267 adios(NULL, "no messages in %s", folder);
269 /* parse all the message ranges/sequences and set SELECTED */
270 for (msgnum = 0; msgnum < msgp; msgnum++)
271 if (!m_convert(mp, msgs[msgnum]))
273 seq_setprev(mp); /* set the previous sequence */
276 if (filter && access(filter, R_OK) == NOTOK)
277 adios(filter, "unable to read");
280 ** Open form (component) file.
284 snprintf(buf, sizeof(buf), IFORMAT, digest);
285 if (volume == 0 && (cp = context_find(buf))
286 && ((issue = atoi(cp)) < 0))
291 snprintf(buf, sizeof(buf), VFORMAT, digest);
292 if ((cp = context_find(buf)) == NULL ||
293 (volume = atoi(cp)) <= 0)
297 in = build_form(form, digest, volume, issue);
299 in = open_form(&form, forwcomps);
301 if ((out = creat(drft, m_gmprot())) == NOTOK)
302 adios(drft, "unable to create");
305 ** copy the components into the draft
307 cpydata(in, out, form, drft);
311 /* just copy the file into the draft */
312 if ((in = open(file, O_RDONLY)) == NOTOK)
313 adios(file, "unable to open");
314 cpydata(in, out, file, drft);
319 ** If filter file is defined, then format the
320 ** messages into the draft using mhlproc.
323 mhl_draft(out, digest, volume, issue, drft, filter);
329 copy_draft(out, digest, drft, volume, issue);
334 snprintf(buf, sizeof(buf), IFORMAT, digest);
335 snprintf(value, sizeof(value), "%d", issue);
336 context_replace(buf, getcpy(value));
337 snprintf(buf, sizeof(buf), VFORMAT, digest);
338 snprintf(value, sizeof(value), "%d", volume);
339 context_replace(buf, getcpy(value));
342 context_replace(curfolder, folder); /* update current folder */
343 seq_setcur(mp, mp->lowsel); /* update current message */
344 seq_save(mp); /* synchronize sequences */
345 context_save(); /* save the context file */
350 what_now(ed, nedit, NOUSE, drft, NULL, 0, mp,
351 anot ? "Forwarded" : NULL, cwd);
358 ** Filter the messages you are forwarding, into the
359 ** draft calling the mhlproc, and reading its output
363 mhl_draft(int out, char *digest, int volume, int issue,
364 char *file, char *filter)
367 int i, msgnum, pd[2];
372 if (pipe(pd) == NOTOK)
373 adios("pipe", "unable to create");
375 vec[0] = mhbasename(mhlproc);
377 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
381 adios("fork", "unable to");
389 vec[i++] = "-forwall";
394 vec[i++] = "-digest";
397 snprintf(buf1, sizeof(buf1), "%d", issue);
399 vec[i++] = "-volume";
400 snprintf(buf2, sizeof(buf2), "%d", volume);
404 if (mp->numsel >= MAXARGS - i)
405 adios(NULL, "more than %d messages for %s exec",
406 MAXARGS - i, vec[0]);
409 ** Now add the message names to filter. We can only
410 ** handle about 995 messages (because vec is fixed
411 ** size), but that should be plenty.
413 for (msgnum = mp->lowsel;
414 msgnum <= mp->hghsel && i < sizeof(vec) - 1;
416 if (is_selected(mp, msgnum))
417 vec[i++] = getcpy(m_name(msgnum));
420 execvp(mhlproc, vec);
421 fprintf(stderr, "unable to exec ");
427 cpydata(pd[0], out, vec[0], file);
429 pidXwait(child_id, mhlproc);
436 ** Copy the messages into the draft. The messages are
437 ** not filtered through the mhlproc. Do dashstuffing.
440 copy_draft(int out, char *digest, char *file, int volume, int issue)
442 int fd,i, msgcnt, msgnum;
444 register char *bp, *msgnam;
448 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
449 if (is_selected(mp, msgnum)) {
451 strncpy(buffer, msgnum == mp->lowsel ?
452 delim3 : delim4, sizeof(buffer));
454 /* Get buffer ready to go */
456 buflen = sizeof(buffer);
458 strncpy(bp, "\n-------", buflen);
463 if (msgnum == mp->lowsel) {
464 snprintf(bp, buflen, " Forwarded Message%s", mp->numsel > 1 ? "s" : "");
466 snprintf(bp, buflen, " Message %d", msgcnt);
472 strncpy(bp, "\n\n", buflen);
474 write(out, buffer, strlen(buffer));
476 if ((fd = open(msgnam = m_name (msgnum), O_RDONLY))
478 admonish(msgnam, "unable to read message");
483 ** Copy the message. Add RFC934 quoting (dashstuffing)
485 cpydgst(fd, out, msgnam, file);
493 strncpy(buffer, delim4, sizeof(buffer));
495 snprintf(buffer, sizeof(buffer),
496 "\n------- End of Forwarded Message%s\n\n",
497 mp->numsel > 1 ? "s" : "");
499 write(out, buffer, strlen(buffer));
502 snprintf(buffer, sizeof(buffer),
503 "End of %s Digest [Volume %d Issue %d]\n",
504 digest, volume, issue);
506 for (bp = buffer + i; i > 1; i--)
510 write(out, buffer, strlen(buffer));
516 ** Create an attachment header for the to be forward messages.
519 add_forw_hdr(char *draft)
524 snprintf(buffer, sizeof(buffer), "+%s", mp->foldpath);
525 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
526 if (!is_selected(mp, msgnum)) {
529 /* TODO: improve the code */
530 strncat(buffer, " ", sizeof(buffer)-strlen(buffer)-1);
531 strncat(buffer, m_name(msgnum),
532 sizeof(buffer)-strlen(buffer)-1);
534 annotate(draft, attach_hdr, buffer, 0, -2, 1);
539 build_form(char *form, char *digest, int volume, int issue)
544 char *line, tmpfil[BUFSIZ];
546 register struct comp *cptr;
551 /* Get new format string */
552 nfs = new_fs(form, NULL, NULL);
553 fmtsize = strlen(nfs) + 256;
555 /* Compile format string */
556 fmt_compile(nfs, &fmt);
558 FINDCOMP(cptr, "digest");
560 cptr->c_text = digest;
561 FINDCOMP(cptr, "date");
563 cptr->c_text = getcpy(dtimenow(0));
571 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
572 if (cp == NULL) adios("forw", "unable to create temporary file");
573 strncpy(tmpfil, cp, sizeof(tmpfil));
575 if ((in = dup(fileno(tmp))) == NOTOK)
576 adios("dup", "unable to");
578 line = mh_xmalloc((unsigned) fmtsize);
579 fmt_scan(fmt, line, fmtsize, dat);
583 adios(tmpfil, "error writing");
585 lseek(in, (off_t) 0, SEEK_SET);