]>
diplodocus.org Git - nmh/blob - uip/forw.c
2482a1c36ecf525cd29ea01b5963052df8995e9f
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/cpydgst.h"
10 #include "sbr/m_draft.h"
11 #include "sbr/m_convert.h"
12 #include "sbr/getfolder.h"
13 #include "sbr/folder_read.h"
14 #include "sbr/context_save.h"
15 #include "sbr/context_replace.h"
16 #include "sbr/context_find.h"
17 #include "sbr/ambigsw.h"
18 #include "sbr/pidstatus.h"
20 #include "sbr/print_version.h"
21 #include "sbr/print_help.h"
22 #include "sbr/arglist.h"
23 #include "sbr/error.h"
28 #include "sbr/m_maildir.h"
32 #define IFORMAT "digest-issue-%s"
33 #define VFORMAT "digest-volume-%s"
35 #define FORW_SWITCHES \
36 X("annotate", 0, ANNOSW) \
37 X("noannotate", 0, NANNOSW) \
38 X("draftfolder +folder", 0, DFOLDSW) \
39 X("draftmessage msg", 0, DMSGSW) \
40 X("nodraftfolder", 0, NDFLDSW) \
41 X("editor editor", 0, EDITRSW) \
42 X("noedit", 0, NEDITSW) \
43 X("filter filterfile", 0, FILTSW) \
44 X("form formfile", 0, FORMSW) \
45 X("format", 5, FRMTSW) \
46 X("noformat", 7, NFRMTSW) \
47 X("inplace", 0, INPLSW) \
48 X("noinplace", 0, NINPLSW) \
49 X("mime", 0, MIMESW) \
50 X("nomime", 0, NMIMESW) \
51 X("digest list", 0, DGSTSW) \
52 X("issue number", 0, ISSUESW) \
53 X("volume number", 0, VOLUMSW) \
54 X("whatnowproc program", 0, WHATSW) \
55 X("nowhatnowproc", 0, NWHATSW) \
56 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
57 X("nodashstuffing", 0, NBITSTUFFSW) \
58 X("version", 0, VERSIONSW) \
59 X("help", 0, HELPSW) \
60 X("file file", 4, FILESW) \
61 X("build", 5, BILDSW) /* interface from mhe */ \
62 X("from address", 0, FROMSW) \
63 X("to address", 0, TOSW) \
64 X("cc address", 0, CCSW) \
65 X("subject text", 0, SUBJECTSW) \
66 X("fcc mailbox", 0, FCCSW) \
67 X("width columns", 0, WIDTHSW) \
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(FORW
);
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(FORW
, switches
);
77 #define DISPO_SWITCHES \
79 X("replace", 0, YESW) \
80 X("list", 0, LISTDSW) \
81 X("refile +folder", 0, REFILSW) \
84 #define X(sw, minchars, id) id,
85 DEFINE_SWITCH_ENUM(DISPO
);
88 #define X(sw, minchars, id) { sw, minchars, id },
89 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
92 static struct swit aqrl
[] = {
94 { "replace", 0, YESW
},
95 { "list", 0, LISTDSW
},
96 { "refile +folder", 0, REFILSW
},
100 static char drft
[BUFSIZ
];
102 static char delim3
[] =
103 "\n------------------------------------------------------------\n\n";
104 static char delim4
[] = "\n------------------------------\n\n";
107 static struct msgs
*mp
= NULL
; /* used a lot */
113 static void mhl_draft (int, char *, int, int, char *, char *, int);
114 static void copy_draft (int, char *, char *, int, int, int);
115 static void copy_mime_draft (int);
119 main (int argc
, char **argv
)
124 int issue
= 0, volume
= 0, dashstuff
= 0;
128 int out
, isdf
= 0, msgnum
= 0;
129 int outputlinelen
= OUTPUTLINELEN
;
131 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
132 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
133 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
134 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
135 char *form
= NULL
, buf
[BUFSIZ
];
136 char **argp
, **arguments
;
138 struct msgs_array msgs
= { 0, 0, NULL
};
139 bool buildsw
= false;
141 if (nmh_init(argv
[0], true, true)) { return 1; }
143 arguments
= getarguments (invo_name
, argc
, argv
, 1);
146 while ((cp
= *argp
++)) {
148 switch (smatch (++cp
, switches
)) {
150 ambigsw (cp
, switches
);
153 die("-%s unknown", cp
);
156 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
158 print_help (buf
, switches
, 1);
161 print_version(invo_name
);
172 if (!(ed
= *argp
++) || *ed
== '-')
173 die("missing argument to %s", argp
[-2]);
181 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
182 die("missing argument to %s", argp
[-2]);
194 die("only one file at a time!");
195 if (!(cp
= *argp
++) || *cp
== '-')
196 die("missing argument to %s", argp
[-2]);
197 file
= path (cp
, TFILE
);
200 if (!(cp
= *argp
++) || *cp
== '-')
201 die("missing argument to %s", argp
[-2]);
202 filter
= mh_xstrdup(etcpath(cp
));
206 if (!(form
= *argp
++) || *form
== '-')
207 die("missing argument to %s", argp
[-2]);
211 filter
= mh_xstrdup(etcpath(mhlforward
));
233 if (!(cp
= *argp
++) || *cp
== '-')
234 die("missing argument to %s", argp
[-2]);
235 digest
= mh_xstrdup(cp
);
239 if (!(cp
= *argp
++) || *cp
== '-')
240 die("missing argument to %s", argp
[-2]);
241 if ((issue
= atoi (cp
)) < 1)
242 die("bad argument %s %s", argp
[-2], cp
);
245 if (!(cp
= *argp
++) || *cp
== '-')
246 die("missing argument to %s", argp
[-2]);
247 if ((volume
= atoi (cp
)) < 1)
248 die("bad argument %s %s", argp
[-2], cp
);
253 die("only one draft folder at a time!");
254 if (!(cp
= *argp
++) || *cp
== '-')
255 die("missing argument to %s", argp
[-2]);
256 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
257 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
261 die("only one draft message at a time!");
262 if (!(dmsg
= *argp
++) || *dmsg
== '-')
263 die("missing argument to %s", argp
[-2]);
271 dashstuff
= 1; /* ternary logic */
274 dashstuff
= -1; /* ternary logic */
278 if (!(cp
= *argp
++) || *cp
== '-')
279 die("missing argument to %s", argp
[-2]);
280 from
= addlist(from
, cp
);
283 if (!(cp
= *argp
++) || *cp
== '-')
284 die("missing argument to %s", argp
[-2]);
285 to
= addlist(to
, cp
);
288 if (!(cp
= *argp
++) || *cp
== '-')
289 die("missing argument to %s", argp
[-2]);
290 cc
= addlist(cc
, cp
);
293 if (!(cp
= *argp
++) || *cp
== '-')
294 die("missing argument to %s", argp
[-2]);
295 fcc
= addlist(fcc
, cp
);
298 if (!(cp
= *argp
++) || *cp
== '-')
299 die("missing argument to %s", argp
[-2]);
300 subject
= mh_xstrdup(cp
);
304 if (!(cp
= *argp
++) || *cp
== '-')
305 die("missing argument to %s", argp
[-2]);
306 if ((outputlinelen
= atoi(cp
)) < 10)
307 die("impossible width %d", outputlinelen
);
311 if (*cp
== '+' || *cp
== '@') {
313 die("only one folder at a time!");
314 folder
= pluspath (cp
);
316 app_msgarg(&msgs
, cp
);
320 cwd
= mh_xstrdup(pwd ());
322 if (!context_find ("path"))
323 free (path ("./", TFOLDER
));
324 if (file
&& (msgs
.size
|| folder
))
325 die("can't mix files and folders/msgs");
329 strncpy (drft
, buildsw
? m_maildir ("draft")
330 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
332 /* Check if a draft already exists */
333 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
334 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
335 for (i
= LISTDSW
; i
!= YESW
;) {
336 if (!(argp
= read_switch_multiword ("\nDisposition? ",
337 isdf
? aqrnl
: aqrl
)))
339 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
348 showfile (++argp
, drft
);
351 if (refile (++argp
, drft
) == 0)
365 anot
= false; /* don't want to annotate a file */
368 * Forwarding a message.
371 app_msgarg(&msgs
, "cur");
373 folder
= getfolder (1);
374 maildir
= m_maildir (folder
);
376 if (chdir (maildir
) == NOTOK
)
377 adios (maildir
, "unable to change directory to");
379 /* read folder and create message structure */
380 if (!(mp
= folder_read (folder
, 1)))
381 die("unable to read folder %s", folder
);
383 /* check for empty folder */
385 die("no messages in %s", folder
);
387 /* parse all the message ranges/sequences and set SELECTED */
388 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
389 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
392 seq_setprev (mp
); /* set the previous sequence */
395 * Find the first message in our set and use it as the input
396 * for the component scanner
399 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
400 if (is_selected (mp
, msgnum
)) {
401 fwdmsg
= mh_xstrdup(m_name(msgnum
));
406 die("Unable to find input message");
409 if (filter
&& access (filter
, R_OK
) == NOTOK
)
410 adios (filter
, "unable to read");
413 * Open form (component) file.
417 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
419 && (cp
= context_find (buf
))
420 && ((issue
= atoi (cp
)) < 0))
425 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
426 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
436 dat
[0] = digest
? issue
: msgnum
;
439 dat
[3] = outputlinelen
;
443 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
444 file
? file
: fwdmsg
);
446 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
447 adios (drft
, "unable to create");
450 * copy the components into the draft
452 cpydata (in
, out
, form
, drft
);
456 /* just copy the file into the draft */
457 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
458 adios (file
, "unable to open");
459 cpydata (in
, out
, file
, drft
);
464 * If filter file is defined, then format the
465 * messages into the draft using mhlproc.
468 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
470 copy_mime_draft (out
);
472 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
476 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
477 context_replace (buf
, mh_xstrdup(m_str(issue
)));
478 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
479 context_replace (buf
, mh_xstrdup(m_str(volume
)));
482 context_replace (pfolder
, folder
); /* update current folder */
483 seq_setcur (mp
, mp
->lowsel
); /* update current message */
484 seq_save (mp
); /* synchronize sequences */
485 context_save (); /* save the context file */
490 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
491 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
498 * Filter the messages you are forwarding, into the
499 * draft calling the mhlproc, and reading its output
504 mhl_draft (int out
, char *digest
, int volume
, int issue
,
505 char *file
, char *filter
, int dashstuff
)
512 struct msgs_array vec
= { 0, 0, NULL
};
514 if (pipe (pd
) == NOTOK
)
515 adios ("pipe", "unable to create");
517 argsplit_msgarg(&vec
, mhlproc
, &program
);
522 adios ("fork", "unable to");
529 app_msgarg(&vec
, "-forwall");
530 app_msgarg(&vec
, "-form");
531 app_msgarg(&vec
, filter
);
534 app_msgarg(&vec
, "-digest");
535 app_msgarg(&vec
, digest
);
536 app_msgarg(&vec
, "-issue");
537 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
538 app_msgarg(&vec
, buf1
);
539 app_msgarg(&vec
, "-volume");
540 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
541 app_msgarg(&vec
, buf2
);
545 * Are we dashstuffing (quoting) the lines that begin
546 * with `-'. We use the mhl default (don't add any flag)
547 * unless the user has specified a specific flag.
550 app_msgarg(&vec
, "-dashstuffing");
551 else if (dashstuff
< 0)
552 app_msgarg(&vec
, "-nodashstuffing");
554 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
555 if (is_selected (mp
, msgnum
))
556 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
559 app_msgarg(&vec
, NULL
);
561 execvp (program
, vec
.msgs
);
562 fprintf (stderr
, "unable to exec ");
568 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
570 pidXwait(child_id
, mhlproc
);
577 * Copy the messages into the draft. The messages are
578 * not filtered through the mhlproc. Do dashstuffing if
583 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
585 int fd
,i
, msgcnt
, msgnum
;
591 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
592 if (is_selected (mp
, msgnum
)) {
594 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
597 /* Get buffer ready to go */
599 buflen
= sizeof(buffer
);
601 strncpy (bp
, "\n-------", buflen
);
606 if (msgnum
== mp
->lowsel
) {
607 snprintf (bp
, buflen
, " Forwarded Message%s",
608 PLURALS(mp
->numsel
));
610 snprintf (bp
, buflen
, " Message %d", msgcnt
);
616 strncpy (bp
, "\n\n", buflen
);
618 if (write (out
, buffer
, strlen (buffer
)) < 0) {
619 advise (drft
, "write");
622 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
623 admonish (msgnam
, "unable to read message");
628 * Copy the message. Add RFC934 quoting (dashstuffing)
629 * unless given the -nodashstuffing flag.
632 cpydgst (fd
, out
, msgnam
, file
);
634 cpydata (fd
, out
, msgnam
, file
);
642 strncpy (buffer
, delim4
, sizeof(buffer
));
644 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
645 PLURALS(mp
->numsel
));
647 if (write (out
, buffer
, strlen (buffer
)) < 0) {
648 advise (drft
, "write");
652 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
653 digest
, volume
, issue
);
655 for (bp
= buffer
+ i
; i
> 1; i
--)
659 if (write (out
, buffer
, strlen (buffer
)) < 0) {
660 advise (drft
, "write");
667 * Create a mhn composition file for forwarding message.
671 copy_mime_draft (int out
)
676 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
677 PLURALS(mp
->numsel
), mp
->foldpath
);
678 if (write (out
, buffer
, strlen (buffer
)) < 0) {
679 advise (drft
, "write");
681 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
682 if (is_selected (mp
, msgnum
)) {
683 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
684 if (write (out
, buffer
, strlen (buffer
)) < 0) {
685 advise (drft
, "write");
688 if (write (out
, "\n", 1) < 0) {
689 advise (drft
, "write newline");