]>
diplodocus.org Git - nmh/blob - uip/forw.c
654a3f74456410b7f6314aadec8af60690572848
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/context_replace.h"
10 #include "sbr/context_find.h"
11 #include "sbr/ambigsw.h"
12 #include "sbr/pidstatus.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/arglist.h"
17 #include "sbr/error.h"
22 #include "sbr/m_maildir.h"
26 #define IFORMAT "digest-issue-%s"
27 #define VFORMAT "digest-volume-%s"
29 #define FORW_SWITCHES \
30 X("annotate", 0, ANNOSW) \
31 X("noannotate", 0, NANNOSW) \
32 X("draftfolder +folder", 0, DFOLDSW) \
33 X("draftmessage msg", 0, DMSGSW) \
34 X("nodraftfolder", 0, NDFLDSW) \
35 X("editor editor", 0, EDITRSW) \
36 X("noedit", 0, NEDITSW) \
37 X("filter filterfile", 0, FILTSW) \
38 X("form formfile", 0, FORMSW) \
39 X("format", 5, FRMTSW) \
40 X("noformat", 7, NFRMTSW) \
41 X("inplace", 0, INPLSW) \
42 X("noinplace", 0, NINPLSW) \
43 X("mime", 0, MIMESW) \
44 X("nomime", 0, NMIMESW) \
45 X("digest list", 0, DGSTSW) \
46 X("issue number", 0, ISSUESW) \
47 X("volume number", 0, VOLUMSW) \
48 X("whatnowproc program", 0, WHATSW) \
49 X("nowhatnowproc", 0, NWHATSW) \
50 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
51 X("nodashstuffing", 0, NBITSTUFFSW) \
52 X("version", 0, VERSIONSW) \
53 X("help", 0, HELPSW) \
54 X("file file", 4, FILESW) \
55 X("build", 5, BILDSW) /* interface from mhe */ \
56 X("from address", 0, FROMSW) \
57 X("to address", 0, TOSW) \
58 X("cc address", 0, CCSW) \
59 X("subject text", 0, SUBJECTSW) \
60 X("fcc mailbox", 0, FCCSW) \
61 X("width columns", 0, WIDTHSW) \
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(FORW
);
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(FORW
, switches
);
71 #define DISPO_SWITCHES \
73 X("replace", 0, YESW) \
74 X("list", 0, LISTDSW) \
75 X("refile +folder", 0, REFILSW) \
78 #define X(sw, minchars, id) id,
79 DEFINE_SWITCH_ENUM(DISPO
);
82 #define X(sw, minchars, id) { sw, minchars, id },
83 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
86 static struct swit aqrl
[] = {
88 { "replace", 0, YESW
},
89 { "list", 0, LISTDSW
},
90 { "refile +folder", 0, REFILSW
},
94 static char drft
[BUFSIZ
];
96 static char delim3
[] =
97 "\n------------------------------------------------------------\n\n";
98 static char delim4
[] = "\n------------------------------\n\n";
101 static struct msgs
*mp
= NULL
; /* used a lot */
107 static void mhl_draft (int, char *, int, int, char *, char *, int);
108 static void copy_draft (int, char *, char *, int, int, int);
109 static void copy_mime_draft (int);
113 main (int argc
, char **argv
)
118 int issue
= 0, volume
= 0, dashstuff
= 0;
122 int out
, isdf
= 0, msgnum
= 0;
123 int outputlinelen
= OUTPUTLINELEN
;
125 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
126 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
127 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
128 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
129 char *form
= NULL
, buf
[BUFSIZ
];
130 char **argp
, **arguments
;
132 struct msgs_array msgs
= { 0, 0, NULL
};
133 bool buildsw
= false;
135 if (nmh_init(argv
[0], true, true)) { return 1; }
137 arguments
= getarguments (invo_name
, argc
, argv
, 1);
140 while ((cp
= *argp
++)) {
142 switch (smatch (++cp
, switches
)) {
144 ambigsw (cp
, switches
);
147 die("-%s unknown", cp
);
150 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
152 print_help (buf
, switches
, 1);
155 print_version(invo_name
);
166 if (!(ed
= *argp
++) || *ed
== '-')
167 die("missing argument to %s", argp
[-2]);
175 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
176 die("missing argument to %s", argp
[-2]);
188 die("only one file at a time!");
189 if (!(cp
= *argp
++) || *cp
== '-')
190 die("missing argument to %s", argp
[-2]);
191 file
= path (cp
, TFILE
);
194 if (!(cp
= *argp
++) || *cp
== '-')
195 die("missing argument to %s", argp
[-2]);
196 filter
= mh_xstrdup(etcpath(cp
));
200 if (!(form
= *argp
++) || *form
== '-')
201 die("missing argument to %s", argp
[-2]);
205 filter
= mh_xstrdup(etcpath(mhlforward
));
227 if (!(cp
= *argp
++) || *cp
== '-')
228 die("missing argument to %s", argp
[-2]);
229 digest
= mh_xstrdup(cp
);
233 if (!(cp
= *argp
++) || *cp
== '-')
234 die("missing argument to %s", argp
[-2]);
235 if ((issue
= atoi (cp
)) < 1)
236 die("bad argument %s %s", argp
[-2], cp
);
239 if (!(cp
= *argp
++) || *cp
== '-')
240 die("missing argument to %s", argp
[-2]);
241 if ((volume
= atoi (cp
)) < 1)
242 die("bad argument %s %s", argp
[-2], cp
);
247 die("only one draft folder at a time!");
248 if (!(cp
= *argp
++) || *cp
== '-')
249 die("missing argument to %s", argp
[-2]);
250 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
251 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
255 die("only one draft message at a time!");
256 if (!(dmsg
= *argp
++) || *dmsg
== '-')
257 die("missing argument to %s", argp
[-2]);
265 dashstuff
= 1; /* ternary logic */
268 dashstuff
= -1; /* ternary logic */
272 if (!(cp
= *argp
++) || *cp
== '-')
273 die("missing argument to %s", argp
[-2]);
274 from
= addlist(from
, cp
);
277 if (!(cp
= *argp
++) || *cp
== '-')
278 die("missing argument to %s", argp
[-2]);
279 to
= addlist(to
, cp
);
282 if (!(cp
= *argp
++) || *cp
== '-')
283 die("missing argument to %s", argp
[-2]);
284 cc
= addlist(cc
, cp
);
287 if (!(cp
= *argp
++) || *cp
== '-')
288 die("missing argument to %s", argp
[-2]);
289 fcc
= addlist(fcc
, cp
);
292 if (!(cp
= *argp
++) || *cp
== '-')
293 die("missing argument to %s", argp
[-2]);
294 subject
= mh_xstrdup(cp
);
298 if (!(cp
= *argp
++) || *cp
== '-')
299 die("missing argument to %s", argp
[-2]);
300 if ((outputlinelen
= atoi(cp
)) < 10)
301 die("impossible width %d", outputlinelen
);
305 if (*cp
== '+' || *cp
== '@') {
307 die("only one folder at a time!");
308 folder
= pluspath (cp
);
310 app_msgarg(&msgs
, cp
);
314 cwd
= mh_xstrdup(pwd ());
316 if (!context_find ("path"))
317 free (path ("./", TFOLDER
));
318 if (file
&& (msgs
.size
|| folder
))
319 die("can't mix files and folders/msgs");
323 strncpy (drft
, buildsw
? m_maildir ("draft")
324 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
326 /* Check if a draft already exists */
327 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
328 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
329 for (i
= LISTDSW
; i
!= YESW
;) {
330 if (!(argp
= read_switch_multiword ("\nDisposition? ",
331 isdf
? aqrnl
: aqrl
)))
333 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
342 showfile (++argp
, drft
);
345 if (refile (++argp
, drft
) == 0)
359 anot
= false; /* don't want to annotate a file */
362 * Forwarding a message.
365 app_msgarg(&msgs
, "cur");
367 folder
= getfolder (1);
368 maildir
= m_maildir (folder
);
370 if (chdir (maildir
) == NOTOK
)
371 adios (maildir
, "unable to change directory to");
373 /* read folder and create message structure */
374 if (!(mp
= folder_read (folder
, 1)))
375 die("unable to read folder %s", folder
);
377 /* check for empty folder */
379 die("no messages in %s", folder
);
381 /* parse all the message ranges/sequences and set SELECTED */
382 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
383 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
386 seq_setprev (mp
); /* set the previous sequence */
389 * Find the first message in our set and use it as the input
390 * for the component scanner
393 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
394 if (is_selected (mp
, msgnum
)) {
395 fwdmsg
= mh_xstrdup(m_name(msgnum
));
400 die("Unable to find input message");
403 if (filter
&& access (filter
, R_OK
) == NOTOK
)
404 adios (filter
, "unable to read");
407 * Open form (component) file.
411 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
413 && (cp
= context_find (buf
))
414 && ((issue
= atoi (cp
)) < 0))
419 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
420 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
430 dat
[0] = digest
? issue
: msgnum
;
433 dat
[3] = outputlinelen
;
437 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
438 file
? file
: fwdmsg
);
440 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
441 adios (drft
, "unable to create");
444 * copy the components into the draft
446 cpydata (in
, out
, form
, drft
);
450 /* just copy the file into the draft */
451 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
452 adios (file
, "unable to open");
453 cpydata (in
, out
, file
, drft
);
458 * If filter file is defined, then format the
459 * messages into the draft using mhlproc.
462 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
464 copy_mime_draft (out
);
466 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
470 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
471 context_replace (buf
, mh_xstrdup(m_str(issue
)));
472 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
473 context_replace (buf
, mh_xstrdup(m_str(volume
)));
476 context_replace (pfolder
, folder
); /* update current folder */
477 seq_setcur (mp
, mp
->lowsel
); /* update current message */
478 seq_save (mp
); /* synchronize sequences */
479 context_save (); /* save the context file */
484 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
485 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
492 * Filter the messages you are forwarding, into the
493 * draft calling the mhlproc, and reading its output
498 mhl_draft (int out
, char *digest
, int volume
, int issue
,
499 char *file
, char *filter
, int dashstuff
)
506 struct msgs_array vec
= { 0, 0, NULL
};
508 if (pipe (pd
) == NOTOK
)
509 adios ("pipe", "unable to create");
511 argsplit_msgarg(&vec
, mhlproc
, &program
);
516 adios ("fork", "unable to");
523 app_msgarg(&vec
, "-forwall");
524 app_msgarg(&vec
, "-form");
525 app_msgarg(&vec
, filter
);
528 app_msgarg(&vec
, "-digest");
529 app_msgarg(&vec
, digest
);
530 app_msgarg(&vec
, "-issue");
531 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
532 app_msgarg(&vec
, buf1
);
533 app_msgarg(&vec
, "-volume");
534 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
535 app_msgarg(&vec
, buf2
);
539 * Are we dashstuffing (quoting) the lines that begin
540 * with `-'. We use the mhl default (don't add any flag)
541 * unless the user has specified a specific flag.
544 app_msgarg(&vec
, "-dashstuffing");
545 else if (dashstuff
< 0)
546 app_msgarg(&vec
, "-nodashstuffing");
548 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
549 if (is_selected (mp
, msgnum
))
550 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
553 app_msgarg(&vec
, NULL
);
555 execvp (program
, vec
.msgs
);
556 fprintf (stderr
, "unable to exec ");
562 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
564 pidXwait(child_id
, mhlproc
);
571 * Copy the messages into the draft. The messages are
572 * not filtered through the mhlproc. Do dashstuffing if
577 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
579 int fd
,i
, msgcnt
, msgnum
;
585 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
586 if (is_selected (mp
, msgnum
)) {
588 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
591 /* Get buffer ready to go */
593 buflen
= sizeof(buffer
);
595 strncpy (bp
, "\n-------", buflen
);
600 if (msgnum
== mp
->lowsel
) {
601 snprintf (bp
, buflen
, " Forwarded Message%s",
602 PLURALS(mp
->numsel
));
604 snprintf (bp
, buflen
, " Message %d", msgcnt
);
610 strncpy (bp
, "\n\n", buflen
);
612 if (write (out
, buffer
, strlen (buffer
)) < 0) {
613 advise (drft
, "write");
616 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
617 admonish (msgnam
, "unable to read message");
622 * Copy the message. Add RFC934 quoting (dashstuffing)
623 * unless given the -nodashstuffing flag.
626 cpydgst (fd
, out
, msgnam
, file
);
628 cpydata (fd
, out
, msgnam
, file
);
636 strncpy (buffer
, delim4
, sizeof(buffer
));
638 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
639 PLURALS(mp
->numsel
));
641 if (write (out
, buffer
, strlen (buffer
)) < 0) {
642 advise (drft
, "write");
646 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
647 digest
, volume
, issue
);
649 for (bp
= buffer
+ i
; i
> 1; i
--)
653 if (write (out
, buffer
, strlen (buffer
)) < 0) {
654 advise (drft
, "write");
661 * Create a mhn composition file for forwarding message.
665 copy_mime_draft (int out
)
670 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
671 PLURALS(mp
->numsel
), mp
->foldpath
);
672 if (write (out
, buffer
, strlen (buffer
)) < 0) {
673 advise (drft
, "write");
675 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
676 if (is_selected (mp
, msgnum
)) {
677 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
678 if (write (out
, buffer
, strlen (buffer
)) < 0) {
679 advise (drft
, "write");
682 if (write (out
, "\n", 1) < 0) {
683 advise (drft
, "write newline");