]>
diplodocus.org Git - nmh/blob - uip/sendsbr.c
3 * sendsbr.c -- routines to help WhatNow/Send along
15 #ifdef TIME_WITH_SYS_TIME
16 # include <sys/time.h>
19 # ifdef TM_IN_SYS_TIME
20 # include <sys/time.h>
26 int debugsw
= 0; /* global */
34 char *altmsg
= NULL
; /* .. */
35 char *annotext
= NULL
;
36 char *distfile
= NULL
;
44 int sendsbr (char **, int, char *, struct stat
*, int);
46 char *getusername (void);
51 static void alert (char *, int);
52 static int tmp_fd (void);
53 static void anno (int, struct stat
*);
54 static void annoaux (int);
55 static int splitmsg (char **, int, char *, struct stat
*, int);
56 static int sendaux (char **, int, char *, struct stat
*);
60 * Entry point into (back-end) routines to send message.
64 sendsbr (char **vec
, int vecp
, char *drft
, struct stat
*st
, int rename_drft
)
67 char buffer
[BUFSIZ
], file
[BUFSIZ
];
71 switch (setjmp (env
)) {
74 * If given -push and -unique (which is undocumented), then
75 * rename the draft file. I'm not quite sure why.
77 if (pushsw
&& unique
) {
78 if (rename (drft
, strncpy (file
, m_scratch (drft
, invo_name
), sizeof(file
)))
80 adios (file
, "unable to rename %s to", drft
);
85 * Check if we need to split the message into
86 * multiple messages of type "message/partial".
88 if (splitsw
>= 0 && !distfile
&& stat (drft
, &sts
) != NOTOK
89 && sts
.st_size
>= CPERMSG
) {
90 status
= splitmsg (vec
, vecp
, drft
, st
, splitsw
) ? NOTOK
: OK
;
92 status
= sendaux (vec
, vecp
, drft
, st
) ? NOTOK
: OK
;
95 /* rename the original draft */
96 if (rename_drft
&& status
== OK
&&
97 rename (drft
, strncpy (buffer
, m_backup (drft
), sizeof(buffer
))) == NOTOK
)
98 advise (buffer
, "unable to rename %s to", drft
);
115 * Split large message into several messages of
116 * type "message/partial" and send them.
120 splitmsg (char **vec
, int vecp
, char *drft
, struct stat
*st
, int delay
)
122 int compnum
, nparts
, partno
, state
, status
;
125 char *cp
, *dp
, buffer
[BUFSIZ
], msgid
[BUFSIZ
];
126 char subject
[BUFSIZ
];
127 char name
[NAMESZ
], partnum
[BUFSIZ
];
130 if ((in
= fopen (drft
, "r")) == NULL
)
131 adios (drft
, "unable to open for reading");
137 * Scan through the message and examine the various header fields,
138 * as well as locate the beginning of the message body.
140 for (compnum
= 1, state
= FLD
;;) {
141 switch (state
= m_getfld (state
, name
, buffer
, sizeof(buffer
), in
)) {
148 * This header field is discarded.
150 if (!strcasecmp (name
, "Message-ID")) {
151 while (state
== FLDPLUS
)
152 state
= m_getfld (state
, name
, buffer
, sizeof(buffer
), in
);
153 } else if (uprf (name
, XXX_FIELD_PRF
)
154 || !strcasecmp (name
, VRSN_FIELD
)
155 || !strcasecmp (name
, "Subject")
156 || !strcasecmp (name
, "Encrypted")) {
158 * These header fields are copied to the enclosed
159 * header of the first message in the collection
160 * of message/partials. For the "Subject" header
161 * field, we also record it, so that a modified
162 * version of it, can be copied to the header
163 * of each messsage/partial in the collection.
165 if (!strcasecmp (name
, "Subject")) {
168 strncpy (subject
, buffer
, BUFSIZ
);
169 sublen
= strlen (subject
);
170 if (sublen
> 0 && subject
[sublen
- 1] == '\n')
171 subject
[sublen
- 1] = '\0';
174 dp
= add (concat (name
, ":", buffer
, NULL
), dp
);
175 while (state
== FLDPLUS
) {
176 state
= m_getfld (state
, name
, buffer
, sizeof(buffer
), in
);
177 dp
= add (buffer
, dp
);
181 * These header fields are copied to the header of
182 * each message/partial in the collection.
184 cp
= add (concat (name
, ":", buffer
, NULL
), cp
);
185 while (state
== FLDPLUS
) {
186 state
= m_getfld (state
, name
, buffer
, sizeof(buffer
), in
);
187 cp
= add (buffer
, cp
);
191 if (state
!= FLDEOF
) {
192 start
= ftell (in
) + 1;
204 adios (NULL
, "message format error in component #%d", compnum
);
207 adios (NULL
, "getfld () returned %d", state
);
213 adios (NULL
, "headers missing from draft");
217 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
220 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
226 /* Only one part, nothing to split */
233 return sendaux (vec
, vecp
, drft
, st
);
237 printf ("Sending as %d Partial Messages\n", nparts
);
242 vec
[vecp
++] = "-partno";
243 vec
[vecp
++] = partnum
;
245 vec
[vecp
++] = "-queued";
248 snprintf (msgid
, sizeof(msgid
), "<%d.%ld@%s>",
249 (int) getpid(), (long) clock
, LocalName());
251 fseek (in
, start
, SEEK_SET
);
252 for (partno
= 1; partno
<= nparts
; partno
++) {
256 strncpy (tmpdrf
, m_scratch (drft
, invo_name
), sizeof(tmpdrf
));
257 if ((out
= fopen (tmpdrf
, "w")) == NULL
)
258 adios (tmpdrf
, "unable to open for writing");
259 chmod (tmpdrf
, 0600);
262 * Output the header fields
265 fprintf (out
, "Subject: %s (part %d of %d)\n", subject
, partno
, nparts
);
266 fprintf (out
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
267 fprintf (out
, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD
, msgid
);
268 fprintf (out
, "\tnumber=%d; total=%d\n", partno
, nparts
);
269 fprintf (out
, "%s: part %d of %d\n\n", DESCR_FIELD
, partno
, nparts
);
272 * If this is the first in the collection, output the
273 * header fields we are encapsulating at the beginning
274 * of the body of the first message.
279 fprintf (out
, "Message-ID: %s\n", msgid
);
287 if (!fgets (buffer
, sizeof(buffer
) - 1, in
)) {
288 if (partno
== nparts
)
290 adios (NULL
, "premature eof");
293 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
294 fseek (in
, -len
, SEEK_CUR
);
302 adios (tmpdrf
, "error writing to");
306 if (!pushsw
&& verbsw
) {
311 /* Pause here, if a delay is specified */
312 if (delay
> 0 && 1 < partno
&& partno
<= nparts
) {
314 printf ("pausing %d seconds before sending part %d...\n",
318 sleep ((unsigned int) delay
);
321 snprintf (partnum
, sizeof(partnum
), "%d", partno
);
322 status
= sendaux (vec
, vecp
, tmpdrf
, st
);
328 * This is so sendaux will only annotate
329 * the altmsg the first time it is called.
338 fclose (in
); /* close the draft */
344 * Annotate original message, and
345 * call `postproc' to send message.
349 sendaux (char **vec
, int vecp
, char *drft
, struct stat
*st
)
352 int i
, status
, fd
, fd2
;
353 char backup
[BUFSIZ
], buf
[BUFSIZ
];
355 fd
= pushsw
? tmp_fd () : NOTOK
;
360 if ((fd2
= tmp_fd ()) != NOTOK
) {
361 vec
[vecp
++] = "-idanno";
362 snprintf (buf
, sizeof(buf
), "%d", fd2
);
365 admonish (NULL
, "unable to create file for annotation list");
368 if (distfile
&& distout (drft
, distfile
, backup
) == NOTOK
)
372 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
377 /* oops -- fork error */
378 adios ("fork", "unable to");
379 break; /* NOT REACHED */
383 * child process -- send it
385 * If fd is ok, then we are pushing and fd points to temp
386 * file, so capture anything on stdout and stderr there.
389 dup2 (fd
, fileno (stdout
));
390 dup2 (fd
, fileno (stderr
));
393 execvp (postproc
, vec
);
394 fprintf (stderr
, "unable to exec ");
397 break; /* NOT REACHED */
401 * parent process -- wait for it
403 if ((status
= pidwait(child_id
, NOTOK
)) == OK
) {
404 if (annotext
&& fd2
!= NOTOK
)
408 * If postproc failed, and we have good fd (which means
409 * we pushed), then mail error message (and possibly the
410 * draft) back to the user.
416 advise (NULL
, "message not delivered to anyone");
418 if (annotext
&& fd2
!= NOTOK
)
422 if (rename (backup
, drft
) == NOTOK
)
423 advise (drft
, "unable to rename %s to", backup
);
434 * Mail error notification (and possibly a copy of the
435 * message) back to the user, using the mailproc
439 alert (char *file
, int out
)
445 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
450 /* oops -- fork error */
451 advise ("fork", "unable to");
454 /* child process -- send it */
455 SIGNAL (SIGHUP
, SIG_IGN
);
456 SIGNAL (SIGINT
, SIG_IGN
);
457 SIGNAL (SIGQUIT
, SIG_IGN
);
458 SIGNAL (SIGTERM
, SIG_IGN
);
460 if ((in
= open (file
, O_RDONLY
)) == NOTOK
) {
461 admonish (file
, "unable to re-open");
463 lseek (out
, (off_t
) 0, SEEK_END
);
464 strncpy (buf
, "\nMessage not delivered to anyone.\n", sizeof(buf
));
465 write (out
, buf
, strlen (buf
));
466 strncpy (buf
, "\n------- Unsent Draft\n\n", sizeof(buf
));
467 write (out
, buf
, strlen (buf
));
468 cpydgst (in
, out
, file
, "temporary file");
470 strncpy (buf
, "\n------- End of Unsent Draft\n", sizeof(buf
));
471 write (out
, buf
, strlen (buf
));
472 if (rename (file
, strncpy (buf
, m_backup (file
), sizeof(buf
))) == NOTOK
)
473 admonish (buf
, "unable to rename %s to", file
);
476 lseek (out
, (off_t
) 0, SEEK_SET
);
477 dup2 (out
, fileno (stdin
));
479 /* create subject for error notification */
480 snprintf (buf
, sizeof(buf
), "send failed on %s",
481 forwsw
? "enclosed draft" : file
);
483 execlp (mailproc
, r1bindex (mailproc
, '/'), getusername (),
484 "-subject", buf
, NULL
);
485 fprintf (stderr
, "unable to exec ");
489 default: /* no waiting... */
501 strncpy (tmpfil
, m_tmpfil (invo_name
), sizeof(tmpfil
));
502 if ((fd
= open (tmpfil
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600)) == NOTOK
)
505 advise (NULL
, "temporary file %s selected", tmpfil
);
507 if (unlink (tmpfil
) == NOTOK
)
508 advise (tmpfil
, "unable to remove");
515 anno (int fd
, struct stat
*st
)
519 static char *cwd
= NULL
;
523 (stat (altmsg
, &st2
) == NOTOK
524 || st
->st_mtime
!= st2
.st_mtime
525 || st
->st_dev
!= st2
.st_dev
526 || st
->st_ino
!= st2
.st_ino
)) {
528 admonish (NULL
, "$mhaltmsg mismatch");
532 child_id
= debugsw
? NOTOK
: fork ();
534 case NOTOK
: /* oops */
537 "unable to fork, so doing annotations by hand...");
539 cwd
= getcpy (pwd ());
542 /* block a few signals */
544 sigaddset (&set
, SIGHUP
);
545 sigaddset (&set
, SIGINT
);
546 sigaddset (&set
, SIGQUIT
);
547 sigaddset (&set
, SIGTERM
);
548 SIGPROCMASK (SIG_BLOCK
, &set
, &oset
);
554 /* reset the signal mask */
555 SIGPROCMASK (SIG_SETMASK
, &oset
, &set
);
560 default: /* no waiting... */
570 int fd2
, fd3
, msgnum
;
571 char *cp
, *folder
, *maildir
;
572 char buffer
[BUFSIZ
], **ap
;
576 if ((folder
= getenv ("mhfolder")) == NULL
|| *folder
== 0) {
578 admonish (NULL
, "$mhfolder not set");
581 maildir
= m_maildir (folder
);
582 if (chdir (maildir
) == NOTOK
) {
584 admonish (maildir
, "unable to change directory to");
587 if (!(mp
= folder_read (folder
))) {
589 admonish (NULL
, "unable to read folder %s");
593 /* check for empty folder */
594 if (mp
->nummsg
== 0) {
596 admonish (NULL
, "no messages in %s", folder
);
600 if ((cp
= getenv ("mhmessages")) == NULL
|| *cp
== 0) {
602 admonish (NULL
, "$mhmessages not set");
605 if (!debugsw
/* MOBY HACK... */
607 && (fd3
= open ("/dev/null", O_RDWR
)) != NOTOK
608 && (fd2
= dup (fileno (stderr
))) != NOTOK
) {
609 dup2 (fd3
, fileno (stderr
));
614 for (ap
= brkstring (cp
= getcpy (cp
), " ", NULL
); *ap
; ap
++)
618 dup2 (fd2
, fileno (stderr
));
619 if (mp
->numsel
== 0) {
621 admonish (NULL
, "no messages to annotate");
625 lseek (fd
, (off_t
) 0, SEEK_SET
);
626 if ((fp
= fdopen (fd
, "r")) == NULL
) {
628 admonish (NULL
, "unable to fdopen annotation list");
632 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
633 cp
= add (buffer
, cp
);
637 advise (NULL
, "annotate%s with %s: \"%s\"",
638 inplace
? " inplace" : "", annotext
, cp
);
639 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
640 if (is_selected(mp
, msgnum
)) {
642 advise (NULL
, "annotate message %d", msgnum
);
643 annotate (m_name (msgnum
), annotext
, cp
, inplace
, 1);
650 folder_free (mp
); /* free folder/message structure */
658 longjmp (env
, status
? status
: NOTOK
);
661 return 1; /* dead code to satisfy the compiler */