]>
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.
9 #include "whatnowproc.h"
10 #include "sbr/m_name.h"
11 #include "sbr/m_gmprot.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/read_switch_multiword.h"
14 #include "sbr/seq_setprev.h"
15 #include "sbr/seq_setcur.h"
16 #include "sbr/seq_save.h"
17 #include "sbr/showfile.h"
18 #include "sbr/smatch.h"
19 #include "sbr/refile.h"
20 #include "sbr/cpydata.h"
21 #include "sbr/cpydgst.h"
22 #include "sbr/m_draft.h"
23 #include "sbr/m_convert.h"
24 #include "sbr/getfolder.h"
25 #include "sbr/folder_read.h"
26 #include "sbr/context_save.h"
27 #include "sbr/context_replace.h"
28 #include "sbr/context_find.h"
29 #include "sbr/ambigsw.h"
30 #include "sbr/pidstatus.h"
32 #include "sbr/print_version.h"
33 #include "sbr/print_help.h"
34 #include "sbr/arglist.h"
35 #include "sbr/error.h"
40 #include "sbr/m_maildir.h"
44 #define IFORMAT "digest-issue-%s"
45 #define VFORMAT "digest-volume-%s"
47 #define FORW_SWITCHES \
48 X("annotate", 0, ANNOSW) \
49 X("noannotate", 0, NANNOSW) \
50 X("draftfolder +folder", 0, DFOLDSW) \
51 X("draftmessage msg", 0, DMSGSW) \
52 X("nodraftfolder", 0, NDFLDSW) \
53 X("editor editor", 0, EDITRSW) \
54 X("noedit", 0, NEDITSW) \
55 X("filter filterfile", 0, FILTSW) \
56 X("form formfile", 0, FORMSW) \
57 X("format", 5, FRMTSW) \
58 X("noformat", 7, NFRMTSW) \
59 X("inplace", 0, INPLSW) \
60 X("noinplace", 0, NINPLSW) \
61 X("mime", 0, MIMESW) \
62 X("nomime", 0, NMIMESW) \
63 X("digest list", 0, DGSTSW) \
64 X("issue number", 0, ISSUESW) \
65 X("volume number", 0, VOLUMSW) \
66 X("whatnowproc program", 0, WHATSW) \
67 X("nowhatnowproc", 0, NWHATSW) \
68 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
69 X("nodashstuffing", 0, NBITSTUFFSW) \
70 X("version", 0, VERSIONSW) \
71 X("help", 0, HELPSW) \
72 X("file file", 4, FILESW) \
73 X("build", 5, BILDSW) /* interface from mhe */ \
74 X("from address", 0, FROMSW) \
75 X("to address", 0, TOSW) \
76 X("cc address", 0, CCSW) \
77 X("subject text", 0, SUBJECTSW) \
78 X("fcc mailbox", 0, FCCSW) \
79 X("width columns", 0, WIDTHSW) \
81 #define X(sw, minchars, id) id,
82 DEFINE_SWITCH_ENUM(FORW
);
85 #define X(sw, minchars, id) { sw, minchars, id },
86 DEFINE_SWITCH_ARRAY(FORW
, switches
);
89 #define DISPO_SWITCHES \
91 X("replace", 0, YESW) \
92 X("list", 0, LISTDSW) \
93 X("refile +folder", 0, REFILSW) \
96 #define X(sw, minchars, id) id,
97 DEFINE_SWITCH_ENUM(DISPO
);
100 #define X(sw, minchars, id) { sw, minchars, id },
101 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
104 static struct swit aqrl
[] = {
106 { "replace", 0, YESW
},
107 { "list", 0, LISTDSW
},
108 { "refile +folder", 0, REFILSW
},
112 static char drft
[BUFSIZ
];
114 static char delim3
[] =
115 "\n------------------------------------------------------------\n\n";
116 static char delim4
[] = "\n------------------------------\n\n";
119 static struct msgs
*mp
= NULL
; /* used a lot */
125 static void mhl_draft (int, char *, int, int, char *, char *, int);
126 static void copy_draft (int, char *, char *, int, int, int);
127 static void copy_mime_draft (int);
131 main (int argc
, char **argv
)
136 int issue
= 0, volume
= 0, dashstuff
= 0;
140 int out
, isdf
= 0, msgnum
= 0;
141 int outputlinelen
= OUTPUTLINELEN
;
143 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
144 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
145 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
146 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
147 char *form
= NULL
, buf
[BUFSIZ
];
148 char **argp
, **arguments
;
150 struct msgs_array msgs
= { 0, 0, NULL
};
151 bool buildsw
= false;
153 if (nmh_init(argv
[0], true, true)) { return 1; }
155 arguments
= getarguments (invo_name
, argc
, argv
, 1);
158 while ((cp
= *argp
++)) {
160 switch (smatch (++cp
, switches
)) {
162 ambigsw (cp
, switches
);
165 die("-%s unknown", cp
);
168 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
170 print_help (buf
, switches
, 1);
173 print_version(invo_name
);
184 if (!(ed
= *argp
++) || *ed
== '-')
185 die("missing argument to %s", argp
[-2]);
193 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
194 die("missing argument to %s", argp
[-2]);
206 die("only one file at a time!");
207 if (!(cp
= *argp
++) || *cp
== '-')
208 die("missing argument to %s", argp
[-2]);
209 file
= path (cp
, TFILE
);
212 if (!(cp
= *argp
++) || *cp
== '-')
213 die("missing argument to %s", argp
[-2]);
214 filter
= mh_xstrdup(etcpath(cp
));
218 if (!(form
= *argp
++) || *form
== '-')
219 die("missing argument to %s", argp
[-2]);
223 filter
= mh_xstrdup(etcpath(mhlforward
));
245 if (!(cp
= *argp
++) || *cp
== '-')
246 die("missing argument to %s", argp
[-2]);
247 digest
= mh_xstrdup(cp
);
251 if (!(cp
= *argp
++) || *cp
== '-')
252 die("missing argument to %s", argp
[-2]);
253 if ((issue
= atoi (cp
)) < 1)
254 die("bad argument %s %s", argp
[-2], cp
);
257 if (!(cp
= *argp
++) || *cp
== '-')
258 die("missing argument to %s", argp
[-2]);
259 if ((volume
= atoi (cp
)) < 1)
260 die("bad argument %s %s", argp
[-2], cp
);
265 die("only one draft folder at a time!");
266 if (!(cp
= *argp
++) || *cp
== '-')
267 die("missing argument to %s", argp
[-2]);
268 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
269 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
273 die("only one draft message at a time!");
274 if (!(dmsg
= *argp
++) || *dmsg
== '-')
275 die("missing argument to %s", argp
[-2]);
283 dashstuff
= 1; /* ternary logic */
286 dashstuff
= -1; /* ternary logic */
290 if (!(cp
= *argp
++) || *cp
== '-')
291 die("missing argument to %s", argp
[-2]);
292 from
= addlist(from
, cp
);
295 if (!(cp
= *argp
++) || *cp
== '-')
296 die("missing argument to %s", argp
[-2]);
297 to
= addlist(to
, cp
);
300 if (!(cp
= *argp
++) || *cp
== '-')
301 die("missing argument to %s", argp
[-2]);
302 cc
= addlist(cc
, cp
);
305 if (!(cp
= *argp
++) || *cp
== '-')
306 die("missing argument to %s", argp
[-2]);
307 fcc
= addlist(fcc
, cp
);
310 if (!(cp
= *argp
++) || *cp
== '-')
311 die("missing argument to %s", argp
[-2]);
312 subject
= mh_xstrdup(cp
);
316 if (!(cp
= *argp
++) || *cp
== '-')
317 die("missing argument to %s", argp
[-2]);
318 if ((outputlinelen
= atoi(cp
)) < 10)
319 die("impossible width %d", outputlinelen
);
323 if (*cp
== '+' || *cp
== '@') {
325 die("only one folder at a time!");
326 folder
= pluspath (cp
);
328 app_msgarg(&msgs
, cp
);
332 cwd
= mh_xstrdup(pwd ());
334 if (!context_find ("path"))
335 free (path ("./", TFOLDER
));
336 if (file
&& (msgs
.size
|| folder
))
337 die("can't mix files and folders/msgs");
341 strncpy (drft
, buildsw
? m_maildir ("draft")
342 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
344 /* Check if a draft already exists */
345 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
346 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
347 for (i
= LISTDSW
; i
!= YESW
;) {
348 if (!(argp
= read_switch_multiword ("\nDisposition? ",
349 isdf
? aqrnl
: aqrl
)))
351 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
360 showfile (++argp
, drft
);
363 if (refile (++argp
, drft
) == 0)
377 anot
= false; /* don't want to annotate a file */
380 * Forwarding a message.
383 app_msgarg(&msgs
, "cur");
385 folder
= getfolder (1);
386 maildir
= m_maildir (folder
);
388 if (chdir (maildir
) == NOTOK
)
389 adios (maildir
, "unable to change directory to");
391 /* read folder and create message structure */
392 if (!(mp
= folder_read (folder
, 1)))
393 die("unable to read folder %s", folder
);
395 /* check for empty folder */
397 die("no messages in %s", folder
);
399 /* parse all the message ranges/sequences and set SELECTED */
400 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
401 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
404 seq_setprev (mp
); /* set the previous sequence */
407 * Find the first message in our set and use it as the input
408 * for the component scanner
411 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
412 if (is_selected (mp
, msgnum
)) {
413 fwdmsg
= mh_xstrdup(m_name(msgnum
));
418 die("Unable to find input message");
421 if (filter
&& access (filter
, R_OK
) == NOTOK
)
422 adios (filter
, "unable to read");
425 * Open form (component) file.
429 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
431 && (cp
= context_find (buf
))
432 && ((issue
= atoi (cp
)) < 0))
437 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
438 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
448 dat
[0] = digest
? issue
: msgnum
;
451 dat
[3] = outputlinelen
;
455 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
456 file
? file
: fwdmsg
);
458 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
459 adios (drft
, "unable to create");
462 * copy the components into the draft
464 cpydata (in
, out
, form
, drft
);
468 /* just copy the file into the draft */
469 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
470 adios (file
, "unable to open");
471 cpydata (in
, out
, file
, drft
);
476 * If filter file is defined, then format the
477 * messages into the draft using mhlproc.
480 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
482 copy_mime_draft (out
);
484 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
488 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
489 context_replace (buf
, mh_xstrdup(m_str(issue
)));
490 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
491 context_replace (buf
, mh_xstrdup(m_str(volume
)));
494 context_replace (pfolder
, folder
); /* update current folder */
495 seq_setcur (mp
, mp
->lowsel
); /* update current message */
496 seq_save (mp
); /* synchronize sequences */
497 context_save (); /* save the context file */
502 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
503 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
510 * Filter the messages you are forwarding, into the
511 * draft calling the mhlproc, and reading its output
516 mhl_draft (int out
, char *digest
, int volume
, int issue
,
517 char *file
, char *filter
, int dashstuff
)
524 struct msgs_array vec
= { 0, 0, NULL
};
526 if (pipe (pd
) == NOTOK
)
527 adios ("pipe", "unable to create");
529 argsplit_msgarg(&vec
, mhlproc
, &program
);
534 adios ("fork", "unable to");
541 app_msgarg(&vec
, "-forwall");
542 app_msgarg(&vec
, "-form");
543 app_msgarg(&vec
, filter
);
546 app_msgarg(&vec
, "-digest");
547 app_msgarg(&vec
, digest
);
548 app_msgarg(&vec
, "-issue");
549 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
550 app_msgarg(&vec
, buf1
);
551 app_msgarg(&vec
, "-volume");
552 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
553 app_msgarg(&vec
, buf2
);
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 app_msgarg(&vec
, "-dashstuffing");
563 else if (dashstuff
< 0)
564 app_msgarg(&vec
, "-nodashstuffing");
566 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
567 if (is_selected (mp
, msgnum
))
568 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
571 app_msgarg(&vec
, NULL
);
573 execvp (program
, vec
.msgs
);
574 fprintf (stderr
, "unable to exec ");
580 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
582 pidXwait(child_id
, mhlproc
);
589 * Copy the messages into the draft. The messages are
590 * not filtered through the mhlproc. Do dashstuffing if
595 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
597 int fd
,i
, msgcnt
, msgnum
;
603 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
604 if (is_selected (mp
, msgnum
)) {
606 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
609 /* Get buffer ready to go */
611 buflen
= sizeof(buffer
);
613 strncpy (bp
, "\n-------", buflen
);
618 if (msgnum
== mp
->lowsel
) {
619 snprintf (bp
, buflen
, " Forwarded Message%s",
620 PLURALS(mp
->numsel
));
622 snprintf (bp
, buflen
, " Message %d", msgcnt
);
628 strncpy (bp
, "\n\n", buflen
);
630 if (write (out
, buffer
, strlen (buffer
)) < 0) {
631 advise (drft
, "write");
634 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
635 admonish (msgnam
, "unable to read message");
640 * Copy the message. Add RFC934 quoting (dashstuffing)
641 * unless given the -nodashstuffing flag.
644 cpydgst (fd
, out
, msgnam
, file
);
646 cpydata (fd
, out
, msgnam
, file
);
654 strncpy (buffer
, delim4
, sizeof(buffer
));
656 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
657 PLURALS(mp
->numsel
));
659 if (write (out
, buffer
, strlen (buffer
)) < 0) {
660 advise (drft
, "write");
664 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
665 digest
, volume
, issue
);
667 for (bp
= buffer
+ i
; i
> 1; i
--)
671 if (write (out
, buffer
, strlen (buffer
)) < 0) {
672 advise (drft
, "write");
679 * Create a mhn composition file for forwarding message.
683 copy_mime_draft (int out
)
688 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
689 PLURALS(mp
->numsel
), mp
->foldpath
);
690 if (write (out
, buffer
, strlen (buffer
)) < 0) {
691 advise (drft
, "write");
693 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
694 if (is_selected (mp
, msgnum
)) {
695 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
696 if (write (out
, buffer
, strlen (buffer
)) < 0) {
697 advise (drft
, "write");
700 if (write (out
, "\n", 1) < 0) {
701 advise (drft
, "write newline");