]>
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.
12 #include <h/fmt_scan.h>
17 #define IFORMAT "digest-issue-%s"
18 #define VFORMAT "digest-volume-%s"
20 static struct swit switches
[] = {
26 { "draftfolder +folder", 0 },
28 { "draftmessage msg", 0 },
30 { "nodraftfolder", 0 },
32 { "editor editor", 0 },
36 { "filter filterfile", 0 },
38 { "form formfile", 0 },
54 { "issue number", 0 },
56 { "volume number", 0 },
58 { "whatnowproc program", 0 },
60 { "nowhatnowproc", 0 },
62 { "dashstuffing", 0 }, /* interface to mhl */
63 #define NBITSTUFFSW 21
64 { "nodashstuffing", 0 },
70 { "file file", 4 }, /* interface from msh */
74 { "build", 5 }, /* interface from mhe */
80 static struct swit aqrnl
[] = {
88 { "refile +folder", 0 },
94 static struct swit aqrl
[] = {
98 { "refile +folder", 0 },
102 static char drft
[BUFSIZ
];
104 static char delim3
[] =
105 "\n------------------------------------------------------------\n\n";
106 static char delim4
[] = "\n------------------------------\n\n";
109 static struct msgs
*mp
= NULL
; /* used a lot */
115 static void mhl_draft (int, char *, int, int, char *, char *, int);
116 static void copy_draft (int, char *, char *, int, int, int);
117 static void copy_mime_draft (int);
118 static int build_form (char *, char *, int, int);
122 main (int argc
, char **argv
)
124 int msgp
= 0, anot
= 0, inplace
= 1, mime
= 0;
125 int issue
= 0, volume
= 0, dashstuff
= 0;
126 int nedit
= 0, nwhat
= 0, i
, in
;
127 int out
, isdf
= 0, msgnum
;
128 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
129 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
130 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
;
131 char *form
= NULL
, buf
[BUFSIZ
], value
[10];
132 char **argp
, **arguments
, *msgs
[MAXARGS
];
140 setlocale(LC_ALL
, "");
142 invo_name
= r1bindex (argv
[0], '/');
144 /* read user profile/context */
147 arguments
= getarguments (invo_name
, argc
, argv
, 1);
150 while ((cp
= *argp
++)) {
152 switch (smatch (++cp
, switches
)) {
154 ambigsw (cp
, switches
);
157 adios (NULL
, "-%s unknown", cp
);
160 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
162 print_help (buf
, switches
, 1);
165 print_version(invo_name
);
176 if (!(ed
= *argp
++) || *ed
== '-')
177 adios (NULL
, "missing argument to %s", argp
[-2]);
185 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
191 buildsw
++; /* fall... */
199 adios (NULL
, "only one file at a time!");
200 if (!(cp
= *argp
++) || *cp
== '-')
201 adios (NULL
, "missing argument to %s", argp
[-2]);
202 file
= path (cp
, TFILE
);
205 if (!(cp
= *argp
++) || *cp
== '-')
206 adios (NULL
, "missing argument to %s", argp
[-2]);
207 filter
= getcpy (etcpath (cp
));
211 if (!(form
= *argp
++) || *form
== '-')
212 adios (NULL
, "missing argument to %s", argp
[-2]);
216 filter
= getcpy (etcpath (mhlforward
));
238 if (!(digest
= *argp
++) || *digest
== '-')
239 adios (NULL
, "missing argument to %s", argp
[-2]);
243 if (!(cp
= *argp
++) || *cp
== '-')
244 adios (NULL
, "missing argument to %s", argp
[-2]);
245 if ((issue
= atoi (cp
)) < 1)
246 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
249 if (!(cp
= *argp
++) || *cp
== '-')
250 adios (NULL
, "missing argument to %s", argp
[-2]);
251 if ((volume
= atoi (cp
)) < 1)
252 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
257 adios (NULL
, "only one draft folder at a time!");
258 if (!(cp
= *argp
++) || *cp
== '-')
259 adios (NULL
, "missing argument to %s", argp
[-2]);
260 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
261 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
265 adios (NULL
, "only one draft message at a time!");
266 if (!(dmsg
= *argp
++) || *dmsg
== '-')
267 adios (NULL
, "missing argument to %s", argp
[-2]);
275 dashstuff
= 1; /* trinary logic */
278 dashstuff
= -1; /* trinary logic */
282 if (*cp
== '+' || *cp
== '@') {
284 adios (NULL
, "only one folder at a time!");
286 folder
= pluspath (cp
);
292 cwd
= getcpy (pwd ());
294 if (!context_find ("path"))
295 free (path ("./", TFOLDER
));
296 if (file
&& (msgp
|| folder
))
297 adios (NULL
, "can't mix files and folders/msgs");
302 strncpy (drft
, buildsw
? m_maildir ("draft")
303 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
305 /* Check if a draft already exists */
306 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
308 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
310 /* Check if a draft already exists */
311 if (stat (drft
, &st
) != NOTOK
) {
313 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
314 for (i
= LISTDSW
; i
!= YESW
;) {
315 if (!(argp
= getans ("\nDisposition? ", isdf
? aqrnl
: aqrl
)))
317 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
326 showfile (++argp
, drft
);
329 if (refile (++argp
, drft
) == 0)
333 advise (NULL
, "say what?");
343 anot
= 0; /* don't want to annotate a file */
346 * Forwarding a message.
349 msgs
[msgp
++] = "cur";
351 folder
= getfolder (1);
352 maildir
= m_maildir (folder
);
354 if (chdir (maildir
) == NOTOK
)
355 adios (maildir
, "unable to change directory to");
357 /* read folder and create message structure */
358 if (!(mp
= folder_read (folder
)))
359 adios (NULL
, "unable to read folder %s", folder
);
361 /* check for empty folder */
363 adios (NULL
, "no messages in %s", folder
);
365 /* parse all the message ranges/sequences and set SELECTED */
366 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
367 if (!m_convert (mp
, msgs
[msgnum
]))
369 seq_setprev (mp
); /* set the previous sequence */
372 if (filter
&& access (filter
, R_OK
) == NOTOK
)
373 adios (filter
, "unable to read");
376 * Open form (component) file.
380 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
382 && (cp
= context_find (buf
))
383 && ((issue
= atoi (cp
)) < 0))
388 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
389 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
393 in
= build_form (form
, digest
, volume
, issue
);
395 in
= open_form(&form
, forwcomps
);
397 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
398 adios (drft
, "unable to create");
401 * copy the components into the draft
403 cpydata (in
, out
, form
, drft
);
407 /* just copy the file into the draft */
408 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
409 adios (file
, "unable to open");
410 cpydata (in
, out
, file
, drft
);
415 * If filter file is defined, then format the
416 * messages into the draft using mhlproc.
419 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
421 copy_mime_draft (out
);
423 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
427 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
428 snprintf (value
, sizeof(value
), "%d", issue
);
429 context_replace (buf
, getcpy (value
));
430 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
431 snprintf (value
, sizeof(value
), "%d", volume
);
432 context_replace (buf
, getcpy (value
));
435 context_replace (pfolder
, folder
); /* update current folder */
436 seq_setcur (mp
, mp
->lowsel
); /* update current message */
437 seq_save (mp
); /* synchronize sequences */
438 context_save (); /* save the context file */
443 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
444 anot
? "Forwarded" : NULL
, inplace
, cwd
);
451 * Filter the messages you are forwarding, into the
452 * draft calling the mhlproc, and reading its output
457 mhl_draft (int out
, char *digest
, int volume
, int issue
,
458 char *file
, char *filter
, int dashstuff
)
461 int i
, msgnum
, pd
[2];
466 if (pipe (pd
) == NOTOK
)
467 adios ("pipe", "unable to create");
469 vec
[0] = r1bindex (mhlproc
, '/');
471 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
475 adios ("fork", "unable to");
483 vec
[i
++] = "-forwall";
488 vec
[i
++] = "-digest";
491 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
493 vec
[i
++] = "-volume";
494 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
499 * Are we dashstuffing (quoting) the lines that begin
500 * with `-'. We use the mhl default (don't add any flag)
501 * unless the user has specified a specific flag.
504 vec
[i
++] = "-dashstuffing";
505 else if (dashstuff
< 0)
506 vec
[i
++] = "-nodashstuffing";
508 if (mp
->numsel
>= MAXARGS
- i
)
509 adios (NULL
, "more than %d messages for %s exec",
510 MAXARGS
- i
, vec
[0]);
513 * Now add the message names to filter. We can only
514 * handle about 995 messages (because vec is fixed size),
515 * but that should be plenty.
517 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
&& i
< sizeof(vec
) - 1;
519 if (is_selected (mp
, msgnum
))
520 vec
[i
++] = getcpy (m_name (msgnum
));
523 execvp (mhlproc
, vec
);
524 fprintf (stderr
, "unable to exec ");
530 cpydata (pd
[0], out
, vec
[0], file
);
532 pidXwait(child_id
, mhlproc
);
539 * Copy the messages into the draft. The messages are
540 * not filtered through the mhlproc. Do dashstuffing if
545 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
547 int fd
,i
, msgcnt
, msgnum
;
549 register char *bp
, *msgnam
;
553 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
554 if (is_selected (mp
, msgnum
)) {
556 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
559 /* Get buffer ready to go */
561 buflen
= sizeof(buffer
);
563 strncpy (bp
, "\n-------", buflen
);
568 if (msgnum
== mp
->lowsel
) {
569 snprintf (bp
, buflen
, " Forwarded Message%s",
570 mp
->numsel
> 1 ? "s" : "");
572 snprintf (bp
, buflen
, " Message %d", msgcnt
);
578 strncpy (bp
, "\n\n", buflen
);
580 write (out
, buffer
, strlen (buffer
));
582 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
583 admonish (msgnam
, "unable to read message");
588 * Copy the message. Add RFC934 quoting (dashstuffing)
589 * unless given the -nodashstuffing flag.
592 cpydgst (fd
, out
, msgnam
, file
);
594 cpydata (fd
, out
, msgnam
, file
);
602 strncpy (buffer
, delim4
, sizeof(buffer
));
604 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n\n",
605 mp
->numsel
> 1 ? "s" : "");
607 write (out
, buffer
, strlen (buffer
));
610 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
611 digest
, volume
, issue
);
613 for (bp
= buffer
+ i
; i
> 1; i
--)
617 write (out
, buffer
, strlen (buffer
));
623 * Create a mhn composition file for forwarding message.
627 copy_mime_draft (int out
)
632 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
633 mp
->numsel
== 1 ? "" : "s", mp
->foldpath
);
634 write (out
, buffer
, strlen (buffer
));
635 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
636 if (is_selected (mp
, msgnum
)) {
637 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
638 write (out
, buffer
, strlen (buffer
));
640 write (out
, "\n", 1);
645 build_form (char *form
, char *digest
, int volume
, int issue
)
650 char *line
, tmpfil
[BUFSIZ
];
652 register struct comp
*cptr
;
657 /* Get new format string */
658 nfs
= new_fs (form
, NULL
, NULL
);
659 fmtsize
= strlen (nfs
) + 256;
661 /* Compile format string */
662 fmt_compile (nfs
, &fmt
);
664 FINDCOMP (cptr
, "digest");
666 cptr
->c_text
= digest
;
667 FINDCOMP (cptr
, "date");
669 cptr
->c_text
= getcpy(dtimenow (0));
677 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &tmp
);
678 if (cp
== NULL
) adios("forw", "unable to create temporary file");
679 strncpy (tmpfil
, cp
, sizeof(tmpfil
));
681 if ((in
= dup (fileno (tmp
))) == NOTOK
)
682 adios ("dup", "unable to");
684 line
= mh_xmalloc ((unsigned) fmtsize
);
685 fmt_scan (fmt
, line
, fmtsize
, dat
);
689 adios (tmpfil
, "error writing");
691 lseek (in
, (off_t
) 0, SEEK_SET
);