2 ** sendsbr.c -- routines to help WhatNow/Send along
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 #include <h/signals.h>
18 #ifdef TIME_WITH_SYS_TIME
19 # include <sys/time.h>
22 # ifdef TM_IN_SYS_TIME
23 # include <sys/time.h>
29 int debugsw = 0; /* global */
35 char *altmsg = NULL; /* .. */
36 char *annotext = NULL;
37 char *distfile = NULL;
41 /* name of temp file for body content */
42 static char body_file_name[MAXPATHLEN + 1];
43 /* name of mhbuild composition temporary file */
44 static char composition_file_name[MAXPATHLEN + 1];
45 static int field_size; /* size of header field buffer */
46 static char *field; /* header field buffer */
47 static FILE *draft_file; /* draft file pointer */
48 static FILE *body_file; /* body file pointer */
49 static FILE *composition_file; /* composition file pointer */
52 ** external prototypes
54 int sendsbr(char **, int, char *, struct stat *, int);
55 char *getusername(void);
60 static void armed_done(int) NORETURN;
61 static void alert(char *, int);
62 static int tmp_fd(void);
63 static void anno(int, struct stat *);
64 static void annoaux(int);
65 static int sendaux(char **, int, char *, struct stat *);
67 static int attach(char *);
68 static void clean_up_temporary_files(void);
69 static int get_line(void);
70 static void make_mime_composition_file_entry(char *);
74 ** Entry point into (back-end) routines to send message.
78 sendsbr(char **vec, int vecp, char *drft, struct stat *st, int rename_drft)
82 char *original_draft; /* name of original draft file */
83 char *p; /* string pointer for building file name */
86 ** Save the original name of the draft file. The name of the
87 ** draft file is changed to a temporary file containing the built
88 ** MIME message if there are attachments. We need the original
89 ** name so that it can be renamed after the message is sent.
92 original_draft = drft;
95 ** Convert the draft to a MIME message.
96 ** Use the mhbuild composition file for the draft if there was
97 ** a successful conversion because that now contains the MIME
98 ** message. A nice side effect of this is that it leaves the
99 ** original draft file untouched so that it can be retrieved
100 ** and modified if desired.
102 switch (attach(drft)) {
104 drft = composition_file_name;
115 switch (setjmp(env)) {
117 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
118 /* rename the original draft */
119 if (rename_drft && status == OK &&
120 rename(original_draft, strncpy(buffer,
121 m_backup(original_draft), sizeof(buffer)))
123 advise(buffer, "unable to rename %s to", drft);
136 ** Get rid of any temporary files that we created for attachments.
137 ** Also get rid of the renamed composition file that mhbuild
138 ** leaves as a turd. It looks confusing, but we use the body
139 ** file name to help build the renamed composition file name.
142 if (drft == composition_file_name) {
143 clean_up_temporary_files();
145 if (strlen(composition_file_name) >=
146 sizeof (composition_file_name) - 6)
147 advise(NULL, "unable to remove original composition file.");
150 if ((p = strrchr(composition_file_name, '/')) == NULL)
151 p = composition_file_name;
155 strcpy(body_file_name, p);
157 strcpy(p, body_file_name);
160 unlink(composition_file_name);
168 attach(char *draft_file_name)
170 char buf[MAXPATHLEN + 6]; /* miscellaneous buffer */
171 int c; /* current character for body copy */
172 int has_attachment; /* draft has at least one attachment */
173 int has_body; /* draft has a message body */
174 int non_ascii; /* msg body contains non-ASCII chars */
175 int length; /* length of attachment header field name */
176 char *p; /* miscellaneous string pointer */
178 /* Open up the draft file. */
179 if ((draft_file = fopen(draft_file_name, "r")) == (FILE *)0)
180 adios(NULL, "can't open draft file `%s'.",
184 ** Allocate a buffer to hold the header components as they're read in.
185 ** This buffer might need to be quite large, so we grow it as needed.
187 field = (char *)mh_xmalloc(field_size = 256);
190 ** Scan the draft file for an attachment header field name.
191 ** The existence of one indicates that the
192 ** draft has attachments. Bail out if there are no attachments
193 ** because we're done. Read to the end of the headers even if
194 ** we have no attachments.
196 length = strlen(attach_hdr);
200 while (get_line() != EOF && *field != '\0' && *field != '-') {
201 if (strncasecmp(field, attach_hdr, length) == 0 &&
202 field[length] == ':') {
208 ** Look for at least one non-blank line in the body of the
209 ** message which indicates content in the body.
210 ** Check if body contains at least one non-blank (= not empty)
211 ** and if it contains any non-ASCII chars (= need MIME).
216 while (get_line() != EOF) {
217 for (p = field; *p != '\0'; p++) {
218 if (*p != ' ' && *p != '\t') {
221 if (*p > 127 || *p < 0) {
225 if (has_body && non_ascii)
230 ** Bail out if there are no attachments and only ASCII text.
231 ** This means we don't need to convert it to MIME.
233 if (!has_attachment && non_ascii==0) {
241 /* Make names for the temporary files. */
242 strncpy(body_file_name,
243 m_mktemp(toabsdir(invo_name), NULL, NULL),
244 sizeof (body_file_name));
245 strncpy(composition_file_name,
246 m_mktemp(toabsdir(invo_name), NULL, NULL),
247 sizeof (composition_file_name));
250 body_file = fopen(body_file_name, "w");
252 composition_file = fopen(composition_file_name, "w");
254 if ((has_body && !body_file) || !composition_file) {
255 clean_up_temporary_files();
256 adios(NULL, "unable to open all of the temporary files.");
260 ** Start at the beginning of the draft file. Copy all
261 ** non-attachment header fields to the temporary composition file.
262 ** Then add the dashed line separator.
265 while (get_line() != EOF && *field && *field != '-') {
266 if (strncasecmp(field, attach_hdr, length) != 0 ||
267 field[length] != ':') {
268 fprintf(composition_file, "%s\n", field);
271 fputs("--------\n", composition_file);
274 /* Copy the message body to the temporary file. */
275 while ((c = getc(draft_file)) != EOF) {
280 /* Add a mhbuild MIME composition file line for the body */
281 /* charset will be discovered/guessed by buildmimeproc */
282 fprintf(composition_file, "#text/plain %s\n", body_file_name);
286 ** Now, go back to the beginning of the draft file and look for
287 ** header fields that specify attachments. Add a mhbuild MIME
288 ** composition file for each.
291 while (get_line() != EOF && *field && *field != '-') {
292 if (strncasecmp(field, attach_hdr, length) == 0 &&
293 field[length] == ':') {
294 for (p = field+length+1; *p==' ' || *p=='\t'; p++) {
298 /* forwarded message */
299 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
301 make_mime_composition_file_entry(p);
306 fclose(composition_file);
309 ** We're ready to roll! Run mhbuild on the composition file.
310 ** Note that mhbuild is in the context as buildmimeproc.
312 sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
314 if (system(buf) != 0) {
315 clean_up_temporary_files();
323 clean_up_temporary_files(void)
325 unlink(body_file_name);
326 unlink(composition_file_name);
334 int c; /* current character */
335 int n; /* number of bytes in buffer */
336 char *p; /* buffer pointer */
339 ** Get a line from the input file, growing the field buffer as
340 ** needed. We do this so that we can fit an entire line in the
341 ** buffer making it easy to do a string comparison on both the
342 ** field name and the field body which might be a long path name.
345 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
346 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
347 ungetc(c, draft_file);
352 if (++n >= field_size - 1) {
353 field = (char *)mh_xrealloc((void *)field, field_size += 256);
359 /* NUL-terminate the field. */
366 make_mime_composition_file_entry(char *file_name)
368 int binary; /* binary character found flag */
369 int c; /* current character */
370 char *content_type; /* mime content type */
371 FILE *fp; /* content and pipe file pointer */
372 struct node *np; /* context scan node pointer */
373 char *p; /* miscellaneous string pointer */
379 ** Check the file name for a suffix. Scan the context for that
380 ** suffix on a mhshow-suffix- entry. We use these entries to
381 ** be compatible with mhshow, and there's no reason to make the
382 ** user specify each suffix twice. Context entries of the form
383 ** "mhshow-suffix-contenttype" in the name have the suffix in
384 ** the field, including the dot.
387 if ((p = strrchr(file_name, '.')) != NULL) {
388 for (np = m_defs; np; np = np->n_next) {
389 if (strncasecmp(np->n_name, "mhshow-suffix-", 14) == 0
390 && mh_strcasecmp(p, np->n_field) == 0)
392 content_type = np->n_name + 14;
399 ** No content type was found, either because there was no matching
400 ** entry in the context or because the file name has no suffix.
401 ** Open the file and check for non-ASCII characters. Choose the
402 ** content type based on this check.
405 if (content_type == NULL) {
406 if ((fp = fopen(file_name, "r")) == (FILE *)0) {
407 clean_up_temporary_files();
408 adios(NULL, "unable to access file \"%s\"",
414 while ((c = getc(fp)) != EOF) {
415 if (c > 127 || c < 0) {
423 content_type = binary ?
424 "application/octet-stream" : "text/plain";
428 ** Make sure that the attachment file exists and is readable.
429 ** Append a mhbuild directive to the draft file. This starts with
430 ** the content type. Append a file name attribute and a private
431 ** x-unix-mode attribute. Also append a description obtained
432 ** (if possible) by running the "file" command on the file.
435 if (access(file_name, R_OK) != 0) {
436 clean_up_temporary_files();
437 adios(NULL, "unable to access file \"%s\"", file_name);
441 if (stringdex(toabsdir(invo_name), file_name) == 0) {
443 ** Content had been placed by send into a temp file.
444 ** Don't generate Content-Disposition header, because
445 ** it confuses Microsoft Outlook, Build 10.0.6626, at
448 fprintf(composition_file, "#%s <>", content_type);
451 ** Suppress Content-Id, insert simple
452 ** Content-Disposition.
454 fprintf(composition_file,
455 "#%s; name=\"%s\" <>{attachment}",
457 ((p = strrchr(file_name, '/')) == NULL) ?
461 /* Finish up with the file name. */
462 fprintf(composition_file, " %s\n", file_name);
468 ** Annotate original message, and
469 ** call `postproc' to send message.
473 sendaux(char **vec, int vecp, char *drft, struct stat *st)
476 int i, status, fd, fd2;
479 fd = pushsw ? tmp_fd() : NOTOK;
483 if (annotext && (fd2 = tmp_fd()) == NOTOK) {
484 admonish(NULL, "unable to create file for annotation list");
486 if (distfile && distout(drft, distfile, backup) == NOTOK)
490 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
495 /* oops -- fork error */
496 adios("fork", "unable to");
497 break; /* NOT REACHED */
501 ** child process -- send it
503 ** If fd is ok, then we are pushing and fd points to temp
504 ** file, so capture anything on stdout and stderr there.
507 dup2(fd, fileno(stdout));
508 dup2(fd, fileno(stderr));
511 execvp(postproc, vec);
512 fprintf(stderr, "unable to exec ");
515 break; /* NOT REACHED */
519 ** parent process -- wait for it
521 if ((status = pidwait(child_id, NOTOK)) == OK) {
522 if (annotext && fd2 != NOTOK)
526 ** If postproc failed, and we have good fd (which
527 ** means we pushed), then mail error message
528 ** (and possibly the draft) back to the user.
534 advise(NULL, "message not delivered to anyone");
536 if (annotext && fd2 != NOTOK)
540 if (rename(backup, drft) == NOTOK)
541 advise(drft, "unable to rename %s to",
553 ** Mail error notification (and possibly a copy of the
554 ** message) back to the user, using the mailproc
558 alert(char *file, int out)
564 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
569 /* oops -- fork error */
570 advise("fork", "unable to");
573 /* child process -- send it */
574 SIGNAL(SIGHUP, SIG_IGN);
575 SIGNAL(SIGINT, SIG_IGN);
576 SIGNAL(SIGQUIT, SIG_IGN);
577 SIGNAL(SIGTERM, SIG_IGN);
579 if ((in = open(file, O_RDONLY)) == NOTOK) {
580 admonish(file, "unable to re-open");
582 lseek(out, (off_t) 0, SEEK_END);
583 strncpy(buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
584 write(out, buf, strlen(buf));
585 strncpy(buf, "\n------- Unsent Draft\n\n", sizeof(buf));
586 write(out, buf, strlen(buf));
587 cpydgst(in, out, file, "temporary file");
589 strncpy(buf, "\n------- End of Unsent Draft\n", sizeof(buf));
590 write(out, buf, strlen(buf));
591 if (rename(file, strncpy(buf, m_backup(file), sizeof(buf))) == NOTOK)
592 admonish(buf, "unable to rename %s to", file);
595 lseek(out, (off_t) 0, SEEK_SET);
596 dup2(out, fileno(stdin));
598 /* create subject for error notification */
599 snprintf(buf, sizeof(buf), "send failed on %s",
600 forwsw ? "enclosed draft" : file);
602 execlp(mailproc, mhbasename(mailproc), getusername(),
603 "-subject", buf, NULL);
604 fprintf(stderr, "unable to exec ");
608 default: /* no waiting... */
620 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
621 if (tfile == NULL) return NOTOK;
625 advise(NULL, "temporary file %s selected", tfile);
627 if (unlink(tfile) == NOTOK)
628 advise(tfile, "unable to remove");
635 anno(int fd, struct stat *st)
639 static char *cwd = NULL;
642 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
643 st->st_mtime != st2.st_mtime ||
644 st->st_dev != st2.st_dev ||
645 st->st_ino != st2.st_ino)) {
647 admonish(NULL, "$mhaltmsg mismatch");
651 child_id = debugsw ? NOTOK : fork();
653 case NOTOK: /* oops */
655 advise(NULL, "unable to fork, so doing annotations by hand...");
660 /* block a few signals */
662 sigaddset(&set, SIGHUP);
663 sigaddset(&set, SIGINT);
664 sigaddset(&set, SIGQUIT);
665 sigaddset(&set, SIGTERM);
666 SIGPROCMASK(SIG_BLOCK, &set, &oset);
672 /* reset the signal mask */
673 SIGPROCMASK(SIG_SETMASK, &oset, &set);
678 default: /* no waiting... */
688 int fd2, fd3, msgnum;
689 char *cp, *folder, *maildir;
690 char buffer[BUFSIZ], **ap;
694 if ((folder = getenv("mhfolder")) == NULL || *folder == 0) {
696 admonish(NULL, "$mhfolder not set");
699 maildir = toabsdir(folder);
700 if (chdir(maildir) == NOTOK) {
702 admonish(maildir, "unable to change directory to");
705 if (!(mp = folder_read(folder))) {
707 admonish(NULL, "unable to read folder %s", folder);
711 /* check for empty folder */
712 if (mp->nummsg == 0) {
714 admonish(NULL, "no messages in %s", folder);
718 if ((cp = getenv("mhmessages")) == NULL || *cp == 0) {
720 admonish(NULL, "$mhmessages not set");
723 if (!debugsw /* MOBY HACK... */
725 && (fd3 = open("/dev/null", O_RDWR)) != NOTOK
726 && (fd2 = dup(fileno(stderr))) != NOTOK) {
727 dup2(fd3, fileno(stderr));
732 for (ap = brkstring(cp = getcpy(cp), " ", NULL); *ap; ap++)
736 dup2(fd2, fileno(stderr));
737 if (mp->numsel == 0) {
739 admonish(NULL, "no messages to annotate");
743 lseek(fd, (off_t) 0, SEEK_SET);
744 if ((fp = fdopen(fd, "r")) == NULL) {
746 admonish(NULL, "unable to fdopen annotation list");
750 while (fgets(buffer, sizeof(buffer), fp) != NULL)
751 cp = add(buffer, cp);
755 advise(NULL, "annotate%s with %s: \"%s\"",
756 inplace ? " inplace" : "", annotext, cp);
757 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
758 if (is_selected(mp, msgnum)) {
760 advise(NULL, "annotate message %d", msgnum);
761 annotate(m_name(msgnum), annotext, cp, inplace,
769 folder_free(mp); /* free folder/message structure */
774 armed_done(int status)
776 longjmp(env, status ? status : NOTOK);