]>
diplodocus.org Git - nmh/blob - uip/forw.c
a0dbf913c08a9c67717dfd1a12db1a61ec4a00de
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/print_help.h"
10 #include "sbr/arglist.h"
11 #include "sbr/error.h"
16 #include "sbr/m_maildir.h"
20 #define IFORMAT "digest-issue-%s"
21 #define VFORMAT "digest-volume-%s"
23 #define FORW_SWITCHES \
24 X("annotate", 0, ANNOSW) \
25 X("noannotate", 0, NANNOSW) \
26 X("draftfolder +folder", 0, DFOLDSW) \
27 X("draftmessage msg", 0, DMSGSW) \
28 X("nodraftfolder", 0, NDFLDSW) \
29 X("editor editor", 0, EDITRSW) \
30 X("noedit", 0, NEDITSW) \
31 X("filter filterfile", 0, FILTSW) \
32 X("form formfile", 0, FORMSW) \
33 X("format", 5, FRMTSW) \
34 X("noformat", 7, NFRMTSW) \
35 X("inplace", 0, INPLSW) \
36 X("noinplace", 0, NINPLSW) \
37 X("mime", 0, MIMESW) \
38 X("nomime", 0, NMIMESW) \
39 X("digest list", 0, DGSTSW) \
40 X("issue number", 0, ISSUESW) \
41 X("volume number", 0, VOLUMSW) \
42 X("whatnowproc program", 0, WHATSW) \
43 X("nowhatnowproc", 0, NWHATSW) \
44 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
45 X("nodashstuffing", 0, NBITSTUFFSW) \
46 X("version", 0, VERSIONSW) \
47 X("help", 0, HELPSW) \
48 X("file file", 4, FILESW) \
49 X("build", 5, BILDSW) /* interface from mhe */ \
50 X("from address", 0, FROMSW) \
51 X("to address", 0, TOSW) \
52 X("cc address", 0, CCSW) \
53 X("subject text", 0, SUBJECTSW) \
54 X("fcc mailbox", 0, FCCSW) \
55 X("width columns", 0, WIDTHSW) \
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(FORW
);
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(FORW
, switches
);
65 #define DISPO_SWITCHES \
67 X("replace", 0, YESW) \
68 X("list", 0, LISTDSW) \
69 X("refile +folder", 0, REFILSW) \
72 #define X(sw, minchars, id) id,
73 DEFINE_SWITCH_ENUM(DISPO
);
76 #define X(sw, minchars, id) { sw, minchars, id },
77 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
80 static struct swit aqrl
[] = {
82 { "replace", 0, YESW
},
83 { "list", 0, LISTDSW
},
84 { "refile +folder", 0, REFILSW
},
88 static char drft
[BUFSIZ
];
90 static char delim3
[] =
91 "\n------------------------------------------------------------\n\n";
92 static char delim4
[] = "\n------------------------------\n\n";
95 static struct msgs
*mp
= NULL
; /* used a lot */
101 static void mhl_draft (int, char *, int, int, char *, char *, int);
102 static void copy_draft (int, char *, char *, int, int, int);
103 static void copy_mime_draft (int);
107 main (int argc
, char **argv
)
112 int issue
= 0, volume
= 0, dashstuff
= 0;
116 int out
, isdf
= 0, msgnum
= 0;
117 int outputlinelen
= OUTPUTLINELEN
;
119 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
120 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
121 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
122 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
123 char *form
= NULL
, buf
[BUFSIZ
];
124 char **argp
, **arguments
;
126 struct msgs_array msgs
= { 0, 0, NULL
};
127 bool buildsw
= false;
129 if (nmh_init(argv
[0], true, true)) { return 1; }
131 arguments
= getarguments (invo_name
, argc
, argv
, 1);
134 while ((cp
= *argp
++)) {
136 switch (smatch (++cp
, switches
)) {
138 ambigsw (cp
, switches
);
141 die("-%s unknown", cp
);
144 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
146 print_help (buf
, switches
, 1);
149 print_version(invo_name
);
160 if (!(ed
= *argp
++) || *ed
== '-')
161 die("missing argument to %s", argp
[-2]);
169 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
170 die("missing argument to %s", argp
[-2]);
182 die("only one file at a time!");
183 if (!(cp
= *argp
++) || *cp
== '-')
184 die("missing argument to %s", argp
[-2]);
185 file
= path (cp
, TFILE
);
188 if (!(cp
= *argp
++) || *cp
== '-')
189 die("missing argument to %s", argp
[-2]);
190 filter
= mh_xstrdup(etcpath(cp
));
194 if (!(form
= *argp
++) || *form
== '-')
195 die("missing argument to %s", argp
[-2]);
199 filter
= mh_xstrdup(etcpath(mhlforward
));
221 if (!(cp
= *argp
++) || *cp
== '-')
222 die("missing argument to %s", argp
[-2]);
223 digest
= mh_xstrdup(cp
);
227 if (!(cp
= *argp
++) || *cp
== '-')
228 die("missing argument to %s", argp
[-2]);
229 if ((issue
= atoi (cp
)) < 1)
230 die("bad argument %s %s", argp
[-2], cp
);
233 if (!(cp
= *argp
++) || *cp
== '-')
234 die("missing argument to %s", argp
[-2]);
235 if ((volume
= atoi (cp
)) < 1)
236 die("bad argument %s %s", argp
[-2], cp
);
241 die("only one draft folder at a time!");
242 if (!(cp
= *argp
++) || *cp
== '-')
243 die("missing argument to %s", argp
[-2]);
244 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
245 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
249 die("only one draft message at a time!");
250 if (!(dmsg
= *argp
++) || *dmsg
== '-')
251 die("missing argument to %s", argp
[-2]);
259 dashstuff
= 1; /* ternary logic */
262 dashstuff
= -1; /* ternary logic */
266 if (!(cp
= *argp
++) || *cp
== '-')
267 die("missing argument to %s", argp
[-2]);
268 from
= addlist(from
, cp
);
271 if (!(cp
= *argp
++) || *cp
== '-')
272 die("missing argument to %s", argp
[-2]);
273 to
= addlist(to
, cp
);
276 if (!(cp
= *argp
++) || *cp
== '-')
277 die("missing argument to %s", argp
[-2]);
278 cc
= addlist(cc
, cp
);
281 if (!(cp
= *argp
++) || *cp
== '-')
282 die("missing argument to %s", argp
[-2]);
283 fcc
= addlist(fcc
, cp
);
286 if (!(cp
= *argp
++) || *cp
== '-')
287 die("missing argument to %s", argp
[-2]);
288 subject
= mh_xstrdup(cp
);
292 if (!(cp
= *argp
++) || *cp
== '-')
293 die("missing argument to %s", argp
[-2]);
294 if ((outputlinelen
= atoi(cp
)) < 10)
295 die("impossible width %d", outputlinelen
);
299 if (*cp
== '+' || *cp
== '@') {
301 die("only one folder at a time!");
302 folder
= pluspath (cp
);
304 app_msgarg(&msgs
, cp
);
308 cwd
= mh_xstrdup(pwd ());
310 if (!context_find ("path"))
311 free (path ("./", TFOLDER
));
312 if (file
&& (msgs
.size
|| folder
))
313 die("can't mix files and folders/msgs");
317 strncpy (drft
, buildsw
? m_maildir ("draft")
318 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
320 /* Check if a draft already exists */
321 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
322 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
323 for (i
= LISTDSW
; i
!= YESW
;) {
324 if (!(argp
= read_switch_multiword ("\nDisposition? ",
325 isdf
? aqrnl
: aqrl
)))
327 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
336 showfile (++argp
, drft
);
339 if (refile (++argp
, drft
) == 0)
353 anot
= false; /* don't want to annotate a file */
356 * Forwarding a message.
359 app_msgarg(&msgs
, "cur");
361 folder
= getfolder (1);
362 maildir
= m_maildir (folder
);
364 if (chdir (maildir
) == NOTOK
)
365 adios (maildir
, "unable to change directory to");
367 /* read folder and create message structure */
368 if (!(mp
= folder_read (folder
, 1)))
369 die("unable to read folder %s", folder
);
371 /* check for empty folder */
373 die("no messages in %s", folder
);
375 /* parse all the message ranges/sequences and set SELECTED */
376 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
377 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
380 seq_setprev (mp
); /* set the previous sequence */
383 * Find the first message in our set and use it as the input
384 * for the component scanner
387 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
388 if (is_selected (mp
, msgnum
)) {
389 fwdmsg
= mh_xstrdup(m_name(msgnum
));
394 die("Unable to find input message");
397 if (filter
&& access (filter
, R_OK
) == NOTOK
)
398 adios (filter
, "unable to read");
401 * Open form (component) file.
405 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
407 && (cp
= context_find (buf
))
408 && ((issue
= atoi (cp
)) < 0))
413 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
414 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
424 dat
[0] = digest
? issue
: msgnum
;
427 dat
[3] = outputlinelen
;
431 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
432 file
? file
: fwdmsg
);
434 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
435 adios (drft
, "unable to create");
438 * copy the components into the draft
440 cpydata (in
, out
, form
, drft
);
444 /* just copy the file into the draft */
445 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
446 adios (file
, "unable to open");
447 cpydata (in
, out
, file
, drft
);
452 * If filter file is defined, then format the
453 * messages into the draft using mhlproc.
456 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
458 copy_mime_draft (out
);
460 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
464 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
465 context_replace (buf
, mh_xstrdup(m_str(issue
)));
466 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
467 context_replace (buf
, mh_xstrdup(m_str(volume
)));
470 context_replace (pfolder
, folder
); /* update current folder */
471 seq_setcur (mp
, mp
->lowsel
); /* update current message */
472 seq_save (mp
); /* synchronize sequences */
473 context_save (); /* save the context file */
478 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
479 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
486 * Filter the messages you are forwarding, into the
487 * draft calling the mhlproc, and reading its output
492 mhl_draft (int out
, char *digest
, int volume
, int issue
,
493 char *file
, char *filter
, int dashstuff
)
500 struct msgs_array vec
= { 0, 0, NULL
};
502 if (pipe (pd
) == NOTOK
)
503 adios ("pipe", "unable to create");
505 argsplit_msgarg(&vec
, mhlproc
, &program
);
510 adios ("fork", "unable to");
517 app_msgarg(&vec
, "-forwall");
518 app_msgarg(&vec
, "-form");
519 app_msgarg(&vec
, filter
);
522 app_msgarg(&vec
, "-digest");
523 app_msgarg(&vec
, digest
);
524 app_msgarg(&vec
, "-issue");
525 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
526 app_msgarg(&vec
, buf1
);
527 app_msgarg(&vec
, "-volume");
528 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
529 app_msgarg(&vec
, buf2
);
533 * Are we dashstuffing (quoting) the lines that begin
534 * with `-'. We use the mhl default (don't add any flag)
535 * unless the user has specified a specific flag.
538 app_msgarg(&vec
, "-dashstuffing");
539 else if (dashstuff
< 0)
540 app_msgarg(&vec
, "-nodashstuffing");
542 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
543 if (is_selected (mp
, msgnum
))
544 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
547 app_msgarg(&vec
, NULL
);
549 execvp (program
, vec
.msgs
);
550 fprintf (stderr
, "unable to exec ");
556 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
558 pidXwait(child_id
, mhlproc
);
565 * Copy the messages into the draft. The messages are
566 * not filtered through the mhlproc. Do dashstuffing if
571 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
573 int fd
,i
, msgcnt
, msgnum
;
579 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
580 if (is_selected (mp
, msgnum
)) {
582 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
585 /* Get buffer ready to go */
587 buflen
= sizeof(buffer
);
589 strncpy (bp
, "\n-------", buflen
);
594 if (msgnum
== mp
->lowsel
) {
595 snprintf (bp
, buflen
, " Forwarded Message%s",
596 PLURALS(mp
->numsel
));
598 snprintf (bp
, buflen
, " Message %d", msgcnt
);
604 strncpy (bp
, "\n\n", buflen
);
606 if (write (out
, buffer
, strlen (buffer
)) < 0) {
607 advise (drft
, "write");
610 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
611 admonish (msgnam
, "unable to read message");
616 * Copy the message. Add RFC934 quoting (dashstuffing)
617 * unless given the -nodashstuffing flag.
620 cpydgst (fd
, out
, msgnam
, file
);
622 cpydata (fd
, out
, msgnam
, file
);
630 strncpy (buffer
, delim4
, sizeof(buffer
));
632 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
633 PLURALS(mp
->numsel
));
635 if (write (out
, buffer
, strlen (buffer
)) < 0) {
636 advise (drft
, "write");
640 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
641 digest
, volume
, issue
);
643 for (bp
= buffer
+ i
; i
> 1; i
--)
647 if (write (out
, buffer
, strlen (buffer
)) < 0) {
648 advise (drft
, "write");
655 * Create a mhn composition file for forwarding message.
659 copy_mime_draft (int out
)
664 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
665 PLURALS(mp
->numsel
), mp
->foldpath
);
666 if (write (out
, buffer
, strlen (buffer
)) < 0) {
667 advise (drft
, "write");
669 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
670 if (is_selected (mp
, msgnum
)) {
671 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
672 if (write (out
, buffer
, strlen (buffer
)) < 0) {
673 advise (drft
, "write");
676 if (write (out
, "\n", 1) < 0) {
677 advise (drft
, "write newline");