]>
diplodocus.org Git - nmh/blob - uip/forw.c
1 /* forw.c -- forward a message, or group of messages.
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
13 #include "sbr/m_maildir.h"
17 #define IFORMAT "digest-issue-%s"
18 #define VFORMAT "digest-volume-%s"
20 #define FORW_SWITCHES \
21 X("annotate", 0, ANNOSW) \
22 X("noannotate", 0, NANNOSW) \
23 X("draftfolder +folder", 0, DFOLDSW) \
24 X("draftmessage msg", 0, DMSGSW) \
25 X("nodraftfolder", 0, NDFLDSW) \
26 X("editor editor", 0, EDITRSW) \
27 X("noedit", 0, NEDITSW) \
28 X("filter filterfile", 0, FILTSW) \
29 X("form formfile", 0, FORMSW) \
30 X("format", 5, FRMTSW) \
31 X("noformat", 7, NFRMTSW) \
32 X("inplace", 0, INPLSW) \
33 X("noinplace", 0, NINPLSW) \
34 X("mime", 0, MIMESW) \
35 X("nomime", 0, NMIMESW) \
36 X("digest list", 0, DGSTSW) \
37 X("issue number", 0, ISSUESW) \
38 X("volume number", 0, VOLUMSW) \
39 X("whatnowproc program", 0, WHATSW) \
40 X("nowhatnowproc", 0, NWHATSW) \
41 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
42 X("nodashstuffing", 0, NBITSTUFFSW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
45 X("file file", 4, FILESW) \
46 X("build", 5, BILDSW) /* interface from mhe */ \
47 X("from address", 0, FROMSW) \
48 X("to address", 0, TOSW) \
49 X("cc address", 0, CCSW) \
50 X("subject text", 0, SUBJECTSW) \
51 X("fcc mailbox", 0, FCCSW) \
52 X("width columns", 0, WIDTHSW) \
54 #define X(sw, minchars, id) id,
55 DEFINE_SWITCH_ENUM(FORW
);
58 #define X(sw, minchars, id) { sw, minchars, id },
59 DEFINE_SWITCH_ARRAY(FORW
, switches
);
62 #define DISPO_SWITCHES \
64 X("replace", 0, YESW) \
65 X("list", 0, LISTDSW) \
66 X("refile +folder", 0, REFILSW) \
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(DISPO
);
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
77 static struct swit aqrl
[] = {
79 { "replace", 0, YESW
},
80 { "list", 0, LISTDSW
},
81 { "refile +folder", 0, REFILSW
},
85 static char drft
[BUFSIZ
];
87 static char delim3
[] =
88 "\n------------------------------------------------------------\n\n";
89 static char delim4
[] = "\n------------------------------\n\n";
92 static struct msgs
*mp
= NULL
; /* used a lot */
98 static void mhl_draft (int, char *, int, int, char *, char *, int);
99 static void copy_draft (int, char *, char *, int, int, int);
100 static void copy_mime_draft (int);
104 main (int argc
, char **argv
)
109 int issue
= 0, volume
= 0, dashstuff
= 0;
113 int out
, isdf
= 0, msgnum
= 0;
114 int outputlinelen
= OUTPUTLINELEN
;
116 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
117 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
118 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
119 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
120 char *form
= NULL
, buf
[BUFSIZ
];
121 char **argp
, **arguments
;
123 struct msgs_array msgs
= { 0, 0, NULL
};
124 bool buildsw
= false;
126 if (nmh_init(argv
[0], true, true)) { return 1; }
128 arguments
= getarguments (invo_name
, argc
, argv
, 1);
131 while ((cp
= *argp
++)) {
133 switch (smatch (++cp
, switches
)) {
135 ambigsw (cp
, switches
);
138 die("-%s unknown", cp
);
141 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
143 print_help (buf
, switches
, 1);
146 print_version(invo_name
);
157 if (!(ed
= *argp
++) || *ed
== '-')
158 die("missing argument to %s", argp
[-2]);
166 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
167 die("missing argument to %s", argp
[-2]);
179 die("only one file at a time!");
180 if (!(cp
= *argp
++) || *cp
== '-')
181 die("missing argument to %s", argp
[-2]);
182 file
= path (cp
, TFILE
);
185 if (!(cp
= *argp
++) || *cp
== '-')
186 die("missing argument to %s", argp
[-2]);
187 filter
= mh_xstrdup(etcpath(cp
));
191 if (!(form
= *argp
++) || *form
== '-')
192 die("missing argument to %s", argp
[-2]);
196 filter
= mh_xstrdup(etcpath(mhlforward
));
218 if (!(cp
= *argp
++) || *cp
== '-')
219 die("missing argument to %s", argp
[-2]);
220 digest
= mh_xstrdup(cp
);
224 if (!(cp
= *argp
++) || *cp
== '-')
225 die("missing argument to %s", argp
[-2]);
226 if ((issue
= atoi (cp
)) < 1)
227 die("bad argument %s %s", argp
[-2], cp
);
230 if (!(cp
= *argp
++) || *cp
== '-')
231 die("missing argument to %s", argp
[-2]);
232 if ((volume
= atoi (cp
)) < 1)
233 die("bad argument %s %s", argp
[-2], cp
);
238 die("only one draft folder at a time!");
239 if (!(cp
= *argp
++) || *cp
== '-')
240 die("missing argument to %s", argp
[-2]);
241 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
242 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
246 die("only one draft message at a time!");
247 if (!(dmsg
= *argp
++) || *dmsg
== '-')
248 die("missing argument to %s", argp
[-2]);
256 dashstuff
= 1; /* ternary logic */
259 dashstuff
= -1; /* ternary logic */
263 if (!(cp
= *argp
++) || *cp
== '-')
264 die("missing argument to %s", argp
[-2]);
265 from
= addlist(from
, cp
);
268 if (!(cp
= *argp
++) || *cp
== '-')
269 die("missing argument to %s", argp
[-2]);
270 to
= addlist(to
, cp
);
273 if (!(cp
= *argp
++) || *cp
== '-')
274 die("missing argument to %s", argp
[-2]);
275 cc
= addlist(cc
, cp
);
278 if (!(cp
= *argp
++) || *cp
== '-')
279 die("missing argument to %s", argp
[-2]);
280 fcc
= addlist(fcc
, cp
);
283 if (!(cp
= *argp
++) || *cp
== '-')
284 die("missing argument to %s", argp
[-2]);
285 subject
= mh_xstrdup(cp
);
289 if (!(cp
= *argp
++) || *cp
== '-')
290 die("missing argument to %s", argp
[-2]);
291 if ((outputlinelen
= atoi(cp
)) < 10)
292 die("impossible width %d", outputlinelen
);
296 if (*cp
== '+' || *cp
== '@') {
298 die("only one folder at a time!");
299 folder
= pluspath (cp
);
301 app_msgarg(&msgs
, cp
);
305 cwd
= mh_xstrdup(pwd ());
307 if (!context_find ("path"))
308 free (path ("./", TFOLDER
));
309 if (file
&& (msgs
.size
|| folder
))
310 die("can't mix files and folders/msgs");
314 strncpy (drft
, buildsw
? m_maildir ("draft")
315 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
317 /* Check if a draft already exists */
318 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
319 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
320 for (i
= LISTDSW
; i
!= YESW
;) {
321 if (!(argp
= read_switch_multiword ("\nDisposition? ",
322 isdf
? aqrnl
: aqrl
)))
324 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
333 showfile (++argp
, drft
);
336 if (refile (++argp
, drft
) == 0)
350 anot
= false; /* don't want to annotate a file */
353 * Forwarding a message.
356 app_msgarg(&msgs
, "cur");
358 folder
= getfolder (1);
359 maildir
= m_maildir (folder
);
361 if (chdir (maildir
) == NOTOK
)
362 adios (maildir
, "unable to change directory to");
364 /* read folder and create message structure */
365 if (!(mp
= folder_read (folder
, 1)))
366 die("unable to read folder %s", folder
);
368 /* check for empty folder */
370 die("no messages in %s", folder
);
372 /* parse all the message ranges/sequences and set SELECTED */
373 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
374 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
377 seq_setprev (mp
); /* set the previous sequence */
380 * Find the first message in our set and use it as the input
381 * for the component scanner
384 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
385 if (is_selected (mp
, msgnum
)) {
386 fwdmsg
= mh_xstrdup(m_name(msgnum
));
391 die("Unable to find input message");
394 if (filter
&& access (filter
, R_OK
) == NOTOK
)
395 adios (filter
, "unable to read");
398 * Open form (component) file.
402 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
404 && (cp
= context_find (buf
))
405 && ((issue
= atoi (cp
)) < 0))
410 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
411 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
421 dat
[0] = digest
? issue
: msgnum
;
424 dat
[3] = outputlinelen
;
428 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
429 file
? file
: fwdmsg
);
431 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
432 adios (drft
, "unable to create");
435 * copy the components into the draft
437 cpydata (in
, out
, form
, drft
);
441 /* just copy the file into the draft */
442 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
443 adios (file
, "unable to open");
444 cpydata (in
, out
, file
, drft
);
449 * If filter file is defined, then format the
450 * messages into the draft using mhlproc.
453 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
455 copy_mime_draft (out
);
457 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
461 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
462 context_replace (buf
, mh_xstrdup(m_str(issue
)));
463 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
464 context_replace (buf
, mh_xstrdup(m_str(volume
)));
467 context_replace (pfolder
, folder
); /* update current folder */
468 seq_setcur (mp
, mp
->lowsel
); /* update current message */
469 seq_save (mp
); /* synchronize sequences */
470 context_save (); /* save the context file */
475 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
476 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
483 * Filter the messages you are forwarding, into the
484 * draft calling the mhlproc, and reading its output
489 mhl_draft (int out
, char *digest
, int volume
, int issue
,
490 char *file
, char *filter
, int dashstuff
)
497 struct msgs_array vec
= { 0, 0, NULL
};
499 if (pipe (pd
) == NOTOK
)
500 adios ("pipe", "unable to create");
502 argsplit_msgarg(&vec
, mhlproc
, &program
);
507 adios ("fork", "unable to");
514 app_msgarg(&vec
, "-forwall");
515 app_msgarg(&vec
, "-form");
516 app_msgarg(&vec
, filter
);
519 app_msgarg(&vec
, "-digest");
520 app_msgarg(&vec
, digest
);
521 app_msgarg(&vec
, "-issue");
522 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
523 app_msgarg(&vec
, buf1
);
524 app_msgarg(&vec
, "-volume");
525 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
526 app_msgarg(&vec
, buf2
);
530 * Are we dashstuffing (quoting) the lines that begin
531 * with `-'. We use the mhl default (don't add any flag)
532 * unless the user has specified a specific flag.
535 app_msgarg(&vec
, "-dashstuffing");
536 else if (dashstuff
< 0)
537 app_msgarg(&vec
, "-nodashstuffing");
539 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
540 if (is_selected (mp
, msgnum
))
541 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
544 app_msgarg(&vec
, NULL
);
546 execvp (program
, vec
.msgs
);
547 fprintf (stderr
, "unable to exec ");
553 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
555 pidXwait(child_id
, mhlproc
);
562 * Copy the messages into the draft. The messages are
563 * not filtered through the mhlproc. Do dashstuffing if
568 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
570 int fd
,i
, msgcnt
, msgnum
;
576 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
577 if (is_selected (mp
, msgnum
)) {
579 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
582 /* Get buffer ready to go */
584 buflen
= sizeof(buffer
);
586 strncpy (bp
, "\n-------", buflen
);
591 if (msgnum
== mp
->lowsel
) {
592 snprintf (bp
, buflen
, " Forwarded Message%s",
593 PLURALS(mp
->numsel
));
595 snprintf (bp
, buflen
, " Message %d", msgcnt
);
601 strncpy (bp
, "\n\n", buflen
);
603 if (write (out
, buffer
, strlen (buffer
)) < 0) {
604 advise (drft
, "write");
607 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
608 admonish (msgnam
, "unable to read message");
613 * Copy the message. Add RFC934 quoting (dashstuffing)
614 * unless given the -nodashstuffing flag.
617 cpydgst (fd
, out
, msgnam
, file
);
619 cpydata (fd
, out
, msgnam
, file
);
627 strncpy (buffer
, delim4
, sizeof(buffer
));
629 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
630 PLURALS(mp
->numsel
));
632 if (write (out
, buffer
, strlen (buffer
)) < 0) {
633 advise (drft
, "write");
637 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
638 digest
, volume
, issue
);
640 for (bp
= buffer
+ i
; i
> 1; i
--)
644 if (write (out
, buffer
, strlen (buffer
)) < 0) {
645 advise (drft
, "write");
652 * Create a mhn composition file for forwarding message.
656 copy_mime_draft (int out
)
661 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
662 PLURALS(mp
->numsel
), mp
->foldpath
);
663 if (write (out
, buffer
, strlen (buffer
)) < 0) {
664 advise (drft
, "write");
666 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
667 if (is_selected (mp
, msgnum
)) {
668 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
669 if (write (out
, buffer
, strlen (buffer
)) < 0) {
670 advise (drft
, "write");
673 if (write (out
, "\n", 1) < 0) {
674 advise (drft
, "write newline");