]>
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/seq_setprev.h"
10 #include "sbr/seq_setcur.h"
11 #include "sbr/seq_save.h"
12 #include "sbr/showfile.h"
13 #include "sbr/smatch.h"
14 #include "sbr/refile.h"
15 #include "sbr/cpydata.h"
16 #include "sbr/cpydgst.h"
17 #include "sbr/m_draft.h"
18 #include "sbr/m_convert.h"
19 #include "sbr/getfolder.h"
20 #include "sbr/folder_read.h"
21 #include "sbr/context_save.h"
22 #include "sbr/context_replace.h"
23 #include "sbr/context_find.h"
24 #include "sbr/ambigsw.h"
25 #include "sbr/pidstatus.h"
27 #include "sbr/print_version.h"
28 #include "sbr/print_help.h"
29 #include "sbr/arglist.h"
30 #include "sbr/error.h"
35 #include "sbr/m_maildir.h"
39 #define IFORMAT "digest-issue-%s"
40 #define VFORMAT "digest-volume-%s"
42 #define FORW_SWITCHES \
43 X("annotate", 0, ANNOSW) \
44 X("noannotate", 0, NANNOSW) \
45 X("draftfolder +folder", 0, DFOLDSW) \
46 X("draftmessage msg", 0, DMSGSW) \
47 X("nodraftfolder", 0, NDFLDSW) \
48 X("editor editor", 0, EDITRSW) \
49 X("noedit", 0, NEDITSW) \
50 X("filter filterfile", 0, FILTSW) \
51 X("form formfile", 0, FORMSW) \
52 X("format", 5, FRMTSW) \
53 X("noformat", 7, NFRMTSW) \
54 X("inplace", 0, INPLSW) \
55 X("noinplace", 0, NINPLSW) \
56 X("mime", 0, MIMESW) \
57 X("nomime", 0, NMIMESW) \
58 X("digest list", 0, DGSTSW) \
59 X("issue number", 0, ISSUESW) \
60 X("volume number", 0, VOLUMSW) \
61 X("whatnowproc program", 0, WHATSW) \
62 X("nowhatnowproc", 0, NWHATSW) \
63 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
64 X("nodashstuffing", 0, NBITSTUFFSW) \
65 X("version", 0, VERSIONSW) \
66 X("help", 0, HELPSW) \
67 X("file file", 4, FILESW) \
68 X("build", 5, BILDSW) /* interface from mhe */ \
69 X("from address", 0, FROMSW) \
70 X("to address", 0, TOSW) \
71 X("cc address", 0, CCSW) \
72 X("subject text", 0, SUBJECTSW) \
73 X("fcc mailbox", 0, FCCSW) \
74 X("width columns", 0, WIDTHSW) \
76 #define X(sw, minchars, id) id,
77 DEFINE_SWITCH_ENUM(FORW
);
80 #define X(sw, minchars, id) { sw, minchars, id },
81 DEFINE_SWITCH_ARRAY(FORW
, switches
);
84 #define DISPO_SWITCHES \
86 X("replace", 0, YESW) \
87 X("list", 0, LISTDSW) \
88 X("refile +folder", 0, REFILSW) \
91 #define X(sw, minchars, id) id,
92 DEFINE_SWITCH_ENUM(DISPO
);
95 #define X(sw, minchars, id) { sw, minchars, id },
96 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
99 static struct swit aqrl
[] = {
101 { "replace", 0, YESW
},
102 { "list", 0, LISTDSW
},
103 { "refile +folder", 0, REFILSW
},
107 static char drft
[BUFSIZ
];
109 static char delim3
[] =
110 "\n------------------------------------------------------------\n\n";
111 static char delim4
[] = "\n------------------------------\n\n";
114 static struct msgs
*mp
= NULL
; /* used a lot */
120 static void mhl_draft (int, char *, int, int, char *, char *, int);
121 static void copy_draft (int, char *, char *, int, int, int);
122 static void copy_mime_draft (int);
126 main (int argc
, char **argv
)
131 int issue
= 0, volume
= 0, dashstuff
= 0;
135 int out
, isdf
= 0, msgnum
= 0;
136 int outputlinelen
= OUTPUTLINELEN
;
138 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
139 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
140 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
141 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
142 char *form
= NULL
, buf
[BUFSIZ
];
143 char **argp
, **arguments
;
145 struct msgs_array msgs
= { 0, 0, NULL
};
146 bool buildsw
= false;
148 if (nmh_init(argv
[0], true, true)) { return 1; }
150 arguments
= getarguments (invo_name
, argc
, argv
, 1);
153 while ((cp
= *argp
++)) {
155 switch (smatch (++cp
, switches
)) {
157 ambigsw (cp
, switches
);
160 die("-%s unknown", cp
);
163 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
165 print_help (buf
, switches
, 1);
168 print_version(invo_name
);
179 if (!(ed
= *argp
++) || *ed
== '-')
180 die("missing argument to %s", argp
[-2]);
188 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
189 die("missing argument to %s", argp
[-2]);
201 die("only one file at a time!");
202 if (!(cp
= *argp
++) || *cp
== '-')
203 die("missing argument to %s", argp
[-2]);
204 file
= path (cp
, TFILE
);
207 if (!(cp
= *argp
++) || *cp
== '-')
208 die("missing argument to %s", argp
[-2]);
209 filter
= mh_xstrdup(etcpath(cp
));
213 if (!(form
= *argp
++) || *form
== '-')
214 die("missing argument to %s", argp
[-2]);
218 filter
= mh_xstrdup(etcpath(mhlforward
));
240 if (!(cp
= *argp
++) || *cp
== '-')
241 die("missing argument to %s", argp
[-2]);
242 digest
= mh_xstrdup(cp
);
246 if (!(cp
= *argp
++) || *cp
== '-')
247 die("missing argument to %s", argp
[-2]);
248 if ((issue
= atoi (cp
)) < 1)
249 die("bad argument %s %s", argp
[-2], cp
);
252 if (!(cp
= *argp
++) || *cp
== '-')
253 die("missing argument to %s", argp
[-2]);
254 if ((volume
= atoi (cp
)) < 1)
255 die("bad argument %s %s", argp
[-2], cp
);
260 die("only one draft folder at a time!");
261 if (!(cp
= *argp
++) || *cp
== '-')
262 die("missing argument to %s", argp
[-2]);
263 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
264 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
268 die("only one draft message at a time!");
269 if (!(dmsg
= *argp
++) || *dmsg
== '-')
270 die("missing argument to %s", argp
[-2]);
278 dashstuff
= 1; /* ternary logic */
281 dashstuff
= -1; /* ternary logic */
285 if (!(cp
= *argp
++) || *cp
== '-')
286 die("missing argument to %s", argp
[-2]);
287 from
= addlist(from
, cp
);
290 if (!(cp
= *argp
++) || *cp
== '-')
291 die("missing argument to %s", argp
[-2]);
292 to
= addlist(to
, cp
);
295 if (!(cp
= *argp
++) || *cp
== '-')
296 die("missing argument to %s", argp
[-2]);
297 cc
= addlist(cc
, cp
);
300 if (!(cp
= *argp
++) || *cp
== '-')
301 die("missing argument to %s", argp
[-2]);
302 fcc
= addlist(fcc
, cp
);
305 if (!(cp
= *argp
++) || *cp
== '-')
306 die("missing argument to %s", argp
[-2]);
307 subject
= mh_xstrdup(cp
);
311 if (!(cp
= *argp
++) || *cp
== '-')
312 die("missing argument to %s", argp
[-2]);
313 if ((outputlinelen
= atoi(cp
)) < 10)
314 die("impossible width %d", outputlinelen
);
318 if (*cp
== '+' || *cp
== '@') {
320 die("only one folder at a time!");
321 folder
= pluspath (cp
);
323 app_msgarg(&msgs
, cp
);
327 cwd
= mh_xstrdup(pwd ());
329 if (!context_find ("path"))
330 free (path ("./", TFOLDER
));
331 if (file
&& (msgs
.size
|| folder
))
332 die("can't mix files and folders/msgs");
336 strncpy (drft
, buildsw
? m_maildir ("draft")
337 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
339 /* Check if a draft already exists */
340 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
341 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
342 for (i
= LISTDSW
; i
!= YESW
;) {
343 if (!(argp
= read_switch_multiword ("\nDisposition? ",
344 isdf
? aqrnl
: aqrl
)))
346 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
355 showfile (++argp
, drft
);
358 if (refile (++argp
, drft
) == 0)
372 anot
= false; /* don't want to annotate a file */
375 * Forwarding a message.
378 app_msgarg(&msgs
, "cur");
380 folder
= getfolder (1);
381 maildir
= m_maildir (folder
);
383 if (chdir (maildir
) == NOTOK
)
384 adios (maildir
, "unable to change directory to");
386 /* read folder and create message structure */
387 if (!(mp
= folder_read (folder
, 1)))
388 die("unable to read folder %s", folder
);
390 /* check for empty folder */
392 die("no messages in %s", folder
);
394 /* parse all the message ranges/sequences and set SELECTED */
395 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
396 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
399 seq_setprev (mp
); /* set the previous sequence */
402 * Find the first message in our set and use it as the input
403 * for the component scanner
406 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
407 if (is_selected (mp
, msgnum
)) {
408 fwdmsg
= mh_xstrdup(m_name(msgnum
));
413 die("Unable to find input message");
416 if (filter
&& access (filter
, R_OK
) == NOTOK
)
417 adios (filter
, "unable to read");
420 * Open form (component) file.
424 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
426 && (cp
= context_find (buf
))
427 && ((issue
= atoi (cp
)) < 0))
432 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
433 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
443 dat
[0] = digest
? issue
: msgnum
;
446 dat
[3] = outputlinelen
;
450 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
451 file
? file
: fwdmsg
);
453 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
454 adios (drft
, "unable to create");
457 * copy the components into the draft
459 cpydata (in
, out
, form
, drft
);
463 /* just copy the file into the draft */
464 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
465 adios (file
, "unable to open");
466 cpydata (in
, out
, file
, drft
);
471 * If filter file is defined, then format the
472 * messages into the draft using mhlproc.
475 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
477 copy_mime_draft (out
);
479 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
483 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
484 context_replace (buf
, mh_xstrdup(m_str(issue
)));
485 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
486 context_replace (buf
, mh_xstrdup(m_str(volume
)));
489 context_replace (pfolder
, folder
); /* update current folder */
490 seq_setcur (mp
, mp
->lowsel
); /* update current message */
491 seq_save (mp
); /* synchronize sequences */
492 context_save (); /* save the context file */
497 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
498 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
505 * Filter the messages you are forwarding, into the
506 * draft calling the mhlproc, and reading its output
511 mhl_draft (int out
, char *digest
, int volume
, int issue
,
512 char *file
, char *filter
, int dashstuff
)
519 struct msgs_array vec
= { 0, 0, NULL
};
521 if (pipe (pd
) == NOTOK
)
522 adios ("pipe", "unable to create");
524 argsplit_msgarg(&vec
, mhlproc
, &program
);
529 adios ("fork", "unable to");
536 app_msgarg(&vec
, "-forwall");
537 app_msgarg(&vec
, "-form");
538 app_msgarg(&vec
, filter
);
541 app_msgarg(&vec
, "-digest");
542 app_msgarg(&vec
, digest
);
543 app_msgarg(&vec
, "-issue");
544 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
545 app_msgarg(&vec
, buf1
);
546 app_msgarg(&vec
, "-volume");
547 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
548 app_msgarg(&vec
, buf2
);
552 * Are we dashstuffing (quoting) the lines that begin
553 * with `-'. We use the mhl default (don't add any flag)
554 * unless the user has specified a specific flag.
557 app_msgarg(&vec
, "-dashstuffing");
558 else if (dashstuff
< 0)
559 app_msgarg(&vec
, "-nodashstuffing");
561 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
562 if (is_selected (mp
, msgnum
))
563 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
566 app_msgarg(&vec
, NULL
);
568 execvp (program
, vec
.msgs
);
569 fprintf (stderr
, "unable to exec ");
575 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
577 pidXwait(child_id
, mhlproc
);
584 * Copy the messages into the draft. The messages are
585 * not filtered through the mhlproc. Do dashstuffing if
590 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
592 int fd
,i
, msgcnt
, msgnum
;
598 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
599 if (is_selected (mp
, msgnum
)) {
601 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
604 /* Get buffer ready to go */
606 buflen
= sizeof(buffer
);
608 strncpy (bp
, "\n-------", buflen
);
613 if (msgnum
== mp
->lowsel
) {
614 snprintf (bp
, buflen
, " Forwarded Message%s",
615 PLURALS(mp
->numsel
));
617 snprintf (bp
, buflen
, " Message %d", msgcnt
);
623 strncpy (bp
, "\n\n", buflen
);
625 if (write (out
, buffer
, strlen (buffer
)) < 0) {
626 advise (drft
, "write");
629 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
630 admonish (msgnam
, "unable to read message");
635 * Copy the message. Add RFC934 quoting (dashstuffing)
636 * unless given the -nodashstuffing flag.
639 cpydgst (fd
, out
, msgnam
, file
);
641 cpydata (fd
, out
, msgnam
, file
);
649 strncpy (buffer
, delim4
, sizeof(buffer
));
651 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
652 PLURALS(mp
->numsel
));
654 if (write (out
, buffer
, strlen (buffer
)) < 0) {
655 advise (drft
, "write");
659 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
660 digest
, volume
, issue
);
662 for (bp
= buffer
+ i
; i
> 1; i
--)
666 if (write (out
, buffer
, strlen (buffer
)) < 0) {
667 advise (drft
, "write");
674 * Create a mhn composition file for forwarding message.
678 copy_mime_draft (int out
)
683 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
684 PLURALS(mp
->numsel
), mp
->foldpath
);
685 if (write (out
, buffer
, strlen (buffer
)) < 0) {
686 advise (drft
, "write");
688 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
689 if (is_selected (mp
, msgnum
)) {
690 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
691 if (write (out
, buffer
, strlen (buffer
)) < 0) {
692 advise (drft
, "write");
695 if (write (out
, "\n", 1) < 0) {
696 advise (drft
, "write newline");