]>
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.
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)
445 dat
[0] = digest
? issue
: msgnum
;
448 dat
[3] = outputlinelen
;
452 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
453 file
? file
: fwdmsg
);
455 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
456 adios (drft
, "unable to create");
459 * copy the components into the draft
461 cpydata (in
, out
, form
, drft
);
465 /* just copy the file into the draft */
466 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
467 adios (file
, "unable to open");
468 cpydata (in
, out
, file
, drft
);
473 * If filter file is defined, then format the
474 * messages into the draft using mhlproc.
477 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
479 copy_mime_draft (out
);
481 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
485 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
486 snprintf (value
, sizeof(value
), "%d", issue
);
487 context_replace (buf
, getcpy (value
));
488 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
489 snprintf (value
, sizeof(value
), "%d", volume
);
490 context_replace (buf
, getcpy (value
));
493 context_replace (pfolder
, folder
); /* update current folder */
494 seq_setcur (mp
, mp
->lowsel
); /* update current message */
495 seq_save (mp
); /* synchronize sequences */
496 context_save (); /* save the context file */
501 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
502 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
509 * Filter the messages you are forwarding, into the
510 * draft calling the mhlproc, and reading its output
515 mhl_draft (int out
, char *digest
, int volume
, int issue
,
516 char *file
, char *filter
, int dashstuff
)
519 int i
, msgnum
, pd
[2];
524 if (pipe (pd
) == NOTOK
)
525 adios ("pipe", "unable to create");
527 vec
[0] = r1bindex (mhlproc
, '/');
529 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
533 adios ("fork", "unable to");
541 vec
[i
++] = "-forwall";
546 vec
[i
++] = "-digest";
549 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
551 vec
[i
++] = "-volume";
552 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
557 * Are we dashstuffing (quoting) the lines that begin
558 * with `-'. We use the mhl default (don't add any flag)
559 * unless the user has specified a specific flag.
562 vec
[i
++] = "-dashstuffing";
563 else if (dashstuff
< 0)
564 vec
[i
++] = "-nodashstuffing";
566 if (mp
->numsel
>= MAXARGS
- i
)
567 adios (NULL
, "more than %d messages for %s exec",
568 MAXARGS
- i
, vec
[0]);
571 * Now add the message names to filter. We can only
572 * handle about 995 messages (because vec is fixed size),
573 * but that should be plenty.
575 for (msgnum
= mp
->lowsel
;
576 msgnum
<= mp
->hghsel
&& i
< (int) sizeof(vec
) - 1;
578 if (is_selected (mp
, msgnum
))
579 vec
[i
++] = getcpy (m_name (msgnum
));
582 execvp (mhlproc
, vec
);
583 fprintf (stderr
, "unable to exec ");
589 cpydata (pd
[0], out
, vec
[0], file
);
591 pidXwait(child_id
, mhlproc
);
598 * Copy the messages into the draft. The messages are
599 * not filtered through the mhlproc. Do dashstuffing if
604 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
606 int fd
,i
, msgcnt
, msgnum
;
608 register char *bp
, *msgnam
;
612 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
613 if (is_selected (mp
, msgnum
)) {
615 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
618 /* Get buffer ready to go */
620 buflen
= sizeof(buffer
);
622 strncpy (bp
, "\n-------", buflen
);
627 if (msgnum
== mp
->lowsel
) {
628 snprintf (bp
, buflen
, " Forwarded Message%s",
629 mp
->numsel
> 1 ? "s" : "");
631 snprintf (bp
, buflen
, " Message %d", msgcnt
);
637 strncpy (bp
, "\n\n", buflen
);
639 write (out
, buffer
, strlen (buffer
));
641 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
642 admonish (msgnam
, "unable to read message");
647 * Copy the message. Add RFC934 quoting (dashstuffing)
648 * unless given the -nodashstuffing flag.
651 cpydgst (fd
, out
, msgnam
, file
);
653 cpydata (fd
, out
, msgnam
, file
);
661 strncpy (buffer
, delim4
, sizeof(buffer
));
663 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
664 mp
->numsel
> 1 ? "s" : "");
666 write (out
, buffer
, strlen (buffer
));
669 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
670 digest
, volume
, issue
);
672 for (bp
= buffer
+ i
; i
> 1; i
--)
676 write (out
, buffer
, strlen (buffer
));
682 * Create a mhn composition file for forwarding message.
686 copy_mime_draft (int out
)
691 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
692 mp
->numsel
== 1 ? "" : "s", mp
->foldpath
);
693 write (out
, buffer
, strlen (buffer
));
694 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
695 if (is_selected (mp
, msgnum
)) {
696 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
697 write (out
, buffer
, strlen (buffer
));
699 write (out
, "\n", 1);