]>
diplodocus.org Git - nmh/blob - uip/forw.c
3 * forw.c -- forward a message, or group of messages.
10 #include <h/fmt_scan.h>
11 #include <zotnet/tws/tws.h>
14 #define IFORMAT "digest-issue-%s"
15 #define VFORMAT "digest-volume-%s"
17 static struct swit switches
[] = {
23 { "draftfolder +folder", 0 },
25 { "draftmessage msg", 0 },
27 { "nodraftfolder", 0 },
29 { "editor editor", 0 },
33 { "filter filterfile", 0 },
35 { "form formfile", 0 },
51 { "issue number", 0 },
53 { "volume number", 0 },
55 { "whatnowproc program", 0 },
57 { "nowhatnowproc", 0 },
59 { "dashstuffing", 0 }, /* interface to mhl */
60 #define NBITSTUFFSW 21
61 { "nodashstuffing", 0 },
67 { "file file", 4 }, /* interface from msh */
71 { "build", 5 }, /* interface from mhe */
77 static struct swit aqrnl
[] = {
85 { "refile +folder", 0 },
91 static struct swit aqrl
[] = {
95 { "refile +folder", 0 },
99 static char drft
[BUFSIZ
];
101 static char delim3
[] =
102 "\n------------------------------------------------------------\n\n";
103 static char delim4
[] = "\n------------------------------\n\n";
106 static struct msgs
*mp
= NULL
; /* used a lot */
112 static void mhl_draft (int, char *, int, int, char *, char *, int);
113 static void copy_draft (int, char *, char *, int, int, int);
114 static void copy_mime_draft (int);
115 static int build_form (char *, char *, int, int);
119 main (int argc
, char **argv
)
121 int msgp
= 0, anot
= 0, inplace
= 1, mime
= 0;
122 int issue
= 0, volume
= 0, dashstuff
= 0;
123 int nedit
= 0, nwhat
= 0, i
, in
;
124 int out
, isdf
= 0, msgnum
;
125 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
126 char *dmsg
= NULL
, *digest
= NULL
, *ed
= NULL
;
127 char *file
= NULL
, *filter
= NULL
, *folder
= NULL
;
128 char *form
= NULL
, buf
[BUFSIZ
], value
[10];
129 char **argp
, **arguments
, *msgs
[MAXARGS
];
137 setlocale(LC_ALL
, "");
139 invo_name
= r1bindex (argv
[0], '/');
141 /* read user profile/context */
144 arguments
= getarguments (invo_name
, argc
, argv
, 1);
147 while ((cp
= *argp
++)) {
149 switch (smatch (++cp
, switches
)) {
151 ambigsw (cp
, switches
);
154 adios (NULL
, "-%s unknown", cp
);
157 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
159 print_help (buf
, switches
, 1);
162 print_version(invo_name
);
173 if (!(ed
= *argp
++) || *ed
== '-')
174 adios (NULL
, "missing argument to %s", argp
[-2]);
182 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
183 adios (NULL
, "missing argument to %s", argp
[-2]);
188 buildsw
++; /* fall... */
196 adios (NULL
, "only one file at a time!");
197 if (!(cp
= *argp
++) || *cp
== '-')
198 adios (NULL
, "missing argument to %s", argp
[-2]);
199 file
= path (cp
, TFILE
);
202 if (!(cp
= *argp
++) || *cp
== '-')
203 adios (NULL
, "missing argument to %s", argp
[-2]);
204 filter
= getcpy (etcpath (cp
));
208 if (!(form
= *argp
++) || *form
== '-')
209 adios (NULL
, "missing argument to %s", argp
[-2]);
213 filter
= getcpy (etcpath (mhlforward
));
235 if (!(digest
= *argp
++) || *digest
== '-')
236 adios (NULL
, "missing argument to %s", argp
[-2]);
240 if (!(cp
= *argp
++) || *cp
== '-')
241 adios (NULL
, "missing argument to %s", argp
[-2]);
242 if ((issue
= atoi (cp
)) < 1)
243 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
246 if (!(cp
= *argp
++) || *cp
== '-')
247 adios (NULL
, "missing argument to %s", argp
[-2]);
248 if ((volume
= atoi (cp
)) < 1)
249 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
254 adios (NULL
, "only one draft folder at a time!");
255 if (!(cp
= *argp
++) || *cp
== '-')
256 adios (NULL
, "missing argument to %s", argp
[-2]);
257 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
258 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
262 adios (NULL
, "only one draft message at a time!");
263 if (!(dmsg
= *argp
++) || *dmsg
== '-')
264 adios (NULL
, "missing argument to %s", argp
[-2]);
272 dashstuff
= 1; /* trinary logic */
275 dashstuff
= -1; /* trinary logic */
279 if (*cp
== '+' || *cp
== '@') {
281 adios (NULL
, "only one folder at a time!");
283 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
289 cwd
= getcpy (pwd ());
291 if (!context_find ("path"))
292 free (path ("./", TFOLDER
));
293 if (file
&& (msgp
|| folder
))
294 adios (NULL
, "can't mix files and folders/msgs");
299 strncpy (drft
, buildsw
? m_maildir ("draft")
300 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
302 /* Check if a draft already exists */
303 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
305 strncpy (drft
, m_draft (dfolder
, dmsg
, NOUSE
, &isdf
), sizeof(drft
));
307 /* Check if a draft already exists */
308 if (stat (drft
, &st
) != NOTOK
) {
310 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
311 for (i
= LISTDSW
; i
!= YESW
;) {
312 if (!(argp
= getans ("\nDisposition? ", isdf
? aqrnl
: aqrl
)))
314 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
323 showfile (++argp
, drft
);
326 if (refile (++argp
, drft
) == 0)
330 advise (NULL
, "say what?");
340 anot
= 0; /* don't want to annotate a file */
343 * Forwarding a message.
346 msgs
[msgp
++] = "cur";
348 folder
= getfolder (1);
349 maildir
= m_maildir (folder
);
351 if (chdir (maildir
) == NOTOK
)
352 adios (maildir
, "unable to change directory to");
354 /* read folder and create message structure */
355 if (!(mp
= folder_read (folder
)))
356 adios (NULL
, "unable to read folder %s", folder
);
358 /* check for empty folder */
360 adios (NULL
, "no messages in %s", folder
);
362 /* parse all the message ranges/sequences and set SELECTED */
363 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
364 if (!m_convert (mp
, msgs
[msgnum
]))
366 seq_setprev (mp
); /* set the previous sequence */
369 if (filter
&& access (filter
, R_OK
) == NOTOK
)
370 adios (filter
, "unable to read");
373 * Open form (component) file.
377 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
379 && (cp
= context_find (buf
))
380 && ((issue
= atoi (cp
)) < 0))
385 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
386 if ((cp
= context_find (buf
)) == NULL
|| (volume
= atoi (cp
)) <= 0)
390 in
= build_form (form
, digest
, volume
, issue
);
393 if ((in
= open (etcpath (form
), O_RDONLY
)) == NOTOK
)
394 adios (form
, "unable to open form file");
396 if ((in
= open (etcpath (forwcomps
), O_RDONLY
)) == NOTOK
)
397 adios (forwcomps
, "unable to open default components file");
402 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
403 adios (drft
, "unable to create");
406 * copy the components into the draft
408 cpydata (in
, out
, form
, drft
);
412 /* just copy the file into the draft */
413 if ((in
= open (file
, O_RDONLY
)) == NOTOK
)
414 adios (file
, "unable to open");
415 cpydata (in
, out
, file
, drft
);
420 * If filter file is defined, then format the
421 * messages into the draft using mhlproc.
424 mhl_draft (out
, digest
, volume
, issue
, drft
, filter
, dashstuff
);
426 copy_mime_draft (out
);
428 copy_draft (out
, digest
, drft
, volume
, issue
, dashstuff
);
432 snprintf (buf
, sizeof(buf
), IFORMAT
, digest
);
433 snprintf (value
, sizeof(value
), "%d", issue
);
434 context_replace (buf
, getcpy (value
));
435 snprintf (buf
, sizeof(buf
), VFORMAT
, digest
);
436 snprintf (value
, sizeof(value
), "%d", volume
);
437 context_replace (buf
, getcpy (value
));
440 context_replace (pfolder
, folder
); /* update current folder */
441 seq_setcur (mp
, mp
->lowsel
); /* update current message */
442 seq_save (mp
); /* synchronize sequences */
443 context_save (); /* save the context file */
448 what_now (ed
, nedit
, NOUSE
, drft
, NULL
, 0, mp
,
449 anot
? "Forwarded" : NULL
, inplace
, cwd
);
455 * Filter the messages you are forwarding, into the
456 * draft calling the mhlproc, and reading its output
461 mhl_draft (int out
, char *digest
, int volume
, int issue
,
462 char *file
, char *filter
, int dashstuff
)
465 int i
, msgnum
, pd
[2];
470 if (pipe (pd
) == NOTOK
)
471 adios ("pipe", "unable to create");
473 vec
[0] = r1bindex (mhlproc
, '/');
475 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
479 adios ("fork", "unable to");
487 vec
[i
++] = "-forwall";
492 vec
[i
++] = "-digest";
495 snprintf (buf1
, sizeof(buf1
), "%d", issue
);
497 vec
[i
++] = "-volume";
498 snprintf (buf2
, sizeof(buf2
), "%d", volume
);
503 * Are we dashstuffing (quoting) the lines that begin
504 * with `-'. We use the mhl default (don't add any flag)
505 * unless the user has specified a specific flag.
508 vec
[i
++] = "-dashstuffing";
509 else if (dashstuff
< 0)
510 vec
[i
++] = "-nodashstuffing";
512 if (mp
->numsel
>= MAXARGS
- i
)
513 adios (NULL
, "more than %d messages for %s exec",
514 vec
[0], MAXARGS
- i
);
517 * Now add the message names to filter. We can only
518 * handle about 995 messages (because vec is fixed size),
519 * but that should be plenty.
521 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
&& i
< sizeof(vec
) - 1;
523 if (is_selected (mp
, msgnum
))
524 vec
[i
++] = getcpy (m_name (msgnum
));
527 execvp (mhlproc
, vec
);
528 fprintf (stderr
, "unable to exec ");
534 cpydata (pd
[0], out
, vec
[0], file
);
536 pidXwait(child_id
, mhlproc
);
543 * Copy the messages into the draft. The messages are
544 * not filtered through the mhlproc. Do dashstuffing if
549 copy_draft (int out
, char *digest
, char *file
, int volume
, int issue
, int dashstuff
)
551 int fd
,i
, msgcnt
, msgnum
;
553 register char *bp
, *msgnam
;
557 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
558 if (is_selected (mp
, msgnum
)) {
560 strncpy (buffer
, msgnum
== mp
->lowsel
? delim3
: delim4
,
563 /* Get buffer ready to go */
565 buflen
= sizeof(buffer
);
567 strncpy (bp
, "\n-------", buflen
);
572 if (msgnum
== mp
->lowsel
) {
573 snprintf (bp
, buflen
, " Forwarded Message%s",
574 mp
->numsel
> 1 ? "s" : "");
576 snprintf (bp
, buflen
, " Message %d", msgcnt
);
582 strncpy (bp
, "\n\n", buflen
);
584 write (out
, buffer
, strlen (buffer
));
586 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
587 admonish (msgnam
, "unable to read message");
592 * Copy the message. Add RFC934 quoting (dashstuffing)
593 * unless given the -nodashstuffing flag.
596 cpydgst (fd
, out
, msgnam
, file
);
598 cpydata (fd
, out
, msgnam
, file
);
606 strncpy (buffer
, delim4
, sizeof(buffer
));
608 snprintf (buffer
, sizeof(buffer
), "\n------- End of Forwarded Message%s\n\n",
609 mp
->numsel
> 1 ? "s" : "");
611 write (out
, buffer
, strlen (buffer
));
614 snprintf (buffer
, sizeof(buffer
), "End of %s Digest [Volume %d Issue %d]\n",
615 digest
, volume
, issue
);
617 for (bp
= buffer
+ i
; i
> 1; i
--)
621 write (out
, buffer
, strlen (buffer
));
627 * Create a mhn composition file for forwarding message.
631 copy_mime_draft (int out
)
636 snprintf (buffer
, sizeof(buffer
), "#forw [forwarded message%s] +%s",
637 mp
->numsel
== 1 ? "" : "s", mp
->foldpath
);
638 write (out
, buffer
, strlen (buffer
));
639 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
640 if (is_selected (mp
, msgnum
)) {
641 snprintf (buffer
, sizeof(buffer
), " %s", m_name (msgnum
));
642 write (out
, buffer
, strlen (buffer
));
644 write (out
, "\n", 1);
649 build_form (char *form
, char *digest
, int volume
, int issue
)
654 char *line
, tmpfil
[BUFSIZ
];
656 register struct comp
*cptr
;
660 /* Get new format string */
661 nfs
= new_fs (form
, NULL
, NULL
);
662 fmtsize
= strlen (nfs
) + 256;
664 /* Compile format string */
665 fmt_compile (nfs
, &fmt
);
667 FINDCOMP (cptr
, "digest");
669 cptr
->c_text
= digest
;
670 FINDCOMP (cptr
, "date");
672 cptr
->c_text
= getcpy(dtimenow (0));
680 strncpy (tmpfil
, m_tmpfil (invo_name
), sizeof(tmpfil
));
681 if ((tmp
= fopen (tmpfil
, "w+")) == NULL
)
682 adios (tmpfil
, "unable to create");
684 if ((in
= dup (fileno (tmp
))) == NOTOK
)
685 adios ("dup", "unable to");
687 if ((line
= malloc ((unsigned) fmtsize
)) == NULL
)
688 adios (NULL
, "unable to allocate format line storage");
689 fmt_scan (fmt
, line
, fmtsize
, dat
);
693 adios (tmpfil
, "error writing");
695 lseek (in
, (off_t
) 0, SEEK_SET
);