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] == ':')
206 ** We have at least one attachment. Look for at least one
207 ** non-blank line in the body of the message which indicates
208 ** content in the body.
209 ** Check if body contains at least one non-blank (= not empty)
210 ** and if it contains any non-ASCII chars (= need MIME).
215 while (get_line() != EOF) {
216 for (p = field; *p != '\0'; p++) {
217 if (*p != ' ' && *p != '\t') {
220 if (*p > 127 || *p < 0) {
224 if (has_body && non_ascii)
229 ** Bail out if there are no attachments and only ASCII text.
230 ** This means we don't need to convert it to MIME.
232 if (!has_attachment && non_ascii==0) {
240 /* Make names for the temporary files. */
241 strncpy(body_file_name,
242 m_mktemp(toabsdir(invo_name), NULL, NULL),
243 sizeof (body_file_name));
244 strncpy(composition_file_name,
245 m_mktemp(toabsdir(invo_name), NULL, NULL),
246 sizeof (composition_file_name));
249 body_file = fopen(body_file_name, "w");
251 composition_file = fopen(composition_file_name, "w");
253 if ((has_body && body_file == (FILE *)0) ||
254 composition_file == (FILE *)0) {
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 != '\0' && *field != '-')
266 if (strncasecmp(field, attach_hdr, length) != 0 ||
267 field[length] != ':')
268 fprintf(composition_file, "%s\n", field);
269 fputs("--------\n", composition_file);
272 ** Copy the message body to a temporary file.
275 while ((c = getc(draft_file)) != EOF)
281 ** Add a mhbuild MIME composition file line for the body if
285 /* old: make_mime_composition_file_entry(body_file_name); */
286 /* charset will be discovered/guessed by buildmimeproc */
287 fprintf(composition_file, "#text/plain %s\n", body_file_name);
291 ** Now, go back to the beginning of the draft file and look for
292 ** header fields that specify attachments. Add a mhbuild MIME
293 ** composition file for each.
296 while (get_line() != EOF && *field != '\0' && *field != '-') {
297 if (strncasecmp(field, attach_hdr, length) == 0 &&
298 field[length] == ':') {
299 for (p = field+length+1; *p==' ' || *p=='\t'; p++) {
303 /* forwarded message */
304 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
306 make_mime_composition_file_entry(p);
311 fclose(composition_file);
314 ** We're ready to roll! Run mhbuild on the composition file.
315 ** Note that mhbuild is in the context as buildmimeproc.
317 sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
319 if (system(buf) != 0) {
320 clean_up_temporary_files();
328 clean_up_temporary_files(void)
330 unlink(body_file_name);
331 unlink(composition_file_name);
339 int c; /* current character */
340 int n; /* number of bytes in buffer */
341 char *p; /* buffer pointer */
344 ** Get a line from the input file, growing the field buffer as
345 ** needed. We do this so that we can fit an entire line in the
346 ** buffer making it easy to do a string comparison on both the
347 ** field name and the field body which might be a long path name.
350 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
351 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
352 ungetc(c, draft_file);
357 if (++n >= field_size - 1) {
358 field = (char *)mh_xrealloc((void *)field, field_size += 256);
364 /* NUL-terminate the field. */
371 make_mime_composition_file_entry(char *file_name)
373 int binary; /* binary character found flag */
374 int c; /* current character */
375 char *content_type; /* mime content type */
376 FILE *fp; /* content and pipe file pointer */
377 struct node *np; /* context scan node pointer */
378 char *p; /* miscellaneous string pointer */
383 ** Check the file name for a suffix. Scan the context for that
384 ** suffix on a mhshow-suffix- entry. We use these entries to
385 ** be compatible with mhshow, and there's no reason to make the
386 ** user specify each suffix twice. Context entries of the form
387 ** "mhshow-suffix-contenttype" in the name have the suffix in
388 ** the field, including the dot.
391 if ((p = strrchr(file_name, '.')) != NULL) {
392 for (np = m_defs; np; np = np->n_next) {
393 if (strncasecmp(np->n_name, "mhshow-suffix-", 14) == 0
394 && mh_strcasecmp(p, np->n_field) == 0)
396 content_type = np->n_name + 14;
403 ** No content type was found, either because there was no matching
404 ** entry in the context or because the file name has no suffix.
405 ** Open the file and check for non-ASCII characters. Choose the
406 ** content type based on this check.
409 if (content_type == NULL) {
410 if ((fp = fopen(file_name, "r")) == (FILE *)0) {
411 clean_up_temporary_files();
412 adios(NULL, "unable to access file \"%s\"",
418 while ((c = getc(fp)) != EOF) {
419 if (c > 127 || c < 0) {
427 content_type = binary ?
428 "application/octet-stream" : "text/plain";
432 ** Make sure that the attachment file exists and is readable.
433 ** Append a mhbuild directive to the draft file. This starts with
434 ** the content type. Append a file name attribute and a private
435 ** x-unix-mode attribute. Also append a description obtained
436 ** (if possible) by running the "file" command on the file.
439 if (access(file_name, R_OK) != 0) {
440 clean_up_temporary_files();
441 adios(NULL, "unable to access file \"%s\"", file_name);
445 if (stringdex(toabsdir(invo_name), file_name) == 0) {
447 ** Content had been placed by send into a temp file.
448 ** Don't generate Content-Disposition header, because
449 ** it confuses Microsoft Outlook, Build 10.0.6626, at
452 fprintf(composition_file, "#%s <>", content_type);
455 ** Suppress Content-Id, insert simple
456 ** Content-Disposition.
458 fprintf(composition_file,
459 "#%s; name=\"%s\" <>{attachment}",
461 ((p = strrchr(file_name, '/')) == NULL) ?
465 /* Finish up with the file name. */
466 fprintf(composition_file, " %s\n", file_name);
472 ** Annotate original message, and
473 ** call `postproc' to send message.
477 sendaux(char **vec, int vecp, char *drft, struct stat *st)
480 int i, status, fd, fd2;
483 fd = pushsw ? tmp_fd() : NOTOK;
487 if (annotext && (fd2 = tmp_fd()) == NOTOK) {
488 admonish(NULL, "unable to create file for annotation list");
490 if (distfile && distout(drft, distfile, backup) == NOTOK)
494 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
499 /* oops -- fork error */
500 adios("fork", "unable to");
501 break; /* NOT REACHED */
505 ** child process -- send it
507 ** If fd is ok, then we are pushing and fd points to temp
508 ** file, so capture anything on stdout and stderr there.
511 dup2(fd, fileno(stdout));
512 dup2(fd, fileno(stderr));
515 execvp(postproc, vec);
516 fprintf(stderr, "unable to exec ");
519 break; /* NOT REACHED */
523 ** parent process -- wait for it
525 if ((status = pidwait(child_id, NOTOK)) == OK) {
526 if (annotext && fd2 != NOTOK)
530 ** If postproc failed, and we have good fd (which
531 ** means we pushed), then mail error message
532 ** (and possibly the draft) back to the user.
538 advise(NULL, "message not delivered to anyone");
540 if (annotext && fd2 != NOTOK)
544 if (rename(backup, drft) == NOTOK)
545 advise(drft, "unable to rename %s to",
557 ** Mail error notification (and possibly a copy of the
558 ** message) back to the user, using the mailproc
562 alert(char *file, int out)
568 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
573 /* oops -- fork error */
574 advise("fork", "unable to");
577 /* child process -- send it */
578 SIGNAL(SIGHUP, SIG_IGN);
579 SIGNAL(SIGINT, SIG_IGN);
580 SIGNAL(SIGQUIT, SIG_IGN);
581 SIGNAL(SIGTERM, SIG_IGN);
583 if ((in = open(file, O_RDONLY)) == NOTOK) {
584 admonish(file, "unable to re-open");
586 lseek(out, (off_t) 0, SEEK_END);
587 strncpy(buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
588 write(out, buf, strlen(buf));
589 strncpy(buf, "\n------- Unsent Draft\n\n", sizeof(buf));
590 write(out, buf, strlen(buf));
591 cpydgst(in, out, file, "temporary file");
593 strncpy(buf, "\n------- End of Unsent Draft\n", sizeof(buf));
594 write(out, buf, strlen(buf));
595 if (rename(file, strncpy(buf, m_backup(file), sizeof(buf))) == NOTOK)
596 admonish(buf, "unable to rename %s to", file);
599 lseek(out, (off_t) 0, SEEK_SET);
600 dup2(out, fileno(stdin));
602 /* create subject for error notification */
603 snprintf(buf, sizeof(buf), "send failed on %s",
604 forwsw ? "enclosed draft" : file);
606 execlp(mailproc, mhbasename(mailproc), getusername(),
607 "-subject", buf, NULL);
608 fprintf(stderr, "unable to exec ");
612 default: /* no waiting... */
624 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
625 if (tfile == NULL) return NOTOK;
629 advise(NULL, "temporary file %s selected", tfile);
631 if (unlink(tfile) == NOTOK)
632 advise(tfile, "unable to remove");
639 anno(int fd, struct stat *st)
643 static char *cwd = NULL;
646 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
647 st->st_mtime != st2.st_mtime ||
648 st->st_dev != st2.st_dev ||
649 st->st_ino != st2.st_ino)) {
651 admonish(NULL, "$mhaltmsg mismatch");
655 child_id = debugsw ? NOTOK : fork();
657 case NOTOK: /* oops */
659 advise(NULL, "unable to fork, so doing annotations by hand...");
664 /* block a few signals */
666 sigaddset(&set, SIGHUP);
667 sigaddset(&set, SIGINT);
668 sigaddset(&set, SIGQUIT);
669 sigaddset(&set, SIGTERM);
670 SIGPROCMASK(SIG_BLOCK, &set, &oset);
676 /* reset the signal mask */
677 SIGPROCMASK(SIG_SETMASK, &oset, &set);
682 default: /* no waiting... */
692 int fd2, fd3, msgnum;
693 char *cp, *folder, *maildir;
694 char buffer[BUFSIZ], **ap;
698 if ((folder = getenv("mhfolder")) == NULL || *folder == 0) {
700 admonish(NULL, "$mhfolder not set");
703 maildir = toabsdir(folder);
704 if (chdir(maildir) == NOTOK) {
706 admonish(maildir, "unable to change directory to");
709 if (!(mp = folder_read(folder))) {
711 admonish(NULL, "unable to read folder %s", folder);
715 /* check for empty folder */
716 if (mp->nummsg == 0) {
718 admonish(NULL, "no messages in %s", folder);
722 if ((cp = getenv("mhmessages")) == NULL || *cp == 0) {
724 admonish(NULL, "$mhmessages not set");
727 if (!debugsw /* MOBY HACK... */
729 && (fd3 = open("/dev/null", O_RDWR)) != NOTOK
730 && (fd2 = dup(fileno(stderr))) != NOTOK) {
731 dup2(fd3, fileno(stderr));
736 for (ap = brkstring(cp = getcpy(cp), " ", NULL); *ap; ap++)
740 dup2(fd2, fileno(stderr));
741 if (mp->numsel == 0) {
743 admonish(NULL, "no messages to annotate");
747 lseek(fd, (off_t) 0, SEEK_SET);
748 if ((fp = fdopen(fd, "r")) == NULL) {
750 admonish(NULL, "unable to fdopen annotation list");
754 while (fgets(buffer, sizeof(buffer), fp) != NULL)
755 cp = add(buffer, cp);
759 advise(NULL, "annotate%s with %s: \"%s\"",
760 inplace ? " inplace" : "", annotext, cp);
761 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
762 if (is_selected(mp, msgnum)) {
764 advise(NULL, "annotate message %d", msgnum);
765 annotate(m_name(msgnum), annotext, cp, inplace,
773 folder_free(mp); /* free folder/message structure */
778 armed_done(int status)
780 longjmp(env, status ? status : NOTOK);