]>
diplodocus.org Git - nmh/blob - uip/forw.c
dc85853936381f6e8e52c358bbe01ed27cb5a393
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/ambigsw.h"
10 #include "sbr/pidstatus.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/arglist.h"
15 #include "sbr/error.h"
20 #include "sbr/m_maildir.h"
24 #define IFORMAT "digest-issue-%s"
25 #define VFORMAT "digest-volume-%s"
27 #define FORW_SWITCHES \
28 X("annotate", 0, ANNOSW) \
29 X("noannotate", 0, NANNOSW) \
30 X("draftfolder +folder", 0, DFOLDSW) \
31 X("draftmessage msg", 0, DMSGSW) \
32 X("nodraftfolder", 0, NDFLDSW) \
33 X("editor editor", 0, EDITRSW) \
34 X("noedit", 0, NEDITSW) \
35 X("filter filterfile", 0, FILTSW) \
36 X("form formfile", 0, FORMSW) \
37 X("format", 5, FRMTSW) \
38 X("noformat", 7, NFRMTSW) \
39 X("inplace", 0, INPLSW) \
40 X("noinplace", 0, NINPLSW) \
41 X("mime", 0, MIMESW) \
42 X("nomime", 0, NMIMESW) \
43 X("digest list", 0, DGSTSW) \
44 X("issue number", 0, ISSUESW) \
45 X("volume number", 0, VOLUMSW) \
46 X("whatnowproc program", 0, WHATSW) \
47 X("nowhatnowproc", 0, NWHATSW) \
48 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
49 X("nodashstuffing", 0, NBITSTUFFSW) \
50 X("version", 0, VERSIONSW) \
51 X("help", 0, HELPSW) \
52 X("file file", 4, FILESW) \
53 X("build", 5, BILDSW) /* interface from mhe */ \
54 X("from address", 0, FROMSW) \
55 X("to address", 0, TOSW) \
56 X("cc address", 0, CCSW) \
57 X("subject text", 0, SUBJECTSW) \
58 X("fcc mailbox", 0, FCCSW) \
59 X("width columns", 0, WIDTHSW) \
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(FORW
);
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(FORW
, switches
);
69 #define DISPO_SWITCHES \
71 X("replace", 0, YESW) \
72 X("list", 0, LISTDSW) \
73 X("refile +folder", 0, REFILSW) \
76 #define X(sw, minchars, id) id,
77 DEFINE_SWITCH_ENUM(DISPO
);
80 #define X(sw, minchars, id) { sw, minchars, id },
81 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
84 static struct swit aqrl
[] = {
86 { "replace", 0, YESW
},
87 { "list", 0, LISTDSW
},
88 { "refile +folder", 0, REFILSW
},
92 static char drft
[BUFSIZ
];
94 static char delim3
[] =
95 "\n------------------------------------------------------------\n\n";
96 static char delim4
[] = "\n------------------------------\n\n";
99 static struct msgs
*mp
= NULL
; /* used a lot */
105 static void mhl_draft (int, char *, int, int, char *, char *, int);
106 static void copy_draft (int, char *, char *, int, int, int);
107 static void copy_mime_draft (int);
111 main (int argc
, char **argv
)
116 int issue
= 0, volume
= 0, dashstuff
= 0;
120 int out
, isdf
= 0, msgnum
= 0;
121 int outputlinelen
= OUTPUTLINELEN
;
123 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
124 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
125 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
126 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
127 char *form
= NULL
, buf
[BUFSIZ
];
128 char **argp
, **arguments
;
130 struct msgs_array msgs
= { 0, 0, NULL
};
131 bool buildsw
= false;
133 if (nmh_init(argv
[0], true, true)) { return 1; }
135 arguments
= getarguments (invo_name
, argc
, argv
, 1);
138 while ((cp
= *argp
++)) {
140 switch (smatch (++cp
, switches
)) {
142 ambigsw (cp
, switches
);
145 die("-%s unknown", cp
);
148 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
150 print_help (buf
, switches
, 1);
153 print_version(invo_name
);
164 if (!(ed
= *argp
++) || *ed
== '-')
165 die("missing argument to %s", argp
[-2]);
173 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
174 die("missing argument to %s", argp
[-2]);
186 die("only one file at a time!");
187 if (!(cp
= *argp
++) || *cp
== '-')
188 die("missing argument to %s", argp
[-2]);
189 file
= path (cp
, TFILE
);
192 if (!(cp
= *argp
++) || *cp
== '-')
193 die("missing argument to %s", argp
[-2]);
194 filter
= mh_xstrdup(etcpath(cp
));
198 if (!(form
= *argp
++) || *form
== '-')
199 die("missing argument to %s", argp
[-2]);
203 filter
= mh_xstrdup(etcpath(mhlforward
));
225 if (!(cp
= *argp
++) || *cp
== '-')
226 die("missing argument to %s", argp
[-2]);
227 digest
= mh_xstrdup(cp
);
231 if (!(cp
= *argp
++) || *cp
== '-')
232 die("missing argument to %s", argp
[-2]);
233 if ((issue
= atoi (cp
)) < 1)
234 die("bad argument %s %s", argp
[-2], cp
);
237 if (!(cp
= *argp
++) || *cp
== '-')
238 die("missing argument to %s", argp
[-2]);
239 if ((volume
= atoi (cp
)) < 1)
240 die("bad argument %s %s", argp
[-2], cp
);
245 die("only one draft folder at a time!");
246 if (!(cp
= *argp
++) || *cp
== '-')
247 die("missing argument to %s", argp
[-2]);
248 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
249 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
253 die("only one draft message at a time!");
254 if (!(dmsg
= *argp
++) || *dmsg
== '-')
255 die("missing argument to %s", argp
[-2]);
263 dashstuff
= 1; /* ternary logic */
266 dashstuff
= -1; /* ternary logic */
270 if (!(cp
= *argp
++) || *cp
== '-')
271 die("missing argument to %s", argp
[-2]);
272 from
= addlist(from
, cp
);
275 if (!(cp
= *argp
++) || *cp
== '-')
276 die("missing argument to %s", argp
[-2]);
277 to
= addlist(to
, cp
);
280 if (!(cp
= *argp
++) || *cp
== '-')
281 die("missing argument to %s", argp
[-2]);
282 cc
= addlist(cc
, cp
);
285 if (!(cp
= *argp
++) || *cp
== '-')
286 die("missing argument to %s", argp
[-2]);
287 fcc
= addlist(fcc
, cp
);
290 if (!(cp
= *argp
++) || *cp
== '-')
291 die("missing argument to %s", argp
[-2]);
292 subject
= mh_xstrdup(cp
);
296 if (!(cp
= *argp
++) || *cp
== '-')
297 die("missing argument to %s", argp
[-2]);
298 if ((outputlinelen
= atoi(cp
)) < 10)
299 die("impossible width %d", outputlinelen
);
303 if (*cp
== '+' || *cp
== '@') {
305 die("only one folder at a time!");
306 folder
= pluspath (cp
);
308 app_msgarg(&msgs
, cp
);
312 cwd
= mh_xstrdup(pwd ());
314 if (!context_find ("path"))
315 free (path ("./", TFOLDER
));
316 if (file
&& (msgs
.size
|| folder
))
317 die("can't mix files and folders/msgs");
321 strncpy (drft
, buildsw
? m_maildir ("draft")
322 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
324 /* Check if a draft already exists */
325 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
326 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
327 for (i
= LISTDSW
; i
!= YESW
;) {
328 if (!(argp
= read_switch_multiword ("\nDisposition? ",
329 isdf
? aqrnl
: aqrl
)))
331 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
340 showfile (++argp
, drft
);
343 if (refile (++argp
, drft
) == 0)
357 anot
= false; /* don't want to annotate a file */
360 * Forwarding a message.
363 app_msgarg(&msgs
, "cur");
365 folder
= getfolder (1);
366 maildir
= m_maildir (folder
);
368 if (chdir (maildir
) == NOTOK
)
369 adios (maildir
, "unable to change directory to");
371 /* read folder and create message structure */
372 if (!(mp
= folder_read (folder
, 1)))
373 die("unable to read folder %s", folder
);
375 /* check for empty folder */
377 die("no messages in %s", folder
);
379 /* parse all the message ranges/sequences and set SELECTED */
380 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
381 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
384 seq_setprev (mp
); /* set the previous sequence */
387 * Find the first message in our set and use it as the input
388 * for the component scanner
391 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
392 if (is_selected (mp
, msgnum
)) {
393 fwdmsg
= mh_xstrdup(m_name(msgnum
));
398 die("Unable to find input message");
401 if (filter
&& access (filter
, R_OK
) == NOTOK
)
402 adios (filter
, "unable to read");
405 * Open form (component) file.
409 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
411 && (cp
= context_find (buf
))
412 && ((issue
= atoi (cp
)) < 0))
417 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
418 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
428 dat
[0] = digest
? issue
: msgnum
;
431 dat
[3] = outputlinelen
;
435 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
436 file
? file
: fwdmsg
);
438 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
439 adios (drft
, "unable to create");
442 * copy the components into the draft
444 cpydata (in
, out
, form
, drft
);
448 /* just copy the file into the draft */
449 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
450 adios (file
, "unable to open");
451 cpydata (in
, out
, file
, drft
);
456 * If filter file is defined, then format the
457 * messages into the draft using mhlproc.
460 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
462 copy_mime_draft (out
);
464 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
468 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
469 context_replace (buf
, mh_xstrdup(m_str(issue
)));
470 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
471 context_replace (buf
, mh_xstrdup(m_str(volume
)));
474 context_replace (pfolder
, folder
); /* update current folder */
475 seq_setcur (mp
, mp
->lowsel
); /* update current message */
476 seq_save (mp
); /* synchronize sequences */
477 context_save (); /* save the context file */
482 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
483 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
490 * Filter the messages you are forwarding, into the
491 * draft calling the mhlproc, and reading its output
496 mhl_draft (int out
, char *digest
, int volume
, int issue
,
497 char *file
, char *filter
, int dashstuff
)
504 struct msgs_array vec
= { 0, 0, NULL
};
506 if (pipe (pd
) == NOTOK
)
507 adios ("pipe", "unable to create");
509 argsplit_msgarg(&vec
, mhlproc
, &program
);
514 adios ("fork", "unable to");
521 app_msgarg(&vec
, "-forwall");
522 app_msgarg(&vec
, "-form");
523 app_msgarg(&vec
, filter
);
526 app_msgarg(&vec
, "-digest");
527 app_msgarg(&vec
, digest
);
528 app_msgarg(&vec
, "-issue");
529 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
530 app_msgarg(&vec
, buf1
);
531 app_msgarg(&vec
, "-volume");
532 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
533 app_msgarg(&vec
, buf2
);
537 * Are we dashstuffing (quoting) the lines that begin
538 * with `-'. We use the mhl default (don't add any flag)
539 * unless the user has specified a specific flag.
542 app_msgarg(&vec
, "-dashstuffing");
543 else if (dashstuff
< 0)
544 app_msgarg(&vec
, "-nodashstuffing");
546 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
547 if (is_selected (mp
, msgnum
))
548 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
551 app_msgarg(&vec
, NULL
);
553 execvp (program
, vec
.msgs
);
554 fprintf (stderr
, "unable to exec ");
560 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
562 pidXwait(child_id
, mhlproc
);
569 * Copy the messages into the draft. The messages are
570 * not filtered through the mhlproc. Do dashstuffing if
575 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
577 int fd
,i
, msgcnt
, msgnum
;
583 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
584 if (is_selected (mp
, msgnum
)) {
586 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
589 /* Get buffer ready to go */
591 buflen
= sizeof(buffer
);
593 strncpy (bp
, "\n-------", buflen
);
598 if (msgnum
== mp
->lowsel
) {
599 snprintf (bp
, buflen
, " Forwarded Message%s",
600 PLURALS(mp
->numsel
));
602 snprintf (bp
, buflen
, " Message %d", msgcnt
);
608 strncpy (bp
, "\n\n", buflen
);
610 if (write (out
, buffer
, strlen (buffer
)) < 0) {
611 advise (drft
, "write");
614 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
615 admonish (msgnam
, "unable to read message");
620 * Copy the message. Add RFC934 quoting (dashstuffing)
621 * unless given the -nodashstuffing flag.
624 cpydgst (fd
, out
, msgnam
, file
);
626 cpydata (fd
, out
, msgnam
, file
);
634 strncpy (buffer
, delim4
, sizeof(buffer
));
636 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
637 PLURALS(mp
->numsel
));
639 if (write (out
, buffer
, strlen (buffer
)) < 0) {
640 advise (drft
, "write");
644 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
645 digest
, volume
, issue
);
647 for (bp
= buffer
+ i
; i
> 1; i
--)
651 if (write (out
, buffer
, strlen (buffer
)) < 0) {
652 advise (drft
, "write");
659 * Create a mhn composition file for forwarding message.
663 copy_mime_draft (int out
)
668 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
669 PLURALS(mp
->numsel
), mp
->foldpath
);
670 if (write (out
, buffer
, strlen (buffer
)) < 0) {
671 advise (drft
, "write");
673 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
674 if (is_selected (mp
, msgnum
)) {
675 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
676 if (write (out
, buffer
, strlen (buffer
)) < 0) {
677 advise (drft
, "write");
680 if (write (out
, "\n", 1) < 0) {
681 advise (drft
, "write newline");