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);
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 *);
69 static void clean_up_temporary_files(void);
70 static int get_line(void);
71 static void make_mime_composition_file_entry(char *);
75 ** Entry point into (back-end) routines to send message.
79 sendsbr(char **vec, int vecp, char *drft, struct stat *st, int rename_drft)
82 char buffer[BUFSIZ], file[BUFSIZ];
83 char *original_draft; /* name of original draft file */
84 char *p; /* string pointer for building file name */
87 ** Save the original name of the draft file. The name of the
88 ** draft file is changed to a temporary file containing the built
89 ** MIME message if there are attachments. We need the original
90 ** name so that it can be renamed after the message is sent.
93 original_draft = drft;
96 ** Convert the draft to a MIME message.
97 ** Use the mhbuild composition file for the draft if there was
98 ** a successful conversion because that now contains the MIME
99 ** message. A nice side effect of this is that it leaves the
100 ** original draft file untouched so that it can be retrieved
101 ** and modified if desired.
103 switch (attach(drft)) {
105 drft = composition_file_name;
116 switch (setjmp(env)) {
119 ** If given -push and -unique (which is undocumented), then
120 ** rename the draft file. I'm not quite sure why.
122 if (pushsw && unique) {
123 char *cp = m_mktemp2(drft, invo_name, NULL, NULL);
125 adios("sendsbr", "unable to create temporary file");
127 if (rename(drft, strncpy(file, cp, sizeof(file)))
129 adios(file, "unable to rename %s to", drft);
133 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
135 /* rename the original draft */
136 if (rename_drft && status == OK &&
137 rename(original_draft, strncpy(buffer,
138 m_backup(original_draft), sizeof(buffer)))
140 advise(buffer, "unable to rename %s to", drft);
153 ** Get rid of any temporary files that we created for attachments.
154 ** Also get rid of the renamed composition file that mhbuild
155 ** leaves as a turd. It looks confusing, but we use the body
156 ** file name to help build the renamed composition file name.
159 if (drft == composition_file_name) {
160 clean_up_temporary_files();
162 if (strlen(composition_file_name) >=
163 sizeof (composition_file_name) - 6)
164 advise(NULL, "unable to remove original composition file.");
167 if ((p = strrchr(composition_file_name, '/')) == NULL)
168 p = composition_file_name;
172 strcpy(body_file_name, p);
174 strcpy(p, body_file_name);
177 unlink(composition_file_name);
185 attach(char *draft_file_name)
187 char buf[MAXPATHLEN + 6]; /* miscellaneous buffer */
188 int c; /* current character for body copy */
189 int has_attachment; /* draft has at least one attachment */
190 int has_body; /* draft has a message body */
191 int non_ascii; /* msg body contains non-ASCII chars */
192 int length; /* length of attachment header field name */
193 char *p; /* miscellaneous string pointer */
196 ** Open up the draft file.
199 if ((draft_file = fopen(draft_file_name, "r")) == (FILE *)0)
200 adios(NULL, "can't open draft file `%s'.",
204 ** Allocate a buffer to hold the header components as they're read in.
205 ** This buffer might need to be quite large, so we grow it as needed.
208 field = (char *)mh_xmalloc(field_size = 256);
211 ** Scan the draft file for an attachment header field name.
212 ** The existence of one indicates that the
213 ** draft has attachments. Bail out if there are no attachments
214 ** because we're done. Read to the end of the headers even if
215 ** we have no attachments.
218 length = strlen(attach_hdr);
222 while (get_line() != EOF && *field != '\0' && *field != '-')
223 if (strncasecmp(field, attach_hdr, length) == 0 &&
224 field[length] == ':')
228 ** We have at least one attachment. Look for at least one
229 ** non-blank line in the body of the message which indicates
230 ** content in the body.
231 ** Check if body contains at least one non-blank (= not empty)
232 ** and if it contains any non-ASCII chars (= need MIME).
238 while (get_line() != EOF) {
239 for (p = field; *p != '\0'; p++) {
240 if (*p != ' ' && *p != '\t') {
243 if (*p > 127 || *p < 0) {
247 if (has_body && non_ascii)
252 ** Bail out if there are no attachments and only ASCII text.
253 ** This means we don't need to convert it to MIME.
255 if (!has_attachment && non_ascii==0) {
263 /* Make names for the temporary files. */
264 strncpy(body_file_name,
265 m_mktemp(toabsdir(invo_name), NULL, NULL),
266 sizeof (body_file_name));
267 strncpy(composition_file_name,
268 m_mktemp(toabsdir(invo_name), NULL, NULL),
269 sizeof (composition_file_name));
272 body_file = fopen(body_file_name, "w");
274 composition_file = fopen(composition_file_name, "w");
276 if ((has_body && body_file == (FILE *)0) ||
277 composition_file == (FILE *)0) {
278 clean_up_temporary_files();
279 adios(NULL, "unable to open all of the temporary files.");
283 ** Start at the beginning of the draft file. Copy all
284 ** non-attachment header fields to the temporary composition file.
285 ** Then add the dashed line separator.
288 while (get_line() != EOF && *field != '\0' && *field != '-')
289 if (strncasecmp(field, attach_hdr, length) != 0 ||
290 field[length] != ':')
291 fprintf(composition_file, "%s\n", field);
292 fputs("--------\n", composition_file);
295 ** Copy the message body to a temporary file.
298 while ((c = getc(draft_file)) != EOF)
304 ** Add a mhbuild MIME composition file line for the body if
308 /* old: make_mime_composition_file_entry(body_file_name); */
309 /* charset will be discovered/guessed by buildmimeproc */
310 fprintf(composition_file, "#text/plain %s\n", body_file_name);
314 ** Now, go back to the beginning of the draft file and look for
315 ** header fields that specify attachments. Add a mhbuild MIME
316 ** composition file for each.
319 while (get_line() != EOF && *field != '\0' && *field != '-') {
320 if (strncasecmp(field, attach_hdr, length) == 0 &&
321 field[length] == ':') {
322 for (p = field + length + 1; *p == ' ' || *p == '\t';
325 make_mime_composition_file_entry(p);
329 fclose(composition_file);
332 ** We're ready to roll! Run mhbuild on the composition file.
333 ** Note that mhbuild is in the context as buildmimeproc.
335 sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
337 if (system(buf) != 0) {
338 clean_up_temporary_files();
346 clean_up_temporary_files(void)
348 unlink(body_file_name);
349 unlink(composition_file_name);
357 int c; /* current character */
358 int n; /* number of bytes in buffer */
359 char *p; /* buffer pointer */
362 ** Get a line from the input file, growing the field buffer as
363 ** needed. We do this so that we can fit an entire line in the
364 ** buffer making it easy to do a string comparison on both the
365 ** field name and the field body which might be a long path name.
368 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
369 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
370 ungetc(c, draft_file);
375 if (++n >= field_size - 1) {
376 field = (char *)mh_xrealloc((void *)field, field_size += 256);
382 /* NUL-terminate the field. */
389 make_mime_composition_file_entry(char *file_name)
391 int binary; /* binary character found flag */
392 int c; /* current character */
393 char *content_type; /* mime content type */
394 FILE *fp; /* content and pipe file pointer */
395 struct node *np; /* context scan node pointer */
396 char *p; /* miscellaneous string pointer */
401 ** Check the file name for a suffix. Scan the context for that
402 ** suffix on a mhshow-suffix- entry. We use these entries to
403 ** be compatible with mhshow, and there's no reason to make the
404 ** user specify each suffix twice. Context entries of the form
405 ** "mhshow-suffix-contenttype" in the name have the suffix in
406 ** the field, including the dot.
409 if ((p = strrchr(file_name, '.')) != NULL) {
410 for (np = m_defs; np; np = np->n_next) {
411 if (strncasecmp(np->n_name, "mhshow-suffix-", 14) == 0
412 && mh_strcasecmp(p, np->n_field) == 0)
414 content_type = np->n_name + 14;
421 ** No content type was found, either because there was no matching
422 ** entry in the context or because the file name has no suffix.
423 ** Open the file and check for non-ASCII characters. Choose the
424 ** content type based on this check.
427 if (content_type == NULL) {
428 if ((fp = fopen(file_name, "r")) == (FILE *)0) {
429 clean_up_temporary_files();
430 adios(NULL, "unable to access file \"%s\"",
436 while ((c = getc(fp)) != EOF) {
437 if (c > 127 || c < 0) {
445 content_type = binary ?
446 "application/octet-stream" : "text/plain";
450 ** Make sure that the attachment file exists and is readable.
451 ** Append a mhbuild directive to the draft file. This starts with
452 ** the content type. Append a file name attribute and a private
453 ** x-unix-mode attribute. Also append a description obtained
454 ** (if possible) by running the "file" command on the file.
457 if (access(file_name, R_OK) != 0) {
458 clean_up_temporary_files();
459 adios(NULL, "unable to access file \"%s\"", file_name);
463 if (stringdex(toabsdir(invo_name), file_name) == 0) {
465 ** Content had been placed by send into a temp file.
466 ** Don't generate Content-Disposition header, because
467 ** it confuses Microsoft Outlook, Build 10.0.6626, at
470 fprintf(composition_file, "#%s <>", content_type);
473 ** Suppress Content-Id, insert simple
474 ** Content-Disposition.
476 fprintf(composition_file,
477 "#%s; name=\"%s\" <>{attachment}",
479 ((p = strrchr(file_name, '/')) == NULL) ?
483 /* Finish up with the file name. */
484 fprintf(composition_file, " %s\n", file_name);
490 ** Annotate original message, and
491 ** call `postproc' to send message.
495 sendaux(char **vec, int vecp, char *drft, struct stat *st)
498 int i, status, fd, fd2;
499 char backup[BUFSIZ], buf[BUFSIZ];
501 fd = pushsw ? tmp_fd() : NOTOK;
506 if ((fd2 = tmp_fd()) != NOTOK) {
507 vec[vecp++] = "-idanno";
508 snprintf(buf, sizeof(buf), "%d", fd2);
511 admonish(NULL, "unable to create file for annotation list");
514 if (distfile && distout(drft, distfile, backup) == NOTOK)
518 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
523 /* oops -- fork error */
524 adios("fork", "unable to");
525 break; /* NOT REACHED */
529 ** child process -- send it
531 ** If fd is ok, then we are pushing and fd points to temp
532 ** file, so capture anything on stdout and stderr there.
535 dup2(fd, fileno(stdout));
536 dup2(fd, fileno(stderr));
539 execvp(postproc, vec);
540 fprintf(stderr, "unable to exec ");
543 break; /* NOT REACHED */
547 ** parent process -- wait for it
549 if ((status = pidwait(child_id, NOTOK)) == OK) {
550 if (annotext && fd2 != NOTOK)
554 ** If postproc failed, and we have good fd (which
555 ** means we pushed), then mail error message
556 ** (and possibly the draft) back to the user.
562 advise(NULL, "message not delivered to anyone");
564 if (annotext && fd2 != NOTOK)
568 if (rename(backup, drft) == NOTOK)
569 advise(drft, "unable to rename %s to",
581 ** Mail error notification (and possibly a copy of the
582 ** message) back to the user, using the mailproc
586 alert(char *file, int out)
592 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
597 /* oops -- fork error */
598 advise("fork", "unable to");
601 /* child process -- send it */
602 SIGNAL(SIGHUP, SIG_IGN);
603 SIGNAL(SIGINT, SIG_IGN);
604 SIGNAL(SIGQUIT, SIG_IGN);
605 SIGNAL(SIGTERM, SIG_IGN);
607 if ((in = open(file, O_RDONLY)) == NOTOK) {
608 admonish(file, "unable to re-open");
610 lseek(out, (off_t) 0, SEEK_END);
611 strncpy(buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
612 write(out, buf, strlen(buf));
613 strncpy(buf, "\n------- Unsent Draft\n\n", sizeof(buf));
614 write(out, buf, strlen(buf));
615 cpydgst(in, out, file, "temporary file");
617 strncpy(buf, "\n------- End of Unsent Draft\n", sizeof(buf));
618 write(out, buf, strlen(buf));
619 if (rename(file, strncpy(buf, m_backup(file), sizeof(buf))) == NOTOK)
620 admonish(buf, "unable to rename %s to", file);
623 lseek(out, (off_t) 0, SEEK_SET);
624 dup2(out, fileno(stdin));
626 /* create subject for error notification */
627 snprintf(buf, sizeof(buf), "send failed on %s",
628 forwsw ? "enclosed draft" : file);
630 execlp(mailproc, mhbasename(mailproc), getusername(),
631 "-subject", buf, NULL);
632 fprintf(stderr, "unable to exec ");
636 default: /* no waiting... */
648 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
649 if (tfile == NULL) return NOTOK;
653 advise(NULL, "temporary file %s selected", tfile);
655 if (unlink(tfile) == NOTOK)
656 advise(tfile, "unable to remove");
663 anno(int fd, struct stat *st)
667 static char *cwd = NULL;
670 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
671 st->st_mtime != st2.st_mtime ||
672 st->st_dev != st2.st_dev ||
673 st->st_ino != st2.st_ino)) {
675 admonish(NULL, "$mhaltmsg mismatch");
679 child_id = debugsw ? NOTOK : fork();
681 case NOTOK: /* oops */
683 advise(NULL, "unable to fork, so doing annotations by hand...");
688 /* block a few signals */
690 sigaddset(&set, SIGHUP);
691 sigaddset(&set, SIGINT);
692 sigaddset(&set, SIGQUIT);
693 sigaddset(&set, SIGTERM);
694 SIGPROCMASK(SIG_BLOCK, &set, &oset);
700 /* reset the signal mask */
701 SIGPROCMASK(SIG_SETMASK, &oset, &set);
706 default: /* no waiting... */
716 int fd2, fd3, msgnum;
717 char *cp, *folder, *maildir;
718 char buffer[BUFSIZ], **ap;
722 if ((folder = getenv("mhfolder")) == NULL || *folder == 0) {
724 admonish(NULL, "$mhfolder not set");
727 maildir = toabsdir(folder);
728 if (chdir(maildir) == NOTOK) {
730 admonish(maildir, "unable to change directory to");
733 if (!(mp = folder_read(folder))) {
735 admonish(NULL, "unable to read folder %s", folder);
739 /* check for empty folder */
740 if (mp->nummsg == 0) {
742 admonish(NULL, "no messages in %s", folder);
746 if ((cp = getenv("mhmessages")) == NULL || *cp == 0) {
748 admonish(NULL, "$mhmessages not set");
751 if (!debugsw /* MOBY HACK... */
753 && (fd3 = open("/dev/null", O_RDWR)) != NOTOK
754 && (fd2 = dup(fileno(stderr))) != NOTOK) {
755 dup2(fd3, fileno(stderr));
760 for (ap = brkstring(cp = getcpy(cp), " ", NULL); *ap; ap++)
764 dup2(fd2, fileno(stderr));
765 if (mp->numsel == 0) {
767 admonish(NULL, "no messages to annotate");
771 lseek(fd, (off_t) 0, SEEK_SET);
772 if ((fp = fdopen(fd, "r")) == NULL) {
774 admonish(NULL, "unable to fdopen annotation list");
778 while (fgets(buffer, sizeof(buffer), fp) != NULL)
779 cp = add(buffer, cp);
783 advise(NULL, "annotate%s with %s: \"%s\"",
784 inplace ? " inplace" : "", annotext, cp);
785 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
786 if (is_selected(mp, msgnum)) {
788 advise(NULL, "annotate message %d", msgnum);
789 annotate(m_name (msgnum), annotext, cp, inplace,
797 folder_free(mp); /* free folder/message structure */
802 armed_done(int status)
804 longjmp(env, status ? status : NOTOK);