]>
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 *draft
, struct stat
*st
,
60 char buffer
[BUFSIZ
], file
[BUFSIZ
];
62 char **buildvec
, *buildprogram
;
63 char *volatile drft
= draft
;
66 * Run the mimebuildproc (which is by default mhbuild) on the message
67 * with the addition of the "-auto" flag
70 switch (child
= fork()) {
72 adios("fork", "unable to");
76 buildvec
= argsplit(buildmimeproc
, &buildprogram
, &i
);
77 buildvec
[i
++] = "-auto";
79 buildvec
[i
++] = "-dist";
80 buildvec
[i
++] = (char *) drft
;
82 execvp(buildprogram
, buildvec
);
83 fprintf(stderr
, "unable to exec ");
84 perror(buildmimeproc
);
89 if (pidXwait(child
, buildmimeproc
))
95 switch (setjmp (env
)) {
98 * If given -push and -unique (which is undocumented), then
99 * rename the draft file. I'm not quite sure why.
101 if (pushsw
&& unique
) {
102 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, NULL
);
104 adios(NULL
, "unable to create temporary file in %s",
107 if (rename (drft
, strncpy(file
, cp
, sizeof(file
))) == NOTOK
)
108 adios (file
, "unable to rename %s to", drft
);
113 * Check if we need to split the message into
114 * multiple messages of type "message/partial".
116 if (splitsw
>= 0 && !distfile
&& stat ((char *) drft
, &sts
) != NOTOK
117 && sts
.st_size
>= CPERMSG
) {
118 status
= splitmsg (vec
, vecp
, program
, drft
,
119 st
, splitsw
) ? NOTOK
: OK
;
121 status
= sendaux (vec
, vecp
, program
, drft
, st
) ? NOTOK
: OK
;
124 /* rename the original draft */
125 if (rename_drft
&& status
== OK
&&
126 rename (drft
, strncpy (buffer
, m_backup (drft
),
127 sizeof(buffer
))) == NOTOK
)
128 advise (buffer
, "unable to rename %s to", drft
);
138 (void) m_unlink (distfile
);
144 * Split large message into several messages of
145 * type "message/partial" and send them.
149 splitmsg (char **vec
, int vecp
, char *program
, char *drft
,
150 struct stat
*st
, int delay
)
152 int compnum
, nparts
, partno
, state
, status
;
155 char *cp
, *dp
, buffer
[BUFSIZ
], msgid
[BUFSIZ
];
156 char subject
[BUFSIZ
];
157 char name
[NAMESZ
], partnum
[BUFSIZ
];
159 m_getfld_state_t gstate
= 0;
161 if ((in
= fopen (drft
, "r")) == NULL
)
162 adios (drft
, "unable to open for reading");
168 * Scan through the message and examine the various header fields,
169 * as well as locate the beginning of the message body.
171 m_getfld_track_filepos (&gstate
, in
);
172 for (compnum
= 1;;) {
173 int bufsz
= sizeof buffer
;
174 switch (state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
)) {
180 * This header field is discarded.
182 if (!strcasecmp (name
, "Message-ID")) {
183 while (state
== FLDPLUS
) {
184 bufsz
= sizeof buffer
;
185 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
187 } else if (uprf (name
, XXX_FIELD_PRF
)
188 || !strcasecmp (name
, VRSN_FIELD
)
189 || !strcasecmp (name
, "Subject")
190 || !strcasecmp (name
, "Encrypted")) {
192 * These header fields are copied to the enclosed
193 * header of the first message in the collection
194 * of message/partials. For the "Subject" header
195 * field, we also record it, so that a modified
196 * version of it, can be copied to the header
197 * of each messsage/partial in the collection.
199 if (!strcasecmp (name
, "Subject")) {
202 strncpy (subject
, buffer
, BUFSIZ
);
203 sublen
= strlen (subject
);
204 if (sublen
> 0 && subject
[sublen
- 1] == '\n')
205 subject
[sublen
- 1] = '\0';
208 dp
= add (concat (name
, ":", buffer
, NULL
), dp
);
209 while (state
== FLDPLUS
) {
210 bufsz
= sizeof buffer
;
211 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
212 dp
= add (buffer
, dp
);
216 * These header fields are copied to the header of
217 * each message/partial in the collection.
219 cp
= add (concat (name
, ":", buffer
, NULL
), cp
);
220 while (state
== FLDPLUS
) {
221 bufsz
= sizeof buffer
;
222 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
223 cp
= add (buffer
, cp
);
227 start
= ftell (in
) + 1;
236 adios (NULL
, "message format error in component #%d", compnum
);
239 adios (NULL
, "getfld () returned %d", state
);
244 m_getfld_state_destroy (&gstate
);
246 adios (NULL
, "headers missing from draft");
250 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
253 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
259 /* Only one part, nothing to split */
266 return sendaux (vec
, vecp
, program
, drft
, st
);
270 printf ("Sending as %d Partial Messages\n", nparts
);
275 vec
[vecp
++] = "-partno";
276 vec
[vecp
++] = partnum
;
278 vec
[vecp
++] = "-queued";
281 snprintf (msgid
, sizeof(msgid
), "%s", message_id (clock
, 0));
283 fseek (in
, start
, SEEK_SET
);
284 for (partno
= 1; partno
<= nparts
; partno
++) {
288 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, &out
);
290 adios(NULL
, "unable to create temporary file in %s",
293 strncpy(tmpdrf
, cp
, sizeof(tmpdrf
));
296 * Output the header fields
299 fprintf (out
, "Subject: %s (part %d of %d)\n", subject
, partno
, nparts
);
300 fprintf (out
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
301 fprintf (out
, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD
, msgid
);
302 fprintf (out
, "\tnumber=%d; total=%d\n", partno
, nparts
);
303 fprintf (out
, "%s: part %d of %d\n\n", DESCR_FIELD
, partno
, nparts
);
306 * If this is the first in the collection, output the
307 * header fields we are encapsulating at the beginning
308 * of the body of the first message.
313 fprintf (out
, "Message-ID: %s\n", msgid
);
321 if (!fgets (buffer
, sizeof(buffer
) - 1, in
)) {
322 if (partno
== nparts
)
324 adios (NULL
, "premature eof");
327 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
328 fseek (in
, -len
, SEEK_CUR
);
336 adios (tmpdrf
, "error writing to");
340 if (!pushsw
&& verbsw
) {
345 /* Pause here, if a delay is specified */
346 if (delay
> 0 && 1 < partno
&& partno
<= nparts
) {
348 printf ("pausing %d seconds before sending part %d...\n",
352 sleep ((unsigned int) delay
);
355 snprintf (partnum
, sizeof(partnum
), "%d", partno
);
356 status
= sendaux (vec
, vecp
, program
, tmpdrf
, st
);
357 (void) m_unlink (tmpdrf
);
362 * This is so sendaux will only annotate
363 * the altmsg the first time it is called.
372 fclose (in
); /* close the draft */
378 * Annotate original message, and
379 * call `postproc' (which is passed down in "program") to send message.
383 sendaux (char **vec
, int vecp
, char *program
, char *drft
, struct stat
*st
)
386 int i
, status
, fd
, fd2
;
387 char backup
[BUFSIZ
], buf
[BUFSIZ
];
389 fd
= pushsw
? tmp_fd () : NOTOK
;
393 if ((fd2
= tmp_fd ()) != NOTOK
) {
394 vec
[vecp
++] = "-idanno";
395 snprintf (buf
, sizeof(buf
), "%d", fd2
);
398 admonish (NULL
, "unable to create temporary file in %s "
399 "for annotation list", get_temp_dir());
403 if (distfile
&& distout (drft
, distfile
, backup
) == NOTOK
)
407 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
412 /* oops -- fork error */
413 adios ("fork", "unable to");
414 break; /* NOT REACHED */
418 * child process -- send it
420 * If fd is ok, then we are pushing and fd points to temp
421 * file, so capture anything on stdout and stderr there.
424 dup2 (fd
, fileno (stdout
));
425 dup2 (fd
, fileno (stderr
));
428 execvp (program
, vec
);
429 fprintf (stderr
, "unable to exec ");
435 * parent process -- wait for it
437 if ((status
= pidwait(child_id
, NOTOK
)) == OK
) {
438 if (annotext
&& fd2
!= NOTOK
)
442 * If postproc failed, and we have good fd (which means
443 * we pushed), then mail error message (and possibly the
444 * draft) back to the user.
450 advise (NULL
, "message not delivered to anyone");
452 if (annotext
&& fd2
!= NOTOK
)
455 (void) m_unlink (drft
);
456 if (rename (backup
, drft
) == NOTOK
)
457 advise (drft
, "unable to rename %s to", backup
);
468 * Mail error notification (and possibly a copy of the
469 * message) back to the user, using the mailproc
473 alert (char *file
, int out
)
481 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
486 /* oops -- fork error */
487 advise ("fork", "unable to");
490 /* child process -- send it */
491 SIGNAL (SIGHUP
, SIG_IGN
);
492 SIGNAL (SIGINT
, SIG_IGN
);
493 SIGNAL (SIGQUIT
, SIG_IGN
);
494 SIGNAL (SIGTERM
, SIG_IGN
);
496 if ((in
= open (file
, O_RDONLY
)) == NOTOK
) {
497 admonish (file
, "unable to re-open");
499 lseek (out
, (off_t
) 0, SEEK_END
);
500 strncpy (buf
, "\nMessage not delivered to anyone.\n", sizeof(buf
));
501 if (write (out
, buf
, strlen (buf
)) < 0) {
502 advise (file
, "write");
504 strncpy (buf
, "\n------- Unsent Draft\n\n", sizeof(buf
));
505 if (write (out
, buf
, strlen (buf
)) < 0) {
506 advise (file
, "write");
508 cpydgst (in
, out
, file
, "temporary file");
510 strncpy (buf
, "\n------- End of Unsent Draft\n", sizeof(buf
));
511 if (write (out
, buf
, strlen (buf
)) < 0) {
512 advise (file
, "write");
514 if (rename (file
, strncpy (buf
, m_backup (file
), sizeof(buf
))) == NOTOK
)
515 admonish (buf
, "unable to rename %s to", file
);
518 lseek (out
, (off_t
) 0, SEEK_SET
);
519 dup2 (out
, fileno (stdin
));
521 /* create subject for error notification */
522 snprintf (buf
, sizeof(buf
), "send failed on %s",
523 forwsw
? "enclosed draft" : file
);
525 arglist
= argsplit(mailproc
, &program
, &argp
);
527 arglist
[argp
++] = getusername();
528 arglist
[argp
++] = "-subject";
529 arglist
[argp
++] = buf
;
530 arglist
[argp
] = NULL
;
532 execvp (program
, arglist
);
533 fprintf (stderr
, "unable to exec ");
537 default: /* no waiting... */
549 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) return NOTOK
;
552 advise (NULL
, "temporary file %s selected", tfile
);
554 if (m_unlink (tfile
) == NOTOK
)
555 advise (tfile
, "unable to remove");
562 anno (int fd
, struct stat
*st
)
566 static char *cwd
= NULL
;
570 (stat (altmsg
, &st2
) == NOTOK
571 || st
->st_mtime
!= st2
.st_mtime
572 || st
->st_dev
!= st2
.st_dev
573 || st
->st_ino
!= st2
.st_ino
)) {
575 admonish (NULL
, "$mhaltmsg mismatch");
579 child_id
= debugsw
? NOTOK
: fork ();
581 case NOTOK
: /* oops */
584 "unable to fork, so doing annotations by hand...");
586 cwd
= getcpy (pwd ());
589 /* block a few signals */
591 sigaddset (&set
, SIGHUP
);
592 sigaddset (&set
, SIGINT
);
593 sigaddset (&set
, SIGQUIT
);
594 sigaddset (&set
, SIGTERM
);
595 sigprocmask (SIG_BLOCK
, &set
, &oset
);
597 unregister_for_removal(0);
603 /* reset the signal mask */
604 sigprocmask (SIG_SETMASK
, &oset
, &set
);
606 if (chdir (cwd
) < 0) {
607 advise (cwd
, "chdir");
611 default: /* no waiting... */
621 int fd2
, fd3
, msgnum
;
622 char *cp
, *folder
, *maildir
;
623 char buffer
[BUFSIZ
], **ap
;
627 if ((folder
= getenv ("mhfolder")) == NULL
|| *folder
== 0) {
629 admonish (NULL
, "$mhfolder not set");
632 maildir
= m_maildir (folder
);
633 if (chdir (maildir
) == NOTOK
) {
635 admonish (maildir
, "unable to change directory to");
638 if (!(mp
= folder_read (folder
, 0))) {
640 admonish (NULL
, "unable to read folder %s", folder
);
644 /* check for empty folder */
645 if (mp
->nummsg
== 0) {
647 admonish (NULL
, "no messages in %s", folder
);
651 if ((cp
= getenv ("mhmessages")) == NULL
|| *cp
== 0) {
653 admonish (NULL
, "$mhmessages not set");
656 if (!debugsw
/* MOBY HACK... */
658 && (fd3
= open ("/dev/null", O_RDWR
)) != NOTOK
659 && (fd2
= dup (fileno (stderr
))) != NOTOK
) {
660 dup2 (fd3
, fileno (stderr
));
665 for (ap
= brkstring (cp
= getcpy (cp
), " ", NULL
); *ap
; ap
++)
669 dup2 (fd2
, fileno (stderr
));
670 if (mp
->numsel
== 0) {
672 admonish (NULL
, "no messages to annotate");
676 lseek (fd
, (off_t
) 0, SEEK_SET
);
677 if ((fp
= fdopen (fd
, "r")) == NULL
) {
679 admonish (NULL
, "unable to fdopen annotation list");
683 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
684 cp
= add (buffer
, cp
);
688 advise (NULL
, "annotate%s with %s: \"%s\"",
689 inplace
? " inplace" : "", annotext
, cp
);
690 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
691 if (is_selected(mp
, msgnum
)) {
693 advise (NULL
, "annotate message %d", msgnum
);
694 annotate (m_name (msgnum
), annotext
, cp
, inplace
, 1, -2, 0);
701 folder_free (mp
); /* free folder/message structure */
706 armed_done (int status
)
708 longjmp (env
, status
? status
: NOTOK
);