]>
diplodocus.org Git - nmh/blob - uip/forw.c
3 * forw.c -- forward a message, or group of messages.
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
16 #define IFORMAT "digest-issue-%s"
17 #define VFORMAT "digest-volume-%s"
19 #define FORW_SWITCHES \
20 X("annotate", 0, ANNOSW) \
21 X("noannotate", 0, NANNOSW) \
22 X("draftfolder +folder", 0, DFOLDSW) \
23 X("draftmessage msg", 0, DMSGSW) \
24 X("nodraftfolder", 0, NDFLDSW) \
25 X("editor editor", 0, EDITRSW) \
26 X("noedit", 0, NEDITSW) \
27 X("filter filterfile", 0, FILTSW) \
28 X("form formfile", 0, FORMSW) \
29 X("format", 5, FRMTSW) \
30 X("noformat", 7, NFRMTSW) \
31 X("inplace", 0, INPLSW) \
32 X("noinplace", 0, NINPLSW) \
33 X("mime", 0, MIMESW) \
34 X("nomime", 0, NMIMESW) \
35 X("digest list", 0, DGSTSW) \
36 X("issue number", 0, ISSUESW) \
37 X("volume number", 0, VOLUMSW) \
38 X("whatnowproc program", 0, WHATSW) \
39 X("nowhatnowproc", 0, NWHATSW) \
40 X("dashstuffing", 0, BITSTUFFSW) /* interface to mhl */ \
41 X("nodashstuffing", 0, NBITSTUFFSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
44 X("file file", 4, FILESW) /* interface from msh */ \
45 X("build", 5, BILDSW) /* interface from mhe */ \
46 X("from address", 0, FROMSW) \
47 X("to address", 0, TOSW) \
48 X("cc address", 0, CCSW) \
49 X("subject text", 0, SUBJECTSW) \
50 X("fcc mailbox", 0, FCCSW) \
51 X("width columns", 0, WIDTHSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(FORW
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(FORW
, switches
);
61 #define DISPO_SWITCHES \
63 X("replace", 0, YESW) \
64 X("list", 0, LISTDSW) \
65 X("refile +folder", 0, REFILSW) \
68 #define X(sw, minchars, id) id,
69 DEFINE_SWITCH_ENUM(DISPO
);
72 #define X(sw, minchars, id) { sw, minchars, id },
73 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
76 static struct swit aqrl
[] = {
78 { "replace", 0, YESW
},
79 { "list", 0, LISTDSW
},
80 { "refile +folder", 0, REFILSW
},
84 static char drft
[BUFSIZ
];
86 static char delim3
[] =
87 "\n------------------------------------------------------------\n\n";
88 static char delim4
[] = "\n------------------------------\n\n";
91 static struct msgs
*mp
= NULL
; /* used a lot */
97 static void mhl_draft (int, char *, int, int, char *, char *, int);
98 static void copy_draft (int, char *, char *, int, int, int);
99 static void copy_mime_draft (int);
103 main (int argc
, char **argv
)
105 int anot
= 0, inplace
= 1, mime
= 0;
106 int issue
= 0, volume
= 0, dashstuff
= 0;
107 int nedit
= 0, nwhat
= 0, i
, in
;
108 int out
, isdf
= 0, msgnum
= 0;
109 int outputlinelen
= OUTPUTLINELEN
;
111 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
112 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
113 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
114 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
115 char *form
= NULL
, buf
[BUFSIZ
], value
[10];
116 char **argp
, **arguments
;
118 struct msgs_array msgs
= { 0, 0, NULL
};
121 if (nmh_init(argv
[0], 1)) { return 1; }
123 arguments
= getarguments (invo_name
, argc
, argv
, 1);
126 while ((cp
= *argp
++)) {
128 switch (smatch (++cp
, switches
)) {
130 ambigsw (cp
, switches
);
133 adios (NULL
, "-%s unknown", cp
);
136 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
138 print_help (buf
, switches
, 1);
141 print_version(invo_name
);
152 if (!(ed
= *argp
++) || *ed
== '-')
153 adios (NULL
, "missing argument to %s", argp
[-2]);
161 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
162 adios (NULL
, "missing argument to %s", argp
[-2]);
166 buildsw
++; /* fall... */
173 adios (NULL
, "only one file at a time!");
174 if (!(cp
= *argp
++) || *cp
== '-')
175 adios (NULL
, "missing argument to %s", argp
[-2]);
176 file
= path (cp
, TFILE
);
179 if (!(cp
= *argp
++) || *cp
== '-')
180 adios (NULL
, "missing argument to %s", argp
[-2]);
181 filter
= getcpy (etcpath (cp
));
185 if (!(form
= *argp
++) || *form
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
190 filter
= getcpy (etcpath (mhlforward
));
212 if (!(cp
= *argp
++) || *cp
== '-')
213 adios (NULL
, "missing argument to %s", argp
[-2]);
218 if (!(cp
= *argp
++) || *cp
== '-')
219 adios (NULL
, "missing argument to %s", argp
[-2]);
220 if ((issue
= atoi (cp
)) < 1)
221 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
224 if (!(cp
= *argp
++) || *cp
== '-')
225 adios (NULL
, "missing argument to %s", argp
[-2]);
226 if ((volume
= atoi (cp
)) < 1)
227 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
232 adios (NULL
, "only one draft folder at a time!");
233 if (!(cp
= *argp
++) || *cp
== '-')
234 adios (NULL
, "missing argument to %s", argp
[-2]);
235 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
236 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
240 adios (NULL
, "only one draft message at a time!");
241 if (!(dmsg
= *argp
++) || *dmsg
== '-')
242 adios (NULL
, "missing argument to %s", argp
[-2]);
250 dashstuff
= 1; /* trinary logic */
253 dashstuff
= -1; /* trinary logic */
257 if (!(cp
= *argp
++) || *cp
== '-')
258 adios (NULL
, "missing argument to %s", argp
[-2]);
259 from
= addlist(from
, cp
);
262 if (!(cp
= *argp
++) || *cp
== '-')
263 adios (NULL
, "missing argument to %s", argp
[-2]);
264 to
= addlist(to
, cp
);
267 if (!(cp
= *argp
++) || *cp
== '-')
268 adios (NULL
, "missing argument to %s", argp
[-2]);
269 cc
= addlist(cc
, cp
);
272 if (!(cp
= *argp
++) || *cp
== '-')
273 adios (NULL
, "missing argument to %s", argp
[-2]);
274 fcc
= addlist(fcc
, cp
);
277 if (!(cp
= *argp
++) || *cp
== '-')
278 adios (NULL
, "missing argument to %s", argp
[-2]);
279 subject
= getcpy(cp
);
283 if (!(cp
= *argp
++) || *cp
== '-')
284 adios (NULL
, "missing argument to %s", argp
[-2]);
285 if ((outputlinelen
= atoi(cp
)) < 10)
286 adios (NULL
, "impossible width %d", outputlinelen
);
290 if (*cp
== '+' || *cp
== '@') {
292 adios (NULL
, "only one folder at a time!");
294 folder
= pluspath (cp
);
296 app_msgarg(&msgs
, cp
);
300 cwd
= getcpy (pwd ());
302 if (!context_find ("path"))
303 free (path ("./", TFOLDER
));
304 if (file
&& (msgs
.size
|| folder
))
305 adios (NULL
, "can't mix files and folders/msgs");
309 strncpy (drft
, buildsw
? m_maildir ("draft")
310 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
312 /* Check if a draft already exists */
313 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
314 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
315 for (i
= LISTDSW
; i
!= YESW
;) {
316 if (!(argp
= getans ("\nDisposition? ", isdf
? aqrnl
: aqrl
)))
318 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
327 showfile (++argp
, drft
);
330 if (refile (++argp
, drft
) == 0)
334 advise (NULL
, "say what?");
344 anot
= 0; /* don't want to annotate a file */
347 * Forwarding a message.
350 app_msgarg(&msgs
, "cur");
352 folder
= getfolder (1);
353 maildir
= m_maildir (folder
);
355 if (chdir (maildir
) == NOTOK
)
356 adios (maildir
, "unable to change directory to");
358 /* read folder and create message structure */
359 if (!(mp
= folder_read (folder
, 1)))
360 adios (NULL
, "unable to read folder %s", folder
);
362 /* check for empty folder */
364 adios (NULL
, "no messages in %s", folder
);
366 /* parse all the message ranges/sequences and set SELECTED */
367 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
368 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
371 seq_setprev (mp
); /* set the previous sequence */
374 * Find the first message in our set and use it as the input
375 * for the component scanner
378 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
379 if (is_selected (mp
, msgnum
)) {
380 fwdmsg
= strdup(m_name(msgnum
));
385 adios (NULL
, "Unable to find input message");
388 if (filter
&& access (filter
, R_OK
) == NOTOK
)
389 adios (filter
, "unable to read");
392 * Open form (component) file.
396 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
398 && (cp
= context_find (buf
))
399 && ((issue
= atoi (cp
)) < 0))
404 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
405 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
415 dat
[0] = digest
? issue
: msgnum
;
418 dat
[3] = outputlinelen
;
422 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
423 file
? file
: fwdmsg
);
425 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
426 adios (drft
, "unable to create");
429 * copy the components into the draft
431 cpydata (in
, out
, form
, drft
);
435 /* just copy the file into the draft */
436 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
437 adios (file
, "unable to open");
438 cpydata (in
, out
, file
, drft
);
443 * If filter file is defined, then format the
444 * messages into the draft using mhlproc.
447 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
449 copy_mime_draft (out
);
451 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
455 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
456 snprintf (value
, sizeof(value
), "%d", issue
);
457 context_replace (buf
, getcpy (value
));
458 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
459 snprintf (value
, sizeof(value
), "%d", volume
);
460 context_replace (buf
, getcpy (value
));
463 context_replace (pfolder
, folder
); /* update current folder */
464 seq_setcur (mp
, mp
->lowsel
); /* update current message */
465 seq_save (mp
); /* synchronize sequences */
466 context_save (); /* save the context file */
471 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
472 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
479 * Filter the messages you are forwarding, into the
480 * draft calling the mhlproc, and reading its output
485 mhl_draft (int out
, char *digest
, int volume
, int issue
,
486 char *file
, char *filter
, int dashstuff
)
489 int i
, msgnum
, pd
[2];
493 struct msgs_array vec
= { 0, 0, NULL
};
495 if (pipe (pd
) == NOTOK
)
496 adios ("pipe", "unable to create");
498 argsplit_msgarg(&vec
, mhlproc
, &program
);
500 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
505 adios ("fork", "unable to");
512 app_msgarg(&vec
, "-forwall");
513 app_msgarg(&vec
, "-form");
514 app_msgarg(&vec
, filter
);
517 app_msgarg(&vec
, "-digest");
518 app_msgarg(&vec
, digest
);
519 app_msgarg(&vec
, "-issue");
520 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
521 app_msgarg(&vec
, buf1
);
522 app_msgarg(&vec
, "-volume");
523 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
524 app_msgarg(&vec
, buf2
);
528 * Are we dashstuffing (quoting) the lines that begin
529 * with `-'. We use the mhl default (don't add any flag)
530 * unless the user has specified a specific flag.
533 app_msgarg(&vec
, "-dashstuffing");
534 else if (dashstuff
< 0)
535 app_msgarg(&vec
, "-nodashstuffing");
537 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
538 if (is_selected (mp
, msgnum
))
539 app_msgarg(&vec
, getcpy (m_name (msgnum
)));
542 app_msgarg(&vec
, NULL
);
544 execvp (program
, vec
.msgs
);
545 fprintf (stderr
, "unable to exec ");
551 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
553 pidXwait(child_id
, mhlproc
);
560 * Copy the messages into the draft. The messages are
561 * not filtered through the mhlproc. Do dashstuffing if
566 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
568 int fd
,i
, msgcnt
, msgnum
;
570 register char *bp
, *msgnam
;
574 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
575 if (is_selected (mp
, msgnum
)) {
577 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
580 /* Get buffer ready to go */
582 buflen
= sizeof(buffer
);
584 strncpy (bp
, "\n-------", buflen
);
589 if (msgnum
== mp
->lowsel
) {
590 snprintf (bp
, buflen
, " Forwarded Message%s",
591 mp
->numsel
> 1 ? "s" : "");
593 snprintf (bp
, buflen
, " Message %d", msgcnt
);
599 strncpy (bp
, "\n\n", buflen
);
601 if (write (out
, buffer
, strlen (buffer
)) < 0) {
602 advise (drft
, "write");
605 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
606 admonish (msgnam
, "unable to read message");
611 * Copy the message. Add RFC934 quoting (dashstuffing)
612 * unless given the -nodashstuffing flag.
615 cpydgst (fd
, out
, msgnam
, file
);
617 cpydata (fd
, out
, msgnam
, file
);
625 strncpy (buffer
, delim4
, sizeof(buffer
));
627 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
628 mp
->numsel
> 1 ? "s" : "");
630 if (write (out
, buffer
, strlen (buffer
)) < 0) {
631 advise (drft
, "write");
635 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
636 digest
, volume
, issue
);
638 for (bp
= buffer
+ i
; i
> 1; i
--)
642 if (write (out
, buffer
, strlen (buffer
)) < 0) {
643 advise (drft
, "write");
650 * Create a mhn composition file for forwarding message.
654 copy_mime_draft (int out
)
659 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
660 mp
->numsel
== 1 ? "" : "s", mp
->foldpath
);
661 if (write (out
, buffer
, strlen (buffer
)) < 0) {
662 advise (drft
, "write");
664 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
665 if (is_selected (mp
, msgnum
)) {
666 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
667 if (write (out
, buffer
, strlen (buffer
)) < 0) {
668 advise (drft
, "write");
671 if (write (out
, "\n", 1) < 0) {
672 advise (drft
, "write newline");