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 },
47 { "issue number", 0 },
49 { "volume number", 0 },
51 { "whatnowproc program", 0 },
53 { "nowhatnowproc", 0 },
59 { "file file", 4 }, /* interface from msh */
63 { "build", 5 }, /* interface from mhe */
69 static char drft[BUFSIZ];
71 static char delim3[] =
72 "\n------------------------------------------------------------\n\n";
73 static char delim4[] = "\n------------------------------\n\n";
76 static struct msgs *mp = NULL; /* used a lot */
82 static void mhl_draft(int, char *, int, int, char *, char *);
83 static void copy_draft(int, char *, char *, int, int);
84 static void add_forw_hdr(char *);
85 static int build_form(char *, char *, int, int);
89 main(int argc, char **argv)
91 int msgp = 0, anot = 0, inplace = 1, mime = 0;
92 int issue = 0, volume = 0;
93 int nedit = 0, nwhat = 0, in;
95 char *cp, *cwd, *maildir;
96 char *digest = NULL, *ed = NULL;
97 char *file = NULL, *filter = NULL, *folder = NULL;
98 char *form = NULL, buf[BUFSIZ], value[10];
99 char **argp, **arguments, *msgs[MAXARGS];
106 setlocale(LC_ALL, "");
108 invo_name = mhbasename(argv[0]);
110 /* read user profile/context */
113 arguments = getarguments(invo_name, argc, argv, 1);
116 while ((cp = *argp++)) {
118 switch (smatch(++cp, switches)) {
120 ambigsw(cp, switches);
123 adios(NULL, "-%s unknown", cp);
126 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
127 print_help(buf, switches, 1);
130 print_version(invo_name);
141 if (!(ed = *argp++) || *ed == '-')
142 adios(NULL, "missing argument to %s",
151 if (!(whatnowproc = *argp++) ||
153 adios(NULL, "missing argument to %s",
159 buildsw++; /* fall... */
167 adios(NULL, "only one file at a time!");
168 if (!(cp = *argp++) || *cp == '-')
169 adios(NULL, "missing argument to %s",
171 file = getcpy(expanddir(cp));
174 if (!(cp = *argp++) || *cp == '-')
175 adios(NULL, "missing argument to %s",
177 filter = getcpy(etcpath(cp));
181 if (!(form = *argp++) || *form == '-')
182 adios(NULL, "missing argument to %s",
187 filter = getcpy(etcpath(mhlforward));
209 if (!(digest = *argp++) || *digest == '-')
210 adios(NULL, "missing argument to %s",
215 if (!(cp = *argp++) || *cp == '-')
216 adios(NULL, "missing argument to %s",
218 if ((issue = atoi(cp)) < 1)
219 adios(NULL, "bad argument %s %s",
223 if (!(cp = *argp++) || *cp == '-')
224 adios(NULL, "missing argument to %s",
226 if ((volume = atoi(cp)) < 1)
227 adios(NULL, "bad argument %s %s",
232 if (*cp == '+' || *cp == '@') {
234 adios(NULL, "only one folder at a time!");
236 folder = getcpy(expandfol(cp));
244 if (file && (msgp || folder))
245 adios(NULL, "can't mix files and folders/msgs");
248 strncpy(drft, buildsw ? toabsdir("draft")
249 : m_draft(seq_beyond), sizeof(drft));
251 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
256 ** Forwarding a file.
258 anot = 0; /* don't want to annotate a file */
261 ** Forwarding a message.
264 msgs[msgp++] = seq_cur;
266 folder = getcurfol();
267 maildir = toabsdir(folder);
269 if (chdir(maildir) == NOTOK)
270 adios(maildir, "unable to change directory to");
272 /* read folder and create message structure */
273 if (!(mp = folder_read(folder)))
274 adios(NULL, "unable to read folder %s", folder);
276 /* check for empty folder */
278 adios(NULL, "no messages in %s", folder);
280 /* parse all the message ranges/sequences and set SELECTED */
281 for (msgnum = 0; msgnum < msgp; msgnum++)
282 if (!m_convert(mp, msgs[msgnum]))
284 seq_setprev(mp); /* set the previous sequence */
287 if (filter && access(filter, R_OK) == NOTOK)
288 adios(filter, "unable to read");
291 ** Open form (component) file.
295 snprintf(buf, sizeof(buf), IFORMAT, digest);
296 if (volume == 0 && (cp = context_find(buf))
297 && ((issue = atoi(cp)) < 0))
302 snprintf(buf, sizeof(buf), VFORMAT, digest);
303 if ((cp = context_find(buf)) == NULL ||
304 (volume = atoi(cp)) <= 0)
308 in = build_form(form, digest, volume, issue);
310 in = open_form(&form, forwcomps);
312 if ((out = creat(drft, m_gmprot())) == NOTOK)
313 adios(drft, "unable to create");
316 ** copy the components into the draft
318 cpydata(in, out, form, drft);
322 /* just copy the file into the draft */
323 if ((in = open(file, O_RDONLY)) == NOTOK)
324 adios(file, "unable to open");
325 cpydata(in, out, file, drft);
330 ** If filter file is defined, then format the
331 ** messages into the draft using mhlproc.
334 mhl_draft(out, digest, volume, issue, drft, filter);
340 copy_draft(out, digest, drft, volume, issue);
345 snprintf(buf, sizeof(buf), IFORMAT, digest);
346 snprintf(value, sizeof(value), "%d", issue);
347 context_replace(buf, getcpy(value));
348 snprintf(buf, sizeof(buf), VFORMAT, digest);
349 snprintf(value, sizeof(value), "%d", volume);
350 context_replace(buf, getcpy(value));
353 context_replace(curfolder, folder); /* update current folder */
354 seq_setcur(mp, mp->lowsel); /* update current message */
355 seq_save(mp); /* synchronize sequences */
356 context_save(); /* save the context file */
361 what_now(ed, nedit, NOUSE, drft, NULL, 0, mp,
362 anot ? "Forwarded" : NULL, inplace, cwd);
369 ** Filter the messages you are forwarding, into the
370 ** draft calling the mhlproc, and reading its output
374 mhl_draft(int out, char *digest, int volume, int issue,
375 char *file, char *filter)
378 int i, msgnum, pd[2];
383 if (pipe(pd) == NOTOK)
384 adios("pipe", "unable to create");
386 vec[0] = mhbasename(mhlproc);
388 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
392 adios("fork", "unable to");
400 vec[i++] = "-forwall";
405 vec[i++] = "-digest";
408 snprintf(buf1, sizeof(buf1), "%d", issue);
410 vec[i++] = "-volume";
411 snprintf(buf2, sizeof(buf2), "%d", volume);
415 if (mp->numsel >= MAXARGS - i)
416 adios(NULL, "more than %d messages for %s exec",
417 MAXARGS - i, vec[0]);
420 ** Now add the message names to filter. We can only
421 ** handle about 995 messages (because vec is fixed
422 ** size), but that should be plenty.
424 for (msgnum = mp->lowsel;
425 msgnum <= mp->hghsel && i < sizeof(vec) - 1;
427 if (is_selected(mp, msgnum))
428 vec[i++] = getcpy(m_name(msgnum));
431 execvp(mhlproc, vec);
432 fprintf(stderr, "unable to exec ");
438 cpydata(pd[0], out, vec[0], file);
440 pidXwait(child_id, mhlproc);
447 ** Copy the messages into the draft. The messages are
448 ** not filtered through the mhlproc. Do dashstuffing.
451 copy_draft(int out, char *digest, char *file, int volume, int issue)
453 int fd,i, msgcnt, msgnum;
455 register char *bp, *msgnam;
459 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
460 if (is_selected(mp, msgnum)) {
462 strncpy(buffer, msgnum == mp->lowsel ?
463 delim3 : delim4, sizeof(buffer));
465 /* Get buffer ready to go */
467 buflen = sizeof(buffer);
469 strncpy(bp, "\n-------", buflen);
474 if (msgnum == mp->lowsel) {
475 snprintf(bp, buflen, " Forwarded Message%s", mp->numsel > 1 ? "s" : "");
477 snprintf(bp, buflen, " Message %d", msgcnt);
483 strncpy(bp, "\n\n", buflen);
485 write(out, buffer, strlen(buffer));
487 if ((fd = open(msgnam = m_name (msgnum), O_RDONLY))
489 admonish(msgnam, "unable to read message");
494 ** Copy the message. Add RFC934 quoting (dashstuffing)
496 cpydgst(fd, out, msgnam, file);
504 strncpy(buffer, delim4, sizeof(buffer));
506 snprintf(buffer, sizeof(buffer),
507 "\n------- End of Forwarded Message%s\n\n",
508 mp->numsel > 1 ? "s" : "");
510 write(out, buffer, strlen(buffer));
513 snprintf(buffer, sizeof(buffer),
514 "End of %s Digest [Volume %d Issue %d]\n",
515 digest, volume, issue);
517 for (bp = buffer + i; i > 1; i--)
521 write(out, buffer, strlen(buffer));
527 ** Create an attachment header for the to be forward messages.
530 add_forw_hdr(char *draft)
535 snprintf(buffer, sizeof(buffer), "+%s", mp->foldpath);
536 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
537 if (!is_selected(mp, msgnum)) {
540 /* TODO: improve the code */
541 strncat(buffer, " ", sizeof(buffer)-strlen(buffer)-1);
542 strncat(buffer, m_name(msgnum),
543 sizeof(buffer)-strlen(buffer)-1);
545 annotate(draft, attach_hdr, buffer, 1, 0, -2, 1);
550 build_form(char *form, char *digest, int volume, int issue)
555 char *line, tmpfil[BUFSIZ];
557 register struct comp *cptr;
562 /* Get new format string */
563 nfs = new_fs(form, NULL, NULL);
564 fmtsize = strlen(nfs) + 256;
566 /* Compile format string */
567 fmt_compile(nfs, &fmt);
569 FINDCOMP(cptr, "digest");
571 cptr->c_text = digest;
572 FINDCOMP(cptr, "date");
574 cptr->c_text = getcpy(dtimenow(0));
582 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
583 if (cp == NULL) adios("forw", "unable to create temporary file");
584 strncpy(tmpfil, cp, sizeof(tmpfil));
586 if ((in = dup(fileno(tmp))) == NOTOK)
587 adios("dup", "unable to");
589 line = mh_xmalloc((unsigned) fmtsize);
590 fmt_scan(fmt, line, fmtsize, dat);
594 adios(tmpfil, "error writing");
596 lseek(in, (off_t) 0, SEEK_SET);