]>
diplodocus.org Git - nmh/blob - uip/forw.c
9cb068fca0ea4ff1a28e92038dca2702ce9df96a
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/getfolder.h"
10 #include "sbr/folder_read.h"
11 #include "sbr/context_save.h"
12 #include "sbr/context_replace.h"
13 #include "sbr/context_find.h"
14 #include "sbr/ambigsw.h"
15 #include "sbr/pidstatus.h"
17 #include "sbr/print_version.h"
18 #include "sbr/print_help.h"
19 #include "sbr/arglist.h"
20 #include "sbr/error.h"
25 #include "sbr/m_maildir.h"
29 #define IFORMAT "digest-issue-%s"
30 #define VFORMAT "digest-volume-%s"
32 #define FORW_SWITCHES \
33 X("annotate", 0, ANNOSW) \
34 X("noannotate", 0, NANNOSW) \
35 X("draftfolder +folder", 0, DFOLDSW) \
36 X("draftmessage msg", 0, DMSGSW) \
37 X("nodraftfolder", 0, NDFLDSW) \
38 X("editor editor", 0, EDITRSW) \
39 X("noedit", 0, NEDITSW) \
40 X("filter filterfile", 0, FILTSW) \
41 X("form formfile", 0, FORMSW) \
42 X("format", 5, FRMTSW) \
43 X("noformat", 7, NFRMTSW) \
44 X("inplace", 0, INPLSW) \
45 X("noinplace", 0, NINPLSW) \
46 X("mime", 0, MIMESW) \
47 X("nomime", 0, NMIMESW) \
48 X("digest list", 0, DGSTSW) \
49 X("issue number", 0, ISSUESW) \
50 X("volume number", 0, VOLUMSW) \
51 X("whatnowproc program", 0, WHATSW) \
52 X("nowhatnowproc", 0, NWHATSW) \
53 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
54 X("nodashstuffing", 0, NBITSTUFFSW) \
55 X("version", 0, VERSIONSW) \
56 X("help", 0, HELPSW) \
57 X("file file", 4, FILESW) \
58 X("build", 5, BILDSW) /* interface from mhe */ \
59 X("from address", 0, FROMSW) \
60 X("to address", 0, TOSW) \
61 X("cc address", 0, CCSW) \
62 X("subject text", 0, SUBJECTSW) \
63 X("fcc mailbox", 0, FCCSW) \
64 X("width columns", 0, WIDTHSW) \
66 #define X(sw, minchars, id) id,
67 DEFINE_SWITCH_ENUM(FORW
);
70 #define X(sw, minchars, id) { sw, minchars, id },
71 DEFINE_SWITCH_ARRAY(FORW
, switches
);
74 #define DISPO_SWITCHES \
76 X("replace", 0, YESW) \
77 X("list", 0, LISTDSW) \
78 X("refile +folder", 0, REFILSW) \
81 #define X(sw, minchars, id) id,
82 DEFINE_SWITCH_ENUM(DISPO
);
85 #define X(sw, minchars, id) { sw, minchars, id },
86 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
89 static struct swit aqrl
[] = {
91 { "replace", 0, YESW
},
92 { "list", 0, LISTDSW
},
93 { "refile +folder", 0, REFILSW
},
97 static char drft
[BUFSIZ
];
99 static char delim3
[] =
100 "\n------------------------------------------------------------\n\n";
101 static char delim4
[] = "\n------------------------------\n\n";
104 static struct msgs
*mp
= NULL
; /* used a lot */
110 static void mhl_draft (int, char *, int, int, char *, char *, int);
111 static void copy_draft (int, char *, char *, int, int, int);
112 static void copy_mime_draft (int);
116 main (int argc
, char **argv
)
121 int issue
= 0, volume
= 0, dashstuff
= 0;
125 int out
, isdf
= 0, msgnum
= 0;
126 int outputlinelen
= OUTPUTLINELEN
;
128 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
129 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
130 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
131 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
132 char *form
= NULL
, buf
[BUFSIZ
];
133 char **argp
, **arguments
;
135 struct msgs_array msgs
= { 0, 0, NULL
};
136 bool buildsw
= false;
138 if (nmh_init(argv
[0], true, true)) { return 1; }
140 arguments
= getarguments (invo_name
, argc
, argv
, 1);
143 while ((cp
= *argp
++)) {
145 switch (smatch (++cp
, switches
)) {
147 ambigsw (cp
, switches
);
150 die("-%s unknown", cp
);
153 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
155 print_help (buf
, switches
, 1);
158 print_version(invo_name
);
169 if (!(ed
= *argp
++) || *ed
== '-')
170 die("missing argument to %s", argp
[-2]);
178 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
179 die("missing argument to %s", argp
[-2]);
191 die("only one file at a time!");
192 if (!(cp
= *argp
++) || *cp
== '-')
193 die("missing argument to %s", argp
[-2]);
194 file
= path (cp
, TFILE
);
197 if (!(cp
= *argp
++) || *cp
== '-')
198 die("missing argument to %s", argp
[-2]);
199 filter
= mh_xstrdup(etcpath(cp
));
203 if (!(form
= *argp
++) || *form
== '-')
204 die("missing argument to %s", argp
[-2]);
208 filter
= mh_xstrdup(etcpath(mhlforward
));
230 if (!(cp
= *argp
++) || *cp
== '-')
231 die("missing argument to %s", argp
[-2]);
232 digest
= mh_xstrdup(cp
);
236 if (!(cp
= *argp
++) || *cp
== '-')
237 die("missing argument to %s", argp
[-2]);
238 if ((issue
= atoi (cp
)) < 1)
239 die("bad argument %s %s", argp
[-2], cp
);
242 if (!(cp
= *argp
++) || *cp
== '-')
243 die("missing argument to %s", argp
[-2]);
244 if ((volume
= atoi (cp
)) < 1)
245 die("bad argument %s %s", argp
[-2], cp
);
250 die("only one draft folder at a time!");
251 if (!(cp
= *argp
++) || *cp
== '-')
252 die("missing argument to %s", argp
[-2]);
253 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
254 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
258 die("only one draft message at a time!");
259 if (!(dmsg
= *argp
++) || *dmsg
== '-')
260 die("missing argument to %s", argp
[-2]);
268 dashstuff
= 1; /* ternary logic */
271 dashstuff
= -1; /* ternary logic */
275 if (!(cp
= *argp
++) || *cp
== '-')
276 die("missing argument to %s", argp
[-2]);
277 from
= addlist(from
, cp
);
280 if (!(cp
= *argp
++) || *cp
== '-')
281 die("missing argument to %s", argp
[-2]);
282 to
= addlist(to
, cp
);
285 if (!(cp
= *argp
++) || *cp
== '-')
286 die("missing argument to %s", argp
[-2]);
287 cc
= addlist(cc
, cp
);
290 if (!(cp
= *argp
++) || *cp
== '-')
291 die("missing argument to %s", argp
[-2]);
292 fcc
= addlist(fcc
, cp
);
295 if (!(cp
= *argp
++) || *cp
== '-')
296 die("missing argument to %s", argp
[-2]);
297 subject
= mh_xstrdup(cp
);
301 if (!(cp
= *argp
++) || *cp
== '-')
302 die("missing argument to %s", argp
[-2]);
303 if ((outputlinelen
= atoi(cp
)) < 10)
304 die("impossible width %d", outputlinelen
);
308 if (*cp
== '+' || *cp
== '@') {
310 die("only one folder at a time!");
311 folder
= pluspath (cp
);
313 app_msgarg(&msgs
, cp
);
317 cwd
= mh_xstrdup(pwd ());
319 if (!context_find ("path"))
320 free (path ("./", TFOLDER
));
321 if (file
&& (msgs
.size
|| folder
))
322 die("can't mix files and folders/msgs");
326 strncpy (drft
, buildsw
? m_maildir ("draft")
327 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
329 /* Check if a draft already exists */
330 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
331 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
332 for (i
= LISTDSW
; i
!= YESW
;) {
333 if (!(argp
= read_switch_multiword ("\nDisposition? ",
334 isdf
? aqrnl
: aqrl
)))
336 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
345 showfile (++argp
, drft
);
348 if (refile (++argp
, drft
) == 0)
362 anot
= false; /* don't want to annotate a file */
365 * Forwarding a message.
368 app_msgarg(&msgs
, "cur");
370 folder
= getfolder (1);
371 maildir
= m_maildir (folder
);
373 if (chdir (maildir
) == NOTOK
)
374 adios (maildir
, "unable to change directory to");
376 /* read folder and create message structure */
377 if (!(mp
= folder_read (folder
, 1)))
378 die("unable to read folder %s", folder
);
380 /* check for empty folder */
382 die("no messages in %s", folder
);
384 /* parse all the message ranges/sequences and set SELECTED */
385 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
386 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
389 seq_setprev (mp
); /* set the previous sequence */
392 * Find the first message in our set and use it as the input
393 * for the component scanner
396 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
397 if (is_selected (mp
, msgnum
)) {
398 fwdmsg
= mh_xstrdup(m_name(msgnum
));
403 die("Unable to find input message");
406 if (filter
&& access (filter
, R_OK
) == NOTOK
)
407 adios (filter
, "unable to read");
410 * Open form (component) file.
414 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
416 && (cp
= context_find (buf
))
417 && ((issue
= atoi (cp
)) < 0))
422 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
423 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
433 dat
[0] = digest
? issue
: msgnum
;
436 dat
[3] = outputlinelen
;
440 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
441 file
? file
: fwdmsg
);
443 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
444 adios (drft
, "unable to create");
447 * copy the components into the draft
449 cpydata (in
, out
, form
, drft
);
453 /* just copy the file into the draft */
454 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
455 adios (file
, "unable to open");
456 cpydata (in
, out
, file
, drft
);
461 * If filter file is defined, then format the
462 * messages into the draft using mhlproc.
465 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
467 copy_mime_draft (out
);
469 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
473 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
474 context_replace (buf
, mh_xstrdup(m_str(issue
)));
475 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
476 context_replace (buf
, mh_xstrdup(m_str(volume
)));
479 context_replace (pfolder
, folder
); /* update current folder */
480 seq_setcur (mp
, mp
->lowsel
); /* update current message */
481 seq_save (mp
); /* synchronize sequences */
482 context_save (); /* save the context file */
487 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
488 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
495 * Filter the messages you are forwarding, into the
496 * draft calling the mhlproc, and reading its output
501 mhl_draft (int out
, char *digest
, int volume
, int issue
,
502 char *file
, char *filter
, int dashstuff
)
509 struct msgs_array vec
= { 0, 0, NULL
};
511 if (pipe (pd
) == NOTOK
)
512 adios ("pipe", "unable to create");
514 argsplit_msgarg(&vec
, mhlproc
, &program
);
519 adios ("fork", "unable to");
526 app_msgarg(&vec
, "-forwall");
527 app_msgarg(&vec
, "-form");
528 app_msgarg(&vec
, filter
);
531 app_msgarg(&vec
, "-digest");
532 app_msgarg(&vec
, digest
);
533 app_msgarg(&vec
, "-issue");
534 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
535 app_msgarg(&vec
, buf1
);
536 app_msgarg(&vec
, "-volume");
537 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
538 app_msgarg(&vec
, buf2
);
542 * Are we dashstuffing (quoting) the lines that begin
543 * with `-'. We use the mhl default (don't add any flag)
544 * unless the user has specified a specific flag.
547 app_msgarg(&vec
, "-dashstuffing");
548 else if (dashstuff
< 0)
549 app_msgarg(&vec
, "-nodashstuffing");
551 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
552 if (is_selected (mp
, msgnum
))
553 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
556 app_msgarg(&vec
, NULL
);
558 execvp (program
, vec
.msgs
);
559 fprintf (stderr
, "unable to exec ");
565 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
567 pidXwait(child_id
, mhlproc
);
574 * Copy the messages into the draft. The messages are
575 * not filtered through the mhlproc. Do dashstuffing if
580 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
582 int fd
,i
, msgcnt
, msgnum
;
588 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
589 if (is_selected (mp
, msgnum
)) {
591 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
594 /* Get buffer ready to go */
596 buflen
= sizeof(buffer
);
598 strncpy (bp
, "\n-------", buflen
);
603 if (msgnum
== mp
->lowsel
) {
604 snprintf (bp
, buflen
, " Forwarded Message%s",
605 PLURALS(mp
->numsel
));
607 snprintf (bp
, buflen
, " Message %d", msgcnt
);
613 strncpy (bp
, "\n\n", buflen
);
615 if (write (out
, buffer
, strlen (buffer
)) < 0) {
616 advise (drft
, "write");
619 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
620 admonish (msgnam
, "unable to read message");
625 * Copy the message. Add RFC934 quoting (dashstuffing)
626 * unless given the -nodashstuffing flag.
629 cpydgst (fd
, out
, msgnam
, file
);
631 cpydata (fd
, out
, msgnam
, file
);
639 strncpy (buffer
, delim4
, sizeof(buffer
));
641 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
642 PLURALS(mp
->numsel
));
644 if (write (out
, buffer
, strlen (buffer
)) < 0) {
645 advise (drft
, "write");
649 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
650 digest
, volume
, issue
);
652 for (bp
= buffer
+ i
; i
> 1; i
--)
656 if (write (out
, buffer
, strlen (buffer
)) < 0) {
657 advise (drft
, "write");
664 * Create a mhn composition file for forwarding message.
668 copy_mime_draft (int out
)
673 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
674 PLURALS(mp
->numsel
), mp
->foldpath
);
675 if (write (out
, buffer
, strlen (buffer
)) < 0) {
676 advise (drft
, "write");
678 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
679 if (is_selected (mp
, msgnum
)) {
680 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
681 if (write (out
, buffer
, strlen (buffer
)) < 0) {
682 advise (drft
, "write");
685 if (write (out
, "\n", 1) < 0) {
686 advise (drft
, "write newline");