]>
diplodocus.org Git - nmh/blob - uip/forw.c
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.
12 #include <h/fmt_scan.h>
17 #define IFORMAT "digest-issue-%s"
18 #define VFORMAT "digest-volume-%s"
20 static struct swit switches
[] = {
26 { "draftfolder +folder", 0 },
28 { "draftmessage msg", 0 },
30 { "nodraftfolder", 0 },
32 { "editor editor", 0 },
36 { "filter filterfile", 0 },
38 { "form formfile", 0 },
54 { "issue number", 0 },
56 { "volume number", 0 },
58 { "whatnowproc program", 0 },
60 { "nowhatnowproc", 0 },
62 { "dashstuffing", 0 }, /* interface to mhl */
63 #define NBITSTUFFSW 21
64 { "nodashstuffing", 0 },
70 { "file file", 4 }, /* interface from msh */
72 { "build", 5 }, /* interface from mhe */
77 static struct swit aqrnl
[] = {
85 { "refile +folder", 0 },
91 static struct swit aqrl
[] = {
95 { "refile +folder", 0 },
99 static char drft
[BUFSIZ
];
101 static char delim3
[] =
102 "\n------------------------------------------------------------\n\n";
103 static char delim4
[] = "\n------------------------------\n\n";
106 static struct msgs
*mp
= NULL
; /* used a lot */
112 static void mhl_draft (int, char *, int, int, char *, char *, int);
113 static void copy_draft (int, char *, char *, int, int, int);
114 static void copy_mime_draft (int);
115 static int build_form (char *, char *, int, int);
119 main (int argc
, char **argv
)
121 int msgp
= 0, anot
= 0, inplace
= 1, mime
= 0;
122 int issue
= 0, volume
= 0, dashstuff
= 0;
123 int nedit
= 0, nwhat
= 0, i
, in
;
124 int out
, isdf
= 0, msgnum
;
125 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
126 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
127 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
;
128 char *form
= NULL
, buf
[BUFSIZ
], value
[10];
129 char **argp
, **arguments
, *msgs
[MAXARGS
];
135 setlocale(LC_ALL
, "");
137 invo_name
= r1bindex (argv
[0], '/');
139 /* read user profile/context */
142 arguments
= getarguments (invo_name
, argc
, argv
, 1);
145 while ((cp
= *argp
++)) {
147 switch (smatch (++cp
, switches
)) {
149 ambigsw (cp
, switches
);
152 adios (NULL
, "-%s unknown", cp
);
155 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
157 print_help (buf
, switches
, 1);
160 print_version(invo_name
);
171 if (!(ed
= *argp
++) || *ed
== '-')
172 adios (NULL
, "missing argument to %s", argp
[-2]);
180 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
181 adios (NULL
, "missing argument to %s", argp
[-2]);
185 buildsw
++; /* fall... */
192 adios (NULL
, "only one file at a time!");
193 if (!(cp
= *argp
++) || *cp
== '-')
194 adios (NULL
, "missing argument to %s", argp
[-2]);
195 file
= path (cp
, TFILE
);
198 if (!(cp
= *argp
++) || *cp
== '-')
199 adios (NULL
, "missing argument to %s", argp
[-2]);
200 filter
= getcpy (etcpath (cp
));
204 if (!(form
= *argp
++) || *form
== '-')
205 adios (NULL
, "missing argument to %s", argp
[-2]);
209 filter
= getcpy (etcpath (mhlforward
));
231 if (!(digest
= *argp
++) || *digest
== '-')
232 adios (NULL
, "missing argument to %s", argp
[-2]);
236 if (!(cp
= *argp
++) || *cp
== '-')
237 adios (NULL
, "missing argument to %s", argp
[-2]);
238 if ((issue
= atoi (cp
)) < 1)
239 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
242 if (!(cp
= *argp
++) || *cp
== '-')
243 adios (NULL
, "missing argument to %s", argp
[-2]);
244 if ((volume
= atoi (cp
)) < 1)
245 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
250 adios (NULL
, "only one draft folder at a time!");
251 if (!(cp
= *argp
++) || *cp
== '-')
252 adios (NULL
, "missing argument to %s", argp
[-2]);
253 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
254 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
258 adios (NULL
, "only one draft message at a time!");
259 if (!(dmsg
= *argp
++) || *dmsg
== '-')
260 adios (NULL
, "missing argument to %s", argp
[-2]);
268 dashstuff
= 1; /* trinary logic */
271 dashstuff
= -1; /* trinary logic */
275 if (*cp
== '+' || *cp
== '@') {
277 adios (NULL
, "only one folder at a time!");
279 folder
= pluspath (cp
);
285 cwd
= getcpy (pwd ());
287 if (!context_find ("path"))
288 free (path ("./", TFOLDER
));
289 if (file
&& (msgp
|| folder
))
290 adios (NULL
, "can't mix files and folders/msgs");
294 strncpy (drft
, buildsw
? m_maildir ("draft")
295 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
297 /* Check if a draft already exists */
298 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
299 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
300 for (i
= LISTDSW
; i
!= YESW
;) {
301 if (!(argp
= getans ("\nDisposition? ", isdf
? aqrnl
: aqrl
)))
303 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
312 showfile (++argp
, drft
);
315 if (refile (++argp
, drft
) == 0)
319 advise (NULL
, "say what?");
329 anot
= 0; /* don't want to annotate a file */
332 * Forwarding a message.
335 msgs
[msgp
++] = "cur";
337 folder
= getfolder (1);
338 maildir
= m_maildir (folder
);
340 if (chdir (maildir
) == NOTOK
)
341 adios (maildir
, "unable to change directory to");
343 /* read folder and create message structure */
344 if (!(mp
= folder_read (folder
)))
345 adios (NULL
, "unable to read folder %s", folder
);
347 /* check for empty folder */
349 adios (NULL
, "no messages in %s", folder
);
351 /* parse all the message ranges/sequences and set SELECTED */
352 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
353 if (!m_convert (mp
, msgs
[msgnum
]))
355 seq_setprev (mp
); /* set the previous sequence */
358 if (filter
&& access (filter
, R_OK
) == NOTOK
)
359 adios (filter
, "unable to read");
362 * Open form (component) file.
366 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
368 && (cp
= context_find (buf
))
369 && ((issue
= atoi (cp
)) < 0))
374 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
375 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
379 in
= build_form (form
, digest
, volume
, issue
);
381 in
= open_form(&form
, forwcomps
);
383 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
384 adios (drft
, "unable to create");
387 * copy the components into the draft
389 cpydata (in
, out
, form
, drft
);
393 /* just copy the file into the draft */
394 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
395 adios (file
, "unable to open");
396 cpydata (in
, out
, file
, drft
);
401 * If filter file is defined, then format the
402 * messages into the draft using mhlproc.
405 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
407 copy_mime_draft (out
);
409 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
413 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
414 snprintf (value
, sizeof(value
), "%d", issue
);
415 context_replace (buf
, getcpy (value
));
416 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
417 snprintf (value
, sizeof(value
), "%d", volume
);
418 context_replace (buf
, getcpy (value
));
421 context_replace (pfolder
, folder
); /* update current folder */
422 seq_setcur (mp
, mp
->lowsel
); /* update current message */
423 seq_save (mp
); /* synchronize sequences */
424 context_save (); /* save the context file */
429 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
430 anot
? "Forwarded" : NULL
, inplace
, cwd
);
437 * Filter the messages you are forwarding, into the
438 * draft calling the mhlproc, and reading its output
443 mhl_draft (int out
, char *digest
, int volume
, int issue
,
444 char *file
, char *filter
, int dashstuff
)
447 int i
, msgnum
, pd
[2];
452 if (pipe (pd
) == NOTOK
)
453 adios ("pipe", "unable to create");
455 vec
[0] = r1bindex (mhlproc
, '/');
457 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
461 adios ("fork", "unable to");
469 vec
[i
++] = "-forwall";
474 vec
[i
++] = "-digest";
477 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
479 vec
[i
++] = "-volume";
480 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
485 * Are we dashstuffing (quoting) the lines that begin
486 * with `-'. We use the mhl default (don't add any flag)
487 * unless the user has specified a specific flag.
490 vec
[i
++] = "-dashstuffing";
491 else if (dashstuff
< 0)
492 vec
[i
++] = "-nodashstuffing";
494 if (mp
->numsel
>= MAXARGS
- i
)
495 adios (NULL
, "more than %d messages for %s exec",
496 MAXARGS
- i
, vec
[0]);
499 * Now add the message names to filter. We can only
500 * handle about 995 messages (because vec is fixed size),
501 * but that should be plenty.
503 for (msgnum
= mp
->lowsel
;
504 msgnum
<= mp
->hghsel
&& i
< (int) sizeof(vec
) - 1;
506 if (is_selected (mp
, msgnum
))
507 vec
[i
++] = getcpy (m_name (msgnum
));
510 execvp (mhlproc
, vec
);
511 fprintf (stderr
, "unable to exec ");
517 cpydata (pd
[0], out
, vec
[0], file
);
519 pidXwait(child_id
, mhlproc
);
526 * Copy the messages into the draft. The messages are
527 * not filtered through the mhlproc. Do dashstuffing if
532 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
534 int fd
,i
, msgcnt
, msgnum
;
536 register char *bp
, *msgnam
;
540 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
541 if (is_selected (mp
, msgnum
)) {
543 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
546 /* Get buffer ready to go */
548 buflen
= sizeof(buffer
);
550 strncpy (bp
, "\n-------", buflen
);
555 if (msgnum
== mp
->lowsel
) {
556 snprintf (bp
, buflen
, " Forwarded Message%s",
557 mp
->numsel
> 1 ? "s" : "");
559 snprintf (bp
, buflen
, " Message %d", msgcnt
);
565 strncpy (bp
, "\n\n", buflen
);
567 write (out
, buffer
, strlen (buffer
));
569 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
570 admonish (msgnam
, "unable to read message");
575 * Copy the message. Add RFC934 quoting (dashstuffing)
576 * unless given the -nodashstuffing flag.
579 cpydgst (fd
, out
, msgnam
, file
);
581 cpydata (fd
, out
, msgnam
, file
);
589 strncpy (buffer
, delim4
, sizeof(buffer
));
591 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n\n",
592 mp
->numsel
> 1 ? "s" : "");
594 write (out
, buffer
, strlen (buffer
));
597 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
598 digest
, volume
, issue
);
600 for (bp
= buffer
+ i
; i
> 1; i
--)
604 write (out
, buffer
, strlen (buffer
));
610 * Create a mhn composition file for forwarding message.
614 copy_mime_draft (int out
)
619 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
620 mp
->numsel
== 1 ? "" : "s", mp
->foldpath
);
621 write (out
, buffer
, strlen (buffer
));
622 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
623 if (is_selected (mp
, msgnum
)) {
624 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
625 write (out
, buffer
, strlen (buffer
));
627 write (out
, "\n", 1);
632 build_form (char *form
, char *digest
, int volume
, int issue
)
637 char *line
, tmpfil
[BUFSIZ
];
639 register struct comp
*cptr
;
644 /* Get new format string */
645 nfs
= new_fs (form
, NULL
, NULL
);
646 fmtsize
= strlen (nfs
) + 256;
648 /* Compile format string */
649 fmt_compile (nfs
, &fmt
);
651 FINDCOMP (cptr
, "digest");
653 cptr
->c_text
= digest
;
654 FINDCOMP (cptr
, "date");
656 cptr
->c_text
= getcpy(dtimenow (0));
664 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &tmp
);
665 if (cp
== NULL
) adios("forw", "unable to create temporary file");
666 strncpy (tmpfil
, cp
, sizeof(tmpfil
));
668 if ((in
= dup (fileno (tmp
))) == NOTOK
)
669 adios ("dup", "unable to");
671 line
= mh_xmalloc ((unsigned) fmtsize
);
672 fmt_scan (fmt
, line
, fmtsize
, dat
);
676 adios (tmpfil
, "error writing");
678 lseek (in
, (off_t
) 0, SEEK_SET
);