]>
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 "sbr/folder_read.h"
10 #include "sbr/context_save.h"
11 #include "sbr/context_replace.h"
12 #include "sbr/context_find.h"
13 #include "sbr/ambigsw.h"
14 #include "sbr/pidstatus.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/arglist.h"
19 #include "sbr/error.h"
24 #include "sbr/m_maildir.h"
28 #define IFORMAT "digest-issue-%s"
29 #define VFORMAT "digest-volume-%s"
31 #define FORW_SWITCHES \
32 X("annotate", 0, ANNOSW) \
33 X("noannotate", 0, NANNOSW) \
34 X("draftfolder +folder", 0, DFOLDSW) \
35 X("draftmessage msg", 0, DMSGSW) \
36 X("nodraftfolder", 0, NDFLDSW) \
37 X("editor editor", 0, EDITRSW) \
38 X("noedit", 0, NEDITSW) \
39 X("filter filterfile", 0, FILTSW) \
40 X("form formfile", 0, FORMSW) \
41 X("format", 5, FRMTSW) \
42 X("noformat", 7, NFRMTSW) \
43 X("inplace", 0, INPLSW) \
44 X("noinplace", 0, NINPLSW) \
45 X("mime", 0, MIMESW) \
46 X("nomime", 0, NMIMESW) \
47 X("digest list", 0, DGSTSW) \
48 X("issue number", 0, ISSUESW) \
49 X("volume number", 0, VOLUMSW) \
50 X("whatnowproc program", 0, WHATSW) \
51 X("nowhatnowproc", 0, NWHATSW) \
52 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
53 X("nodashstuffing", 0, NBITSTUFFSW) \
54 X("version", 0, VERSIONSW) \
55 X("help", 0, HELPSW) \
56 X("file file", 4, FILESW) \
57 X("build", 5, BILDSW) /* interface from mhe */ \
58 X("from address", 0, FROMSW) \
59 X("to address", 0, TOSW) \
60 X("cc address", 0, CCSW) \
61 X("subject text", 0, SUBJECTSW) \
62 X("fcc mailbox", 0, FCCSW) \
63 X("width columns", 0, WIDTHSW) \
65 #define X(sw, minchars, id) id,
66 DEFINE_SWITCH_ENUM(FORW
);
69 #define X(sw, minchars, id) { sw, minchars, id },
70 DEFINE_SWITCH_ARRAY(FORW
, switches
);
73 #define DISPO_SWITCHES \
75 X("replace", 0, YESW) \
76 X("list", 0, LISTDSW) \
77 X("refile +folder", 0, REFILSW) \
80 #define X(sw, minchars, id) id,
81 DEFINE_SWITCH_ENUM(DISPO
);
84 #define X(sw, minchars, id) { sw, minchars, id },
85 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
88 static struct swit aqrl
[] = {
90 { "replace", 0, YESW
},
91 { "list", 0, LISTDSW
},
92 { "refile +folder", 0, REFILSW
},
96 static char drft
[BUFSIZ
];
98 static char delim3
[] =
99 "\n------------------------------------------------------------\n\n";
100 static char delim4
[] = "\n------------------------------\n\n";
103 static struct msgs
*mp
= NULL
; /* used a lot */
109 static void mhl_draft (int, char *, int, int, char *, char *, int);
110 static void copy_draft (int, char *, char *, int, int, int);
111 static void copy_mime_draft (int);
115 main (int argc
, char **argv
)
120 int issue
= 0, volume
= 0, dashstuff
= 0;
124 int out
, isdf
= 0, msgnum
= 0;
125 int outputlinelen
= OUTPUTLINELEN
;
127 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
128 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
129 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
130 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
131 char *form
= NULL
, buf
[BUFSIZ
];
132 char **argp
, **arguments
;
134 struct msgs_array msgs
= { 0, 0, NULL
};
135 bool buildsw
= false;
137 if (nmh_init(argv
[0], true, true)) { return 1; }
139 arguments
= getarguments (invo_name
, argc
, argv
, 1);
142 while ((cp
= *argp
++)) {
144 switch (smatch (++cp
, switches
)) {
146 ambigsw (cp
, switches
);
149 die("-%s unknown", cp
);
152 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
154 print_help (buf
, switches
, 1);
157 print_version(invo_name
);
168 if (!(ed
= *argp
++) || *ed
== '-')
169 die("missing argument to %s", argp
[-2]);
177 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
178 die("missing argument to %s", argp
[-2]);
190 die("only one file at a time!");
191 if (!(cp
= *argp
++) || *cp
== '-')
192 die("missing argument to %s", argp
[-2]);
193 file
= path (cp
, TFILE
);
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
198 filter
= mh_xstrdup(etcpath(cp
));
202 if (!(form
= *argp
++) || *form
== '-')
203 die("missing argument to %s", argp
[-2]);
207 filter
= mh_xstrdup(etcpath(mhlforward
));
229 if (!(cp
= *argp
++) || *cp
== '-')
230 die("missing argument to %s", argp
[-2]);
231 digest
= mh_xstrdup(cp
);
235 if (!(cp
= *argp
++) || *cp
== '-')
236 die("missing argument to %s", argp
[-2]);
237 if ((issue
= atoi (cp
)) < 1)
238 die("bad argument %s %s", argp
[-2], cp
);
241 if (!(cp
= *argp
++) || *cp
== '-')
242 die("missing argument to %s", argp
[-2]);
243 if ((volume
= atoi (cp
)) < 1)
244 die("bad argument %s %s", argp
[-2], cp
);
249 die("only one draft folder at a time!");
250 if (!(cp
= *argp
++) || *cp
== '-')
251 die("missing argument to %s", argp
[-2]);
252 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
253 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
257 die("only one draft message at a time!");
258 if (!(dmsg
= *argp
++) || *dmsg
== '-')
259 die("missing argument to %s", argp
[-2]);
267 dashstuff
= 1; /* ternary logic */
270 dashstuff
= -1; /* ternary logic */
274 if (!(cp
= *argp
++) || *cp
== '-')
275 die("missing argument to %s", argp
[-2]);
276 from
= addlist(from
, cp
);
279 if (!(cp
= *argp
++) || *cp
== '-')
280 die("missing argument to %s", argp
[-2]);
281 to
= addlist(to
, cp
);
284 if (!(cp
= *argp
++) || *cp
== '-')
285 die("missing argument to %s", argp
[-2]);
286 cc
= addlist(cc
, cp
);
289 if (!(cp
= *argp
++) || *cp
== '-')
290 die("missing argument to %s", argp
[-2]);
291 fcc
= addlist(fcc
, cp
);
294 if (!(cp
= *argp
++) || *cp
== '-')
295 die("missing argument to %s", argp
[-2]);
296 subject
= mh_xstrdup(cp
);
300 if (!(cp
= *argp
++) || *cp
== '-')
301 die("missing argument to %s", argp
[-2]);
302 if ((outputlinelen
= atoi(cp
)) < 10)
303 die("impossible width %d", outputlinelen
);
307 if (*cp
== '+' || *cp
== '@') {
309 die("only one folder at a time!");
310 folder
= pluspath (cp
);
312 app_msgarg(&msgs
, cp
);
316 cwd
= mh_xstrdup(pwd ());
318 if (!context_find ("path"))
319 free (path ("./", TFOLDER
));
320 if (file
&& (msgs
.size
|| folder
))
321 die("can't mix files and folders/msgs");
325 strncpy (drft
, buildsw
? m_maildir ("draft")
326 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
328 /* Check if a draft already exists */
329 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
330 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
331 for (i
= LISTDSW
; i
!= YESW
;) {
332 if (!(argp
= read_switch_multiword ("\nDisposition? ",
333 isdf
? aqrnl
: aqrl
)))
335 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
344 showfile (++argp
, drft
);
347 if (refile (++argp
, drft
) == 0)
361 anot
= false; /* don't want to annotate a file */
364 * Forwarding a message.
367 app_msgarg(&msgs
, "cur");
369 folder
= getfolder (1);
370 maildir
= m_maildir (folder
);
372 if (chdir (maildir
) == NOTOK
)
373 adios (maildir
, "unable to change directory to");
375 /* read folder and create message structure */
376 if (!(mp
= folder_read (folder
, 1)))
377 die("unable to read folder %s", folder
);
379 /* check for empty folder */
381 die("no messages in %s", folder
);
383 /* parse all the message ranges/sequences and set SELECTED */
384 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
385 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
388 seq_setprev (mp
); /* set the previous sequence */
391 * Find the first message in our set and use it as the input
392 * for the component scanner
395 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
396 if (is_selected (mp
, msgnum
)) {
397 fwdmsg
= mh_xstrdup(m_name(msgnum
));
402 die("Unable to find input message");
405 if (filter
&& access (filter
, R_OK
) == NOTOK
)
406 adios (filter
, "unable to read");
409 * Open form (component) file.
413 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
415 && (cp
= context_find (buf
))
416 && ((issue
= atoi (cp
)) < 0))
421 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
422 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
432 dat
[0] = digest
? issue
: msgnum
;
435 dat
[3] = outputlinelen
;
439 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
440 file
? file
: fwdmsg
);
442 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
443 adios (drft
, "unable to create");
446 * copy the components into the draft
448 cpydata (in
, out
, form
, drft
);
452 /* just copy the file into the draft */
453 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
454 adios (file
, "unable to open");
455 cpydata (in
, out
, file
, drft
);
460 * If filter file is defined, then format the
461 * messages into the draft using mhlproc.
464 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
466 copy_mime_draft (out
);
468 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
472 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
473 context_replace (buf
, mh_xstrdup(m_str(issue
)));
474 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
475 context_replace (buf
, mh_xstrdup(m_str(volume
)));
478 context_replace (pfolder
, folder
); /* update current folder */
479 seq_setcur (mp
, mp
->lowsel
); /* update current message */
480 seq_save (mp
); /* synchronize sequences */
481 context_save (); /* save the context file */
486 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
487 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
494 * Filter the messages you are forwarding, into the
495 * draft calling the mhlproc, and reading its output
500 mhl_draft (int out
, char *digest
, int volume
, int issue
,
501 char *file
, char *filter
, int dashstuff
)
508 struct msgs_array vec
= { 0, 0, NULL
};
510 if (pipe (pd
) == NOTOK
)
511 adios ("pipe", "unable to create");
513 argsplit_msgarg(&vec
, mhlproc
, &program
);
518 adios ("fork", "unable to");
525 app_msgarg(&vec
, "-forwall");
526 app_msgarg(&vec
, "-form");
527 app_msgarg(&vec
, filter
);
530 app_msgarg(&vec
, "-digest");
531 app_msgarg(&vec
, digest
);
532 app_msgarg(&vec
, "-issue");
533 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
534 app_msgarg(&vec
, buf1
);
535 app_msgarg(&vec
, "-volume");
536 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
537 app_msgarg(&vec
, buf2
);
541 * Are we dashstuffing (quoting) the lines that begin
542 * with `-'. We use the mhl default (don't add any flag)
543 * unless the user has specified a specific flag.
546 app_msgarg(&vec
, "-dashstuffing");
547 else if (dashstuff
< 0)
548 app_msgarg(&vec
, "-nodashstuffing");
550 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
551 if (is_selected (mp
, msgnum
))
552 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
555 app_msgarg(&vec
, NULL
);
557 execvp (program
, vec
.msgs
);
558 fprintf (stderr
, "unable to exec ");
564 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
566 pidXwait(child_id
, mhlproc
);
573 * Copy the messages into the draft. The messages are
574 * not filtered through the mhlproc. Do dashstuffing if
579 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
581 int fd
,i
, msgcnt
, msgnum
;
587 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
588 if (is_selected (mp
, msgnum
)) {
590 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
593 /* Get buffer ready to go */
595 buflen
= sizeof(buffer
);
597 strncpy (bp
, "\n-------", buflen
);
602 if (msgnum
== mp
->lowsel
) {
603 snprintf (bp
, buflen
, " Forwarded Message%s",
604 PLURALS(mp
->numsel
));
606 snprintf (bp
, buflen
, " Message %d", msgcnt
);
612 strncpy (bp
, "\n\n", buflen
);
614 if (write (out
, buffer
, strlen (buffer
)) < 0) {
615 advise (drft
, "write");
618 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
619 admonish (msgnam
, "unable to read message");
624 * Copy the message. Add RFC934 quoting (dashstuffing)
625 * unless given the -nodashstuffing flag.
628 cpydgst (fd
, out
, msgnam
, file
);
630 cpydata (fd
, out
, msgnam
, file
);
638 strncpy (buffer
, delim4
, sizeof(buffer
));
640 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
641 PLURALS(mp
->numsel
));
643 if (write (out
, buffer
, strlen (buffer
)) < 0) {
644 advise (drft
, "write");
648 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
649 digest
, volume
, issue
);
651 for (bp
= buffer
+ i
; i
> 1; i
--)
655 if (write (out
, buffer
, strlen (buffer
)) < 0) {
656 advise (drft
, "write");
663 * Create a mhn composition file for forwarding message.
667 copy_mime_draft (int out
)
672 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
673 PLURALS(mp
->numsel
), mp
->foldpath
);
674 if (write (out
, buffer
, strlen (buffer
)) < 0) {
675 advise (drft
, "write");
677 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
678 if (is_selected (mp
, msgnum
)) {
679 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
680 if (write (out
, buffer
, strlen (buffer
)) < 0) {
681 advise (drft
, "write");
684 if (write (out
, "\n", 1) < 0) {
685 advise (drft
, "write newline");