]>
diplodocus.org Git - nmh/blob - uip/forw.c
864622163138ff6750f287ad535b0d243b623d85
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/m_gmprot.h"
10 #include "sbr/getarguments.h"
11 #include "sbr/read_switch_multiword.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_setcur.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/showfile.h"
16 #include "sbr/smatch.h"
17 #include "sbr/refile.h"
18 #include "sbr/cpydata.h"
19 #include "sbr/cpydgst.h"
20 #include "sbr/m_draft.h"
21 #include "sbr/m_convert.h"
22 #include "sbr/getfolder.h"
23 #include "sbr/folder_read.h"
24 #include "sbr/context_save.h"
25 #include "sbr/context_replace.h"
26 #include "sbr/context_find.h"
27 #include "sbr/ambigsw.h"
28 #include "sbr/pidstatus.h"
30 #include "sbr/print_version.h"
31 #include "sbr/print_help.h"
32 #include "sbr/arglist.h"
33 #include "sbr/error.h"
38 #include "sbr/m_maildir.h"
42 #define IFORMAT "digest-issue-%s"
43 #define VFORMAT "digest-volume-%s"
45 #define FORW_SWITCHES \
46 X("annotate", 0, ANNOSW) \
47 X("noannotate", 0, NANNOSW) \
48 X("draftfolder +folder", 0, DFOLDSW) \
49 X("draftmessage msg", 0, DMSGSW) \
50 X("nodraftfolder", 0, NDFLDSW) \
51 X("editor editor", 0, EDITRSW) \
52 X("noedit", 0, NEDITSW) \
53 X("filter filterfile", 0, FILTSW) \
54 X("form formfile", 0, FORMSW) \
55 X("format", 5, FRMTSW) \
56 X("noformat", 7, NFRMTSW) \
57 X("inplace", 0, INPLSW) \
58 X("noinplace", 0, NINPLSW) \
59 X("mime", 0, MIMESW) \
60 X("nomime", 0, NMIMESW) \
61 X("digest list", 0, DGSTSW) \
62 X("issue number", 0, ISSUESW) \
63 X("volume number", 0, VOLUMSW) \
64 X("whatnowproc program", 0, WHATSW) \
65 X("nowhatnowproc", 0, NWHATSW) \
66 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
67 X("nodashstuffing", 0, NBITSTUFFSW) \
68 X("version", 0, VERSIONSW) \
69 X("help", 0, HELPSW) \
70 X("file file", 4, FILESW) \
71 X("build", 5, BILDSW) /* interface from mhe */ \
72 X("from address", 0, FROMSW) \
73 X("to address", 0, TOSW) \
74 X("cc address", 0, CCSW) \
75 X("subject text", 0, SUBJECTSW) \
76 X("fcc mailbox", 0, FCCSW) \
77 X("width columns", 0, WIDTHSW) \
79 #define X(sw, minchars, id) id,
80 DEFINE_SWITCH_ENUM(FORW
);
83 #define X(sw, minchars, id) { sw, minchars, id },
84 DEFINE_SWITCH_ARRAY(FORW
, switches
);
87 #define DISPO_SWITCHES \
89 X("replace", 0, YESW) \
90 X("list", 0, LISTDSW) \
91 X("refile +folder", 0, REFILSW) \
94 #define X(sw, minchars, id) id,
95 DEFINE_SWITCH_ENUM(DISPO
);
98 #define X(sw, minchars, id) { sw, minchars, id },
99 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
102 static struct swit aqrl
[] = {
104 { "replace", 0, YESW
},
105 { "list", 0, LISTDSW
},
106 { "refile +folder", 0, REFILSW
},
110 static char drft
[BUFSIZ
];
112 static char delim3
[] =
113 "\n------------------------------------------------------------\n\n";
114 static char delim4
[] = "\n------------------------------\n\n";
117 static struct msgs
*mp
= NULL
; /* used a lot */
123 static void mhl_draft (int, char *, int, int, char *, char *, int);
124 static void copy_draft (int, char *, char *, int, int, int);
125 static void copy_mime_draft (int);
129 main (int argc
, char **argv
)
134 int issue
= 0, volume
= 0, dashstuff
= 0;
138 int out
, isdf
= 0, msgnum
= 0;
139 int outputlinelen
= OUTPUTLINELEN
;
141 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
142 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
143 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
144 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
145 char *form
= NULL
, buf
[BUFSIZ
];
146 char **argp
, **arguments
;
148 struct msgs_array msgs
= { 0, 0, NULL
};
149 bool buildsw
= false;
151 if (nmh_init(argv
[0], true, true)) { return 1; }
153 arguments
= getarguments (invo_name
, argc
, argv
, 1);
156 while ((cp
= *argp
++)) {
158 switch (smatch (++cp
, switches
)) {
160 ambigsw (cp
, switches
);
163 die("-%s unknown", cp
);
166 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
168 print_help (buf
, switches
, 1);
171 print_version(invo_name
);
182 if (!(ed
= *argp
++) || *ed
== '-')
183 die("missing argument to %s", argp
[-2]);
191 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
192 die("missing argument to %s", argp
[-2]);
204 die("only one file at a time!");
205 if (!(cp
= *argp
++) || *cp
== '-')
206 die("missing argument to %s", argp
[-2]);
207 file
= path (cp
, TFILE
);
210 if (!(cp
= *argp
++) || *cp
== '-')
211 die("missing argument to %s", argp
[-2]);
212 filter
= mh_xstrdup(etcpath(cp
));
216 if (!(form
= *argp
++) || *form
== '-')
217 die("missing argument to %s", argp
[-2]);
221 filter
= mh_xstrdup(etcpath(mhlforward
));
243 if (!(cp
= *argp
++) || *cp
== '-')
244 die("missing argument to %s", argp
[-2]);
245 digest
= mh_xstrdup(cp
);
249 if (!(cp
= *argp
++) || *cp
== '-')
250 die("missing argument to %s", argp
[-2]);
251 if ((issue
= atoi (cp
)) < 1)
252 die("bad argument %s %s", argp
[-2], cp
);
255 if (!(cp
= *argp
++) || *cp
== '-')
256 die("missing argument to %s", argp
[-2]);
257 if ((volume
= atoi (cp
)) < 1)
258 die("bad argument %s %s", argp
[-2], cp
);
263 die("only one draft folder at a time!");
264 if (!(cp
= *argp
++) || *cp
== '-')
265 die("missing argument to %s", argp
[-2]);
266 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
267 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
271 die("only one draft message at a time!");
272 if (!(dmsg
= *argp
++) || *dmsg
== '-')
273 die("missing argument to %s", argp
[-2]);
281 dashstuff
= 1; /* ternary logic */
284 dashstuff
= -1; /* ternary logic */
288 if (!(cp
= *argp
++) || *cp
== '-')
289 die("missing argument to %s", argp
[-2]);
290 from
= addlist(from
, cp
);
293 if (!(cp
= *argp
++) || *cp
== '-')
294 die("missing argument to %s", argp
[-2]);
295 to
= addlist(to
, cp
);
298 if (!(cp
= *argp
++) || *cp
== '-')
299 die("missing argument to %s", argp
[-2]);
300 cc
= addlist(cc
, cp
);
303 if (!(cp
= *argp
++) || *cp
== '-')
304 die("missing argument to %s", argp
[-2]);
305 fcc
= addlist(fcc
, cp
);
308 if (!(cp
= *argp
++) || *cp
== '-')
309 die("missing argument to %s", argp
[-2]);
310 subject
= mh_xstrdup(cp
);
314 if (!(cp
= *argp
++) || *cp
== '-')
315 die("missing argument to %s", argp
[-2]);
316 if ((outputlinelen
= atoi(cp
)) < 10)
317 die("impossible width %d", outputlinelen
);
321 if (*cp
== '+' || *cp
== '@') {
323 die("only one folder at a time!");
324 folder
= pluspath (cp
);
326 app_msgarg(&msgs
, cp
);
330 cwd
= mh_xstrdup(pwd ());
332 if (!context_find ("path"))
333 free (path ("./", TFOLDER
));
334 if (file
&& (msgs
.size
|| folder
))
335 die("can't mix files and folders/msgs");
339 strncpy (drft
, buildsw
? m_maildir ("draft")
340 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
342 /* Check if a draft already exists */
343 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
344 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
345 for (i
= LISTDSW
; i
!= YESW
;) {
346 if (!(argp
= read_switch_multiword ("\nDisposition? ",
347 isdf
? aqrnl
: aqrl
)))
349 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
358 showfile (++argp
, drft
);
361 if (refile (++argp
, drft
) == 0)
375 anot
= false; /* don't want to annotate a file */
378 * Forwarding a message.
381 app_msgarg(&msgs
, "cur");
383 folder
= getfolder (1);
384 maildir
= m_maildir (folder
);
386 if (chdir (maildir
) == NOTOK
)
387 adios (maildir
, "unable to change directory to");
389 /* read folder and create message structure */
390 if (!(mp
= folder_read (folder
, 1)))
391 die("unable to read folder %s", folder
);
393 /* check for empty folder */
395 die("no messages in %s", folder
);
397 /* parse all the message ranges/sequences and set SELECTED */
398 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
399 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
402 seq_setprev (mp
); /* set the previous sequence */
405 * Find the first message in our set and use it as the input
406 * for the component scanner
409 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
410 if (is_selected (mp
, msgnum
)) {
411 fwdmsg
= mh_xstrdup(m_name(msgnum
));
416 die("Unable to find input message");
419 if (filter
&& access (filter
, R_OK
) == NOTOK
)
420 adios (filter
, "unable to read");
423 * Open form (component) file.
427 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
429 && (cp
= context_find (buf
))
430 && ((issue
= atoi (cp
)) < 0))
435 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
436 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
446 dat
[0] = digest
? issue
: msgnum
;
449 dat
[3] = outputlinelen
;
453 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
454 file
? file
: fwdmsg
);
456 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
457 adios (drft
, "unable to create");
460 * copy the components into the draft
462 cpydata (in
, out
, form
, drft
);
466 /* just copy the file into the draft */
467 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
468 adios (file
, "unable to open");
469 cpydata (in
, out
, file
, drft
);
474 * If filter file is defined, then format the
475 * messages into the draft using mhlproc.
478 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
480 copy_mime_draft (out
);
482 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
486 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
487 context_replace (buf
, mh_xstrdup(m_str(issue
)));
488 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
489 context_replace (buf
, mh_xstrdup(m_str(volume
)));
492 context_replace (pfolder
, folder
); /* update current folder */
493 seq_setcur (mp
, mp
->lowsel
); /* update current message */
494 seq_save (mp
); /* synchronize sequences */
495 context_save (); /* save the context file */
500 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
501 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
508 * Filter the messages you are forwarding, into the
509 * draft calling the mhlproc, and reading its output
514 mhl_draft (int out
, char *digest
, int volume
, int issue
,
515 char *file
, char *filter
, int dashstuff
)
522 struct msgs_array vec
= { 0, 0, NULL
};
524 if (pipe (pd
) == NOTOK
)
525 adios ("pipe", "unable to create");
527 argsplit_msgarg(&vec
, mhlproc
, &program
);
532 adios ("fork", "unable to");
539 app_msgarg(&vec
, "-forwall");
540 app_msgarg(&vec
, "-form");
541 app_msgarg(&vec
, filter
);
544 app_msgarg(&vec
, "-digest");
545 app_msgarg(&vec
, digest
);
546 app_msgarg(&vec
, "-issue");
547 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
548 app_msgarg(&vec
, buf1
);
549 app_msgarg(&vec
, "-volume");
550 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
551 app_msgarg(&vec
, buf2
);
555 * Are we dashstuffing (quoting) the lines that begin
556 * with `-'. We use the mhl default (don't add any flag)
557 * unless the user has specified a specific flag.
560 app_msgarg(&vec
, "-dashstuffing");
561 else if (dashstuff
< 0)
562 app_msgarg(&vec
, "-nodashstuffing");
564 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
565 if (is_selected (mp
, msgnum
))
566 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
569 app_msgarg(&vec
, NULL
);
571 execvp (program
, vec
.msgs
);
572 fprintf (stderr
, "unable to exec ");
578 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
580 pidXwait(child_id
, mhlproc
);
587 * Copy the messages into the draft. The messages are
588 * not filtered through the mhlproc. Do dashstuffing if
593 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
595 int fd
,i
, msgcnt
, msgnum
;
601 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
602 if (is_selected (mp
, msgnum
)) {
604 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
607 /* Get buffer ready to go */
609 buflen
= sizeof(buffer
);
611 strncpy (bp
, "\n-------", buflen
);
616 if (msgnum
== mp
->lowsel
) {
617 snprintf (bp
, buflen
, " Forwarded Message%s",
618 PLURALS(mp
->numsel
));
620 snprintf (bp
, buflen
, " Message %d", msgcnt
);
626 strncpy (bp
, "\n\n", buflen
);
628 if (write (out
, buffer
, strlen (buffer
)) < 0) {
629 advise (drft
, "write");
632 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
633 admonish (msgnam
, "unable to read message");
638 * Copy the message. Add RFC934 quoting (dashstuffing)
639 * unless given the -nodashstuffing flag.
642 cpydgst (fd
, out
, msgnam
, file
);
644 cpydata (fd
, out
, msgnam
, file
);
652 strncpy (buffer
, delim4
, sizeof(buffer
));
654 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
655 PLURALS(mp
->numsel
));
657 if (write (out
, buffer
, strlen (buffer
)) < 0) {
658 advise (drft
, "write");
662 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
663 digest
, volume
, issue
);
665 for (bp
= buffer
+ i
; i
> 1; i
--)
669 if (write (out
, buffer
, strlen (buffer
)) < 0) {
670 advise (drft
, "write");
677 * Create a mhn composition file for forwarding message.
681 copy_mime_draft (int out
)
686 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
687 PLURALS(mp
->numsel
), mp
->foldpath
);
688 if (write (out
, buffer
, strlen (buffer
)) < 0) {
689 advise (drft
, "write");
691 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
692 if (is_selected (mp
, msgnum
)) {
693 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
694 if (write (out
, buffer
, strlen (buffer
)) < 0) {
695 advise (drft
, "write");
698 if (write (out
, "\n", 1) < 0) {
699 advise (drft
, "write newline");