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 = r1bindex (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]",
132 print_help (buf, switches, 1);
135 print_version(invo_name);
146 if (!(ed = *argp++) || *ed == '-')
147 adios (NULL, "missing argument to %s", argp[-2]);
155 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
156 adios (NULL, "missing argument to %s", argp[-2]);
161 buildsw++; /* fall... */
169 adios (NULL, "only one file at a time!");
170 if (!(cp = *argp++) || *cp == '-')
171 adios (NULL, "missing argument to %s", argp[-2]);
172 file = path (cp, TFILE);
175 if (!(cp = *argp++) || *cp == '-')
176 adios (NULL, "missing argument to %s", argp[-2]);
177 filter = getcpy (etcpath (cp));
181 if (!(form = *argp++) || *form == '-')
182 adios (NULL, "missing argument to %s", argp[-2]);
186 filter = getcpy (etcpath (mhlforward));
208 if (!(digest = *argp++) || *digest == '-')
209 adios (NULL, "missing argument to %s", argp[-2]);
213 if (!(cp = *argp++) || *cp == '-')
214 adios (NULL, "missing argument to %s", argp[-2]);
215 if ((issue = atoi (cp)) < 1)
216 adios (NULL, "bad argument %s %s", argp[-2], cp);
219 if (!(cp = *argp++) || *cp == '-')
220 adios (NULL, "missing argument to %s", argp[-2]);
221 if ((volume = atoi (cp)) < 1)
222 adios (NULL, "bad argument %s %s", argp[-2], cp);
226 dashstuff = 1; /* trinary logic */
229 dashstuff = -1; /* trinary logic */
233 if (*cp == '+' || *cp == '@') {
235 adios (NULL, "only one folder at a time!");
237 folder = pluspath (cp);
243 cwd = getcpy (pwd ());
245 if (!context_find ("path"))
246 free (path ("./", TFOLDER));
247 if (file && (msgp || folder))
248 adios (NULL, "can't mix files and folders/msgs");
251 strncpy (drft, buildsw ? m_maildir ("draft")
252 : m_draft("new"), sizeof(drft));
254 strncpy (drft, m_draft("new"), sizeof(drft));
259 ** Forwarding a file.
261 anot = 0; /* don't want to annotate a file */
264 ** Forwarding a message.
267 msgs[msgp++] = "cur";
269 folder = getfolder (1);
270 maildir = m_maildir (folder);
272 if (chdir (maildir) == NOTOK)
273 adios (maildir, "unable to change directory to");
275 /* read folder and create message structure */
276 if (!(mp = folder_read (folder)))
277 adios (NULL, "unable to read folder %s", folder);
279 /* check for empty folder */
281 adios (NULL, "no messages in %s", folder);
283 /* parse all the message ranges/sequences and set SELECTED */
284 for (msgnum = 0; msgnum < msgp; msgnum++)
285 if (!m_convert (mp, msgs[msgnum]))
287 seq_setprev (mp); /* set the previous sequence */
290 if (filter && access (filter, R_OK) == NOTOK)
291 adios (filter, "unable to read");
294 ** Open form (component) file.
298 snprintf (buf, sizeof(buf), IFORMAT, digest);
300 && (cp = context_find (buf))
301 && ((issue = atoi (cp)) < 0))
306 snprintf (buf, sizeof(buf), VFORMAT, digest);
307 if ((cp = context_find (buf)) == NULL || (volume = atoi (cp)) <= 0)
311 in = build_form (form, digest, volume, issue);
313 in = open_form(&form, forwcomps);
315 if ((out = creat (drft, m_gmprot ())) == NOTOK)
316 adios (drft, "unable to create");
319 ** copy the components into the draft
321 cpydata (in, out, form, drft);
325 /* just copy the file into the draft */
326 if ((in = open (file, O_RDONLY)) == NOTOK)
327 adios (file, "unable to open");
328 cpydata (in, out, file, drft);
333 ** If filter file is defined, then format the
334 ** messages into the draft using mhlproc.
337 mhl_draft (out, digest, volume, issue, drft, filter, dashstuff);
339 copy_mime_draft (out);
341 copy_draft (out, digest, drft, volume, issue, dashstuff);
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 (pfolder, 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
375 mhl_draft (int out, char *digest, int volume, int issue,
376 char *file, char *filter, int dashstuff)
379 int i, msgnum, pd[2];
384 if (pipe (pd) == NOTOK)
385 adios ("pipe", "unable to create");
387 vec[0] = r1bindex (mhlproc, '/');
389 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
393 adios ("fork", "unable to");
401 vec[i++] = "-forwall";
406 vec[i++] = "-digest";
409 snprintf (buf1, sizeof(buf1), "%d", issue);
411 vec[i++] = "-volume";
412 snprintf (buf2, sizeof(buf2), "%d", volume);
417 ** Are we dashstuffing (quoting) the lines that begin
418 ** with `-'. We use the mhl default (don't add any
419 ** flag) unless the user has specified a specific flag.
422 vec[i++] = "-dashstuffing";
423 else if (dashstuff < 0)
424 vec[i++] = "-nodashstuffing";
426 if (mp->numsel >= MAXARGS - i)
427 adios (NULL, "more than %d messages for %s exec",
428 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",
490 mp->numsel > 1 ? "s" : "");
492 snprintf (bp, buflen, " Message %d", msgcnt);
498 strncpy (bp, "\n\n", buflen);
500 write (out, buffer, strlen (buffer));
502 if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
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), "\n------- End of Forwarded Message%s\n\n",
525 mp->numsel > 1 ? "s" : "");
527 write (out, buffer, strlen (buffer));
530 snprintf (buffer, sizeof(buffer), "End of %s Digest [Volume %d Issue %d]\n",
531 digest, volume, issue);
533 for (bp = buffer + i; i > 1; i--)
537 write (out, buffer, strlen (buffer));
543 ** Create a mhbuild composition file for forwarding message.
547 copy_mime_draft (int out)
552 snprintf (buffer, sizeof(buffer), "#forw [forwarded message%s] +%s",
553 mp->numsel == 1 ? "" : "s", mp->foldpath);
554 write (out, buffer, strlen (buffer));
555 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
556 if (is_selected (mp, msgnum)) {
557 snprintf (buffer, sizeof(buffer), " %s", m_name (msgnum));
558 write (out, buffer, strlen (buffer));
560 write (out, "\n", 1);
565 build_form (char *form, char *digest, int volume, int issue)
570 char *line, tmpfil[BUFSIZ];
572 register struct comp *cptr;
577 /* Get new format string */
578 nfs = new_fs (form, NULL, NULL);
579 fmtsize = strlen (nfs) + 256;
581 /* Compile format string */
582 fmt_compile (nfs, &fmt);
584 FINDCOMP (cptr, "digest");
586 cptr->c_text = digest;
587 FINDCOMP (cptr, "date");
589 cptr->c_text = getcpy(dtimenow (0));
597 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
598 if (cp == NULL) adios("forw", "unable to create temporary file");
599 strncpy (tmpfil, cp, sizeof(tmpfil));
601 if ((in = dup (fileno (tmp))) == NOTOK)
602 adios ("dup", "unable to");
604 line = mh_xmalloc ((unsigned) fmtsize);
605 fmt_scan (fmt, line, fmtsize, dat);
609 adios (tmpfil, "error writing");
611 lseek (in, (off_t) 0, SEEK_SET);