]>
diplodocus.org Git - nmh/blob - uip/forw.c
2f1cbffa6ee320e467836bb180e3a9ea57ac49b7
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.
9 #include "sbr/context_save.h"
10 #include "sbr/context_replace.h"
11 #include "sbr/context_find.h"
12 #include "sbr/ambigsw.h"
13 #include "sbr/pidstatus.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/arglist.h"
18 #include "sbr/error.h"
23 #include "sbr/m_maildir.h"
27 #define IFORMAT "digest-issue-%s"
28 #define VFORMAT "digest-volume-%s"
30 #define FORW_SWITCHES \
31 X("annotate", 0, ANNOSW) \
32 X("noannotate", 0, NANNOSW) \
33 X("draftfolder +folder", 0, DFOLDSW) \
34 X("draftmessage msg", 0, DMSGSW) \
35 X("nodraftfolder", 0, NDFLDSW) \
36 X("editor editor", 0, EDITRSW) \
37 X("noedit", 0, NEDITSW) \
38 X("filter filterfile", 0, FILTSW) \
39 X("form formfile", 0, FORMSW) \
40 X("format", 5, FRMTSW) \
41 X("noformat", 7, NFRMTSW) \
42 X("inplace", 0, INPLSW) \
43 X("noinplace", 0, NINPLSW) \
44 X("mime", 0, MIMESW) \
45 X("nomime", 0, NMIMESW) \
46 X("digest list", 0, DGSTSW) \
47 X("issue number", 0, ISSUESW) \
48 X("volume number", 0, VOLUMSW) \
49 X("whatnowproc program", 0, WHATSW) \
50 X("nowhatnowproc", 0, NWHATSW) \
51 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
52 X("nodashstuffing", 0, NBITSTUFFSW) \
53 X("version", 0, VERSIONSW) \
54 X("help", 0, HELPSW) \
55 X("file file", 4, FILESW) \
56 X("build", 5, BILDSW) /* interface from mhe */ \
57 X("from address", 0, FROMSW) \
58 X("to address", 0, TOSW) \
59 X("cc address", 0, CCSW) \
60 X("subject text", 0, SUBJECTSW) \
61 X("fcc mailbox", 0, FCCSW) \
62 X("width columns", 0, WIDTHSW) \
64 #define X(sw, minchars, id) id,
65 DEFINE_SWITCH_ENUM(FORW
);
68 #define X(sw, minchars, id) { sw, minchars, id },
69 DEFINE_SWITCH_ARRAY(FORW
, switches
);
72 #define DISPO_SWITCHES \
74 X("replace", 0, YESW) \
75 X("list", 0, LISTDSW) \
76 X("refile +folder", 0, REFILSW) \
79 #define X(sw, minchars, id) id,
80 DEFINE_SWITCH_ENUM(DISPO
);
83 #define X(sw, minchars, id) { sw, minchars, id },
84 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
87 static struct swit aqrl
[] = {
89 { "replace", 0, YESW
},
90 { "list", 0, LISTDSW
},
91 { "refile +folder", 0, REFILSW
},
95 static char drft
[BUFSIZ
];
97 static char delim3
[] =
98 "\n------------------------------------------------------------\n\n";
99 static char delim4
[] = "\n------------------------------\n\n";
102 static struct msgs
*mp
= NULL
; /* used a lot */
108 static void mhl_draft (int, char *, int, int, char *, char *, int);
109 static void copy_draft (int, char *, char *, int, int, int);
110 static void copy_mime_draft (int);
114 main (int argc
, char **argv
)
119 int issue
= 0, volume
= 0, dashstuff
= 0;
123 int out
, isdf
= 0, msgnum
= 0;
124 int outputlinelen
= OUTPUTLINELEN
;
126 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
127 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
128 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
129 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
130 char *form
= NULL
, buf
[BUFSIZ
];
131 char **argp
, **arguments
;
133 struct msgs_array msgs
= { 0, 0, NULL
};
134 bool buildsw
= false;
136 if (nmh_init(argv
[0], true, true)) { return 1; }
138 arguments
= getarguments (invo_name
, argc
, argv
, 1);
141 while ((cp
= *argp
++)) {
143 switch (smatch (++cp
, switches
)) {
145 ambigsw (cp
, switches
);
148 die("-%s unknown", cp
);
151 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
153 print_help (buf
, switches
, 1);
156 print_version(invo_name
);
167 if (!(ed
= *argp
++) || *ed
== '-')
168 die("missing argument to %s", argp
[-2]);
176 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
177 die("missing argument to %s", argp
[-2]);
189 die("only one file at a time!");
190 if (!(cp
= *argp
++) || *cp
== '-')
191 die("missing argument to %s", argp
[-2]);
192 file
= path (cp
, TFILE
);
195 if (!(cp
= *argp
++) || *cp
== '-')
196 die("missing argument to %s", argp
[-2]);
197 filter
= mh_xstrdup(etcpath(cp
));
201 if (!(form
= *argp
++) || *form
== '-')
202 die("missing argument to %s", argp
[-2]);
206 filter
= mh_xstrdup(etcpath(mhlforward
));
228 if (!(cp
= *argp
++) || *cp
== '-')
229 die("missing argument to %s", argp
[-2]);
230 digest
= mh_xstrdup(cp
);
234 if (!(cp
= *argp
++) || *cp
== '-')
235 die("missing argument to %s", argp
[-2]);
236 if ((issue
= atoi (cp
)) < 1)
237 die("bad argument %s %s", argp
[-2], cp
);
240 if (!(cp
= *argp
++) || *cp
== '-')
241 die("missing argument to %s", argp
[-2]);
242 if ((volume
= atoi (cp
)) < 1)
243 die("bad argument %s %s", argp
[-2], cp
);
248 die("only one draft folder at a time!");
249 if (!(cp
= *argp
++) || *cp
== '-')
250 die("missing argument to %s", argp
[-2]);
251 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
252 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
256 die("only one draft message at a time!");
257 if (!(dmsg
= *argp
++) || *dmsg
== '-')
258 die("missing argument to %s", argp
[-2]);
266 dashstuff
= 1; /* ternary logic */
269 dashstuff
= -1; /* ternary logic */
273 if (!(cp
= *argp
++) || *cp
== '-')
274 die("missing argument to %s", argp
[-2]);
275 from
= addlist(from
, cp
);
278 if (!(cp
= *argp
++) || *cp
== '-')
279 die("missing argument to %s", argp
[-2]);
280 to
= addlist(to
, cp
);
283 if (!(cp
= *argp
++) || *cp
== '-')
284 die("missing argument to %s", argp
[-2]);
285 cc
= addlist(cc
, cp
);
288 if (!(cp
= *argp
++) || *cp
== '-')
289 die("missing argument to %s", argp
[-2]);
290 fcc
= addlist(fcc
, cp
);
293 if (!(cp
= *argp
++) || *cp
== '-')
294 die("missing argument to %s", argp
[-2]);
295 subject
= mh_xstrdup(cp
);
299 if (!(cp
= *argp
++) || *cp
== '-')
300 die("missing argument to %s", argp
[-2]);
301 if ((outputlinelen
= atoi(cp
)) < 10)
302 die("impossible width %d", outputlinelen
);
306 if (*cp
== '+' || *cp
== '@') {
308 die("only one folder at a time!");
309 folder
= pluspath (cp
);
311 app_msgarg(&msgs
, cp
);
315 cwd
= mh_xstrdup(pwd ());
317 if (!context_find ("path"))
318 free (path ("./", TFOLDER
));
319 if (file
&& (msgs
.size
|| folder
))
320 die("can't mix files and folders/msgs");
324 strncpy (drft
, buildsw
? m_maildir ("draft")
325 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
327 /* Check if a draft already exists */
328 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
329 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
330 for (i
= LISTDSW
; i
!= YESW
;) {
331 if (!(argp
= read_switch_multiword ("\nDisposition? ",
332 isdf
? aqrnl
: aqrl
)))
334 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
343 showfile (++argp
, drft
);
346 if (refile (++argp
, drft
) == 0)
360 anot
= false; /* don't want to annotate a file */
363 * Forwarding a message.
366 app_msgarg(&msgs
, "cur");
368 folder
= getfolder (1);
369 maildir
= m_maildir (folder
);
371 if (chdir (maildir
) == NOTOK
)
372 adios (maildir
, "unable to change directory to");
374 /* read folder and create message structure */
375 if (!(mp
= folder_read (folder
, 1)))
376 die("unable to read folder %s", folder
);
378 /* check for empty folder */
380 die("no messages in %s", folder
);
382 /* parse all the message ranges/sequences and set SELECTED */
383 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
384 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
387 seq_setprev (mp
); /* set the previous sequence */
390 * Find the first message in our set and use it as the input
391 * for the component scanner
394 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
395 if (is_selected (mp
, msgnum
)) {
396 fwdmsg
= mh_xstrdup(m_name(msgnum
));
401 die("Unable to find input message");
404 if (filter
&& access (filter
, R_OK
) == NOTOK
)
405 adios (filter
, "unable to read");
408 * Open form (component) file.
412 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
414 && (cp
= context_find (buf
))
415 && ((issue
= atoi (cp
)) < 0))
420 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
421 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
431 dat
[0] = digest
? issue
: msgnum
;
434 dat
[3] = outputlinelen
;
438 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
439 file
? file
: fwdmsg
);
441 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
442 adios (drft
, "unable to create");
445 * copy the components into the draft
447 cpydata (in
, out
, form
, drft
);
451 /* just copy the file into the draft */
452 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
453 adios (file
, "unable to open");
454 cpydata (in
, out
, file
, drft
);
459 * If filter file is defined, then format the
460 * messages into the draft using mhlproc.
463 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
465 copy_mime_draft (out
);
467 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
471 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
472 context_replace (buf
, mh_xstrdup(m_str(issue
)));
473 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
474 context_replace (buf
, mh_xstrdup(m_str(volume
)));
477 context_replace (pfolder
, folder
); /* update current folder */
478 seq_setcur (mp
, mp
->lowsel
); /* update current message */
479 seq_save (mp
); /* synchronize sequences */
480 context_save (); /* save the context file */
485 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
486 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
493 * Filter the messages you are forwarding, into the
494 * draft calling the mhlproc, and reading its output
499 mhl_draft (int out
, char *digest
, int volume
, int issue
,
500 char *file
, char *filter
, int dashstuff
)
507 struct msgs_array vec
= { 0, 0, NULL
};
509 if (pipe (pd
) == NOTOK
)
510 adios ("pipe", "unable to create");
512 argsplit_msgarg(&vec
, mhlproc
, &program
);
517 adios ("fork", "unable to");
524 app_msgarg(&vec
, "-forwall");
525 app_msgarg(&vec
, "-form");
526 app_msgarg(&vec
, filter
);
529 app_msgarg(&vec
, "-digest");
530 app_msgarg(&vec
, digest
);
531 app_msgarg(&vec
, "-issue");
532 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
533 app_msgarg(&vec
, buf1
);
534 app_msgarg(&vec
, "-volume");
535 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
536 app_msgarg(&vec
, buf2
);
540 * Are we dashstuffing (quoting) the lines that begin
541 * with `-'. We use the mhl default (don't add any flag)
542 * unless the user has specified a specific flag.
545 app_msgarg(&vec
, "-dashstuffing");
546 else if (dashstuff
< 0)
547 app_msgarg(&vec
, "-nodashstuffing");
549 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
550 if (is_selected (mp
, msgnum
))
551 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
554 app_msgarg(&vec
, NULL
);
556 execvp (program
, vec
.msgs
);
557 fprintf (stderr
, "unable to exec ");
563 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
565 pidXwait(child_id
, mhlproc
);
572 * Copy the messages into the draft. The messages are
573 * not filtered through the mhlproc. Do dashstuffing if
578 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
580 int fd
,i
, msgcnt
, msgnum
;
586 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
587 if (is_selected (mp
, msgnum
)) {
589 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
592 /* Get buffer ready to go */
594 buflen
= sizeof(buffer
);
596 strncpy (bp
, "\n-------", buflen
);
601 if (msgnum
== mp
->lowsel
) {
602 snprintf (bp
, buflen
, " Forwarded Message%s",
603 PLURALS(mp
->numsel
));
605 snprintf (bp
, buflen
, " Message %d", msgcnt
);
611 strncpy (bp
, "\n\n", buflen
);
613 if (write (out
, buffer
, strlen (buffer
)) < 0) {
614 advise (drft
, "write");
617 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
618 admonish (msgnam
, "unable to read message");
623 * Copy the message. Add RFC934 quoting (dashstuffing)
624 * unless given the -nodashstuffing flag.
627 cpydgst (fd
, out
, msgnam
, file
);
629 cpydata (fd
, out
, msgnam
, file
);
637 strncpy (buffer
, delim4
, sizeof(buffer
));
639 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
640 PLURALS(mp
->numsel
));
642 if (write (out
, buffer
, strlen (buffer
)) < 0) {
643 advise (drft
, "write");
647 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
648 digest
, volume
, issue
);
650 for (bp
= buffer
+ i
; i
> 1; i
--)
654 if (write (out
, buffer
, strlen (buffer
)) < 0) {
655 advise (drft
, "write");
662 * Create a mhn composition file for forwarding message.
666 copy_mime_draft (int out
)
671 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
672 PLURALS(mp
->numsel
), mp
->foldpath
);
673 if (write (out
, buffer
, strlen (buffer
)) < 0) {
674 advise (drft
, "write");
676 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
677 if (is_selected (mp
, msgnum
)) {
678 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
679 if (write (out
, buffer
, strlen (buffer
)) < 0) {
680 advise (drft
, "write");
683 if (write (out
, "\n", 1) < 0) {
684 advise (drft
, "write newline");