3 * forw.c -- forward a message, or group of messages.
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
16 #define IFORMAT "digest-issue-%s"
17 #define VFORMAT "digest-volume-%s"
19 static struct swit switches[] = {
25 { "draftfolder +folder", 0 },
27 { "draftmessage msg", 0 },
29 { "nodraftfolder", 0 },
31 { "editor editor", 0 },
35 { "filter filterfile", 0 },
37 { "form formfile", 0 },
53 { "issue number", 0 },
55 { "volume number", 0 },
57 { "whatnowproc program", 0 },
59 { "nowhatnowproc", 0 },
61 { "dashstuffing", 0 }, /* interface to mhl */
62 #define NBITSTUFFSW 21
63 { "nodashstuffing", 0 },
69 { "file file", 4 }, /* interface from msh */
71 { "build", 5 }, /* interface from mhe */
73 { "from address", 0 },
79 { "subject text", 0 },
83 { "width columns", 0 },
87 static struct swit aqrnl[] = {
95 { "refile +folder", 0 },
101 static struct swit aqrl[] = {
105 { "refile +folder", 0 },
109 static char drft[BUFSIZ];
111 static char delim3[] =
112 "\n------------------------------------------------------------\n\n";
113 static char delim4[] = "\n------------------------------\n\n";
116 static struct msgs *mp = NULL; /* used a lot */
122 static void mhl_draft (int, char *, int, int, char *, char *, int);
123 static void copy_draft (int, char *, char *, int, int, int);
124 static void copy_mime_draft (int);
128 main (int argc, char **argv)
130 int msgp = 0, anot = 0, inplace = 1, mime = 0;
131 int issue = 0, volume = 0, dashstuff = 0;
132 int nedit = 0, nwhat = 0, i, in;
133 int out, isdf = 0, msgnum = 0;
134 int outputlinelen = OUTPUTLINELEN;
136 char *cp, *cwd, *maildir, *dfolder = NULL;
137 char *dmsg = NULL, *digest = NULL, *ed = NULL;
138 char *file = NULL, *filter = NULL, *folder = NULL, *fwdmsg = NULL;
139 char *from = NULL, *to = NULL, *cc = NULL, *subject = NULL, *fcc = NULL;
140 char *form = NULL, buf[BUFSIZ], value[10];
141 char **argp, **arguments, *msgs[MAXARGS];
147 setlocale(LC_ALL, "");
149 invo_name = r1bindex (argv[0], '/');
151 /* read user profile/context */
154 arguments = getarguments (invo_name, argc, argv, 1);
157 while ((cp = *argp++)) {
159 switch (smatch (++cp, switches)) {
161 ambigsw (cp, switches);
164 adios (NULL, "-%s unknown", cp);
167 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
169 print_help (buf, switches, 1);
172 print_version(invo_name);
183 if (!(ed = *argp++) || *ed == '-')
184 adios (NULL, "missing argument to %s", argp[-2]);
192 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
193 adios (NULL, "missing argument to %s", argp[-2]);
197 buildsw++; /* fall... */
204 adios (NULL, "only one file at a time!");
205 if (!(cp = *argp++) || *cp == '-')
206 adios (NULL, "missing argument to %s", argp[-2]);
207 file = path (cp, TFILE);
210 if (!(cp = *argp++) || *cp == '-')
211 adios (NULL, "missing argument to %s", argp[-2]);
212 filter = getcpy (etcpath (cp));
216 if (!(form = *argp++) || *form == '-')
217 adios (NULL, "missing argument to %s", argp[-2]);
221 filter = getcpy (etcpath (mhlforward));
243 if (!(cp = *argp++) || *cp == '-')
244 adios (NULL, "missing argument to %s", argp[-2]);
249 if (!(cp = *argp++) || *cp == '-')
250 adios (NULL, "missing argument to %s", argp[-2]);
251 if ((issue = atoi (cp)) < 1)
252 adios (NULL, "bad argument %s %s", argp[-2], cp);
255 if (!(cp = *argp++) || *cp == '-')
256 adios (NULL, "missing argument to %s", argp[-2]);
257 if ((volume = atoi (cp)) < 1)
258 adios (NULL, "bad argument %s %s", argp[-2], cp);
263 adios (NULL, "only one draft folder at a time!");
264 if (!(cp = *argp++) || *cp == '-')
265 adios (NULL, "missing argument to %s", argp[-2]);
266 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
267 *cp != '@' ? TFOLDER : TSUBCWF);
271 adios (NULL, "only one draft message at a time!");
272 if (!(dmsg = *argp++) || *dmsg == '-')
273 adios (NULL, "missing argument to %s", argp[-2]);
281 dashstuff = 1; /* trinary logic */
284 dashstuff = -1; /* trinary logic */
288 if (!(cp = *argp++) || *cp == '-')
289 adios (NULL, "missing argument to %s", argp[-2]);
290 from = addlist(from, cp);
293 if (!(cp = *argp++) || *cp == '-')
294 adios (NULL, "missing argument to %s", argp[-2]);
295 to = addlist(to, cp);
298 if (!(cp = *argp++) || *cp == '-')
299 adios (NULL, "missing argument to %s", argp[-2]);
300 cc = addlist(cc, cp);
303 if (!(cp = *argp++) || *cp == '-')
304 adios (NULL, "missing argument to %s", argp[-2]);
305 fcc = addlist(fcc, cp);
308 if (!(cp = *argp++) || *cp == '-')
309 adios (NULL, "missing argument to %s", argp[-2]);
310 subject = getcpy(cp);
314 if (!(cp = *argp++) || *cp == '-')
315 adios (NULL, "missing argument to %s", argp[-2]);
316 if ((outputlinelen = atoi(cp)) < 10)
317 adios (NULL, "impossible width %d", outputlinelen);
321 if (*cp == '+' || *cp == '@') {
323 adios (NULL, "only one folder at a time!");
325 folder = pluspath (cp);
331 cwd = getcpy (pwd ());
333 if (!context_find ("path"))
334 free (path ("./", TFOLDER));
335 if (file && (msgp || folder))
336 adios (NULL, "can't mix files and folders/msgs");
340 strncpy (drft, buildsw ? m_maildir ("draft")
341 : m_draft (dfolder, NULL, NOUSE, &isdf), sizeof(drft));
343 /* Check if a draft already exists */
344 if (!buildsw && stat (drft, &st) != NOTOK) {
345 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
346 for (i = LISTDSW; i != YESW;) {
347 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
349 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
358 showfile (++argp, drft);
361 if (refile (++argp, drft) == 0)
365 advise (NULL, "say what?");
375 anot = 0; /* don't want to annotate a file */
378 * Forwarding a message.
381 msgs[msgp++] = "cur";
383 folder = getfolder (1);
384 maildir = m_maildir (folder);
386 if (chdir (maildir) == NOTOK)
387 adios (maildir, "unable to change directory to");
389 /* read folder and create message structure */
390 if (!(mp = folder_read (folder)))
391 adios (NULL, "unable to read folder %s", folder);
393 /* check for empty folder */
395 adios (NULL, "no messages in %s", folder);
397 /* parse all the message ranges/sequences and set SELECTED */
398 for (msgnum = 0; msgnum < msgp; msgnum++)
399 if (!m_convert (mp, msgs[msgnum]))
401 seq_setprev (mp); /* set the previous sequence */
404 * Find the first message in our set and use it as the input
405 * for the component scanner
408 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
409 if (is_selected (mp, msgnum)) {
410 fwdmsg = strdup(m_name(msgnum));
415 adios (NULL, "Unable to find input message");
418 if (filter && access (filter, R_OK) == NOTOK)
419 adios (filter, "unable to read");
422 * Open form (component) file.
426 snprintf (buf, sizeof(buf), IFORMAT, digest);
428 && (cp = context_find (buf))
429 && ((issue = atoi (cp)) < 0))
434 snprintf (buf, sizeof(buf), VFORMAT, digest);
435 if ((cp = context_find (buf)) == NULL || (volume = atoi (cp)) <= 0)
444 dat[0] = digest ? issue : msgnum;
447 dat[3] = outputlinelen;
451 in = build_form (form, digest, dat, from, to, cc, fcc, subject,
452 file ? file : fwdmsg);
454 if ((out = creat (drft, m_gmprot ())) == NOTOK)
455 adios (drft, "unable to create");
458 * copy the components into the draft
460 cpydata (in, out, form, drft);
464 /* just copy the file into the draft */
465 if ((in = open (file, O_RDONLY)) == NOTOK)
466 adios (file, "unable to open");
467 cpydata (in, out, file, drft);
472 * If filter file is defined, then format the
473 * messages into the draft using mhlproc.
476 mhl_draft (out, digest, volume, issue, drft, filter, dashstuff);
478 copy_mime_draft (out);
480 copy_draft (out, digest, drft, volume, issue, dashstuff);
484 snprintf (buf, sizeof(buf), IFORMAT, digest);
485 snprintf (value, sizeof(value), "%d", issue);
486 context_replace (buf, getcpy (value));
487 snprintf (buf, sizeof(buf), VFORMAT, digest);
488 snprintf (value, sizeof(value), "%d", volume);
489 context_replace (buf, getcpy (value));
492 context_replace (pfolder, folder); /* update current folder */
493 seq_setcur (mp, mp->lowsel); /* update current message */
494 seq_save (mp); /* synchronize sequences */
495 context_save (); /* save the context file */
500 what_now (ed, nedit, NOUSE, drft, NULL, 0, mp,
501 anot ? "Forwarded" : NULL, inplace, cwd);
508 * Filter the messages you are forwarding, into the
509 * draft calling the mhlproc, and reading its output
514 mhl_draft (int out, char *digest, int volume, int issue,
515 char *file, char *filter, int dashstuff)
518 int i, msgnum, pd[2];
523 if (pipe (pd) == NOTOK)
524 adios ("pipe", "unable to create");
526 vec[0] = r1bindex (mhlproc, '/');
528 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
532 adios ("fork", "unable to");
540 vec[i++] = "-forwall";
545 vec[i++] = "-digest";
548 snprintf (buf1, sizeof(buf1), "%d", issue);
550 vec[i++] = "-volume";
551 snprintf (buf2, sizeof(buf2), "%d", volume);
556 * Are we dashstuffing (quoting) the lines that begin
557 * with `-'. We use the mhl default (don't add any flag)
558 * unless the user has specified a specific flag.
561 vec[i++] = "-dashstuffing";
562 else if (dashstuff < 0)
563 vec[i++] = "-nodashstuffing";
565 if (mp->numsel >= MAXARGS - i)
566 adios (NULL, "more than %d messages for %s exec",
567 MAXARGS - i, vec[0]);
570 * Now add the message names to filter. We can only
571 * handle about 995 messages (because vec is fixed size),
572 * but that should be plenty.
574 for (msgnum = mp->lowsel;
575 msgnum <= mp->hghsel && i < (int) sizeof(vec) - 1;
577 if (is_selected (mp, msgnum))
578 vec[i++] = getcpy (m_name (msgnum));
581 execvp (mhlproc, vec);
582 fprintf (stderr, "unable to exec ");
588 cpydata (pd[0], out, vec[0], file);
590 pidXwait(child_id, mhlproc);
597 * Copy the messages into the draft. The messages are
598 * not filtered through the mhlproc. Do dashstuffing if
603 copy_draft (int out, char *digest, char *file, int volume, int issue, int dashstuff)
605 int fd,i, msgcnt, msgnum;
607 register char *bp, *msgnam;
611 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
612 if (is_selected (mp, msgnum)) {
614 strncpy (buffer, msgnum == mp->lowsel ? delim3 : delim4,
617 /* Get buffer ready to go */
619 buflen = sizeof(buffer);
621 strncpy (bp, "\n-------", buflen);
626 if (msgnum == mp->lowsel) {
627 snprintf (bp, buflen, " Forwarded Message%s",
628 mp->numsel > 1 ? "s" : "");
630 snprintf (bp, buflen, " Message %d", msgcnt);
636 strncpy (bp, "\n\n", buflen);
638 write (out, buffer, strlen (buffer));
640 if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
641 admonish (msgnam, "unable to read message");
646 * Copy the message. Add RFC934 quoting (dashstuffing)
647 * unless given the -nodashstuffing flag.
650 cpydgst (fd, out, msgnam, file);
652 cpydata (fd, out, msgnam, file);
660 strncpy (buffer, delim4, sizeof(buffer));
662 snprintf (buffer, sizeof(buffer), "\n------- End of Forwarded Message%s\n",
663 mp->numsel > 1 ? "s" : "");
665 write (out, buffer, strlen (buffer));
668 snprintf (buffer, sizeof(buffer), "End of %s Digest [Volume %d Issue %d]\n",
669 digest, volume, issue);
671 for (bp = buffer + i; i > 1; i--)
675 write (out, buffer, strlen (buffer));
681 * Create a mhn composition file for forwarding message.
685 copy_mime_draft (int out)
690 snprintf (buffer, sizeof(buffer), "#forw [forwarded message%s] +%s",
691 mp->numsel == 1 ? "" : "s", mp->foldpath);
692 write (out, buffer, strlen (buffer));
693 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
694 if (is_selected (mp, msgnum)) {
695 snprintf (buffer, sizeof(buffer), " %s", m_name (msgnum));
696 write (out, buffer, strlen (buffer));
698 write (out, "\n", 1);