]>
diplodocus.org Git - nmh/blob - uip/sendsbr.c
3 * sendsbr.c -- routines to help WhatNow/Send along
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.
11 #include <h/signals.h>
19 #ifdef HAVE_SYS_TIME_H
20 # include <sys/time.h>
24 int debugsw
= 0; /* global */
32 char *altmsg
= NULL
; /* .. */
33 char *annotext
= NULL
;
34 char *distfile
= NULL
;
41 static void armed_done (int) NORETURN
;
42 static void alert (char *, int);
43 static int tmp_fd (void);
44 static void anno (int, struct stat
*);
45 static void annoaux (int);
46 static int splitmsg (char **, int, char *, char *, struct stat
*, int);
47 static int sendaux (char **, int, char *, char *, struct stat
*);
51 * Entry point into (back-end) routines to send message.
55 sendsbr (char **vec
, int vecp
, char *program
, char *drft
, struct stat
*st
,
60 char buffer
[BUFSIZ
], file
[BUFSIZ
];
62 char **buildvec
, *buildprogram
;
65 * Run the mimebuildproc (which is by default mhbuild) on the message
66 * with the addition of the "-auto" flag
69 switch (child
= fork()) {
71 adios("fork", "unable to");
75 buildvec
= argsplit(buildmimeproc
, &buildprogram
, &i
);
76 buildvec
[i
++] = "-auto";
78 buildvec
[i
++] = "-dist";
81 execvp(buildprogram
, buildvec
);
82 fprintf(stderr
, "unable to exec ");
83 perror(buildmimeproc
);
88 if (pidXwait(child
, buildmimeproc
))
94 switch (setjmp (env
)) {
97 * If given -push and -unique (which is undocumented), then
98 * rename the draft file. I'm not quite sure why.
100 if (pushsw
&& unique
) {
101 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, NULL
);
103 adios ("sendsbr", "unable to create temporary file");
105 if (rename (drft
, strncpy(file
, cp
, sizeof(file
))) == NOTOK
)
106 adios (file
, "unable to rename %s to", drft
);
111 * Check if we need to split the message into
112 * multiple messages of type "message/partial".
114 if (splitsw
>= 0 && !distfile
&& stat (drft
, &sts
) != NOTOK
115 && sts
.st_size
>= CPERMSG
) {
116 status
= splitmsg (vec
, vecp
, program
, drft
, st
, splitsw
) ? NOTOK
: OK
;
118 status
= sendaux (vec
, vecp
, program
, drft
, st
) ? NOTOK
: OK
;
121 /* rename the original draft */
122 if (rename_drft
&& status
== OK
&&
123 rename (drft
, strncpy (buffer
, m_backup (drft
), sizeof(buffer
))) == NOTOK
)
124 advise (buffer
, "unable to rename %s to", drft
);
140 * Split large message into several messages of
141 * type "message/partial" and send them.
145 splitmsg (char **vec
, int vecp
, char *program
, char *drft
,
146 struct stat
*st
, int delay
)
148 int compnum
, nparts
, partno
, state
, status
;
151 char *cp
, *dp
, buffer
[BUFSIZ
], msgid
[BUFSIZ
];
152 char subject
[BUFSIZ
];
153 char name
[NAMESZ
], partnum
[BUFSIZ
];
155 m_getfld_state_t gstate
= 0;
157 if ((in
= fopen (drft
, "r")) == NULL
)
158 adios (drft
, "unable to open for reading");
164 * Scan through the message and examine the various header fields,
165 * as well as locate the beginning of the message body.
167 m_getfld_track_filepos (&gstate
, in
);
168 for (compnum
= 1;;) {
169 int bufsz
= sizeof buffer
;
170 switch (state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
)) {
176 * This header field is discarded.
178 if (!strcasecmp (name
, "Message-ID")) {
179 while (state
== FLDPLUS
) {
180 bufsz
= sizeof buffer
;
181 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
183 } else if (uprf (name
, XXX_FIELD_PRF
)
184 || !strcasecmp (name
, VRSN_FIELD
)
185 || !strcasecmp (name
, "Subject")
186 || !strcasecmp (name
, "Encrypted")) {
188 * These header fields are copied to the enclosed
189 * header of the first message in the collection
190 * of message/partials. For the "Subject" header
191 * field, we also record it, so that a modified
192 * version of it, can be copied to the header
193 * of each messsage/partial in the collection.
195 if (!strcasecmp (name
, "Subject")) {
198 strncpy (subject
, buffer
, BUFSIZ
);
199 sublen
= strlen (subject
);
200 if (sublen
> 0 && subject
[sublen
- 1] == '\n')
201 subject
[sublen
- 1] = '\0';
204 dp
= add (concat (name
, ":", buffer
, NULL
), dp
);
205 while (state
== FLDPLUS
) {
206 bufsz
= sizeof buffer
;
207 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
208 dp
= add (buffer
, dp
);
212 * These header fields are copied to the header of
213 * each message/partial in the collection.
215 cp
= add (concat (name
, ":", buffer
, NULL
), cp
);
216 while (state
== FLDPLUS
) {
217 bufsz
= sizeof buffer
;
218 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
219 cp
= add (buffer
, cp
);
223 start
= ftell (in
) + 1;
232 adios (NULL
, "message format error in component #%d", compnum
);
235 adios (NULL
, "getfld () returned %d", state
);
240 m_getfld_state_destroy (&gstate
);
242 adios (NULL
, "headers missing from draft");
246 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
249 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
255 /* Only one part, nothing to split */
262 return sendaux (vec
, vecp
, program
, drft
, st
);
266 printf ("Sending as %d Partial Messages\n", nparts
);
271 vec
[vecp
++] = "-partno";
272 vec
[vecp
++] = partnum
;
274 vec
[vecp
++] = "-queued";
277 snprintf (msgid
, sizeof(msgid
), "%s", message_id (clock
, 0));
279 fseek (in
, start
, SEEK_SET
);
280 for (partno
= 1; partno
<= nparts
; partno
++) {
284 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, &out
);
286 adios (drft
, "unable to create temporary file for");
288 strncpy(tmpdrf
, cp
, sizeof(tmpdrf
));
289 chmod (tmpdrf
, 0600);
292 * Output the header fields
295 fprintf (out
, "Subject: %s (part %d of %d)\n", subject
, partno
, nparts
);
296 fprintf (out
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
297 fprintf (out
, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD
, msgid
);
298 fprintf (out
, "\tnumber=%d; total=%d\n", partno
, nparts
);
299 fprintf (out
, "%s: part %d of %d\n\n", DESCR_FIELD
, partno
, nparts
);
302 * If this is the first in the collection, output the
303 * header fields we are encapsulating at the beginning
304 * of the body of the first message.
309 fprintf (out
, "Message-ID: %s\n", msgid
);
317 if (!fgets (buffer
, sizeof(buffer
) - 1, in
)) {
318 if (partno
== nparts
)
320 adios (NULL
, "premature eof");
323 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
324 fseek (in
, -len
, SEEK_CUR
);
332 adios (tmpdrf
, "error writing to");
336 if (!pushsw
&& verbsw
) {
341 /* Pause here, if a delay is specified */
342 if (delay
> 0 && 1 < partno
&& partno
<= nparts
) {
344 printf ("pausing %d seconds before sending part %d...\n",
348 sleep ((unsigned int) delay
);
351 snprintf (partnum
, sizeof(partnum
), "%d", partno
);
352 status
= sendaux (vec
, vecp
, program
, tmpdrf
, st
);
358 * This is so sendaux will only annotate
359 * the altmsg the first time it is called.
368 fclose (in
); /* close the draft */
374 * Annotate original message, and
375 * call `postproc' (which is passed down in "program") to send message.
379 sendaux (char **vec
, int vecp
, char *program
, char *drft
, struct stat
*st
)
382 int i
, status
, fd
, fd2
;
383 char backup
[BUFSIZ
], buf
[BUFSIZ
];
385 fd
= pushsw
? tmp_fd () : NOTOK
;
390 if ((fd2
= tmp_fd ()) != NOTOK
) {
391 vec
[vecp
++] = "-idanno";
392 snprintf (buf
, sizeof(buf
), "%d", fd2
);
395 admonish (NULL
, "unable to create file for annotation list");
398 if (distfile
&& distout (drft
, distfile
, backup
) == NOTOK
)
402 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
407 /* oops -- fork error */
408 adios ("fork", "unable to");
409 break; /* NOT REACHED */
413 * child process -- send it
415 * If fd is ok, then we are pushing and fd points to temp
416 * file, so capture anything on stdout and stderr there.
419 dup2 (fd
, fileno (stdout
));
420 dup2 (fd
, fileno (stderr
));
423 execvp (program
, vec
);
424 fprintf (stderr
, "unable to exec ");
430 * parent process -- wait for it
432 if ((status
= pidwait(child_id
, NOTOK
)) == OK
) {
433 if (annotext
&& fd2
!= NOTOK
)
437 * If postproc failed, and we have good fd (which means
438 * we pushed), then mail error message (and possibly the
439 * draft) back to the user.
445 advise (NULL
, "message not delivered to anyone");
447 if (annotext
&& fd2
!= NOTOK
)
451 if (rename (backup
, drft
) == NOTOK
)
452 advise (drft
, "unable to rename %s to", backup
);
463 * Mail error notification (and possibly a copy of the
464 * message) back to the user, using the mailproc
468 alert (char *file
, int out
)
476 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
481 /* oops -- fork error */
482 advise ("fork", "unable to");
485 /* child process -- send it */
486 SIGNAL (SIGHUP
, SIG_IGN
);
487 SIGNAL (SIGINT
, SIG_IGN
);
488 SIGNAL (SIGQUIT
, SIG_IGN
);
489 SIGNAL (SIGTERM
, SIG_IGN
);
491 if ((in
= open (file
, O_RDONLY
)) == NOTOK
) {
492 admonish (file
, "unable to re-open");
494 lseek (out
, (off_t
) 0, SEEK_END
);
495 strncpy (buf
, "\nMessage not delivered to anyone.\n", sizeof(buf
));
496 write (out
, buf
, strlen (buf
));
497 strncpy (buf
, "\n------- Unsent Draft\n\n", sizeof(buf
));
498 write (out
, buf
, strlen (buf
));
499 cpydgst (in
, out
, file
, "temporary file");
501 strncpy (buf
, "\n------- End of Unsent Draft\n", sizeof(buf
));
502 write (out
, buf
, strlen (buf
));
503 if (rename (file
, strncpy (buf
, m_backup (file
), sizeof(buf
))) == NOTOK
)
504 admonish (buf
, "unable to rename %s to", file
);
507 lseek (out
, (off_t
) 0, SEEK_SET
);
508 dup2 (out
, fileno (stdin
));
510 /* create subject for error notification */
511 snprintf (buf
, sizeof(buf
), "send failed on %s",
512 forwsw
? "enclosed draft" : file
);
514 arglist
= argsplit(mailproc
, &program
, &argp
);
516 arglist
[argp
++] = getusername();
517 arglist
[argp
++] = "-subject";
518 arglist
[argp
++] = buf
;
519 arglist
[argp
] = NULL
;
521 execvp (program
, arglist
);
522 fprintf (stderr
, "unable to exec ");
526 default: /* no waiting... */
538 tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
);
539 if (tfile
== NULL
) return NOTOK
;
543 advise (NULL
, "temporary file %s selected", tfile
);
545 if (unlink (tfile
) == NOTOK
)
546 advise (tfile
, "unable to remove");
553 anno (int fd
, struct stat
*st
)
557 static char *cwd
= NULL
;
561 (stat (altmsg
, &st2
) == NOTOK
562 || st
->st_mtime
!= st2
.st_mtime
563 || st
->st_dev
!= st2
.st_dev
564 || st
->st_ino
!= st2
.st_ino
)) {
566 admonish (NULL
, "$mhaltmsg mismatch");
570 child_id
= debugsw
? NOTOK
: fork ();
572 case NOTOK
: /* oops */
575 "unable to fork, so doing annotations by hand...");
577 cwd
= getcpy (pwd ());
580 /* block a few signals */
582 sigaddset (&set
, SIGHUP
);
583 sigaddset (&set
, SIGINT
);
584 sigaddset (&set
, SIGQUIT
);
585 sigaddset (&set
, SIGTERM
);
586 sigprocmask (SIG_BLOCK
, &set
, &oset
);
592 /* reset the signal mask */
593 sigprocmask (SIG_SETMASK
, &oset
, &set
);
598 default: /* no waiting... */
608 int fd2
, fd3
, msgnum
;
609 char *cp
, *folder
, *maildir
;
610 char buffer
[BUFSIZ
], **ap
;
614 if ((folder
= getenv ("mhfolder")) == NULL
|| *folder
== 0) {
616 admonish (NULL
, "$mhfolder not set");
619 maildir
= m_maildir (folder
);
620 if (chdir (maildir
) == NOTOK
) {
622 admonish (maildir
, "unable to change directory to");
625 if (!(mp
= folder_read (folder
, 0))) {
627 admonish (NULL
, "unable to read folder %s", folder
);
631 /* check for empty folder */
632 if (mp
->nummsg
== 0) {
634 admonish (NULL
, "no messages in %s", folder
);
638 if ((cp
= getenv ("mhmessages")) == NULL
|| *cp
== 0) {
640 admonish (NULL
, "$mhmessages not set");
643 if (!debugsw
/* MOBY HACK... */
645 && (fd3
= open ("/dev/null", O_RDWR
)) != NOTOK
646 && (fd2
= dup (fileno (stderr
))) != NOTOK
) {
647 dup2 (fd3
, fileno (stderr
));
652 for (ap
= brkstring (cp
= getcpy (cp
), " ", NULL
); *ap
; ap
++)
656 dup2 (fd2
, fileno (stderr
));
657 if (mp
->numsel
== 0) {
659 admonish (NULL
, "no messages to annotate");
663 lseek (fd
, (off_t
) 0, SEEK_SET
);
664 if ((fp
= fdopen (fd
, "r")) == NULL
) {
666 admonish (NULL
, "unable to fdopen annotation list");
670 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
671 cp
= add (buffer
, cp
);
675 advise (NULL
, "annotate%s with %s: \"%s\"",
676 inplace
? " inplace" : "", annotext
, cp
);
677 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
678 if (is_selected(mp
, msgnum
)) {
680 advise (NULL
, "annotate message %d", msgnum
);
681 annotate (m_name (msgnum
), annotext
, cp
, inplace
, 1, -2, 0);
688 folder_free (mp
); /* free folder/message structure */
693 armed_done (int status
)
695 longjmp (env
, status
? status
: NOTOK
);