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 },
55 { "dashstuffing", 0 }, /* interface to mhl */
56 #define NBITSTUFFSW 18
57 { "nodashstuffing", 0 },
63 { "file file", 4 }, /* interface from msh */
67 { "build", 5 }, /* interface from mhe */
73 static char drft[BUFSIZ];
75 static char delim3[] =
76 "\n------------------------------------------------------------\n\n";
77 static char delim4[] = "\n------------------------------\n\n";
80 static struct msgs *mp = NULL; /* used a lot */
86 static void mhl_draft(int, char *, int, int, char *, char *, int);
87 static void copy_draft(int, char *, char *, int, int, int);
88 static void copy_mime_draft(int);
89 static int build_form(char *, char *, int, int);
93 main(int argc, char **argv)
95 int msgp = 0, anot = 0, inplace = 1, mime = 0;
96 int issue = 0, volume = 0, dashstuff = 0;
97 int nedit = 0, nwhat = 0, in;
99 char *cp, *cwd, *maildir;
100 char *digest = NULL, *ed = NULL;
101 char *file = NULL, *filter = NULL, *folder = NULL;
102 char *form = NULL, buf[BUFSIZ], value[10];
103 char **argp, **arguments, *msgs[MAXARGS];
110 setlocale(LC_ALL, "");
112 invo_name = mhbasename(argv[0]);
114 /* read user profile/context */
117 arguments = getarguments(invo_name, argc, argv, 1);
120 while ((cp = *argp++)) {
122 switch (smatch(++cp, switches)) {
124 ambigsw(cp, switches);
127 adios(NULL, "-%s unknown", cp);
130 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
131 print_help(buf, switches, 1);
134 print_version(invo_name);
145 if (!(ed = *argp++) || *ed == '-')
146 adios(NULL, "missing argument to %s",
155 if (!(whatnowproc = *argp++) ||
157 adios(NULL, "missing argument to %s",
163 buildsw++; /* fall... */
171 adios(NULL, "only one file at a time!");
172 if (!(cp = *argp++) || *cp == '-')
173 adios(NULL, "missing argument to %s",
175 file = getcpy(expanddir(cp));
178 if (!(cp = *argp++) || *cp == '-')
179 adios(NULL, "missing argument to %s",
181 filter = getcpy(etcpath(cp));
185 if (!(form = *argp++) || *form == '-')
186 adios(NULL, "missing argument to %s",
191 filter = getcpy(etcpath(mhlforward));
213 if (!(digest = *argp++) || *digest == '-')
214 adios(NULL, "missing argument to %s",
219 if (!(cp = *argp++) || *cp == '-')
220 adios(NULL, "missing argument to %s",
222 if ((issue = atoi(cp)) < 1)
223 adios(NULL, "bad argument %s %s",
227 if (!(cp = *argp++) || *cp == '-')
228 adios(NULL, "missing argument to %s",
230 if ((volume = atoi(cp)) < 1)
231 adios(NULL, "bad argument %s %s",
236 dashstuff = 1; /* trinary logic */
239 dashstuff = -1; /* trinary logic */
243 if (*cp == '+' || *cp == '@') {
245 adios(NULL, "only one folder at a time!");
247 folder = getcpy(expandfol(cp));
255 if (file && (msgp || folder))
256 adios(NULL, "can't mix files and folders/msgs");
259 strncpy(drft, buildsw ? toabsdir("draft")
260 : m_draft(seq_beyond), sizeof(drft));
262 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
267 ** Forwarding a file.
269 anot = 0; /* don't want to annotate a file */
272 ** Forwarding a message.
275 msgs[msgp++] = seq_cur;
277 folder = getcurfol();
278 maildir = toabsdir(folder);
280 if (chdir(maildir) == NOTOK)
281 adios(maildir, "unable to change directory to");
283 /* read folder and create message structure */
284 if (!(mp = folder_read(folder)))
285 adios(NULL, "unable to read folder %s", folder);
287 /* check for empty folder */
289 adios(NULL, "no messages in %s", folder);
291 /* parse all the message ranges/sequences and set SELECTED */
292 for (msgnum = 0; msgnum < msgp; msgnum++)
293 if (!m_convert(mp, msgs[msgnum]))
295 seq_setprev(mp); /* set the previous sequence */
298 if (filter && access(filter, R_OK) == NOTOK)
299 adios(filter, "unable to read");
302 ** Open form (component) file.
306 snprintf(buf, sizeof(buf), IFORMAT, digest);
307 if (volume == 0 && (cp = context_find(buf))
308 && ((issue = atoi(cp)) < 0))
313 snprintf(buf, sizeof(buf), VFORMAT, digest);
314 if ((cp = context_find(buf)) == NULL ||
315 (volume = atoi(cp)) <= 0)
319 in = build_form(form, digest, volume, issue);
321 in = open_form(&form, forwcomps);
323 if ((out = creat(drft, m_gmprot())) == NOTOK)
324 adios(drft, "unable to create");
327 ** copy the components into the draft
329 cpydata(in, out, form, drft);
333 /* just copy the file into the draft */
334 if ((in = open(file, O_RDONLY)) == NOTOK)
335 adios(file, "unable to open");
336 cpydata(in, out, file, drft);
341 ** If filter file is defined, then format the
342 ** messages into the draft using mhlproc.
345 mhl_draft(out, digest, volume, issue, drft, filter,
348 copy_mime_draft(out);
350 copy_draft(out, digest, drft, volume, issue,
355 snprintf(buf, sizeof(buf), IFORMAT, digest);
356 snprintf(value, sizeof(value), "%d", issue);
357 context_replace(buf, getcpy(value));
358 snprintf(buf, sizeof(buf), VFORMAT, digest);
359 snprintf(value, sizeof(value), "%d", volume);
360 context_replace(buf, getcpy(value));
363 context_replace(curfolder, folder); /* update current folder */
364 seq_setcur(mp, mp->lowsel); /* update current message */
365 seq_save(mp); /* synchronize sequences */
366 context_save(); /* save the context file */
371 what_now(ed, nedit, NOUSE, drft, NULL, 0, mp,
372 anot ? "Forwarded" : NULL, inplace, cwd);
379 ** Filter the messages you are forwarding, into the
380 ** draft calling the mhlproc, and reading its output
385 mhl_draft(int out, char *digest, int volume, int issue,
386 char *file, char *filter, int dashstuff)
389 int i, msgnum, pd[2];
394 if (pipe(pd) == NOTOK)
395 adios("pipe", "unable to create");
397 vec[0] = mhbasename(mhlproc);
399 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
403 adios("fork", "unable to");
411 vec[i++] = "-forwall";
416 vec[i++] = "-digest";
419 snprintf(buf1, sizeof(buf1), "%d", issue);
421 vec[i++] = "-volume";
422 snprintf(buf2, sizeof(buf2), "%d", volume);
427 ** Are we dashstuffing (quoting) the lines that begin
428 ** with `-'. We use the mhl default (don't add any
429 ** flag) unless the user has specified a specific flag.
432 vec[i++] = "-dashstuffing";
433 else if (dashstuff < 0)
434 vec[i++] = "-nodashstuffing";
436 if (mp->numsel >= MAXARGS - i)
437 adios(NULL, "more than %d messages for %s exec",
438 MAXARGS - i, vec[0]);
441 ** Now add the message names to filter. We can only
442 ** handle about 995 messages (because vec is fixed
443 ** size), but that should be plenty.
445 for (msgnum = mp->lowsel;
446 msgnum <= mp->hghsel && i < sizeof(vec) - 1;
448 if (is_selected(mp, msgnum))
449 vec[i++] = getcpy(m_name(msgnum));
452 execvp(mhlproc, vec);
453 fprintf(stderr, "unable to exec ");
459 cpydata(pd[0], out, vec[0], file);
461 pidXwait(child_id, mhlproc);
468 ** Copy the messages into the draft. The messages are
469 ** not filtered through the mhlproc. Do dashstuffing if
474 copy_draft(int out, char *digest, char *file, int volume, int issue,
477 int fd,i, msgcnt, msgnum;
479 register char *bp, *msgnam;
483 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
484 if (is_selected(mp, msgnum)) {
486 strncpy(buffer, msgnum == mp->lowsel ?
487 delim3 : delim4, sizeof(buffer));
489 /* Get buffer ready to go */
491 buflen = sizeof(buffer);
493 strncpy(bp, "\n-------", buflen);
498 if (msgnum == mp->lowsel) {
499 snprintf(bp, buflen, " Forwarded Message%s", mp->numsel > 1 ? "s" : "");
501 snprintf(bp, buflen, " Message %d", msgcnt);
507 strncpy(bp, "\n\n", buflen);
509 write(out, buffer, strlen(buffer));
511 if ((fd = open(msgnam = m_name (msgnum), O_RDONLY))
513 admonish(msgnam, "unable to read message");
518 ** Copy the message. Add RFC934 quoting (dashstuffing)
519 ** unless given the -nodashstuffing flag.
522 cpydgst(fd, out, msgnam, file);
524 cpydata(fd, out, msgnam, file);
532 strncpy(buffer, delim4, sizeof(buffer));
534 snprintf(buffer, sizeof(buffer),
535 "\n------- End of Forwarded Message%s\n\n",
536 mp->numsel > 1 ? "s" : "");
538 write(out, buffer, strlen(buffer));
541 snprintf(buffer, sizeof(buffer),
542 "End of %s Digest [Volume %d Issue %d]\n",
543 digest, volume, issue);
545 for (bp = buffer + i; i > 1; i--)
549 write(out, buffer, strlen(buffer));
555 ** Create a mhbuild composition file for forwarding message.
559 copy_mime_draft(int out)
564 snprintf(buffer, sizeof(buffer), "#forw [forwarded message%s] +%s",
565 mp->numsel == 1 ? "" : "s", mp->foldpath);
566 write(out, buffer, strlen(buffer));
567 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
568 if (is_selected(mp, msgnum)) {
569 snprintf(buffer, sizeof(buffer), " %s",
571 write(out, buffer, strlen(buffer));
578 build_form(char *form, char *digest, int volume, int issue)
583 char *line, tmpfil[BUFSIZ];
585 register struct comp *cptr;
590 /* Get new format string */
591 nfs = new_fs(form, NULL, NULL);
592 fmtsize = strlen(nfs) + 256;
594 /* Compile format string */
595 fmt_compile(nfs, &fmt);
597 FINDCOMP(cptr, "digest");
599 cptr->c_text = digest;
600 FINDCOMP(cptr, "date");
602 cptr->c_text = getcpy(dtimenow(0));
610 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
611 if (cp == NULL) adios("forw", "unable to create temporary file");
612 strncpy(tmpfil, cp, sizeof(tmpfil));
614 if ((in = dup(fileno(tmp))) == NOTOK)
615 adios("dup", "unable to");
617 line = mh_xmalloc((unsigned) fmtsize);
618 fmt_scan(fmt, line, fmtsize, dat);
622 adios(tmpfil, "error writing");
624 lseek(in, (off_t) 0, SEEK_SET);