]>
diplodocus.org Git - nmh/blob - uip/forw.c
1dffa9750a32d1b19ad32429a33022f199bec4af
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/refile.h"
10 #include "sbr/cpydata.h"
11 #include "sbr/cpydgst.h"
12 #include "sbr/m_draft.h"
13 #include "sbr/m_convert.h"
14 #include "sbr/getfolder.h"
15 #include "sbr/folder_read.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/ambigsw.h"
20 #include "sbr/pidstatus.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/arglist.h"
25 #include "sbr/error.h"
30 #include "sbr/m_maildir.h"
34 #define IFORMAT "digest-issue-%s"
35 #define VFORMAT "digest-volume-%s"
37 #define FORW_SWITCHES \
38 X("annotate", 0, ANNOSW) \
39 X("noannotate", 0, NANNOSW) \
40 X("draftfolder +folder", 0, DFOLDSW) \
41 X("draftmessage msg", 0, DMSGSW) \
42 X("nodraftfolder", 0, NDFLDSW) \
43 X("editor editor", 0, EDITRSW) \
44 X("noedit", 0, NEDITSW) \
45 X("filter filterfile", 0, FILTSW) \
46 X("form formfile", 0, FORMSW) \
47 X("format", 5, FRMTSW) \
48 X("noformat", 7, NFRMTSW) \
49 X("inplace", 0, INPLSW) \
50 X("noinplace", 0, NINPLSW) \
51 X("mime", 0, MIMESW) \
52 X("nomime", 0, NMIMESW) \
53 X("digest list", 0, DGSTSW) \
54 X("issue number", 0, ISSUESW) \
55 X("volume number", 0, VOLUMSW) \
56 X("whatnowproc program", 0, WHATSW) \
57 X("nowhatnowproc", 0, NWHATSW) \
58 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
59 X("nodashstuffing", 0, NBITSTUFFSW) \
60 X("version", 0, VERSIONSW) \
61 X("help", 0, HELPSW) \
62 X("file file", 4, FILESW) \
63 X("build", 5, BILDSW) /* interface from mhe */ \
64 X("from address", 0, FROMSW) \
65 X("to address", 0, TOSW) \
66 X("cc address", 0, CCSW) \
67 X("subject text", 0, SUBJECTSW) \
68 X("fcc mailbox", 0, FCCSW) \
69 X("width columns", 0, WIDTHSW) \
71 #define X(sw, minchars, id) id,
72 DEFINE_SWITCH_ENUM(FORW
);
75 #define X(sw, minchars, id) { sw, minchars, id },
76 DEFINE_SWITCH_ARRAY(FORW
, switches
);
79 #define DISPO_SWITCHES \
81 X("replace", 0, YESW) \
82 X("list", 0, LISTDSW) \
83 X("refile +folder", 0, REFILSW) \
86 #define X(sw, minchars, id) id,
87 DEFINE_SWITCH_ENUM(DISPO
);
90 #define X(sw, minchars, id) { sw, minchars, id },
91 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
94 static struct swit aqrl
[] = {
96 { "replace", 0, YESW
},
97 { "list", 0, LISTDSW
},
98 { "refile +folder", 0, REFILSW
},
102 static char drft
[BUFSIZ
];
104 static char delim3
[] =
105 "\n------------------------------------------------------------\n\n";
106 static char delim4
[] = "\n------------------------------\n\n";
109 static struct msgs
*mp
= NULL
; /* used a lot */
115 static void mhl_draft (int, char *, int, int, char *, char *, int);
116 static void copy_draft (int, char *, char *, int, int, int);
117 static void copy_mime_draft (int);
121 main (int argc
, char **argv
)
126 int issue
= 0, volume
= 0, dashstuff
= 0;
130 int out
, isdf
= 0, msgnum
= 0;
131 int outputlinelen
= OUTPUTLINELEN
;
133 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
134 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
135 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
136 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
137 char *form
= NULL
, buf
[BUFSIZ
];
138 char **argp
, **arguments
;
140 struct msgs_array msgs
= { 0, 0, NULL
};
141 bool buildsw
= false;
143 if (nmh_init(argv
[0], true, true)) { return 1; }
145 arguments
= getarguments (invo_name
, argc
, argv
, 1);
148 while ((cp
= *argp
++)) {
150 switch (smatch (++cp
, switches
)) {
152 ambigsw (cp
, switches
);
155 die("-%s unknown", cp
);
158 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
160 print_help (buf
, switches
, 1);
163 print_version(invo_name
);
174 if (!(ed
= *argp
++) || *ed
== '-')
175 die("missing argument to %s", argp
[-2]);
183 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
184 die("missing argument to %s", argp
[-2]);
196 die("only one file at a time!");
197 if (!(cp
= *argp
++) || *cp
== '-')
198 die("missing argument to %s", argp
[-2]);
199 file
= path (cp
, TFILE
);
202 if (!(cp
= *argp
++) || *cp
== '-')
203 die("missing argument to %s", argp
[-2]);
204 filter
= mh_xstrdup(etcpath(cp
));
208 if (!(form
= *argp
++) || *form
== '-')
209 die("missing argument to %s", argp
[-2]);
213 filter
= mh_xstrdup(etcpath(mhlforward
));
235 if (!(cp
= *argp
++) || *cp
== '-')
236 die("missing argument to %s", argp
[-2]);
237 digest
= mh_xstrdup(cp
);
241 if (!(cp
= *argp
++) || *cp
== '-')
242 die("missing argument to %s", argp
[-2]);
243 if ((issue
= atoi (cp
)) < 1)
244 die("bad argument %s %s", argp
[-2], cp
);
247 if (!(cp
= *argp
++) || *cp
== '-')
248 die("missing argument to %s", argp
[-2]);
249 if ((volume
= atoi (cp
)) < 1)
250 die("bad argument %s %s", argp
[-2], cp
);
255 die("only one draft folder at a time!");
256 if (!(cp
= *argp
++) || *cp
== '-')
257 die("missing argument to %s", argp
[-2]);
258 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
259 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
263 die("only one draft message at a time!");
264 if (!(dmsg
= *argp
++) || *dmsg
== '-')
265 die("missing argument to %s", argp
[-2]);
273 dashstuff
= 1; /* ternary logic */
276 dashstuff
= -1; /* ternary logic */
280 if (!(cp
= *argp
++) || *cp
== '-')
281 die("missing argument to %s", argp
[-2]);
282 from
= addlist(from
, cp
);
285 if (!(cp
= *argp
++) || *cp
== '-')
286 die("missing argument to %s", argp
[-2]);
287 to
= addlist(to
, cp
);
290 if (!(cp
= *argp
++) || *cp
== '-')
291 die("missing argument to %s", argp
[-2]);
292 cc
= addlist(cc
, cp
);
295 if (!(cp
= *argp
++) || *cp
== '-')
296 die("missing argument to %s", argp
[-2]);
297 fcc
= addlist(fcc
, cp
);
300 if (!(cp
= *argp
++) || *cp
== '-')
301 die("missing argument to %s", argp
[-2]);
302 subject
= mh_xstrdup(cp
);
306 if (!(cp
= *argp
++) || *cp
== '-')
307 die("missing argument to %s", argp
[-2]);
308 if ((outputlinelen
= atoi(cp
)) < 10)
309 die("impossible width %d", outputlinelen
);
313 if (*cp
== '+' || *cp
== '@') {
315 die("only one folder at a time!");
316 folder
= pluspath (cp
);
318 app_msgarg(&msgs
, cp
);
322 cwd
= mh_xstrdup(pwd ());
324 if (!context_find ("path"))
325 free (path ("./", TFOLDER
));
326 if (file
&& (msgs
.size
|| folder
))
327 die("can't mix files and folders/msgs");
331 strncpy (drft
, buildsw
? m_maildir ("draft")
332 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
334 /* Check if a draft already exists */
335 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
336 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
337 for (i
= LISTDSW
; i
!= YESW
;) {
338 if (!(argp
= read_switch_multiword ("\nDisposition? ",
339 isdf
? aqrnl
: aqrl
)))
341 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
350 showfile (++argp
, drft
);
353 if (refile (++argp
, drft
) == 0)
367 anot
= false; /* don't want to annotate a file */
370 * Forwarding a message.
373 app_msgarg(&msgs
, "cur");
375 folder
= getfolder (1);
376 maildir
= m_maildir (folder
);
378 if (chdir (maildir
) == NOTOK
)
379 adios (maildir
, "unable to change directory to");
381 /* read folder and create message structure */
382 if (!(mp
= folder_read (folder
, 1)))
383 die("unable to read folder %s", folder
);
385 /* check for empty folder */
387 die("no messages in %s", folder
);
389 /* parse all the message ranges/sequences and set SELECTED */
390 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
391 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
394 seq_setprev (mp
); /* set the previous sequence */
397 * Find the first message in our set and use it as the input
398 * for the component scanner
401 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
402 if (is_selected (mp
, msgnum
)) {
403 fwdmsg
= mh_xstrdup(m_name(msgnum
));
408 die("Unable to find input message");
411 if (filter
&& access (filter
, R_OK
) == NOTOK
)
412 adios (filter
, "unable to read");
415 * Open form (component) file.
419 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
421 && (cp
= context_find (buf
))
422 && ((issue
= atoi (cp
)) < 0))
427 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
428 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
438 dat
[0] = digest
? issue
: msgnum
;
441 dat
[3] = outputlinelen
;
445 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
446 file
? file
: fwdmsg
);
448 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
449 adios (drft
, "unable to create");
452 * copy the components into the draft
454 cpydata (in
, out
, form
, drft
);
458 /* just copy the file into the draft */
459 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
460 adios (file
, "unable to open");
461 cpydata (in
, out
, file
, drft
);
466 * If filter file is defined, then format the
467 * messages into the draft using mhlproc.
470 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
472 copy_mime_draft (out
);
474 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
478 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
479 context_replace (buf
, mh_xstrdup(m_str(issue
)));
480 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
481 context_replace (buf
, mh_xstrdup(m_str(volume
)));
484 context_replace (pfolder
, folder
); /* update current folder */
485 seq_setcur (mp
, mp
->lowsel
); /* update current message */
486 seq_save (mp
); /* synchronize sequences */
487 context_save (); /* save the context file */
492 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
493 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
500 * Filter the messages you are forwarding, into the
501 * draft calling the mhlproc, and reading its output
506 mhl_draft (int out
, char *digest
, int volume
, int issue
,
507 char *file
, char *filter
, int dashstuff
)
514 struct msgs_array vec
= { 0, 0, NULL
};
516 if (pipe (pd
) == NOTOK
)
517 adios ("pipe", "unable to create");
519 argsplit_msgarg(&vec
, mhlproc
, &program
);
524 adios ("fork", "unable to");
531 app_msgarg(&vec
, "-forwall");
532 app_msgarg(&vec
, "-form");
533 app_msgarg(&vec
, filter
);
536 app_msgarg(&vec
, "-digest");
537 app_msgarg(&vec
, digest
);
538 app_msgarg(&vec
, "-issue");
539 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
540 app_msgarg(&vec
, buf1
);
541 app_msgarg(&vec
, "-volume");
542 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
543 app_msgarg(&vec
, buf2
);
547 * Are we dashstuffing (quoting) the lines that begin
548 * with `-'. We use the mhl default (don't add any flag)
549 * unless the user has specified a specific flag.
552 app_msgarg(&vec
, "-dashstuffing");
553 else if (dashstuff
< 0)
554 app_msgarg(&vec
, "-nodashstuffing");
556 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
557 if (is_selected (mp
, msgnum
))
558 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
561 app_msgarg(&vec
, NULL
);
563 execvp (program
, vec
.msgs
);
564 fprintf (stderr
, "unable to exec ");
570 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
572 pidXwait(child_id
, mhlproc
);
579 * Copy the messages into the draft. The messages are
580 * not filtered through the mhlproc. Do dashstuffing if
585 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
587 int fd
,i
, msgcnt
, msgnum
;
593 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
594 if (is_selected (mp
, msgnum
)) {
596 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
599 /* Get buffer ready to go */
601 buflen
= sizeof(buffer
);
603 strncpy (bp
, "\n-------", buflen
);
608 if (msgnum
== mp
->lowsel
) {
609 snprintf (bp
, buflen
, " Forwarded Message%s",
610 PLURALS(mp
->numsel
));
612 snprintf (bp
, buflen
, " Message %d", msgcnt
);
618 strncpy (bp
, "\n\n", buflen
);
620 if (write (out
, buffer
, strlen (buffer
)) < 0) {
621 advise (drft
, "write");
624 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
625 admonish (msgnam
, "unable to read message");
630 * Copy the message. Add RFC934 quoting (dashstuffing)
631 * unless given the -nodashstuffing flag.
634 cpydgst (fd
, out
, msgnam
, file
);
636 cpydata (fd
, out
, msgnam
, file
);
644 strncpy (buffer
, delim4
, sizeof(buffer
));
646 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
647 PLURALS(mp
->numsel
));
649 if (write (out
, buffer
, strlen (buffer
)) < 0) {
650 advise (drft
, "write");
654 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
655 digest
, volume
, issue
);
657 for (bp
= buffer
+ i
; i
> 1; i
--)
661 if (write (out
, buffer
, strlen (buffer
)) < 0) {
662 advise (drft
, "write");
669 * Create a mhn composition file for forwarding message.
673 copy_mime_draft (int out
)
678 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
679 PLURALS(mp
->numsel
), mp
->foldpath
);
680 if (write (out
, buffer
, strlen (buffer
)) < 0) {
681 advise (drft
, "write");
683 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
684 if (is_selected (mp
, msgnum
)) {
685 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
686 if (write (out
, buffer
, strlen (buffer
)) < 0) {
687 advise (drft
, "write");
690 if (write (out
, "\n", 1) < 0) {
691 advise (drft
, "write newline");