]>
diplodocus.org Git - nmh/blob - uip/forw.c
1e875e8283195c81c6999a6632c495b3ef63971e
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/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/arglist.h"
12 #include "sbr/error.h"
17 #include "sbr/m_maildir.h"
21 #define IFORMAT "digest-issue-%s"
22 #define VFORMAT "digest-volume-%s"
24 #define FORW_SWITCHES \
25 X("annotate", 0, ANNOSW) \
26 X("noannotate", 0, NANNOSW) \
27 X("draftfolder +folder", 0, DFOLDSW) \
28 X("draftmessage msg", 0, DMSGSW) \
29 X("nodraftfolder", 0, NDFLDSW) \
30 X("editor editor", 0, EDITRSW) \
31 X("noedit", 0, NEDITSW) \
32 X("filter filterfile", 0, FILTSW) \
33 X("form formfile", 0, FORMSW) \
34 X("format", 5, FRMTSW) \
35 X("noformat", 7, NFRMTSW) \
36 X("inplace", 0, INPLSW) \
37 X("noinplace", 0, NINPLSW) \
38 X("mime", 0, MIMESW) \
39 X("nomime", 0, NMIMESW) \
40 X("digest list", 0, DGSTSW) \
41 X("issue number", 0, ISSUESW) \
42 X("volume number", 0, VOLUMSW) \
43 X("whatnowproc program", 0, WHATSW) \
44 X("nowhatnowproc", 0, NWHATSW) \
45 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
46 X("nodashstuffing", 0, NBITSTUFFSW) \
47 X("version", 0, VERSIONSW) \
48 X("help", 0, HELPSW) \
49 X("file file", 4, FILESW) \
50 X("build", 5, BILDSW) /* interface from mhe */ \
51 X("from address", 0, FROMSW) \
52 X("to address", 0, TOSW) \
53 X("cc address", 0, CCSW) \
54 X("subject text", 0, SUBJECTSW) \
55 X("fcc mailbox", 0, FCCSW) \
56 X("width columns", 0, WIDTHSW) \
58 #define X(sw, minchars, id) id,
59 DEFINE_SWITCH_ENUM(FORW
);
62 #define X(sw, minchars, id) { sw, minchars, id },
63 DEFINE_SWITCH_ARRAY(FORW
, switches
);
66 #define DISPO_SWITCHES \
68 X("replace", 0, YESW) \
69 X("list", 0, LISTDSW) \
70 X("refile +folder", 0, REFILSW) \
73 #define X(sw, minchars, id) id,
74 DEFINE_SWITCH_ENUM(DISPO
);
77 #define X(sw, minchars, id) { sw, minchars, id },
78 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
81 static struct swit aqrl
[] = {
83 { "replace", 0, YESW
},
84 { "list", 0, LISTDSW
},
85 { "refile +folder", 0, REFILSW
},
89 static char drft
[BUFSIZ
];
91 static char delim3
[] =
92 "\n------------------------------------------------------------\n\n";
93 static char delim4
[] = "\n------------------------------\n\n";
96 static struct msgs
*mp
= NULL
; /* used a lot */
102 static void mhl_draft (int, char *, int, int, char *, char *, int);
103 static void copy_draft (int, char *, char *, int, int, int);
104 static void copy_mime_draft (int);
108 main (int argc
, char **argv
)
113 int issue
= 0, volume
= 0, dashstuff
= 0;
117 int out
, isdf
= 0, msgnum
= 0;
118 int outputlinelen
= OUTPUTLINELEN
;
120 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
121 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
122 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
123 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
124 char *form
= NULL
, buf
[BUFSIZ
];
125 char **argp
, **arguments
;
127 struct msgs_array msgs
= { 0, 0, NULL
};
128 bool buildsw
= false;
130 if (nmh_init(argv
[0], true, true)) { return 1; }
132 arguments
= getarguments (invo_name
, argc
, argv
, 1);
135 while ((cp
= *argp
++)) {
137 switch (smatch (++cp
, switches
)) {
139 ambigsw (cp
, switches
);
142 die("-%s unknown", cp
);
145 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
147 print_help (buf
, switches
, 1);
150 print_version(invo_name
);
161 if (!(ed
= *argp
++) || *ed
== '-')
162 die("missing argument to %s", argp
[-2]);
170 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
171 die("missing argument to %s", argp
[-2]);
183 die("only one file at a time!");
184 if (!(cp
= *argp
++) || *cp
== '-')
185 die("missing argument to %s", argp
[-2]);
186 file
= path (cp
, TFILE
);
189 if (!(cp
= *argp
++) || *cp
== '-')
190 die("missing argument to %s", argp
[-2]);
191 filter
= mh_xstrdup(etcpath(cp
));
195 if (!(form
= *argp
++) || *form
== '-')
196 die("missing argument to %s", argp
[-2]);
200 filter
= mh_xstrdup(etcpath(mhlforward
));
222 if (!(cp
= *argp
++) || *cp
== '-')
223 die("missing argument to %s", argp
[-2]);
224 digest
= mh_xstrdup(cp
);
228 if (!(cp
= *argp
++) || *cp
== '-')
229 die("missing argument to %s", argp
[-2]);
230 if ((issue
= atoi (cp
)) < 1)
231 die("bad argument %s %s", argp
[-2], cp
);
234 if (!(cp
= *argp
++) || *cp
== '-')
235 die("missing argument to %s", argp
[-2]);
236 if ((volume
= atoi (cp
)) < 1)
237 die("bad argument %s %s", argp
[-2], cp
);
242 die("only one draft folder at a time!");
243 if (!(cp
= *argp
++) || *cp
== '-')
244 die("missing argument to %s", argp
[-2]);
245 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
246 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
250 die("only one draft message at a time!");
251 if (!(dmsg
= *argp
++) || *dmsg
== '-')
252 die("missing argument to %s", argp
[-2]);
260 dashstuff
= 1; /* ternary logic */
263 dashstuff
= -1; /* ternary logic */
267 if (!(cp
= *argp
++) || *cp
== '-')
268 die("missing argument to %s", argp
[-2]);
269 from
= addlist(from
, cp
);
272 if (!(cp
= *argp
++) || *cp
== '-')
273 die("missing argument to %s", argp
[-2]);
274 to
= addlist(to
, cp
);
277 if (!(cp
= *argp
++) || *cp
== '-')
278 die("missing argument to %s", argp
[-2]);
279 cc
= addlist(cc
, cp
);
282 if (!(cp
= *argp
++) || *cp
== '-')
283 die("missing argument to %s", argp
[-2]);
284 fcc
= addlist(fcc
, cp
);
287 if (!(cp
= *argp
++) || *cp
== '-')
288 die("missing argument to %s", argp
[-2]);
289 subject
= mh_xstrdup(cp
);
293 if (!(cp
= *argp
++) || *cp
== '-')
294 die("missing argument to %s", argp
[-2]);
295 if ((outputlinelen
= atoi(cp
)) < 10)
296 die("impossible width %d", outputlinelen
);
300 if (*cp
== '+' || *cp
== '@') {
302 die("only one folder at a time!");
303 folder
= pluspath (cp
);
305 app_msgarg(&msgs
, cp
);
309 cwd
= mh_xstrdup(pwd ());
311 if (!context_find ("path"))
312 free (path ("./", TFOLDER
));
313 if (file
&& (msgs
.size
|| folder
))
314 die("can't mix files and folders/msgs");
318 strncpy (drft
, buildsw
? m_maildir ("draft")
319 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
321 /* Check if a draft already exists */
322 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
323 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
324 for (i
= LISTDSW
; i
!= YESW
;) {
325 if (!(argp
= read_switch_multiword ("\nDisposition? ",
326 isdf
? aqrnl
: aqrl
)))
328 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
337 showfile (++argp
, drft
);
340 if (refile (++argp
, drft
) == 0)
354 anot
= false; /* don't want to annotate a file */
357 * Forwarding a message.
360 app_msgarg(&msgs
, "cur");
362 folder
= getfolder (1);
363 maildir
= m_maildir (folder
);
365 if (chdir (maildir
) == NOTOK
)
366 adios (maildir
, "unable to change directory to");
368 /* read folder and create message structure */
369 if (!(mp
= folder_read (folder
, 1)))
370 die("unable to read folder %s", folder
);
372 /* check for empty folder */
374 die("no messages in %s", folder
);
376 /* parse all the message ranges/sequences and set SELECTED */
377 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
378 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
381 seq_setprev (mp
); /* set the previous sequence */
384 * Find the first message in our set and use it as the input
385 * for the component scanner
388 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
389 if (is_selected (mp
, msgnum
)) {
390 fwdmsg
= mh_xstrdup(m_name(msgnum
));
395 die("Unable to find input message");
398 if (filter
&& access (filter
, R_OK
) == NOTOK
)
399 adios (filter
, "unable to read");
402 * Open form (component) file.
406 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
408 && (cp
= context_find (buf
))
409 && ((issue
= atoi (cp
)) < 0))
414 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
415 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
425 dat
[0] = digest
? issue
: msgnum
;
428 dat
[3] = outputlinelen
;
432 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
433 file
? file
: fwdmsg
);
435 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
436 adios (drft
, "unable to create");
439 * copy the components into the draft
441 cpydata (in
, out
, form
, drft
);
445 /* just copy the file into the draft */
446 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
447 adios (file
, "unable to open");
448 cpydata (in
, out
, file
, drft
);
453 * If filter file is defined, then format the
454 * messages into the draft using mhlproc.
457 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
459 copy_mime_draft (out
);
461 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
465 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
466 context_replace (buf
, mh_xstrdup(m_str(issue
)));
467 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
468 context_replace (buf
, mh_xstrdup(m_str(volume
)));
471 context_replace (pfolder
, folder
); /* update current folder */
472 seq_setcur (mp
, mp
->lowsel
); /* update current message */
473 seq_save (mp
); /* synchronize sequences */
474 context_save (); /* save the context file */
479 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
480 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
487 * Filter the messages you are forwarding, into the
488 * draft calling the mhlproc, and reading its output
493 mhl_draft (int out
, char *digest
, int volume
, int issue
,
494 char *file
, char *filter
, int dashstuff
)
501 struct msgs_array vec
= { 0, 0, NULL
};
503 if (pipe (pd
) == NOTOK
)
504 adios ("pipe", "unable to create");
506 argsplit_msgarg(&vec
, mhlproc
, &program
);
511 adios ("fork", "unable to");
518 app_msgarg(&vec
, "-forwall");
519 app_msgarg(&vec
, "-form");
520 app_msgarg(&vec
, filter
);
523 app_msgarg(&vec
, "-digest");
524 app_msgarg(&vec
, digest
);
525 app_msgarg(&vec
, "-issue");
526 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
527 app_msgarg(&vec
, buf1
);
528 app_msgarg(&vec
, "-volume");
529 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
530 app_msgarg(&vec
, buf2
);
534 * Are we dashstuffing (quoting) the lines that begin
535 * with `-'. We use the mhl default (don't add any flag)
536 * unless the user has specified a specific flag.
539 app_msgarg(&vec
, "-dashstuffing");
540 else if (dashstuff
< 0)
541 app_msgarg(&vec
, "-nodashstuffing");
543 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
544 if (is_selected (mp
, msgnum
))
545 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
548 app_msgarg(&vec
, NULL
);
550 execvp (program
, vec
.msgs
);
551 fprintf (stderr
, "unable to exec ");
557 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
559 pidXwait(child_id
, mhlproc
);
566 * Copy the messages into the draft. The messages are
567 * not filtered through the mhlproc. Do dashstuffing if
572 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
574 int fd
,i
, msgcnt
, msgnum
;
580 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
581 if (is_selected (mp
, msgnum
)) {
583 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
586 /* Get buffer ready to go */
588 buflen
= sizeof(buffer
);
590 strncpy (bp
, "\n-------", buflen
);
595 if (msgnum
== mp
->lowsel
) {
596 snprintf (bp
, buflen
, " Forwarded Message%s",
597 PLURALS(mp
->numsel
));
599 snprintf (bp
, buflen
, " Message %d", msgcnt
);
605 strncpy (bp
, "\n\n", buflen
);
607 if (write (out
, buffer
, strlen (buffer
)) < 0) {
608 advise (drft
, "write");
611 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
612 admonish (msgnam
, "unable to read message");
617 * Copy the message. Add RFC934 quoting (dashstuffing)
618 * unless given the -nodashstuffing flag.
621 cpydgst (fd
, out
, msgnam
, file
);
623 cpydata (fd
, out
, msgnam
, file
);
631 strncpy (buffer
, delim4
, sizeof(buffer
));
633 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
634 PLURALS(mp
->numsel
));
636 if (write (out
, buffer
, strlen (buffer
)) < 0) {
637 advise (drft
, "write");
641 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
642 digest
, volume
, issue
);
644 for (bp
= buffer
+ i
; i
> 1; i
--)
648 if (write (out
, buffer
, strlen (buffer
)) < 0) {
649 advise (drft
, "write");
656 * Create a mhn composition file for forwarding message.
660 copy_mime_draft (int out
)
665 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
666 PLURALS(mp
->numsel
), mp
->foldpath
);
667 if (write (out
, buffer
, strlen (buffer
)) < 0) {
668 advise (drft
, "write");
670 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
671 if (is_selected (mp
, msgnum
)) {
672 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
673 if (write (out
, buffer
, strlen (buffer
)) < 0) {
674 advise (drft
, "write");
677 if (write (out
, "\n", 1) < 0) {
678 advise (drft
, "write newline");