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 */
36 char *altmsg = NULL; /* .. */
37 char *annotext = NULL;
38 char *distfile = NULL;
42 /* name of temp file for body content */
43 static char body_file_name[MAXPATHLEN + 1];
44 /* name of mhbuild composition temporary file */
45 static char composition_file_name[MAXPATHLEN + 1];
46 static int field_size; /* size of header field buffer */
47 static char *field; /* header field buffer */
48 static FILE *draft_file; /* draft file pointer */
49 static FILE *body_file; /* body file pointer */
50 static FILE *composition_file; /* composition file pointer */
53 ** external prototypes
55 int sendsbr(char **, int, char *, struct stat *, int, char *, int);
56 char *getusername(void);
61 static void armed_done(int) NORETURN;
62 static void alert(char *, int);
63 static int tmp_fd(void);
64 static void anno(int, struct stat *);
65 static void annoaux(int);
66 static int sendaux(char **, int, char *, struct stat *);
68 static int attach(char *, char *, int);
69 static void clean_up_temporary_files(void);
70 static int get_line(void);
71 static void make_mime_composition_file_entry(char *, int);
75 ** Entry point into (back-end) routines to send message.
79 sendsbr(char **vec, int vecp, char *drft, struct stat *st,
80 int rename_drft, char *attachment_header_field_name, int attachformat)
83 char buffer[BUFSIZ], file[BUFSIZ];
84 char *original_draft; /* name of original draft file */
85 char *p; /* string pointer for building file name */
88 ** Save the original name of the draft file. The name of the
89 ** draft file is changed to a temporary file containing the built
90 ** MIME message if there are attachments. We need the original
91 ** name so that it can be renamed after the message is sent.
94 original_draft = drft;
97 ** There might be attachments if a header field name for
98 ** attachments is supplied. Convert the draft to a MIME message.
99 ** Use the mhbuild composition file for the draft if there was
100 ** a successful conversion because that now contains the MIME
101 ** message. A nice side effect of this is that it leaves the
102 ** original draft file untouched so that it can be retrieved
103 ** and modified if desired.
106 if (attachment_header_field_name != NULL) {
107 switch (attach(attachment_header_field_name, drft,
110 drft = composition_file_name;
122 switch (setjmp(env)) {
125 ** If given -push and -unique (which is undocumented), then
126 ** rename the draft file. I'm not quite sure why.
128 if (pushsw && unique) {
129 char *cp = m_mktemp2(drft, invo_name, NULL, NULL);
131 adios("sendsbr", "unable to create temporary file");
133 if (rename(drft, strncpy(file, cp, sizeof(file)))
135 adios(file, "unable to rename %s to", drft);
139 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
141 /* rename the original draft */
142 if (rename_drft && status == OK &&
143 rename(original_draft, strncpy(buffer,
144 m_backup(original_draft), sizeof(buffer)))
146 advise(buffer, "unable to rename %s to", drft);
159 ** Get rid of any temporary files that we created for attachments.
160 ** Also get rid of the renamed composition file that mhbuild
161 ** leaves as a turd. It looks confusing, but we use the body
162 ** file name to help build the renamed composition file name.
165 if (drft == composition_file_name) {
166 clean_up_temporary_files();
168 if (strlen(composition_file_name) >=
169 sizeof (composition_file_name) - 6)
170 advise(NULL, "unable to remove original composition file.");
173 if ((p = strrchr(composition_file_name, '/')) == NULL)
174 p = composition_file_name;
178 strcpy(body_file_name, p);
180 strcpy(p, body_file_name);
183 unlink(composition_file_name);
191 attach(char *attachment_header_field_name, char *draft_file_name,
194 char buf[MAXPATHLEN + 6]; /* miscellaneous buffer */
195 int c; /* current character for body copy */
196 int has_attachment; /* draft has at least one attachment */
197 int has_body; /* draft has a message body */
198 int length; /* length of attachment header field name */
199 char *p; /* miscellaneous string pointer */
202 ** Open up the draft file.
205 if ((draft_file = fopen(draft_file_name, "r")) == (FILE *)0)
206 adios(NULL, "can't open draft file `%s'.",
210 ** Allocate a buffer to hold the header components as they're read in.
211 ** This buffer might need to be quite large, so we grow it as needed.
214 field = (char *)mh_xmalloc(field_size = 256);
217 ** Scan the draft file for a header field name that matches the
218 ** -attach argument. The existence of one indicates that the
219 ** draft has attachments. Bail out if there are no attachments
220 ** because we're done. Read to the end of the headers even if
221 ** we have no attachments.
224 length = strlen(attachment_header_field_name);
228 while (get_line() != EOF && *field != '\0' && *field != '-')
229 if (strncasecmp(field, attachment_header_field_name, length)
230 == 0 && field[length] == ':')
233 if (has_attachment == 0)
237 ** We have at least one attachment. Look for at least one
238 ** non-blank line in the body of the message which indicates
239 ** content in the body.
244 while (get_line() != EOF) {
245 for (p = field; *p != '\0'; p++) {
246 if (*p != ' ' && *p != '\t') {
257 ** Make names for the temporary files.
260 strncpy(body_file_name,
261 m_mktemp(toabsdir(invo_name), NULL, NULL),
262 sizeof (body_file_name));
263 strncpy(composition_file_name,
264 m_mktemp(toabsdir(invo_name), NULL, NULL),
265 sizeof (composition_file_name));
268 body_file = fopen(body_file_name, "w");
270 composition_file = fopen(composition_file_name, "w");
272 if ((has_body && body_file == (FILE *)0) ||
273 composition_file == (FILE *)0) {
274 clean_up_temporary_files();
275 adios(NULL, "unable to open all of the temporary files.");
279 ** Start at the beginning of the draft file. Copy all
280 ** non-attachment header fields to the temporary composition file.
281 ** Then add the dashed line separator.
286 while (get_line() != EOF && *field != '\0' && *field != '-')
287 if (strncasecmp(field, attachment_header_field_name, length)
288 != 0 || field[length] != ':')
289 fprintf(composition_file, "%s\n", field);
291 fputs("--------\n", composition_file);
294 ** Copy the message body to a temporary file.
298 while ((c = getc(draft_file)) != EOF)
305 ** Add a mhbuild MIME composition file line for the body if
310 make_mime_composition_file_entry(body_file_name, attachformat);
313 ** Now, go back to the beginning of the draft file and look for
314 ** header fields that specify attachments. Add a mhbuild MIME
315 ** composition file for each.
320 while (get_line() != EOF && *field != '\0' && *field != '-') {
321 if (strncasecmp(field, attachment_header_field_name, length)
322 == 0 && field[length] == ':') {
323 for (p = field + length + 1; *p == ' ' || *p == '\t';
327 make_mime_composition_file_entry(p, attachformat);
331 fclose(composition_file);
334 ** We're ready to roll! Run mhbuild on the composition file.
335 ** Note that mhbuild is in the context as buildmimeproc.
338 sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
340 if (system(buf) != 0) {
341 clean_up_temporary_files();
349 clean_up_temporary_files(void)
351 unlink(body_file_name);
352 unlink(composition_file_name);
360 int c; /* current character */
361 int n; /* number of bytes in buffer */
362 char *p; /* buffer pointer */
365 ** Get a line from the input file, growing the field buffer as
366 ** needed. We do this so that we can fit an entire line in the
367 ** buffer making it easy to do a string comparison on both the
368 ** field name and the field body which might be a long path name.
371 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
372 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
373 ungetc(c, draft_file);
378 if (++n >= field_size - 1) {
379 field = (char *)mh_xrealloc((void *)field, field_size += 256);
385 /* NUL-terminate the field. */
392 make_mime_composition_file_entry(char *file_name, int attachformat)
394 int binary; /* binary character found flag */
395 int c; /* current character */
396 char cmd[MAXPATHLEN + 6]; /* file command buffer */
397 char *content_type; /* mime content type */
398 FILE *fp; /* content and pipe file pointer */
399 struct node *np; /* context scan node pointer */
400 char *p; /* miscellaneous string pointer */
401 struct stat st; /* file status buffer */
406 ** Check the file name for a suffix. Scan the context for that
407 ** suffix on a mhshow-suffix- entry. We use these entries to
408 ** be compatible with mhshow, and there's no reason to make the
409 ** user specify each suffix twice. Context entries of the form
410 ** "mhshow-suffix-contenttype" in the name have the suffix in
411 ** the field, including the dot.
414 if ((p = strrchr(file_name, '.')) != NULL) {
415 for (np = m_defs; np; np = np->n_next) {
416 if (strncasecmp(np->n_name, "mhshow-suffix-", 14) == 0
417 && mh_strcasecmp(p, np->n_field) == 0)
419 content_type = np->n_name + 14;
426 ** No content type was found, either because there was no matching
427 ** entry in the context or because the file name has no suffix.
428 ** Open the file and check for non-ASCII characters. Choose the
429 ** content type based on this check.
432 if (content_type == NULL) {
433 if ((fp = fopen(file_name, "r")) == (FILE *)0) {
434 clean_up_temporary_files();
435 adios(NULL, "unable to access file \"%s\"",
441 while ((c = getc(fp)) != EOF) {
442 if (c > 127 || c < 0) {
450 content_type = binary ?
451 "application/octet-stream" : "text/plain";
455 ** Make sure that the attachment file exists and is readable.
456 ** Append a mhbuild directive to the draft file. This starts with
457 ** the content type. Append a file name attribute and a private
458 ** x-unix-mode attribute. Also append a description obtained
459 ** (if possible) by running the "file" command on the file.
462 if (stat(file_name, &st) == -1 || access(file_name, R_OK) != 0) {
463 clean_up_temporary_files();
464 adios(NULL, "unable to access file \"%s\"", file_name);
467 switch (attachformat) {
469 /* Insert name, file mode, and Content-Id. */
470 fprintf(composition_file,
471 "#%s; name=\"%s\"; x-unix-mode=0%.3ho",
472 content_type, ((p = strrchr(file_name, '/'))
473 == NULL) ? file_name : p + 1,
474 (unsigned short)(st.st_mode & 0777));
476 if (strlen(file_name) > MAXPATHLEN) {
477 clean_up_temporary_files();
478 adios(NULL, "attachment file name `%s' too long.",
482 sprintf(cmd, "file '%s'", file_name);
484 if ((fp = popen(cmd, "r")) != (FILE *)0 &&
485 fgets(cmd, sizeof (cmd), fp) != NULL) {
486 *strchr(cmd, '\n') = '\0';
489 ** The output of the "file" command is of the form
493 ** Strip off the "file:" and subsequent white space.
496 for (p = cmd; *p != '\0'; p++) {
498 for (p++; *p != '\0'; p++) {
507 /* Insert Content-Description. */
508 fprintf(composition_file, " [ %s ]", p);
515 if (stringdex(toabsdir(invo_name), file_name) == 0) {
517 ** Content had been placed by send into a temp file.
518 ** Don't generate Content-Disposition header, because
519 ** it confuses Microsoft Outlook, Build 10.0.6626, at
522 fprintf(composition_file, "#%s <>", content_type);
525 ** Suppress Content-Id, insert simple
526 ** Content-Disposition.
528 fprintf(composition_file,
529 "#%s; name=\"%s\" <>{attachment}",
531 ((p = strrchr(file_name, '/')) == NULL) ?
537 if (stringdex(toabsdir(invo_name), file_name) == 0) {
539 ** Content had been placed by send into a temp file.
540 ** Don't generate Content-Disposition header, because
541 ** it confuses Microsoft Outlook, Build 10.0.6626, at
544 fprintf(composition_file, "#%s <>", content_type);
547 ** Suppress Content-Id, insert Content-Disposition
548 ** with modification date.
550 fprintf(composition_file,
551 "#%s; name=\"%s\" <>{attachment; modification-date=\"%s\"}",
553 ((p = strrchr(file_name, '/')) == NULL) ?
554 file_name : p + 1, dtime(&st.st_mtime, 0));
559 adios(NULL, "unsupported attachformat %d", attachformat);
563 ** Finish up with the file name.
566 fprintf(composition_file, " %s\n", file_name);
572 ** Annotate original message, and
573 ** call `postproc' to send message.
577 sendaux(char **vec, int vecp, char *drft, struct stat *st)
580 int i, status, fd, fd2;
581 char backup[BUFSIZ], buf[BUFSIZ];
583 fd = pushsw ? tmp_fd() : NOTOK;
588 if ((fd2 = tmp_fd()) != NOTOK) {
589 vec[vecp++] = "-idanno";
590 snprintf(buf, sizeof(buf), "%d", fd2);
593 admonish(NULL, "unable to create file for annotation list");
596 if (distfile && distout(drft, distfile, backup) == NOTOK)
600 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
605 /* oops -- fork error */
606 adios("fork", "unable to");
607 break; /* NOT REACHED */
611 ** child process -- send it
613 ** If fd is ok, then we are pushing and fd points to temp
614 ** file, so capture anything on stdout and stderr there.
617 dup2(fd, fileno(stdout));
618 dup2(fd, fileno(stderr));
621 execvp(postproc, vec);
622 fprintf(stderr, "unable to exec ");
625 break; /* NOT REACHED */
629 ** parent process -- wait for it
631 if ((status = pidwait(child_id, NOTOK)) == OK) {
632 if (annotext && fd2 != NOTOK)
636 ** If postproc failed, and we have good fd (which
637 ** means we pushed), then mail error message
638 ** (and possibly the draft) back to the user.
644 advise(NULL, "message not delivered to anyone");
646 if (annotext && fd2 != NOTOK)
650 if (rename(backup, drft) == NOTOK)
651 advise(drft, "unable to rename %s to",
663 ** Mail error notification (and possibly a copy of the
664 ** message) back to the user, using the mailproc
668 alert(char *file, int out)
674 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
679 /* oops -- fork error */
680 advise("fork", "unable to");
683 /* child process -- send it */
684 SIGNAL(SIGHUP, SIG_IGN);
685 SIGNAL(SIGINT, SIG_IGN);
686 SIGNAL(SIGQUIT, SIG_IGN);
687 SIGNAL(SIGTERM, SIG_IGN);
689 if ((in = open(file, O_RDONLY)) == NOTOK) {
690 admonish(file, "unable to re-open");
692 lseek(out, (off_t) 0, SEEK_END);
693 strncpy(buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
694 write(out, buf, strlen(buf));
695 strncpy(buf, "\n------- Unsent Draft\n\n", sizeof(buf));
696 write(out, buf, strlen(buf));
697 cpydgst(in, out, file, "temporary file");
699 strncpy(buf, "\n------- End of Unsent Draft\n", sizeof(buf));
700 write(out, buf, strlen(buf));
701 if (rename(file, strncpy(buf, m_backup(file), sizeof(buf))) == NOTOK)
702 admonish(buf, "unable to rename %s to", file);
705 lseek(out, (off_t) 0, SEEK_SET);
706 dup2(out, fileno(stdin));
708 /* create subject for error notification */
709 snprintf(buf, sizeof(buf), "send failed on %s",
710 forwsw ? "enclosed draft" : file);
712 execlp(mailproc, mhbasename(mailproc), getusername(),
713 "-subject", buf, NULL);
714 fprintf(stderr, "unable to exec ");
718 default: /* no waiting... */
730 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
731 if (tfile == NULL) return NOTOK;
735 advise(NULL, "temporary file %s selected", tfile);
737 if (unlink(tfile) == NOTOK)
738 advise(tfile, "unable to remove");
745 anno(int fd, struct stat *st)
749 static char *cwd = NULL;
752 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
753 st->st_mtime != st2.st_mtime ||
754 st->st_dev != st2.st_dev ||
755 st->st_ino != st2.st_ino)) {
757 admonish(NULL, "$mhaltmsg mismatch");
761 child_id = debugsw ? NOTOK : fork();
763 case NOTOK: /* oops */
765 advise(NULL, "unable to fork, so doing annotations by hand...");
770 /* block a few signals */
772 sigaddset(&set, SIGHUP);
773 sigaddset(&set, SIGINT);
774 sigaddset(&set, SIGQUIT);
775 sigaddset(&set, SIGTERM);
776 SIGPROCMASK(SIG_BLOCK, &set, &oset);
782 /* reset the signal mask */
783 SIGPROCMASK(SIG_SETMASK, &oset, &set);
788 default: /* no waiting... */
798 int fd2, fd3, msgnum;
799 char *cp, *folder, *maildir;
800 char buffer[BUFSIZ], **ap;
804 if ((folder = getenv("mhfolder")) == NULL || *folder == 0) {
806 admonish(NULL, "$mhfolder not set");
809 maildir = toabsdir(folder);
810 if (chdir(maildir) == NOTOK) {
812 admonish(maildir, "unable to change directory to");
815 if (!(mp = folder_read(folder))) {
817 admonish(NULL, "unable to read folder %s", folder);
821 /* check for empty folder */
822 if (mp->nummsg == 0) {
824 admonish(NULL, "no messages in %s", folder);
828 if ((cp = getenv("mhmessages")) == NULL || *cp == 0) {
830 admonish(NULL, "$mhmessages not set");
833 if (!debugsw /* MOBY HACK... */
835 && (fd3 = open("/dev/null", O_RDWR)) != NOTOK
836 && (fd2 = dup(fileno(stderr))) != NOTOK) {
837 dup2(fd3, fileno(stderr));
842 for (ap = brkstring(cp = getcpy(cp), " ", NULL); *ap; ap++)
846 dup2(fd2, fileno(stderr));
847 if (mp->numsel == 0) {
849 admonish(NULL, "no messages to annotate");
853 lseek(fd, (off_t) 0, SEEK_SET);
854 if ((fp = fdopen(fd, "r")) == NULL) {
856 admonish(NULL, "unable to fdopen annotation list");
860 while (fgets(buffer, sizeof(buffer), fp) != NULL)
861 cp = add(buffer, cp);
865 advise(NULL, "annotate%s with %s: \"%s\"",
866 inplace ? " inplace" : "", annotext, cp);
867 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
868 if (is_selected(mp, msgnum)) {
870 advise(NULL, "annotate message %d", msgnum);
871 annotate(m_name (msgnum), annotext, cp, inplace,
879 folder_free(mp); /* free folder/message structure */
884 armed_done(int status)
886 longjmp(env, status ? status : NOTOK);