]>
diplodocus.org Git - nmh/blob - uip/forw.c
3 * forw.c -- forward a message, or group of messages.
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/fmt_scan.h>
18 #define IFORMAT "digest-issue-%s"
19 #define VFORMAT "digest-volume-%s"
21 static struct swit switches
[] = {
27 { "draftfolder +folder", 0 },
29 { "draftmessage msg", 0 },
31 { "nodraftfolder", 0 },
33 { "editor editor", 0 },
37 { "filter filterfile", 0 },
39 { "form formfile", 0 },
55 { "issue number", 0 },
57 { "volume number", 0 },
59 { "whatnowproc program", 0 },
61 { "nowhatnowproc", 0 },
63 { "dashstuffing", 0 }, /* interface to mhl */
64 #define NBITSTUFFSW 21
65 { "nodashstuffing", 0 },
71 { "file file", 4 }, /* interface from msh */
75 { "build", 5 }, /* interface from mhe */
81 static struct swit aqrnl
[] = {
89 { "refile +folder", 0 },
95 static struct swit aqrl
[] = {
99 { "refile +folder", 0 },
103 static char drft
[BUFSIZ
];
105 static char delim3
[] =
106 "\n------------------------------------------------------------\n\n";
107 static char delim4
[] = "\n------------------------------\n\n";
110 static struct msgs
*mp
= NULL
; /* used a lot */
116 static void mhl_draft (int, char *, int, int, char *, char *, int);
117 static void copy_draft (int, char *, char *, int, int, int);
118 static void copy_mime_draft (int);
119 static int build_form (char *, char *, int, int);
123 main (int argc
, char **argv
)
125 int msgp
= 0, anot
= 0, inplace
= 1, mime
= 0;
126 int issue
= 0, volume
= 0, dashstuff
= 0;
127 int nedit
= 0, nwhat
= 0, i
, in
;
128 int out
, isdf
= 0, msgnum
;
129 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
130 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
131 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
;
132 char *form
= NULL
, buf
[BUFSIZ
], value
[10];
133 char **argp
, **arguments
, *msgs
[MAXARGS
];
141 setlocale(LC_ALL
, "");
143 invo_name
= r1bindex (argv
[0], '/');
145 /* read user profile/context */
148 arguments
= getarguments (invo_name
, argc
, argv
, 1);
151 while ((cp
= *argp
++)) {
153 switch (smatch (++cp
, switches
)) {
155 ambigsw (cp
, switches
);
158 adios (NULL
, "-%s unknown", cp
);
161 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
163 print_help (buf
, switches
, 1);
166 print_version(invo_name
);
177 if (!(ed
= *argp
++) || *ed
== '-')
178 adios (NULL
, "missing argument to %s", argp
[-2]);
186 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
187 adios (NULL
, "missing argument to %s", argp
[-2]);
192 buildsw
++; /* fall... */
200 adios (NULL
, "only one file at a time!");
201 if (!(cp
= *argp
++) || *cp
== '-')
202 adios (NULL
, "missing argument to %s", argp
[-2]);
203 file
= path (cp
, TFILE
);
206 if (!(cp
= *argp
++) || *cp
== '-')
207 adios (NULL
, "missing argument to %s", argp
[-2]);
208 filter
= getcpy (etcpath (cp
));
212 if (!(form
= *argp
++) || *form
== '-')
213 adios (NULL
, "missing argument to %s", argp
[-2]);
217 filter
= getcpy (etcpath (mhlforward
));
239 if (!(digest
= *argp
++) || *digest
== '-')
240 adios (NULL
, "missing argument to %s", argp
[-2]);
244 if (!(cp
= *argp
++) || *cp
== '-')
245 adios (NULL
, "missing argument to %s", argp
[-2]);
246 if ((issue
= atoi (cp
)) < 1)
247 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
250 if (!(cp
= *argp
++) || *cp
== '-')
251 adios (NULL
, "missing argument to %s", argp
[-2]);
252 if ((volume
= atoi (cp
)) < 1)
253 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
258 adios (NULL
, "only one draft folder at a time!");
259 if (!(cp
= *argp
++) || *cp
== '-')
260 adios (NULL
, "missing argument to %s", argp
[-2]);
261 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
262 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
266 adios (NULL
, "only one draft message at a time!");
267 if (!(dmsg
= *argp
++) || *dmsg
== '-')
268 adios (NULL
, "missing argument to %s", argp
[-2]);
276 dashstuff
= 1; /* trinary logic */
279 dashstuff
= -1; /* trinary logic */
283 if (*cp
== '+' || *cp
== '@') {
285 adios (NULL
, "only one folder at a time!");
287 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
293 cwd
= getcpy (pwd ());
295 if (!context_find ("path"))
296 free (path ("./", TFOLDER
));
297 if (file
&& (msgp
|| folder
))
298 adios (NULL
, "can't mix files and folders/msgs");
303 strncpy (drft
, buildsw
? m_maildir ("draft")
304 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
306 /* Check if a draft already exists */
307 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
309 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
311 /* Check if a draft already exists */
312 if (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 msgs
[msgp
++] = "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
)))
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
< msgp
; msgnum
++)
368 if (!m_convert (mp
, msgs
[msgnum
]))
370 seq_setprev (mp
); /* set the previous sequence */
373 if (filter
&& access (filter
, R_OK
) == NOTOK
)
374 adios (filter
, "unable to read");
377 * Open form (component) file.
381 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
383 && (cp
= context_find (buf
))
384 && ((issue
= atoi (cp
)) < 0))
389 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
390 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
394 in
= build_form (form
, digest
, volume
, issue
);
397 if ((in
= open (etcpath (form
), O_RDONLY
)) == NOTOK
)
398 adios (form
, "unable to open form file");
400 if ((in
= open (etcpath (forwcomps
), O_RDONLY
)) == NOTOK
)
401 adios (forwcomps
, "unable to open default components file");
406 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
407 adios (drft
, "unable to create");
410 * copy the components into the draft
412 cpydata (in
, out
, form
, drft
);
416 /* just copy the file into the draft */
417 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
418 adios (file
, "unable to open");
419 cpydata (in
, out
, file
, drft
);
424 * If filter file is defined, then format the
425 * messages into the draft using mhlproc.
428 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
430 copy_mime_draft (out
);
432 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
436 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
437 snprintf (value
, sizeof(value
), "%d", issue
);
438 context_replace (buf
, getcpy (value
));
439 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
440 snprintf (value
, sizeof(value
), "%d", volume
);
441 context_replace (buf
, getcpy (value
));
444 context_replace (pfolder
, folder
); /* update current folder */
445 seq_setcur (mp
, mp
->lowsel
); /* update current message */
446 seq_save (mp
); /* synchronize sequences */
447 context_save (); /* save the context file */
452 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
453 anot
? "Forwarded" : NULL
, inplace
, cwd
);
459 * Filter the messages you are forwarding, into the
460 * draft calling the mhlproc, and reading its output
465 mhl_draft (int out
, char *digest
, int volume
, int issue
,
466 char *file
, char *filter
, int dashstuff
)
469 int i
, msgnum
, pd
[2];
474 if (pipe (pd
) == NOTOK
)
475 adios ("pipe", "unable to create");
477 vec
[0] = r1bindex (mhlproc
, '/');
479 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
483 adios ("fork", "unable to");
491 vec
[i
++] = "-forwall";
496 vec
[i
++] = "-digest";
499 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
501 vec
[i
++] = "-volume";
502 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
507 * Are we dashstuffing (quoting) the lines that begin
508 * with `-'. We use the mhl default (don't add any flag)
509 * unless the user has specified a specific flag.
512 vec
[i
++] = "-dashstuffing";
513 else if (dashstuff
< 0)
514 vec
[i
++] = "-nodashstuffing";
516 if (mp
->numsel
>= MAXARGS
- i
)
517 adios (NULL
, "more than %d messages for %s exec",
518 vec
[0], MAXARGS
- i
);
521 * Now add the message names to filter. We can only
522 * handle about 995 messages (because vec is fixed size),
523 * but that should be plenty.
525 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
&& i
< sizeof(vec
) - 1;
527 if (is_selected (mp
, msgnum
))
528 vec
[i
++] = getcpy (m_name (msgnum
));
531 execvp (mhlproc
, vec
);
532 fprintf (stderr
, "unable to exec ");
538 cpydata (pd
[0], out
, vec
[0], file
);
540 pidXwait(child_id
, mhlproc
);
547 * Copy the messages into the draft. The messages are
548 * not filtered through the mhlproc. Do dashstuffing if
553 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
555 int fd
,i
, msgcnt
, msgnum
;
557 register char *bp
, *msgnam
;
561 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
562 if (is_selected (mp
, msgnum
)) {
564 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
567 /* Get buffer ready to go */
569 buflen
= sizeof(buffer
);
571 strncpy (bp
, "\n-------", buflen
);
576 if (msgnum
== mp
->lowsel
) {
577 snprintf (bp
, buflen
, " Forwarded Message%s",
578 mp
->numsel
> 1 ? "s" : "");
580 snprintf (bp
, buflen
, " Message %d", msgcnt
);
586 strncpy (bp
, "\n\n", buflen
);
588 write (out
, buffer
, strlen (buffer
));
590 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
591 admonish (msgnam
, "unable to read message");
596 * Copy the message. Add RFC934 quoting (dashstuffing)
597 * unless given the -nodashstuffing flag.
600 cpydgst (fd
, out
, msgnam
, file
);
602 cpydata (fd
, out
, msgnam
, file
);
610 strncpy (buffer
, delim4
, sizeof(buffer
));
612 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n\n",
613 mp
->numsel
> 1 ? "s" : "");
615 write (out
, buffer
, strlen (buffer
));
618 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
619 digest
, volume
, issue
);
621 for (bp
= buffer
+ i
; i
> 1; i
--)
625 write (out
, buffer
, strlen (buffer
));
631 * Create a mhn composition file for forwarding message.
635 copy_mime_draft (int out
)
640 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
641 mp
->numsel
== 1 ? "" : "s", mp
->foldpath
);
642 write (out
, buffer
, strlen (buffer
));
643 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
644 if (is_selected (mp
, msgnum
)) {
645 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
646 write (out
, buffer
, strlen (buffer
));
648 write (out
, "\n", 1);
653 build_form (char *form
, char *digest
, int volume
, int issue
)
658 char *line
, tmpfil
[BUFSIZ
];
660 register struct comp
*cptr
;
664 /* Get new format string */
665 nfs
= new_fs (form
, NULL
, NULL
);
666 fmtsize
= strlen (nfs
) + 256;
668 /* Compile format string */
669 fmt_compile (nfs
, &fmt
);
671 FINDCOMP (cptr
, "digest");
673 cptr
->c_text
= digest
;
674 FINDCOMP (cptr
, "date");
676 cptr
->c_text
= getcpy(dtimenow (0));
684 strncpy (tmpfil
, m_tmpfil (invo_name
), sizeof(tmpfil
));
685 if ((tmp
= fopen (tmpfil
, "w+")) == NULL
)
686 adios (tmpfil
, "unable to create");
688 if ((in
= dup (fileno (tmp
))) == NOTOK
)
689 adios ("dup", "unable to");
691 if ((line
= malloc ((unsigned) fmtsize
)) == NULL
)
692 adios (NULL
, "unable to allocate format line storage");
693 fmt_scan (fmt
, line
, fmtsize
, dat
);
697 adios (tmpfil
, "error writing");
699 lseek (in
, (off_t
) 0, SEEK_SET
);