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", argp[-2]);
154 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
155 adios(NULL, "missing argument to %s", argp[-2]);
160 buildsw++; /* fall... */
168 adios(NULL, "only one file at a time!");
169 if (!(cp = *argp++) || *cp == '-')
170 adios(NULL, "missing argument to %s", argp[-2]);
171 file = path(cp, TFILE);
174 if (!(cp = *argp++) || *cp == '-')
175 adios(NULL, "missing argument to %s", argp[-2]);
176 filter = getcpy(etcpath(cp));
180 if (!(form = *argp++) || *form == '-')
181 adios(NULL, "missing argument to %s", argp[-2]);
185 filter = getcpy(etcpath(mhlforward));
207 if (!(digest = *argp++) || *digest == '-')
208 adios(NULL, "missing argument to %s", argp[-2]);
212 if (!(cp = *argp++) || *cp == '-')
213 adios(NULL, "missing argument to %s", argp[-2]);
214 if ((issue = atoi(cp)) < 1)
215 adios(NULL, "bad argument %s %s", argp[-2], cp);
218 if (!(cp = *argp++) || *cp == '-')
219 adios(NULL, "missing argument to %s", argp[-2]);
220 if ((volume = atoi(cp)) < 1)
221 adios(NULL, "bad argument %s %s", argp[-2], cp);
225 dashstuff = 1; /* trinary logic */
228 dashstuff = -1; /* trinary logic */
232 if (*cp == '+' || *cp == '@') {
234 adios(NULL, "only one folder at a time!");
236 folder = pluspath(cp);
244 if (!context_find("path"))
245 free(path("./", TFOLDER));
246 if (file && (msgp || folder))
247 adios(NULL, "can't mix files and folders/msgs");
250 strncpy(drft, buildsw ? m_maildir("draft")
251 : m_draft("new"), sizeof(drft));
253 strncpy(drft, m_draft("new"), sizeof(drft));
258 ** Forwarding a file.
260 anot = 0; /* don't want to annotate a file */
263 ** Forwarding a message.
266 msgs[msgp++] = "cur";
268 folder = getfolder(1);
269 maildir = m_maildir(folder);
271 if (chdir(maildir) == NOTOK)
272 adios(maildir, "unable to change directory to");
274 /* read folder and create message structure */
275 if (!(mp = folder_read(folder)))
276 adios(NULL, "unable to read folder %s", folder);
278 /* check for empty folder */
280 adios(NULL, "no messages in %s", folder);
282 /* parse all the message ranges/sequences and set SELECTED */
283 for (msgnum = 0; msgnum < msgp; msgnum++)
284 if (!m_convert(mp, msgs[msgnum]))
286 seq_setprev(mp); /* set the previous sequence */
289 if (filter && access(filter, R_OK) == NOTOK)
290 adios(filter, "unable to read");
293 ** Open form (component) file.
297 snprintf(buf, sizeof(buf), IFORMAT, digest);
298 if (volume == 0 && (cp = context_find(buf))
299 && ((issue = atoi(cp)) < 0))
304 snprintf(buf, sizeof(buf), VFORMAT, digest);
305 if ((cp = context_find(buf)) == NULL ||
306 (volume = atoi(cp)) <= 0)
310 in = build_form(form, digest, volume, issue);
312 in = open_form(&form, forwcomps);
314 if ((out = creat(drft, m_gmprot())) == NOTOK)
315 adios(drft, "unable to create");
318 ** copy the components into the draft
320 cpydata(in, out, form, drft);
324 /* just copy the file into the draft */
325 if ((in = open(file, O_RDONLY)) == NOTOK)
326 adios(file, "unable to open");
327 cpydata(in, out, file, drft);
332 ** If filter file is defined, then format the
333 ** messages into the draft using mhlproc.
336 mhl_draft(out, digest, volume, issue, drft, filter,
339 copy_mime_draft(out);
341 copy_draft(out, digest, drft, volume, issue,
346 snprintf(buf, sizeof(buf), IFORMAT, digest);
347 snprintf(value, sizeof(value), "%d", issue);
348 context_replace(buf, getcpy(value));
349 snprintf(buf, sizeof(buf), VFORMAT, digest);
350 snprintf(value, sizeof(value), "%d", volume);
351 context_replace(buf, getcpy(value));
354 context_replace(pfolder, folder); /* update current folder */
355 seq_setcur(mp, mp->lowsel); /* update current message */
356 seq_save(mp); /* synchronize sequences */
357 context_save(); /* save the context file */
362 what_now(ed, nedit, NOUSE, drft, NULL, 0, mp,
363 anot ? "Forwarded" : NULL, inplace, cwd);
370 ** Filter the messages you are forwarding, into the
371 ** draft calling the mhlproc, and reading its output
376 mhl_draft(int out, char *digest, int volume, int issue,
377 char *file, char *filter, int dashstuff)
380 int i, msgnum, pd[2];
385 if (pipe(pd) == NOTOK)
386 adios("pipe", "unable to create");
388 vec[0] = mhbasename(mhlproc);
390 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
394 adios("fork", "unable to");
402 vec[i++] = "-forwall";
407 vec[i++] = "-digest";
410 snprintf(buf1, sizeof(buf1), "%d", issue);
412 vec[i++] = "-volume";
413 snprintf(buf2, sizeof(buf2), "%d", volume);
418 ** Are we dashstuffing (quoting) the lines that begin
419 ** with `-'. We use the mhl default (don't add any
420 ** flag) unless the user has specified a specific flag.
423 vec[i++] = "-dashstuffing";
424 else if (dashstuff < 0)
425 vec[i++] = "-nodashstuffing";
427 if (mp->numsel >= MAXARGS - i)
428 adios(NULL, "more than %d messages for %s exec", MAXARGS - i, vec[0]);
431 ** Now add the message names to filter. We can only
432 ** handle about 995 messages (because vec is fixed
433 ** size), but that should be plenty.
435 for (msgnum = mp->lowsel;
436 msgnum <= mp->hghsel && i < sizeof(vec) - 1;
438 if (is_selected(mp, msgnum))
439 vec[i++] = getcpy(m_name(msgnum));
442 execvp(mhlproc, vec);
443 fprintf(stderr, "unable to exec ");
449 cpydata(pd[0], out, vec[0], file);
451 pidXwait(child_id, mhlproc);
458 ** Copy the messages into the draft. The messages are
459 ** not filtered through the mhlproc. Do dashstuffing if
464 copy_draft(int out, char *digest, char *file, int volume, int issue,
467 int fd,i, msgcnt, msgnum;
469 register char *bp, *msgnam;
473 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
474 if (is_selected(mp, msgnum)) {
476 strncpy(buffer, msgnum == mp->lowsel ?
477 delim3 : delim4, sizeof(buffer));
479 /* Get buffer ready to go */
481 buflen = sizeof(buffer);
483 strncpy(bp, "\n-------", buflen);
488 if (msgnum == mp->lowsel) {
489 snprintf(bp, buflen, " Forwarded Message%s", mp->numsel > 1 ? "s" : "");
491 snprintf(bp, buflen, " Message %d", msgcnt);
497 strncpy(bp, "\n\n", buflen);
499 write(out, buffer, strlen(buffer));
501 if ((fd = open(msgnam = m_name (msgnum), O_RDONLY))
503 admonish(msgnam, "unable to read message");
508 ** Copy the message. Add RFC934 quoting (dashstuffing)
509 ** unless given the -nodashstuffing flag.
512 cpydgst(fd, out, msgnam, file);
514 cpydata(fd, out, msgnam, file);
522 strncpy(buffer, delim4, sizeof(buffer));
524 snprintf(buffer, sizeof(buffer),
525 "\n------- End of Forwarded Message%s\n\n",
526 mp->numsel > 1 ? "s" : "");
528 write(out, buffer, strlen(buffer));
531 snprintf(buffer, sizeof(buffer),
532 "End of %s Digest [Volume %d Issue %d]\n",
533 digest, volume, issue);
535 for (bp = buffer + i; i > 1; i--)
539 write(out, buffer, strlen(buffer));
545 ** Create a mhbuild composition file for forwarding message.
549 copy_mime_draft(int out)
554 snprintf(buffer, sizeof(buffer), "#forw [forwarded message%s] +%s",
555 mp->numsel == 1 ? "" : "s", mp->foldpath);
556 write(out, buffer, strlen(buffer));
557 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
558 if (is_selected(mp, msgnum)) {
559 snprintf(buffer, sizeof(buffer), " %s",
561 write(out, buffer, strlen(buffer));
568 build_form(char *form, char *digest, int volume, int issue)
573 char *line, tmpfil[BUFSIZ];
575 register struct comp *cptr;
580 /* Get new format string */
581 nfs = new_fs(form, NULL, NULL);
582 fmtsize = strlen(nfs) + 256;
584 /* Compile format string */
585 fmt_compile(nfs, &fmt);
587 FINDCOMP(cptr, "digest");
589 cptr->c_text = digest;
590 FINDCOMP(cptr, "date");
592 cptr->c_text = getcpy(dtimenow(0));
600 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
601 if (cp == NULL) adios("forw", "unable to create temporary file");
602 strncpy(tmpfil, cp, sizeof(tmpfil));
604 if ((in = dup(fileno(tmp))) == NOTOK)
605 adios("dup", "unable to");
607 line = mh_xmalloc((unsigned) fmtsize);
608 fmt_scan(fmt, line, fmtsize, dat);
612 adios(tmpfil, "error writing");
614 lseek(in, (off_t) 0, SEEK_SET);