]>
diplodocus.org Git - nmh/blob - uip/forw.c
afca66a3cb2a36a029e75a1627a68580df44619c
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.
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/arglist.h"
13 #include "sbr/error.h"
18 #include "sbr/m_maildir.h"
22 #define IFORMAT "digest-issue-%s"
23 #define VFORMAT "digest-volume-%s"
25 #define FORW_SWITCHES \
26 X("annotate", 0, ANNOSW) \
27 X("noannotate", 0, NANNOSW) \
28 X("draftfolder +folder", 0, DFOLDSW) \
29 X("draftmessage msg", 0, DMSGSW) \
30 X("nodraftfolder", 0, NDFLDSW) \
31 X("editor editor", 0, EDITRSW) \
32 X("noedit", 0, NEDITSW) \
33 X("filter filterfile", 0, FILTSW) \
34 X("form formfile", 0, FORMSW) \
35 X("format", 5, FRMTSW) \
36 X("noformat", 7, NFRMTSW) \
37 X("inplace", 0, INPLSW) \
38 X("noinplace", 0, NINPLSW) \
39 X("mime", 0, MIMESW) \
40 X("nomime", 0, NMIMESW) \
41 X("digest list", 0, DGSTSW) \
42 X("issue number", 0, ISSUESW) \
43 X("volume number", 0, VOLUMSW) \
44 X("whatnowproc program", 0, WHATSW) \
45 X("nowhatnowproc", 0, NWHATSW) \
46 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
47 X("nodashstuffing", 0, NBITSTUFFSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
50 X("file file", 4, FILESW) \
51 X("build", 5, BILDSW) /* interface from mhe */ \
52 X("from address", 0, FROMSW) \
53 X("to address", 0, TOSW) \
54 X("cc address", 0, CCSW) \
55 X("subject text", 0, SUBJECTSW) \
56 X("fcc mailbox", 0, FCCSW) \
57 X("width columns", 0, WIDTHSW) \
59 #define X(sw, minchars, id) id,
60 DEFINE_SWITCH_ENUM(FORW
);
63 #define X(sw, minchars, id) { sw, minchars, id },
64 DEFINE_SWITCH_ARRAY(FORW
, switches
);
67 #define DISPO_SWITCHES \
69 X("replace", 0, YESW) \
70 X("list", 0, LISTDSW) \
71 X("refile +folder", 0, REFILSW) \
74 #define X(sw, minchars, id) id,
75 DEFINE_SWITCH_ENUM(DISPO
);
78 #define X(sw, minchars, id) { sw, minchars, id },
79 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
82 static struct swit aqrl
[] = {
84 { "replace", 0, YESW
},
85 { "list", 0, LISTDSW
},
86 { "refile +folder", 0, REFILSW
},
90 static char drft
[BUFSIZ
];
92 static char delim3
[] =
93 "\n------------------------------------------------------------\n\n";
94 static char delim4
[] = "\n------------------------------\n\n";
97 static struct msgs
*mp
= NULL
; /* used a lot */
103 static void mhl_draft (int, char *, int, int, char *, char *, int);
104 static void copy_draft (int, char *, char *, int, int, int);
105 static void copy_mime_draft (int);
109 main (int argc
, char **argv
)
114 int issue
= 0, volume
= 0, dashstuff
= 0;
118 int out
, isdf
= 0, msgnum
= 0;
119 int outputlinelen
= OUTPUTLINELEN
;
121 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
122 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
123 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
124 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
125 char *form
= NULL
, buf
[BUFSIZ
];
126 char **argp
, **arguments
;
128 struct msgs_array msgs
= { 0, 0, NULL
};
129 bool buildsw
= false;
131 if (nmh_init(argv
[0], true, true)) { return 1; }
133 arguments
= getarguments (invo_name
, argc
, argv
, 1);
136 while ((cp
= *argp
++)) {
138 switch (smatch (++cp
, switches
)) {
140 ambigsw (cp
, switches
);
143 die("-%s unknown", cp
);
146 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
148 print_help (buf
, switches
, 1);
151 print_version(invo_name
);
162 if (!(ed
= *argp
++) || *ed
== '-')
163 die("missing argument to %s", argp
[-2]);
171 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
172 die("missing argument to %s", argp
[-2]);
184 die("only one file at a time!");
185 if (!(cp
= *argp
++) || *cp
== '-')
186 die("missing argument to %s", argp
[-2]);
187 file
= path (cp
, TFILE
);
190 if (!(cp
= *argp
++) || *cp
== '-')
191 die("missing argument to %s", argp
[-2]);
192 filter
= mh_xstrdup(etcpath(cp
));
196 if (!(form
= *argp
++) || *form
== '-')
197 die("missing argument to %s", argp
[-2]);
201 filter
= mh_xstrdup(etcpath(mhlforward
));
223 if (!(cp
= *argp
++) || *cp
== '-')
224 die("missing argument to %s", argp
[-2]);
225 digest
= mh_xstrdup(cp
);
229 if (!(cp
= *argp
++) || *cp
== '-')
230 die("missing argument to %s", argp
[-2]);
231 if ((issue
= atoi (cp
)) < 1)
232 die("bad argument %s %s", argp
[-2], cp
);
235 if (!(cp
= *argp
++) || *cp
== '-')
236 die("missing argument to %s", argp
[-2]);
237 if ((volume
= atoi (cp
)) < 1)
238 die("bad argument %s %s", argp
[-2], cp
);
243 die("only one draft folder at a time!");
244 if (!(cp
= *argp
++) || *cp
== '-')
245 die("missing argument to %s", argp
[-2]);
246 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
247 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
251 die("only one draft message at a time!");
252 if (!(dmsg
= *argp
++) || *dmsg
== '-')
253 die("missing argument to %s", argp
[-2]);
261 dashstuff
= 1; /* ternary logic */
264 dashstuff
= -1; /* ternary logic */
268 if (!(cp
= *argp
++) || *cp
== '-')
269 die("missing argument to %s", argp
[-2]);
270 from
= addlist(from
, cp
);
273 if (!(cp
= *argp
++) || *cp
== '-')
274 die("missing argument to %s", argp
[-2]);
275 to
= addlist(to
, cp
);
278 if (!(cp
= *argp
++) || *cp
== '-')
279 die("missing argument to %s", argp
[-2]);
280 cc
= addlist(cc
, cp
);
283 if (!(cp
= *argp
++) || *cp
== '-')
284 die("missing argument to %s", argp
[-2]);
285 fcc
= addlist(fcc
, cp
);
288 if (!(cp
= *argp
++) || *cp
== '-')
289 die("missing argument to %s", argp
[-2]);
290 subject
= mh_xstrdup(cp
);
294 if (!(cp
= *argp
++) || *cp
== '-')
295 die("missing argument to %s", argp
[-2]);
296 if ((outputlinelen
= atoi(cp
)) < 10)
297 die("impossible width %d", outputlinelen
);
301 if (*cp
== '+' || *cp
== '@') {
303 die("only one folder at a time!");
304 folder
= pluspath (cp
);
306 app_msgarg(&msgs
, cp
);
310 cwd
= mh_xstrdup(pwd ());
312 if (!context_find ("path"))
313 free (path ("./", TFOLDER
));
314 if (file
&& (msgs
.size
|| folder
))
315 die("can't mix files and folders/msgs");
319 strncpy (drft
, buildsw
? m_maildir ("draft")
320 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
322 /* Check if a draft already exists */
323 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
324 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
325 for (i
= LISTDSW
; i
!= YESW
;) {
326 if (!(argp
= read_switch_multiword ("\nDisposition? ",
327 isdf
? aqrnl
: aqrl
)))
329 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
338 showfile (++argp
, drft
);
341 if (refile (++argp
, drft
) == 0)
355 anot
= false; /* don't want to annotate a file */
358 * Forwarding a message.
361 app_msgarg(&msgs
, "cur");
363 folder
= getfolder (1);
364 maildir
= m_maildir (folder
);
366 if (chdir (maildir
) == NOTOK
)
367 adios (maildir
, "unable to change directory to");
369 /* read folder and create message structure */
370 if (!(mp
= folder_read (folder
, 1)))
371 die("unable to read folder %s", folder
);
373 /* check for empty folder */
375 die("no messages in %s", folder
);
377 /* parse all the message ranges/sequences and set SELECTED */
378 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
379 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
382 seq_setprev (mp
); /* set the previous sequence */
385 * Find the first message in our set and use it as the input
386 * for the component scanner
389 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
390 if (is_selected (mp
, msgnum
)) {
391 fwdmsg
= mh_xstrdup(m_name(msgnum
));
396 die("Unable to find input message");
399 if (filter
&& access (filter
, R_OK
) == NOTOK
)
400 adios (filter
, "unable to read");
403 * Open form (component) file.
407 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
409 && (cp
= context_find (buf
))
410 && ((issue
= atoi (cp
)) < 0))
415 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
416 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
426 dat
[0] = digest
? issue
: msgnum
;
429 dat
[3] = outputlinelen
;
433 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
434 file
? file
: fwdmsg
);
436 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
437 adios (drft
, "unable to create");
440 * copy the components into the draft
442 cpydata (in
, out
, form
, drft
);
446 /* just copy the file into the draft */
447 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
448 adios (file
, "unable to open");
449 cpydata (in
, out
, file
, drft
);
454 * If filter file is defined, then format the
455 * messages into the draft using mhlproc.
458 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
460 copy_mime_draft (out
);
462 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
466 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
467 context_replace (buf
, mh_xstrdup(m_str(issue
)));
468 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
469 context_replace (buf
, mh_xstrdup(m_str(volume
)));
472 context_replace (pfolder
, folder
); /* update current folder */
473 seq_setcur (mp
, mp
->lowsel
); /* update current message */
474 seq_save (mp
); /* synchronize sequences */
475 context_save (); /* save the context file */
480 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
481 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
488 * Filter the messages you are forwarding, into the
489 * draft calling the mhlproc, and reading its output
494 mhl_draft (int out
, char *digest
, int volume
, int issue
,
495 char *file
, char *filter
, int dashstuff
)
502 struct msgs_array vec
= { 0, 0, NULL
};
504 if (pipe (pd
) == NOTOK
)
505 adios ("pipe", "unable to create");
507 argsplit_msgarg(&vec
, mhlproc
, &program
);
512 adios ("fork", "unable to");
519 app_msgarg(&vec
, "-forwall");
520 app_msgarg(&vec
, "-form");
521 app_msgarg(&vec
, filter
);
524 app_msgarg(&vec
, "-digest");
525 app_msgarg(&vec
, digest
);
526 app_msgarg(&vec
, "-issue");
527 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
528 app_msgarg(&vec
, buf1
);
529 app_msgarg(&vec
, "-volume");
530 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
531 app_msgarg(&vec
, buf2
);
535 * Are we dashstuffing (quoting) the lines that begin
536 * with `-'. We use the mhl default (don't add any flag)
537 * unless the user has specified a specific flag.
540 app_msgarg(&vec
, "-dashstuffing");
541 else if (dashstuff
< 0)
542 app_msgarg(&vec
, "-nodashstuffing");
544 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
545 if (is_selected (mp
, msgnum
))
546 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
549 app_msgarg(&vec
, NULL
);
551 execvp (program
, vec
.msgs
);
552 fprintf (stderr
, "unable to exec ");
558 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
560 pidXwait(child_id
, mhlproc
);
567 * Copy the messages into the draft. The messages are
568 * not filtered through the mhlproc. Do dashstuffing if
573 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
575 int fd
,i
, msgcnt
, msgnum
;
581 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
582 if (is_selected (mp
, msgnum
)) {
584 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
587 /* Get buffer ready to go */
589 buflen
= sizeof(buffer
);
591 strncpy (bp
, "\n-------", buflen
);
596 if (msgnum
== mp
->lowsel
) {
597 snprintf (bp
, buflen
, " Forwarded Message%s",
598 PLURALS(mp
->numsel
));
600 snprintf (bp
, buflen
, " Message %d", msgcnt
);
606 strncpy (bp
, "\n\n", buflen
);
608 if (write (out
, buffer
, strlen (buffer
)) < 0) {
609 advise (drft
, "write");
612 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
613 admonish (msgnam
, "unable to read message");
618 * Copy the message. Add RFC934 quoting (dashstuffing)
619 * unless given the -nodashstuffing flag.
622 cpydgst (fd
, out
, msgnam
, file
);
624 cpydata (fd
, out
, msgnam
, file
);
632 strncpy (buffer
, delim4
, sizeof(buffer
));
634 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
635 PLURALS(mp
->numsel
));
637 if (write (out
, buffer
, strlen (buffer
)) < 0) {
638 advise (drft
, "write");
642 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
643 digest
, volume
, issue
);
645 for (bp
= buffer
+ i
; i
> 1; i
--)
649 if (write (out
, buffer
, strlen (buffer
)) < 0) {
650 advise (drft
, "write");
657 * Create a mhn composition file for forwarding message.
661 copy_mime_draft (int out
)
666 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
667 PLURALS(mp
->numsel
), mp
->foldpath
);
668 if (write (out
, buffer
, strlen (buffer
)) < 0) {
669 advise (drft
, "write");
671 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
672 if (is_selected (mp
, msgnum
)) {
673 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
674 if (write (out
, buffer
, strlen (buffer
)) < 0) {
675 advise (drft
, "write");
678 if (write (out
, "\n", 1) < 0) {
679 advise (drft
, "write newline");