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 add_forw_hdr(char *);
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,
352 copy_draft(out, digest, drft, volume, issue,
358 snprintf(buf, sizeof(buf), IFORMAT, digest);
359 snprintf(value, sizeof(value), "%d", issue);
360 context_replace(buf, getcpy(value));
361 snprintf(buf, sizeof(buf), VFORMAT, digest);
362 snprintf(value, sizeof(value), "%d", volume);
363 context_replace(buf, getcpy(value));
366 context_replace(curfolder, folder); /* update current folder */
367 seq_setcur(mp, mp->lowsel); /* update current message */
368 seq_save(mp); /* synchronize sequences */
369 context_save(); /* save the context file */
374 what_now(ed, nedit, NOUSE, drft, NULL, 0, mp,
375 anot ? "Forwarded" : NULL, inplace, cwd);
382 ** Filter the messages you are forwarding, into the
383 ** draft calling the mhlproc, and reading its output
387 mhl_draft(int out, char *digest, int volume, int issue,
388 char *file, char *filter, int dashstuff)
391 int i, msgnum, pd[2];
396 if (pipe(pd) == NOTOK)
397 adios("pipe", "unable to create");
399 vec[0] = mhbasename(mhlproc);
401 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
405 adios("fork", "unable to");
413 vec[i++] = "-forwall";
418 vec[i++] = "-digest";
421 snprintf(buf1, sizeof(buf1), "%d", issue);
423 vec[i++] = "-volume";
424 snprintf(buf2, sizeof(buf2), "%d", volume);
429 ** Are we dashstuffing (quoting) the lines that begin
430 ** with `-'. We use the mhl default (don't add any
431 ** flag) unless the user has specified a specific flag.
434 vec[i++] = "-dashstuffing";
435 else if (dashstuff < 0)
436 vec[i++] = "-nodashstuffing";
438 if (mp->numsel >= MAXARGS - i)
439 adios(NULL, "more than %d messages for %s exec",
440 MAXARGS - i, vec[0]);
443 ** Now add the message names to filter. We can only
444 ** handle about 995 messages (because vec is fixed
445 ** size), but that should be plenty.
447 for (msgnum = mp->lowsel;
448 msgnum <= mp->hghsel && i < sizeof(vec) - 1;
450 if (is_selected(mp, msgnum))
451 vec[i++] = getcpy(m_name(msgnum));
454 execvp(mhlproc, vec);
455 fprintf(stderr, "unable to exec ");
461 cpydata(pd[0], out, vec[0], file);
463 pidXwait(child_id, mhlproc);
470 ** Copy the messages into the draft. The messages are
471 ** not filtered through the mhlproc. Do dashstuffing if
475 copy_draft(int out, char *digest, char *file, int volume, int issue,
478 int fd,i, msgcnt, msgnum;
480 register char *bp, *msgnam;
484 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
485 if (is_selected(mp, msgnum)) {
487 strncpy(buffer, msgnum == mp->lowsel ?
488 delim3 : delim4, sizeof(buffer));
490 /* Get buffer ready to go */
492 buflen = sizeof(buffer);
494 strncpy(bp, "\n-------", buflen);
499 if (msgnum == mp->lowsel) {
500 snprintf(bp, buflen, " Forwarded Message%s", mp->numsel > 1 ? "s" : "");
502 snprintf(bp, buflen, " Message %d", msgcnt);
508 strncpy(bp, "\n\n", buflen);
510 write(out, buffer, strlen(buffer));
512 if ((fd = open(msgnam = m_name (msgnum), O_RDONLY))
514 admonish(msgnam, "unable to read message");
519 ** Copy the message. Add RFC934 quoting (dashstuffing)
520 ** unless given the -nodashstuffing flag.
523 cpydgst(fd, out, msgnam, file);
525 cpydata(fd, out, msgnam, file);
533 strncpy(buffer, delim4, sizeof(buffer));
535 snprintf(buffer, sizeof(buffer),
536 "\n------- End of Forwarded Message%s\n\n",
537 mp->numsel > 1 ? "s" : "");
539 write(out, buffer, strlen(buffer));
542 snprintf(buffer, sizeof(buffer),
543 "End of %s Digest [Volume %d Issue %d]\n",
544 digest, volume, issue);
546 for (bp = buffer + i; i > 1; i--)
550 write(out, buffer, strlen(buffer));
556 ** Create an attachment header for the to be forward messages.
559 add_forw_hdr(char *draft)
564 snprintf(buffer, sizeof(buffer), "+%s", mp->foldpath);
565 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
566 if (!is_selected(mp, msgnum)) {
569 /* TODO: improve the code */
570 strncat(buffer, " ", sizeof(buffer)-strlen(buffer)-1);
571 strncat(buffer, m_name(msgnum),
572 sizeof(buffer)-strlen(buffer)-1);
574 annotate(draft, attach_hdr, buffer, 1, 0, -2, 1);
579 build_form(char *form, char *digest, int volume, int issue)
584 char *line, tmpfil[BUFSIZ];
586 register struct comp *cptr;
591 /* Get new format string */
592 nfs = new_fs(form, NULL, NULL);
593 fmtsize = strlen(nfs) + 256;
595 /* Compile format string */
596 fmt_compile(nfs, &fmt);
598 FINDCOMP(cptr, "digest");
600 cptr->c_text = digest;
601 FINDCOMP(cptr, "date");
603 cptr->c_text = getcpy(dtimenow(0));
611 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
612 if (cp == NULL) adios("forw", "unable to create temporary file");
613 strncpy(tmpfil, cp, sizeof(tmpfil));
615 if ((in = dup(fileno(tmp))) == NOTOK)
616 adios("dup", "unable to");
618 line = mh_xmalloc((unsigned) fmtsize);
619 fmt_scan(fmt, line, fmtsize, dat);
623 adios(tmpfil, "error writing");
625 lseek(in, (off_t) 0, SEEK_SET);