]>
diplodocus.org Git - nmh/blob - uip/forw.c
25d41da09cf396aa1f85346af18a128ffc685308
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/cpydata.h"
10 #include "sbr/cpydgst.h"
11 #include "sbr/m_draft.h"
12 #include "sbr/m_convert.h"
13 #include "sbr/getfolder.h"
14 #include "sbr/folder_read.h"
15 #include "sbr/context_save.h"
16 #include "sbr/context_replace.h"
17 #include "sbr/context_find.h"
18 #include "sbr/ambigsw.h"
19 #include "sbr/pidstatus.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/arglist.h"
24 #include "sbr/error.h"
29 #include "sbr/m_maildir.h"
33 #define IFORMAT "digest-issue-%s"
34 #define VFORMAT "digest-volume-%s"
36 #define FORW_SWITCHES \
37 X("annotate", 0, ANNOSW) \
38 X("noannotate", 0, NANNOSW) \
39 X("draftfolder +folder", 0, DFOLDSW) \
40 X("draftmessage msg", 0, DMSGSW) \
41 X("nodraftfolder", 0, NDFLDSW) \
42 X("editor editor", 0, EDITRSW) \
43 X("noedit", 0, NEDITSW) \
44 X("filter filterfile", 0, FILTSW) \
45 X("form formfile", 0, FORMSW) \
46 X("format", 5, FRMTSW) \
47 X("noformat", 7, NFRMTSW) \
48 X("inplace", 0, INPLSW) \
49 X("noinplace", 0, NINPLSW) \
50 X("mime", 0, MIMESW) \
51 X("nomime", 0, NMIMESW) \
52 X("digest list", 0, DGSTSW) \
53 X("issue number", 0, ISSUESW) \
54 X("volume number", 0, VOLUMSW) \
55 X("whatnowproc program", 0, WHATSW) \
56 X("nowhatnowproc", 0, NWHATSW) \
57 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
58 X("nodashstuffing", 0, NBITSTUFFSW) \
59 X("version", 0, VERSIONSW) \
60 X("help", 0, HELPSW) \
61 X("file file", 4, FILESW) \
62 X("build", 5, BILDSW) /* interface from mhe */ \
63 X("from address", 0, FROMSW) \
64 X("to address", 0, TOSW) \
65 X("cc address", 0, CCSW) \
66 X("subject text", 0, SUBJECTSW) \
67 X("fcc mailbox", 0, FCCSW) \
68 X("width columns", 0, WIDTHSW) \
70 #define X(sw, minchars, id) id,
71 DEFINE_SWITCH_ENUM(FORW
);
74 #define X(sw, minchars, id) { sw, minchars, id },
75 DEFINE_SWITCH_ARRAY(FORW
, switches
);
78 #define DISPO_SWITCHES \
80 X("replace", 0, YESW) \
81 X("list", 0, LISTDSW) \
82 X("refile +folder", 0, REFILSW) \
85 #define X(sw, minchars, id) id,
86 DEFINE_SWITCH_ENUM(DISPO
);
89 #define X(sw, minchars, id) { sw, minchars, id },
90 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
93 static struct swit aqrl
[] = {
95 { "replace", 0, YESW
},
96 { "list", 0, LISTDSW
},
97 { "refile +folder", 0, REFILSW
},
101 static char drft
[BUFSIZ
];
103 static char delim3
[] =
104 "\n------------------------------------------------------------\n\n";
105 static char delim4
[] = "\n------------------------------\n\n";
108 static struct msgs
*mp
= NULL
; /* used a lot */
114 static void mhl_draft (int, char *, int, int, char *, char *, int);
115 static void copy_draft (int, char *, char *, int, int, int);
116 static void copy_mime_draft (int);
120 main (int argc
, char **argv
)
125 int issue
= 0, volume
= 0, dashstuff
= 0;
129 int out
, isdf
= 0, msgnum
= 0;
130 int outputlinelen
= OUTPUTLINELEN
;
132 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
133 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
134 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
135 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
136 char *form
= NULL
, buf
[BUFSIZ
];
137 char **argp
, **arguments
;
139 struct msgs_array msgs
= { 0, 0, NULL
};
140 bool buildsw
= false;
142 if (nmh_init(argv
[0], true, true)) { return 1; }
144 arguments
= getarguments (invo_name
, argc
, argv
, 1);
147 while ((cp
= *argp
++)) {
149 switch (smatch (++cp
, switches
)) {
151 ambigsw (cp
, switches
);
154 die("-%s unknown", cp
);
157 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
159 print_help (buf
, switches
, 1);
162 print_version(invo_name
);
173 if (!(ed
= *argp
++) || *ed
== '-')
174 die("missing argument to %s", argp
[-2]);
182 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
183 die("missing argument to %s", argp
[-2]);
195 die("only one file at a time!");
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
198 file
= path (cp
, TFILE
);
201 if (!(cp
= *argp
++) || *cp
== '-')
202 die("missing argument to %s", argp
[-2]);
203 filter
= mh_xstrdup(etcpath(cp
));
207 if (!(form
= *argp
++) || *form
== '-')
208 die("missing argument to %s", argp
[-2]);
212 filter
= mh_xstrdup(etcpath(mhlforward
));
234 if (!(cp
= *argp
++) || *cp
== '-')
235 die("missing argument to %s", argp
[-2]);
236 digest
= mh_xstrdup(cp
);
240 if (!(cp
= *argp
++) || *cp
== '-')
241 die("missing argument to %s", argp
[-2]);
242 if ((issue
= atoi (cp
)) < 1)
243 die("bad argument %s %s", argp
[-2], cp
);
246 if (!(cp
= *argp
++) || *cp
== '-')
247 die("missing argument to %s", argp
[-2]);
248 if ((volume
= atoi (cp
)) < 1)
249 die("bad argument %s %s", argp
[-2], cp
);
254 die("only one draft folder at a time!");
255 if (!(cp
= *argp
++) || *cp
== '-')
256 die("missing argument to %s", argp
[-2]);
257 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
258 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
262 die("only one draft message at a time!");
263 if (!(dmsg
= *argp
++) || *dmsg
== '-')
264 die("missing argument to %s", argp
[-2]);
272 dashstuff
= 1; /* ternary logic */
275 dashstuff
= -1; /* ternary logic */
279 if (!(cp
= *argp
++) || *cp
== '-')
280 die("missing argument to %s", argp
[-2]);
281 from
= addlist(from
, cp
);
284 if (!(cp
= *argp
++) || *cp
== '-')
285 die("missing argument to %s", argp
[-2]);
286 to
= addlist(to
, cp
);
289 if (!(cp
= *argp
++) || *cp
== '-')
290 die("missing argument to %s", argp
[-2]);
291 cc
= addlist(cc
, cp
);
294 if (!(cp
= *argp
++) || *cp
== '-')
295 die("missing argument to %s", argp
[-2]);
296 fcc
= addlist(fcc
, cp
);
299 if (!(cp
= *argp
++) || *cp
== '-')
300 die("missing argument to %s", argp
[-2]);
301 subject
= mh_xstrdup(cp
);
305 if (!(cp
= *argp
++) || *cp
== '-')
306 die("missing argument to %s", argp
[-2]);
307 if ((outputlinelen
= atoi(cp
)) < 10)
308 die("impossible width %d", outputlinelen
);
312 if (*cp
== '+' || *cp
== '@') {
314 die("only one folder at a time!");
315 folder
= pluspath (cp
);
317 app_msgarg(&msgs
, cp
);
321 cwd
= mh_xstrdup(pwd ());
323 if (!context_find ("path"))
324 free (path ("./", TFOLDER
));
325 if (file
&& (msgs
.size
|| folder
))
326 die("can't mix files and folders/msgs");
330 strncpy (drft
, buildsw
? m_maildir ("draft")
331 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
333 /* Check if a draft already exists */
334 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
335 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
336 for (i
= LISTDSW
; i
!= YESW
;) {
337 if (!(argp
= read_switch_multiword ("\nDisposition? ",
338 isdf
? aqrnl
: aqrl
)))
340 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
349 showfile (++argp
, drft
);
352 if (refile (++argp
, drft
) == 0)
366 anot
= false; /* don't want to annotate a file */
369 * Forwarding a message.
372 app_msgarg(&msgs
, "cur");
374 folder
= getfolder (1);
375 maildir
= m_maildir (folder
);
377 if (chdir (maildir
) == NOTOK
)
378 adios (maildir
, "unable to change directory to");
380 /* read folder and create message structure */
381 if (!(mp
= folder_read (folder
, 1)))
382 die("unable to read folder %s", folder
);
384 /* check for empty folder */
386 die("no messages in %s", folder
);
388 /* parse all the message ranges/sequences and set SELECTED */
389 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
390 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
393 seq_setprev (mp
); /* set the previous sequence */
396 * Find the first message in our set and use it as the input
397 * for the component scanner
400 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
401 if (is_selected (mp
, msgnum
)) {
402 fwdmsg
= mh_xstrdup(m_name(msgnum
));
407 die("Unable to find input message");
410 if (filter
&& access (filter
, R_OK
) == NOTOK
)
411 adios (filter
, "unable to read");
414 * Open form (component) file.
418 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
420 && (cp
= context_find (buf
))
421 && ((issue
= atoi (cp
)) < 0))
426 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
427 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
437 dat
[0] = digest
? issue
: msgnum
;
440 dat
[3] = outputlinelen
;
444 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
445 file
? file
: fwdmsg
);
447 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
448 adios (drft
, "unable to create");
451 * copy the components into the draft
453 cpydata (in
, out
, form
, drft
);
457 /* just copy the file into the draft */
458 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
459 adios (file
, "unable to open");
460 cpydata (in
, out
, file
, drft
);
465 * If filter file is defined, then format the
466 * messages into the draft using mhlproc.
469 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
471 copy_mime_draft (out
);
473 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
477 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
478 context_replace (buf
, mh_xstrdup(m_str(issue
)));
479 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
480 context_replace (buf
, mh_xstrdup(m_str(volume
)));
483 context_replace (pfolder
, folder
); /* update current folder */
484 seq_setcur (mp
, mp
->lowsel
); /* update current message */
485 seq_save (mp
); /* synchronize sequences */
486 context_save (); /* save the context file */
491 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
492 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
499 * Filter the messages you are forwarding, into the
500 * draft calling the mhlproc, and reading its output
505 mhl_draft (int out
, char *digest
, int volume
, int issue
,
506 char *file
, char *filter
, int dashstuff
)
513 struct msgs_array vec
= { 0, 0, NULL
};
515 if (pipe (pd
) == NOTOK
)
516 adios ("pipe", "unable to create");
518 argsplit_msgarg(&vec
, mhlproc
, &program
);
523 adios ("fork", "unable to");
530 app_msgarg(&vec
, "-forwall");
531 app_msgarg(&vec
, "-form");
532 app_msgarg(&vec
, filter
);
535 app_msgarg(&vec
, "-digest");
536 app_msgarg(&vec
, digest
);
537 app_msgarg(&vec
, "-issue");
538 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
539 app_msgarg(&vec
, buf1
);
540 app_msgarg(&vec
, "-volume");
541 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
542 app_msgarg(&vec
, buf2
);
546 * Are we dashstuffing (quoting) the lines that begin
547 * with `-'. We use the mhl default (don't add any flag)
548 * unless the user has specified a specific flag.
551 app_msgarg(&vec
, "-dashstuffing");
552 else if (dashstuff
< 0)
553 app_msgarg(&vec
, "-nodashstuffing");
555 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
556 if (is_selected (mp
, msgnum
))
557 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
560 app_msgarg(&vec
, NULL
);
562 execvp (program
, vec
.msgs
);
563 fprintf (stderr
, "unable to exec ");
569 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
571 pidXwait(child_id
, mhlproc
);
578 * Copy the messages into the draft. The messages are
579 * not filtered through the mhlproc. Do dashstuffing if
584 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
586 int fd
,i
, msgcnt
, msgnum
;
592 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
593 if (is_selected (mp
, msgnum
)) {
595 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
598 /* Get buffer ready to go */
600 buflen
= sizeof(buffer
);
602 strncpy (bp
, "\n-------", buflen
);
607 if (msgnum
== mp
->lowsel
) {
608 snprintf (bp
, buflen
, " Forwarded Message%s",
609 PLURALS(mp
->numsel
));
611 snprintf (bp
, buflen
, " Message %d", msgcnt
);
617 strncpy (bp
, "\n\n", buflen
);
619 if (write (out
, buffer
, strlen (buffer
)) < 0) {
620 advise (drft
, "write");
623 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
624 admonish (msgnam
, "unable to read message");
629 * Copy the message. Add RFC934 quoting (dashstuffing)
630 * unless given the -nodashstuffing flag.
633 cpydgst (fd
, out
, msgnam
, file
);
635 cpydata (fd
, out
, msgnam
, file
);
643 strncpy (buffer
, delim4
, sizeof(buffer
));
645 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
646 PLURALS(mp
->numsel
));
648 if (write (out
, buffer
, strlen (buffer
)) < 0) {
649 advise (drft
, "write");
653 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
654 digest
, volume
, issue
);
656 for (bp
= buffer
+ i
; i
> 1; i
--)
660 if (write (out
, buffer
, strlen (buffer
)) < 0) {
661 advise (drft
, "write");
668 * Create a mhn composition file for forwarding message.
672 copy_mime_draft (int out
)
677 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
678 PLURALS(mp
->numsel
), mp
->foldpath
);
679 if (write (out
, buffer
, strlen (buffer
)) < 0) {
680 advise (drft
, "write");
682 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
683 if (is_selected (mp
, msgnum
)) {
684 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
685 if (write (out
, buffer
, strlen (buffer
)) < 0) {
686 advise (drft
, "write");
689 if (write (out
, "\n", 1) < 0) {
690 advise (drft
, "write newline");