]>
diplodocus.org Git - nmh/blob - uip/forw.c
3 * forw.c -- forward a message, or group of messages.
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/fmt_scan.h>
19 #define IFORMAT "digest-issue-%s"
20 #define VFORMAT "digest-volume-%s"
22 static struct swit switches
[] = {
28 { "draftfolder +folder", 0 },
30 { "draftmessage msg", 0 },
32 { "nodraftfolder", 0 },
34 { "editor editor", 0 },
38 { "filter filterfile", 0 },
40 { "form formfile", 0 },
56 { "issue number", 0 },
58 { "volume number", 0 },
60 { "whatnowproc program", 0 },
62 { "nowhatnowproc", 0 },
64 { "dashstuffing", 0 }, /* interface to mhl */
65 #define NBITSTUFFSW 21
66 { "nodashstuffing", 0 },
72 { "file file", 4 }, /* interface from msh */
76 { "build", 5 }, /* interface from mhe */
82 static struct swit aqrnl
[] = {
90 { "refile +folder", 0 },
96 static struct swit aqrl
[] = {
100 { "refile +folder", 0 },
104 static char drft
[BUFSIZ
];
106 static char delim3
[] =
107 "\n------------------------------------------------------------\n\n";
108 static char delim4
[] = "\n------------------------------\n\n";
111 static struct msgs
*mp
= NULL
; /* used a lot */
117 static void mhl_draft (int, char *, int, int, char *, char *, int);
118 static void copy_draft (int, char *, char *, int, int, int);
119 static void copy_mime_draft (int);
120 static int build_form (char *, char *, int, int);
124 main (int argc
, char **argv
)
126 int msgp
= 0, anot
= 0, inplace
= 1, mime
= 0;
127 int issue
= 0, volume
= 0, dashstuff
= 0;
128 int nedit
= 0, nwhat
= 0, i
, in
;
129 int out
, isdf
= 0, msgnum
;
130 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
131 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
132 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
;
133 char *form
= NULL
, buf
[BUFSIZ
], value
[10];
134 char **argp
, **arguments
, *msgs
[MAXARGS
];
142 setlocale(LC_ALL
, "");
144 invo_name
= r1bindex (argv
[0], '/');
146 /* read user profile/context */
149 arguments
= getarguments (invo_name
, argc
, argv
, 1);
152 while ((cp
= *argp
++)) {
154 switch (smatch (++cp
, switches
)) {
156 ambigsw (cp
, switches
);
159 adios (NULL
, "-%s unknown", cp
);
162 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
164 print_help (buf
, switches
, 1);
167 print_version(invo_name
);
178 if (!(ed
= *argp
++) || *ed
== '-')
179 adios (NULL
, "missing argument to %s", argp
[-2]);
187 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
188 adios (NULL
, "missing argument to %s", argp
[-2]);
193 buildsw
++; /* fall... */
201 adios (NULL
, "only one file at a time!");
202 if (!(cp
= *argp
++) || *cp
== '-')
203 adios (NULL
, "missing argument to %s", argp
[-2]);
204 file
= path (cp
, TFILE
);
207 if (!(cp
= *argp
++) || *cp
== '-')
208 adios (NULL
, "missing argument to %s", argp
[-2]);
209 filter
= getcpy (etcpath (cp
));
213 if (!(form
= *argp
++) || *form
== '-')
214 adios (NULL
, "missing argument to %s", argp
[-2]);
218 filter
= getcpy (etcpath (mhlforward
));
240 if (!(digest
= *argp
++) || *digest
== '-')
241 adios (NULL
, "missing argument to %s", argp
[-2]);
245 if (!(cp
= *argp
++) || *cp
== '-')
246 adios (NULL
, "missing argument to %s", argp
[-2]);
247 if ((issue
= atoi (cp
)) < 1)
248 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
251 if (!(cp
= *argp
++) || *cp
== '-')
252 adios (NULL
, "missing argument to %s", argp
[-2]);
253 if ((volume
= atoi (cp
)) < 1)
254 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
259 adios (NULL
, "only one draft folder at a time!");
260 if (!(cp
= *argp
++) || *cp
== '-')
261 adios (NULL
, "missing argument to %s", argp
[-2]);
262 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
263 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
267 adios (NULL
, "only one draft message at a time!");
268 if (!(dmsg
= *argp
++) || *dmsg
== '-')
269 adios (NULL
, "missing argument to %s", argp
[-2]);
277 dashstuff
= 1; /* trinary logic */
280 dashstuff
= -1; /* trinary logic */
284 if (*cp
== '+' || *cp
== '@') {
286 adios (NULL
, "only one folder at a time!");
288 folder
= pluspath (cp
);
294 cwd
= getcpy (pwd ());
296 if (!context_find ("path"))
297 free (path ("./", TFOLDER
));
298 if (file
&& (msgp
|| folder
))
299 adios (NULL
, "can't mix files and folders/msgs");
304 strncpy (drft
, buildsw
? m_maildir ("draft")
305 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
307 /* Check if a draft already exists */
308 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
310 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
312 /* Check if a draft already exists */
313 if (stat (drft
, &st
) != NOTOK
) {
315 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
316 for (i
= LISTDSW
; i
!= YESW
;) {
317 if (!(argp
= getans ("\nDisposition? ", isdf
? aqrnl
: aqrl
)))
319 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
328 showfile (++argp
, drft
);
331 if (refile (++argp
, drft
) == 0)
335 advise (NULL
, "say what?");
345 anot
= 0; /* don't want to annotate a file */
348 * Forwarding a message.
351 msgs
[msgp
++] = "cur";
353 folder
= getfolder (1);
354 maildir
= m_maildir (folder
);
356 if (chdir (maildir
) == NOTOK
)
357 adios (maildir
, "unable to change directory to");
359 /* read folder and create message structure */
360 if (!(mp
= folder_read (folder
)))
361 adios (NULL
, "unable to read folder %s", folder
);
363 /* check for empty folder */
365 adios (NULL
, "no messages in %s", folder
);
367 /* parse all the message ranges/sequences and set SELECTED */
368 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
369 if (!m_convert (mp
, msgs
[msgnum
]))
371 seq_setprev (mp
); /* set the previous sequence */
374 if (filter
&& access (filter
, R_OK
) == NOTOK
)
375 adios (filter
, "unable to read");
378 * Open form (component) file.
382 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
384 && (cp
= context_find (buf
))
385 && ((issue
= atoi (cp
)) < 0))
390 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
391 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
395 in
= build_form (form
, digest
, volume
, issue
);
397 in
= open_form(&form
, forwcomps
);
399 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
400 adios (drft
, "unable to create");
403 * copy the components into the draft
405 cpydata (in
, out
, form
, drft
);
409 /* just copy the file into the draft */
410 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
411 adios (file
, "unable to open");
412 cpydata (in
, out
, file
, drft
);
417 * If filter file is defined, then format the
418 * messages into the draft using mhlproc.
421 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
423 copy_mime_draft (out
);
425 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
429 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
430 snprintf (value
, sizeof(value
), "%d", issue
);
431 context_replace (buf
, getcpy (value
));
432 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
433 snprintf (value
, sizeof(value
), "%d", volume
);
434 context_replace (buf
, getcpy (value
));
437 context_replace (pfolder
, folder
); /* update current folder */
438 seq_setcur (mp
, mp
->lowsel
); /* update current message */
439 seq_save (mp
); /* synchronize sequences */
440 context_save (); /* save the context file */
445 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
446 anot
? "Forwarded" : NULL
, inplace
, cwd
);
453 * Filter the messages you are forwarding, into the
454 * draft calling the mhlproc, and reading its output
459 mhl_draft (int out
, char *digest
, int volume
, int issue
,
460 char *file
, char *filter
, int dashstuff
)
463 int i
, msgnum
, pd
[2];
468 if (pipe (pd
) == NOTOK
)
469 adios ("pipe", "unable to create");
471 vec
[0] = r1bindex (mhlproc
, '/');
473 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
477 adios ("fork", "unable to");
485 vec
[i
++] = "-forwall";
490 vec
[i
++] = "-digest";
493 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
495 vec
[i
++] = "-volume";
496 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
501 * Are we dashstuffing (quoting) the lines that begin
502 * with `-'. We use the mhl default (don't add any flag)
503 * unless the user has specified a specific flag.
506 vec
[i
++] = "-dashstuffing";
507 else if (dashstuff
< 0)
508 vec
[i
++] = "-nodashstuffing";
510 if (mp
->numsel
>= MAXARGS
- i
)
511 adios (NULL
, "more than %d messages for %s exec",
512 MAXARGS
- i
, vec
[0]);
515 * Now add the message names to filter. We can only
516 * handle about 995 messages (because vec is fixed size),
517 * but that should be plenty.
519 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
&& i
< sizeof(vec
) - 1;
521 if (is_selected (mp
, msgnum
))
522 vec
[i
++] = getcpy (m_name (msgnum
));
525 execvp (mhlproc
, vec
);
526 fprintf (stderr
, "unable to exec ");
532 cpydata (pd
[0], out
, vec
[0], file
);
534 pidXwait(child_id
, mhlproc
);
541 * Copy the messages into the draft. The messages are
542 * not filtered through the mhlproc. Do dashstuffing if
547 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
549 int fd
,i
, msgcnt
, msgnum
;
551 register char *bp
, *msgnam
;
555 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
556 if (is_selected (mp
, msgnum
)) {
558 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
561 /* Get buffer ready to go */
563 buflen
= sizeof(buffer
);
565 strncpy (bp
, "\n-------", buflen
);
570 if (msgnum
== mp
->lowsel
) {
571 snprintf (bp
, buflen
, " Forwarded Message%s",
572 mp
->numsel
> 1 ? "s" : "");
574 snprintf (bp
, buflen
, " Message %d", msgcnt
);
580 strncpy (bp
, "\n\n", buflen
);
582 write (out
, buffer
, strlen (buffer
));
584 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
585 admonish (msgnam
, "unable to read message");
590 * Copy the message. Add RFC934 quoting (dashstuffing)
591 * unless given the -nodashstuffing flag.
594 cpydgst (fd
, out
, msgnam
, file
);
596 cpydata (fd
, out
, msgnam
, file
);
604 strncpy (buffer
, delim4
, sizeof(buffer
));
606 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n\n",
607 mp
->numsel
> 1 ? "s" : "");
609 write (out
, buffer
, strlen (buffer
));
612 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
613 digest
, volume
, issue
);
615 for (bp
= buffer
+ i
; i
> 1; i
--)
619 write (out
, buffer
, strlen (buffer
));
625 * Create a mhn composition file for forwarding message.
629 copy_mime_draft (int out
)
634 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
635 mp
->numsel
== 1 ? "" : "s", mp
->foldpath
);
636 write (out
, buffer
, strlen (buffer
));
637 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
638 if (is_selected (mp
, msgnum
)) {
639 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
640 write (out
, buffer
, strlen (buffer
));
642 write (out
, "\n", 1);
647 build_form (char *form
, char *digest
, int volume
, int issue
)
652 char *line
, tmpfil
[BUFSIZ
];
654 register struct comp
*cptr
;
658 /* Get new format string */
659 nfs
= new_fs (form
, NULL
, NULL
);
660 fmtsize
= strlen (nfs
) + 256;
662 /* Compile format string */
663 fmt_compile (nfs
, &fmt
);
665 FINDCOMP (cptr
, "digest");
667 cptr
->c_text
= digest
;
668 FINDCOMP (cptr
, "date");
670 cptr
->c_text
= getcpy(dtimenow (0));
678 strncpy (tmpfil
, m_tmpfil (invo_name
), sizeof(tmpfil
));
679 if ((tmp
= fopen (tmpfil
, "w+")) == NULL
)
680 adios (tmpfil
, "unable to create");
682 if ((in
= dup (fileno (tmp
))) == NOTOK
)
683 adios ("dup", "unable to");
685 line
= mh_xmalloc ((unsigned) fmtsize
);
686 fmt_scan (fmt
, line
, fmtsize
, dat
);
690 adios (tmpfil
, "error writing");
692 lseek (in
, (off_t
) 0, SEEK_SET
);