]>
diplodocus.org Git - nmh/blob - uip/forw.c
cd68d0bf754d188bd69837882867419e3c41cb57
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/smatch.h"
10 #include "sbr/refile.h"
11 #include "sbr/cpydata.h"
12 #include "sbr/cpydgst.h"
13 #include "sbr/m_draft.h"
14 #include "sbr/m_convert.h"
15 #include "sbr/getfolder.h"
16 #include "sbr/folder_read.h"
17 #include "sbr/context_save.h"
18 #include "sbr/context_replace.h"
19 #include "sbr/context_find.h"
20 #include "sbr/ambigsw.h"
21 #include "sbr/pidstatus.h"
23 #include "sbr/print_version.h"
24 #include "sbr/print_help.h"
25 #include "sbr/arglist.h"
26 #include "sbr/error.h"
31 #include "sbr/m_maildir.h"
35 #define IFORMAT "digest-issue-%s"
36 #define VFORMAT "digest-volume-%s"
38 #define FORW_SWITCHES \
39 X("annotate", 0, ANNOSW) \
40 X("noannotate", 0, NANNOSW) \
41 X("draftfolder +folder", 0, DFOLDSW) \
42 X("draftmessage msg", 0, DMSGSW) \
43 X("nodraftfolder", 0, NDFLDSW) \
44 X("editor editor", 0, EDITRSW) \
45 X("noedit", 0, NEDITSW) \
46 X("filter filterfile", 0, FILTSW) \
47 X("form formfile", 0, FORMSW) \
48 X("format", 5, FRMTSW) \
49 X("noformat", 7, NFRMTSW) \
50 X("inplace", 0, INPLSW) \
51 X("noinplace", 0, NINPLSW) \
52 X("mime", 0, MIMESW) \
53 X("nomime", 0, NMIMESW) \
54 X("digest list", 0, DGSTSW) \
55 X("issue number", 0, ISSUESW) \
56 X("volume number", 0, VOLUMSW) \
57 X("whatnowproc program", 0, WHATSW) \
58 X("nowhatnowproc", 0, NWHATSW) \
59 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
60 X("nodashstuffing", 0, NBITSTUFFSW) \
61 X("version", 0, VERSIONSW) \
62 X("help", 0, HELPSW) \
63 X("file file", 4, FILESW) \
64 X("build", 5, BILDSW) /* interface from mhe */ \
65 X("from address", 0, FROMSW) \
66 X("to address", 0, TOSW) \
67 X("cc address", 0, CCSW) \
68 X("subject text", 0, SUBJECTSW) \
69 X("fcc mailbox", 0, FCCSW) \
70 X("width columns", 0, WIDTHSW) \
72 #define X(sw, minchars, id) id,
73 DEFINE_SWITCH_ENUM(FORW
);
76 #define X(sw, minchars, id) { sw, minchars, id },
77 DEFINE_SWITCH_ARRAY(FORW
, switches
);
80 #define DISPO_SWITCHES \
82 X("replace", 0, YESW) \
83 X("list", 0, LISTDSW) \
84 X("refile +folder", 0, REFILSW) \
87 #define X(sw, minchars, id) id,
88 DEFINE_SWITCH_ENUM(DISPO
);
91 #define X(sw, minchars, id) { sw, minchars, id },
92 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
95 static struct swit aqrl
[] = {
97 { "replace", 0, YESW
},
98 { "list", 0, LISTDSW
},
99 { "refile +folder", 0, REFILSW
},
103 static char drft
[BUFSIZ
];
105 static char delim3
[] =
106 "\n------------------------------------------------------------\n\n";
107 static char delim4
[] = "\n------------------------------\n\n";
110 static struct msgs
*mp
= NULL
; /* used a lot */
116 static void mhl_draft (int, char *, int, int, char *, char *, int);
117 static void copy_draft (int, char *, char *, int, int, int);
118 static void copy_mime_draft (int);
122 main (int argc
, char **argv
)
127 int issue
= 0, volume
= 0, dashstuff
= 0;
131 int out
, isdf
= 0, msgnum
= 0;
132 int outputlinelen
= OUTPUTLINELEN
;
134 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
135 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
136 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
137 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
138 char *form
= NULL
, buf
[BUFSIZ
];
139 char **argp
, **arguments
;
141 struct msgs_array msgs
= { 0, 0, NULL
};
142 bool buildsw
= false;
144 if (nmh_init(argv
[0], true, true)) { return 1; }
146 arguments
= getarguments (invo_name
, argc
, argv
, 1);
149 while ((cp
= *argp
++)) {
151 switch (smatch (++cp
, switches
)) {
153 ambigsw (cp
, switches
);
156 die("-%s unknown", cp
);
159 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
161 print_help (buf
, switches
, 1);
164 print_version(invo_name
);
175 if (!(ed
= *argp
++) || *ed
== '-')
176 die("missing argument to %s", argp
[-2]);
184 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
185 die("missing argument to %s", argp
[-2]);
197 die("only one file at a time!");
198 if (!(cp
= *argp
++) || *cp
== '-')
199 die("missing argument to %s", argp
[-2]);
200 file
= path (cp
, TFILE
);
203 if (!(cp
= *argp
++) || *cp
== '-')
204 die("missing argument to %s", argp
[-2]);
205 filter
= mh_xstrdup(etcpath(cp
));
209 if (!(form
= *argp
++) || *form
== '-')
210 die("missing argument to %s", argp
[-2]);
214 filter
= mh_xstrdup(etcpath(mhlforward
));
236 if (!(cp
= *argp
++) || *cp
== '-')
237 die("missing argument to %s", argp
[-2]);
238 digest
= mh_xstrdup(cp
);
242 if (!(cp
= *argp
++) || *cp
== '-')
243 die("missing argument to %s", argp
[-2]);
244 if ((issue
= atoi (cp
)) < 1)
245 die("bad argument %s %s", argp
[-2], cp
);
248 if (!(cp
= *argp
++) || *cp
== '-')
249 die("missing argument to %s", argp
[-2]);
250 if ((volume
= atoi (cp
)) < 1)
251 die("bad argument %s %s", argp
[-2], cp
);
256 die("only one draft folder at a time!");
257 if (!(cp
= *argp
++) || *cp
== '-')
258 die("missing argument to %s", argp
[-2]);
259 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
260 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
264 die("only one draft message at a time!");
265 if (!(dmsg
= *argp
++) || *dmsg
== '-')
266 die("missing argument to %s", argp
[-2]);
274 dashstuff
= 1; /* ternary logic */
277 dashstuff
= -1; /* ternary logic */
281 if (!(cp
= *argp
++) || *cp
== '-')
282 die("missing argument to %s", argp
[-2]);
283 from
= addlist(from
, cp
);
286 if (!(cp
= *argp
++) || *cp
== '-')
287 die("missing argument to %s", argp
[-2]);
288 to
= addlist(to
, cp
);
291 if (!(cp
= *argp
++) || *cp
== '-')
292 die("missing argument to %s", argp
[-2]);
293 cc
= addlist(cc
, cp
);
296 if (!(cp
= *argp
++) || *cp
== '-')
297 die("missing argument to %s", argp
[-2]);
298 fcc
= addlist(fcc
, cp
);
301 if (!(cp
= *argp
++) || *cp
== '-')
302 die("missing argument to %s", argp
[-2]);
303 subject
= mh_xstrdup(cp
);
307 if (!(cp
= *argp
++) || *cp
== '-')
308 die("missing argument to %s", argp
[-2]);
309 if ((outputlinelen
= atoi(cp
)) < 10)
310 die("impossible width %d", outputlinelen
);
314 if (*cp
== '+' || *cp
== '@') {
316 die("only one folder at a time!");
317 folder
= pluspath (cp
);
319 app_msgarg(&msgs
, cp
);
323 cwd
= mh_xstrdup(pwd ());
325 if (!context_find ("path"))
326 free (path ("./", TFOLDER
));
327 if (file
&& (msgs
.size
|| folder
))
328 die("can't mix files and folders/msgs");
332 strncpy (drft
, buildsw
? m_maildir ("draft")
333 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
335 /* Check if a draft already exists */
336 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
337 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
338 for (i
= LISTDSW
; i
!= YESW
;) {
339 if (!(argp
= read_switch_multiword ("\nDisposition? ",
340 isdf
? aqrnl
: aqrl
)))
342 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
351 showfile (++argp
, drft
);
354 if (refile (++argp
, drft
) == 0)
368 anot
= false; /* don't want to annotate a file */
371 * Forwarding a message.
374 app_msgarg(&msgs
, "cur");
376 folder
= getfolder (1);
377 maildir
= m_maildir (folder
);
379 if (chdir (maildir
) == NOTOK
)
380 adios (maildir
, "unable to change directory to");
382 /* read folder and create message structure */
383 if (!(mp
= folder_read (folder
, 1)))
384 die("unable to read folder %s", folder
);
386 /* check for empty folder */
388 die("no messages in %s", folder
);
390 /* parse all the message ranges/sequences and set SELECTED */
391 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
392 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
395 seq_setprev (mp
); /* set the previous sequence */
398 * Find the first message in our set and use it as the input
399 * for the component scanner
402 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
403 if (is_selected (mp
, msgnum
)) {
404 fwdmsg
= mh_xstrdup(m_name(msgnum
));
409 die("Unable to find input message");
412 if (filter
&& access (filter
, R_OK
) == NOTOK
)
413 adios (filter
, "unable to read");
416 * Open form (component) file.
420 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
422 && (cp
= context_find (buf
))
423 && ((issue
= atoi (cp
)) < 0))
428 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
429 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
439 dat
[0] = digest
? issue
: msgnum
;
442 dat
[3] = outputlinelen
;
446 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
447 file
? file
: fwdmsg
);
449 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
450 adios (drft
, "unable to create");
453 * copy the components into the draft
455 cpydata (in
, out
, form
, drft
);
459 /* just copy the file into the draft */
460 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
461 adios (file
, "unable to open");
462 cpydata (in
, out
, file
, drft
);
467 * If filter file is defined, then format the
468 * messages into the draft using mhlproc.
471 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
473 copy_mime_draft (out
);
475 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
479 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
480 context_replace (buf
, mh_xstrdup(m_str(issue
)));
481 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
482 context_replace (buf
, mh_xstrdup(m_str(volume
)));
485 context_replace (pfolder
, folder
); /* update current folder */
486 seq_setcur (mp
, mp
->lowsel
); /* update current message */
487 seq_save (mp
); /* synchronize sequences */
488 context_save (); /* save the context file */
493 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
494 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
501 * Filter the messages you are forwarding, into the
502 * draft calling the mhlproc, and reading its output
507 mhl_draft (int out
, char *digest
, int volume
, int issue
,
508 char *file
, char *filter
, int dashstuff
)
515 struct msgs_array vec
= { 0, 0, NULL
};
517 if (pipe (pd
) == NOTOK
)
518 adios ("pipe", "unable to create");
520 argsplit_msgarg(&vec
, mhlproc
, &program
);
525 adios ("fork", "unable to");
532 app_msgarg(&vec
, "-forwall");
533 app_msgarg(&vec
, "-form");
534 app_msgarg(&vec
, filter
);
537 app_msgarg(&vec
, "-digest");
538 app_msgarg(&vec
, digest
);
539 app_msgarg(&vec
, "-issue");
540 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
541 app_msgarg(&vec
, buf1
);
542 app_msgarg(&vec
, "-volume");
543 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
544 app_msgarg(&vec
, buf2
);
548 * Are we dashstuffing (quoting) the lines that begin
549 * with `-'. We use the mhl default (don't add any flag)
550 * unless the user has specified a specific flag.
553 app_msgarg(&vec
, "-dashstuffing");
554 else if (dashstuff
< 0)
555 app_msgarg(&vec
, "-nodashstuffing");
557 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
558 if (is_selected (mp
, msgnum
))
559 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
562 app_msgarg(&vec
, NULL
);
564 execvp (program
, vec
.msgs
);
565 fprintf (stderr
, "unable to exec ");
571 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
573 pidXwait(child_id
, mhlproc
);
580 * Copy the messages into the draft. The messages are
581 * not filtered through the mhlproc. Do dashstuffing if
586 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
588 int fd
,i
, msgcnt
, msgnum
;
594 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
595 if (is_selected (mp
, msgnum
)) {
597 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
600 /* Get buffer ready to go */
602 buflen
= sizeof(buffer
);
604 strncpy (bp
, "\n-------", buflen
);
609 if (msgnum
== mp
->lowsel
) {
610 snprintf (bp
, buflen
, " Forwarded Message%s",
611 PLURALS(mp
->numsel
));
613 snprintf (bp
, buflen
, " Message %d", msgcnt
);
619 strncpy (bp
, "\n\n", buflen
);
621 if (write (out
, buffer
, strlen (buffer
)) < 0) {
622 advise (drft
, "write");
625 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
626 admonish (msgnam
, "unable to read message");
631 * Copy the message. Add RFC934 quoting (dashstuffing)
632 * unless given the -nodashstuffing flag.
635 cpydgst (fd
, out
, msgnam
, file
);
637 cpydata (fd
, out
, msgnam
, file
);
645 strncpy (buffer
, delim4
, sizeof(buffer
));
647 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
648 PLURALS(mp
->numsel
));
650 if (write (out
, buffer
, strlen (buffer
)) < 0) {
651 advise (drft
, "write");
655 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
656 digest
, volume
, issue
);
658 for (bp
= buffer
+ i
; i
> 1; i
--)
662 if (write (out
, buffer
, strlen (buffer
)) < 0) {
663 advise (drft
, "write");
670 * Create a mhn composition file for forwarding message.
674 copy_mime_draft (int out
)
679 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
680 PLURALS(mp
->numsel
), mp
->foldpath
);
681 if (write (out
, buffer
, strlen (buffer
)) < 0) {
682 advise (drft
, "write");
684 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
685 if (is_selected (mp
, msgnum
)) {
686 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
687 if (write (out
, buffer
, strlen (buffer
)) < 0) {
688 advise (drft
, "write");
691 if (write (out
, "\n", 1) < 0) {
692 advise (drft
, "write newline");