]>
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 static struct swit switches
[] = {
25 { "draftfolder +folder", 0 },
27 { "draftmessage msg", 0 },
29 { "nodraftfolder", 0 },
31 { "editor editor", 0 },
35 { "filter filterfile", 0 },
37 { "form formfile", 0 },
53 { "issue number", 0 },
55 { "volume number", 0 },
57 { "whatnowproc program", 0 },
59 { "nowhatnowproc", 0 },
61 { "dashstuffing", 0 }, /* interface to mhl */
62 #define NBITSTUFFSW 21
63 { "nodashstuffing", 0 },
69 { "file file", 4 }, /* interface from msh */
71 { "build", 5 }, /* interface from mhe */
73 { "from address", 0 },
79 { "subject text", 0 },
83 { "width columns", 0 },
87 static struct swit aqrnl
[] = {
95 { "refile +folder", 0 },
101 static struct swit aqrl
[] = {
105 { "refile +folder", 0 },
109 static char drft
[BUFSIZ
];
111 static char delim3
[] =
112 "\n------------------------------------------------------------\n\n";
113 static char delim4
[] = "\n------------------------------\n\n";
116 static struct msgs
*mp
= NULL
; /* used a lot */
122 static void mhl_draft (int, char *, int, int, char *, char *, int);
123 static void copy_draft (int, char *, char *, int, int, int);
124 static void copy_mime_draft (int);
128 main (int argc
, char **argv
)
130 int anot
= 0, inplace
= 1, mime
= 0;
131 int issue
= 0, volume
= 0, dashstuff
= 0;
132 int nedit
= 0, nwhat
= 0, i
, in
;
133 int out
, isdf
= 0, msgnum
= 0;
134 int outputlinelen
= OUTPUTLINELEN
;
136 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
137 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
138 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
, *fwdmsg
= NULL
;
139 char *from
= NULL
, *to
= NULL
, *cc
= NULL
, *subject
= NULL
, *fcc
= NULL
;
140 char *form
= NULL
, buf
[BUFSIZ
], value
[10];
141 char **argp
, **arguments
;
143 struct msgs_array msgs
= { 0, 0, NULL
};
148 setlocale(LC_ALL
, "");
150 invo_name
= r1bindex (argv
[0], '/');
152 /* read user profile/context */
155 arguments
= getarguments (invo_name
, argc
, argv
, 1);
158 while ((cp
= *argp
++)) {
160 switch (smatch (++cp
, switches
)) {
162 ambigsw (cp
, switches
);
165 adios (NULL
, "-%s unknown", cp
);
168 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
170 print_help (buf
, switches
, 1);
173 print_version(invo_name
);
184 if (!(ed
= *argp
++) || *ed
== '-')
185 adios (NULL
, "missing argument to %s", argp
[-2]);
193 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
194 adios (NULL
, "missing argument to %s", argp
[-2]);
198 buildsw
++; /* fall... */
205 adios (NULL
, "only one file at a time!");
206 if (!(cp
= *argp
++) || *cp
== '-')
207 adios (NULL
, "missing argument to %s", argp
[-2]);
208 file
= path (cp
, TFILE
);
211 if (!(cp
= *argp
++) || *cp
== '-')
212 adios (NULL
, "missing argument to %s", argp
[-2]);
213 filter
= getcpy (etcpath (cp
));
217 if (!(form
= *argp
++) || *form
== '-')
218 adios (NULL
, "missing argument to %s", argp
[-2]);
222 filter
= getcpy (etcpath (mhlforward
));
244 if (!(cp
= *argp
++) || *cp
== '-')
245 adios (NULL
, "missing argument to %s", argp
[-2]);
250 if (!(cp
= *argp
++) || *cp
== '-')
251 adios (NULL
, "missing argument to %s", argp
[-2]);
252 if ((issue
= atoi (cp
)) < 1)
253 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
256 if (!(cp
= *argp
++) || *cp
== '-')
257 adios (NULL
, "missing argument to %s", argp
[-2]);
258 if ((volume
= atoi (cp
)) < 1)
259 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
264 adios (NULL
, "only one draft folder at a time!");
265 if (!(cp
= *argp
++) || *cp
== '-')
266 adios (NULL
, "missing argument to %s", argp
[-2]);
267 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
268 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
272 adios (NULL
, "only one draft message at a time!");
273 if (!(dmsg
= *argp
++) || *dmsg
== '-')
274 adios (NULL
, "missing argument to %s", argp
[-2]);
282 dashstuff
= 1; /* trinary logic */
285 dashstuff
= -1; /* trinary logic */
289 if (!(cp
= *argp
++) || *cp
== '-')
290 adios (NULL
, "missing argument to %s", argp
[-2]);
291 from
= addlist(from
, cp
);
294 if (!(cp
= *argp
++) || *cp
== '-')
295 adios (NULL
, "missing argument to %s", argp
[-2]);
296 to
= addlist(to
, cp
);
299 if (!(cp
= *argp
++) || *cp
== '-')
300 adios (NULL
, "missing argument to %s", argp
[-2]);
301 cc
= addlist(cc
, cp
);
304 if (!(cp
= *argp
++) || *cp
== '-')
305 adios (NULL
, "missing argument to %s", argp
[-2]);
306 fcc
= addlist(fcc
, cp
);
309 if (!(cp
= *argp
++) || *cp
== '-')
310 adios (NULL
, "missing argument to %s", argp
[-2]);
311 subject
= getcpy(cp
);
315 if (!(cp
= *argp
++) || *cp
== '-')
316 adios (NULL
, "missing argument to %s", argp
[-2]);
317 if ((outputlinelen
= atoi(cp
)) < 10)
318 adios (NULL
, "impossible width %d", outputlinelen
);
322 if (*cp
== '+' || *cp
== '@') {
324 adios (NULL
, "only one folder at a time!");
326 folder
= pluspath (cp
);
328 app_msgarg(&msgs
, cp
);
332 cwd
= getcpy (pwd ());
334 if (!context_find ("path"))
335 free (path ("./", TFOLDER
));
336 if (file
&& (msgs
.size
|| folder
))
337 adios (NULL
, "can't mix files and folders/msgs");
341 strncpy (drft
, buildsw
? m_maildir ("draft")
342 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
344 /* Check if a draft already exists */
345 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
346 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
347 for (i
= LISTDSW
; i
!= YESW
;) {
348 if (!(argp
= getans ("\nDisposition? ", isdf
? aqrnl
: aqrl
)))
350 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
359 showfile (++argp
, drft
);
362 if (refile (++argp
, drft
) == 0)
366 advise (NULL
, "say what?");
376 anot
= 0; /* don't want to annotate a file */
379 * Forwarding a message.
382 app_msgarg(&msgs
, "cur");
384 folder
= getfolder (1);
385 maildir
= m_maildir (folder
);
387 if (chdir (maildir
) == NOTOK
)
388 adios (maildir
, "unable to change directory to");
390 /* read folder and create message structure */
391 if (!(mp
= folder_read (folder
)))
392 adios (NULL
, "unable to read folder %s", folder
);
394 /* check for empty folder */
396 adios (NULL
, "no messages in %s", folder
);
398 /* parse all the message ranges/sequences and set SELECTED */
399 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
400 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
403 seq_setprev (mp
); /* set the previous sequence */
406 * Find the first message in our set and use it as the input
407 * for the component scanner
410 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
411 if (is_selected (mp
, msgnum
)) {
412 fwdmsg
= strdup(m_name(msgnum
));
417 adios (NULL
, "Unable to find input message");
420 if (filter
&& access (filter
, R_OK
) == NOTOK
)
421 adios (filter
, "unable to read");
424 * Open form (component) file.
428 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
430 && (cp
= context_find (buf
))
431 && ((issue
= atoi (cp
)) < 0))
436 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
437 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
447 dat
[0] = digest
? issue
: msgnum
;
450 dat
[3] = outputlinelen
;
454 in
= build_form (form
, digest
, dat
, from
, to
, cc
, fcc
, subject
,
455 file
? file
: fwdmsg
);
457 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
458 adios (drft
, "unable to create");
461 * copy the components into the draft
463 cpydata (in
, out
, form
, drft
);
467 /* just copy the file into the draft */
468 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
469 adios (file
, "unable to open");
470 cpydata (in
, out
, file
, drft
);
475 * If filter file is defined, then format the
476 * messages into the draft using mhlproc.
479 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
481 copy_mime_draft (out
);
483 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
487 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
488 snprintf (value
, sizeof(value
), "%d", issue
);
489 context_replace (buf
, getcpy (value
));
490 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
491 snprintf (value
, sizeof(value
), "%d", volume
);
492 context_replace (buf
, getcpy (value
));
495 context_replace (pfolder
, folder
); /* update current folder */
496 seq_setcur (mp
, mp
->lowsel
); /* update current message */
497 seq_save (mp
); /* synchronize sequences */
498 context_save (); /* save the context file */
503 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
504 anot
? "Forwarded" : NULL
, inplace
, cwd
, 0);
511 * Filter the messages you are forwarding, into the
512 * draft calling the mhlproc, and reading its output
517 mhl_draft (int out
, char *digest
, int volume
, int issue
,
518 char *file
, char *filter
, int dashstuff
)
521 int i
, msgnum
, pd
[2];
524 struct msgs_array vec
= { 0, 0, NULL
};
526 if (pipe (pd
) == NOTOK
)
527 adios ("pipe", "unable to create");
529 app_msgarg(&vec
, r1bindex (mhlproc
, '/'));
531 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
536 adios ("fork", "unable to");
544 app_msgarg(&vec
, "-forwall");
545 app_msgarg(&vec
, "-form");
546 app_msgarg(&vec
, filter
);
549 app_msgarg(&vec
, "-digest");
550 app_msgarg(&vec
, digest
);
551 app_msgarg(&vec
, "-issue");
552 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
553 app_msgarg(&vec
, buf1
);
554 app_msgarg(&vec
, "-volume");
555 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
556 app_msgarg(&vec
, buf2
);
560 * Are we dashstuffing (quoting) the lines that begin
561 * with `-'. We use the mhl default (don't add any flag)
562 * unless the user has specified a specific flag.
565 app_msgarg(&vec
, "-dashstuffing");
566 else if (dashstuff
< 0)
567 app_msgarg(&vec
, "-nodashstuffing");
569 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
570 if (is_selected (mp
, msgnum
))
571 app_msgarg(&vec
, getcpy (m_name (msgnum
)));
574 app_msgarg(&vec
, NULL
);
576 execvp (mhlproc
, vec
.msgs
);
577 fprintf (stderr
, "unable to exec ");
583 cpydata (pd
[0], out
, vec
.msgs
[0], file
);
585 pidXwait(child_id
, mhlproc
);
592 * Copy the messages into the draft. The messages are
593 * not filtered through the mhlproc. Do dashstuffing if
598 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
600 int fd
,i
, msgcnt
, msgnum
;
602 register char *bp
, *msgnam
;
606 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
607 if (is_selected (mp
, msgnum
)) {
609 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
612 /* Get buffer ready to go */
614 buflen
= sizeof(buffer
);
616 strncpy (bp
, "\n-------", buflen
);
621 if (msgnum
== mp
->lowsel
) {
622 snprintf (bp
, buflen
, " Forwarded Message%s",
623 mp
->numsel
> 1 ? "s" : "");
625 snprintf (bp
, buflen
, " Message %d", msgcnt
);
631 strncpy (bp
, "\n\n", buflen
);
633 write (out
, buffer
, strlen (buffer
));
635 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
636 admonish (msgnam
, "unable to read message");
641 * Copy the message. Add RFC934 quoting (dashstuffing)
642 * unless given the -nodashstuffing flag.
645 cpydgst (fd
, out
, msgnam
, file
);
647 cpydata (fd
, out
, msgnam
, file
);
655 strncpy (buffer
, delim4
, sizeof(buffer
));
657 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n",
658 mp
->numsel
> 1 ? "s" : "");
660 write (out
, buffer
, strlen (buffer
));
663 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
664 digest
, volume
, issue
);
666 for (bp
= buffer
+ i
; i
> 1; i
--)
670 write (out
, buffer
, strlen (buffer
));
676 * Create a mhn composition file for forwarding message.
680 copy_mime_draft (int out
)
685 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
686 mp
->numsel
== 1 ? "" : "s", mp
->foldpath
);
687 write (out
, buffer
, strlen (buffer
));
688 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
689 if (is_selected (mp
, msgnum
)) {
690 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
691 write (out
, buffer
, strlen (buffer
));
693 write (out
, "\n", 1);