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 != (char *)0) {
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, m_backup (original_draft), sizeof(buffer))) == NOTOK)
144 advise (buffer, "unable to rename %s to", drft);
157 ** Get rid of any temporary files that we created for attachments.
158 ** Also get rid of the renamed composition file that mhbuild
159 ** leaves as a turd. It looks confusing, but we use the body
160 ** file name to help build the renamed composition file name.
163 if (drft == composition_file_name) {
164 clean_up_temporary_files();
166 if (strlen(composition_file_name) >=
167 sizeof (composition_file_name) - 6)
168 advise((char *)0, "unable to remove original composition file.");
171 if ((p = strrchr(composition_file_name, '/')) == NULL)
172 p = composition_file_name;
176 (void)strcpy(body_file_name, p);
178 (void)strcpy(p, body_file_name);
179 (void)strcat(p, ".orig");
181 (void)unlink(composition_file_name);
189 attach(char *attachment_header_field_name, char *draft_file_name,
192 char buf[MAXPATHLEN + 6]; /* miscellaneous buffer */
193 int c; /* current character for body copy */
194 int has_attachment; /* draft has at least one attachment */
195 int has_body; /* draft has a message body */
196 int length; /* length of attachment header field name */
197 char *p; /* miscellaneous string pointer */
200 ** Open up the draft file.
203 if ((draft_file = fopen(draft_file_name, "r")) == (FILE *)0)
204 adios((char *)0, "can't open draft file `%s'.",
208 ** Allocate a buffer to hold the header components as they're read in.
209 ** This buffer might need to be quite large, so we grow it as needed.
212 field = (char *)mh_xmalloc(field_size = 256);
215 ** Scan the draft file for a header field name that matches the
216 ** -attach argument. The existence of one indicates that the
217 ** draft has attachments. Bail out if there are no attachments
218 ** because we're done. Read to the end of the headers even if
219 ** we have no attachments.
222 length = strlen(attachment_header_field_name);
226 while (get_line() != EOF && *field != '\0' && *field != '-')
227 if (strncasecmp(field, attachment_header_field_name, length)
228 == 0 && field[length] == ':')
231 if (has_attachment == 0)
235 ** We have at least one attachment. Look for at least one
236 ** non-blank line in the body of the message which indicates
237 ** content in the body.
242 while (get_line() != EOF) {
243 for (p = field; *p != '\0'; p++) {
244 if (*p != ' ' && *p != '\t') {
255 ** Make names for the temporary files.
258 (void)strncpy(body_file_name,
259 m_mktemp(m_maildir(invo_name), NULL, NULL),
260 sizeof (body_file_name));
261 (void)strncpy(composition_file_name,
262 m_mktemp(m_maildir(invo_name), NULL, NULL),
263 sizeof (composition_file_name));
266 body_file = fopen(body_file_name, "w");
268 composition_file = fopen(composition_file_name, "w");
270 if ((has_body && body_file == (FILE *)0) ||
271 composition_file == (FILE *)0) {
272 clean_up_temporary_files();
273 adios((char *)0, "unable to open all of the temporary files.");
277 ** Start at the beginning of the draft file. Copy all
278 ** non-attachment header fields to the temporary composition file.
279 ** Then add the dashed line separator.
284 while (get_line() != EOF && *field != '\0' && *field != '-')
285 if (strncasecmp(field, attachment_header_field_name, length)
286 != 0 || field[length] != ':')
287 (void)fprintf(composition_file, "%s\n", field);
289 (void)fputs("--------\n", composition_file);
292 ** Copy the message body to a temporary file.
296 while ((c = getc(draft_file)) != EOF)
299 (void)fclose(body_file);
303 ** Add a mhbuild MIME composition file line for the body if
308 make_mime_composition_file_entry(body_file_name, attachformat);
311 ** Now, go back to the beginning of the draft file and look for
312 ** header fields that specify attachments. Add a mhbuild MIME
313 ** composition file for each.
318 while (get_line() != EOF && *field != '\0' && *field != '-') {
319 if (strncasecmp(field, attachment_header_field_name, length)
320 == 0 && field[length] == ':') {
321 for (p = field + length + 1; *p == ' ' || *p == '\t';
325 make_mime_composition_file_entry(p, attachformat);
329 (void)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.
336 (void)sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
338 if (system(buf) != 0) {
339 clean_up_temporary_files();
347 clean_up_temporary_files(void)
349 (void)unlink(body_file_name);
350 (void)unlink(composition_file_name);
358 int c; /* current character */
359 int n; /* number of bytes in buffer */
360 char *p; /* buffer pointer */
363 ** Get a line from the input file, growing the field buffer as
364 ** needed. We do this so that we can fit an entire line in the
365 ** buffer making it easy to do a string comparison on both the
366 ** field name and the field body which might be a long path name.
369 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
370 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
371 (void)ungetc(c, draft_file);
376 if (++n >= field_size - 1) {
377 field = (char *)mh_xrealloc((void *)field, field_size += 256);
383 /* NUL-terminate the field. */
390 make_mime_composition_file_entry(char *file_name, int attachformat)
392 int binary; /* binary character found flag */
393 int c; /* current character */
394 char cmd[MAXPATHLEN + 6]; /* file command buffer */
395 char *content_type; /* mime content type */
396 FILE *fp; /* content and pipe file pointer */
397 struct node *np; /* context scan node pointer */
398 char *p; /* miscellaneous string pointer */
399 struct stat st; /* file status buffer */
401 content_type = (char *)0;
404 ** Check the file name for a suffix. Scan the context for that
405 ** suffix on a mhshow-suffix- entry. We use these entries to
406 ** be compatible with mhshow, and there's no reason to make the
407 ** user specify each suffix twice. Context entries of the form
408 ** "mhshow-suffix-contenttype" in the name have the suffix in
409 ** the field, including the dot.
412 if ((p = strrchr(file_name, '.')) != (char *)0) {
413 for (np = m_defs; np; np = np->n_next) {
414 if (strncasecmp(np->n_name, "mhshow-suffix-", 14) == 0
415 && mh_strcasecmp(p, np->n_field) == 0)
417 content_type = np->n_name + 14;
424 ** No content type was found, either because there was no matching
425 ** entry in the context or because the file name has no suffix.
426 ** Open the file and check for non-ASCII characters. Choose the
427 ** content type based on this check.
430 if (content_type == (char *)0) {
431 if ((fp = fopen(file_name, "r")) == (FILE *)0) {
432 clean_up_temporary_files();
433 adios((char *)0, "unable to access file \"%s\"",
439 while ((c = getc(fp)) != EOF) {
440 if (c > 127 || c < 0) {
448 content_type = binary ?
449 "application/octet-stream" : "text/plain";
453 ** Make sure that the attachment file exists and is readable.
454 ** Append a mhbuild directive to the draft file. This starts with
455 ** the content type. Append a file name attribute and a private
456 ** x-unix-mode attribute. Also append a description obtained
457 ** (if possible) by running the "file" command on the file.
460 if (stat(file_name, &st) == -1 || access(file_name, R_OK) != 0) {
461 clean_up_temporary_files();
462 adios((char *)0, "unable to access file \"%s\"", file_name);
465 switch (attachformat) {
467 /* Insert name, file mode, and Content-Id. */
468 (void)fprintf(composition_file,
469 "#%s; name=\"%s\"; x-unix-mode=0%.3ho",
470 content_type, ((p = strrchr(file_name, '/'))
471 == (char *)0) ? file_name : p + 1,
472 (unsigned short)(st.st_mode & 0777));
474 if (strlen(file_name) > MAXPATHLEN) {
475 clean_up_temporary_files();
476 adios((char *)0, "attachment file name `%s' too long.",
480 (void)sprintf(cmd, "file '%s'", file_name);
482 if ((fp = popen(cmd, "r")) != (FILE *)0 &&
483 fgets(cmd, sizeof (cmd), fp) != (char *)0) {
484 *strchr(cmd, '\n') = '\0';
487 ** The output of the "file" command is of the form
491 ** Strip off the "file:" and subsequent white space.
494 for (p = cmd; *p != '\0'; p++) {
496 for (p++; *p != '\0'; p++) {
505 /* Insert Content-Description. */
506 (void)fprintf(composition_file, " [ %s ]", p);
513 if (stringdex (m_maildir(invo_name), file_name) == 0) {
515 ** Content had been placed by send into a temp file.
516 ** Don't generate Content-Disposition header, because
517 ** it confuses Microsoft Outlook, Build 10.0.6626, at
520 (void) fprintf (composition_file, "#%s <>", content_type);
523 ** Suppress Content-Id, insert simple
524 ** Content-Disposition.
526 (void) fprintf (composition_file,
527 "#%s; name=\"%s\" <>{attachment}",
529 ((p = strrchr(file_name, '/')) == (char *)0) ?
535 if (stringdex (m_maildir(invo_name), file_name) == 0) {
537 ** Content had been placed by send into a temp file.
538 ** Don't generate Content-Disposition header, because
539 ** it confuses Microsoft Outlook, Build 10.0.6626, at
542 (void) fprintf (composition_file, "#%s <>",
546 ** Suppress Content-Id, insert Content-Disposition
547 ** with modification date.
549 (void) fprintf (composition_file,
550 "#%s; name=\"%s\" <>{attachment; modification-date=\"%s\"}",
552 ((p = strrchr(file_name, '/')) == (char *)0) ?
553 file_name : p + 1, dtime (&st.st_mtime, 0));
558 adios ((char *)0, "unsupported attachformat %d", attachformat);
562 ** Finish up with the file name.
565 (void)fprintf(composition_file, " %s\n", file_name);
571 ** Annotate original message, and
572 ** call `postproc' to send message.
576 sendaux (char **vec, int vecp, char *drft, struct stat *st)
579 int i, status, fd, fd2;
580 char backup[BUFSIZ], buf[BUFSIZ];
582 fd = pushsw ? tmp_fd () : NOTOK;
587 if ((fd2 = tmp_fd ()) != NOTOK) {
588 vec[vecp++] = "-idanno";
589 snprintf (buf, sizeof(buf), "%d", fd2);
592 admonish (NULL, "unable to create file for annotation list");
595 if (distfile && distout (drft, distfile, backup) == NOTOK)
599 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
604 /* oops -- fork error */
605 adios ("fork", "unable to");
606 break; /* NOT REACHED */
610 ** child process -- send it
612 ** If fd is ok, then we are pushing and fd points to temp
613 ** file, so capture anything on stdout and stderr there.
616 dup2 (fd, fileno (stdout));
617 dup2 (fd, fileno (stderr));
620 execvp (postproc, vec);
621 fprintf (stderr, "unable to exec ");
624 break; /* NOT REACHED */
628 ** parent process -- wait for it
630 if ((status = pidwait(child_id, NOTOK)) == OK) {
631 if (annotext && fd2 != NOTOK)
635 ** If postproc failed, and we have good fd (which
636 ** means we pushed), then mail error message
637 ** (and possibly the draft) back to the user.
643 advise (NULL, "message not delivered to anyone");
645 if (annotext && fd2 != NOTOK)
649 if (rename (backup, drft) == NOTOK)
650 advise (drft, "unable to rename %s to",
662 ** Mail error notification (and possibly a copy of the
663 ** message) back to the user, using the mailproc
667 alert (char *file, int out)
673 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
678 /* oops -- fork error */
679 advise ("fork", "unable to");
682 /* child process -- send it */
683 SIGNAL (SIGHUP, SIG_IGN);
684 SIGNAL (SIGINT, SIG_IGN);
685 SIGNAL (SIGQUIT, SIG_IGN);
686 SIGNAL (SIGTERM, SIG_IGN);
688 if ((in = open (file, O_RDONLY)) == NOTOK) {
689 admonish (file, "unable to re-open");
691 lseek (out, (off_t) 0, SEEK_END);
692 strncpy (buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
693 write (out, buf, strlen (buf));
694 strncpy (buf, "\n------- Unsent Draft\n\n", sizeof(buf));
695 write (out, buf, strlen (buf));
696 cpydgst (in, out, file, "temporary file");
698 strncpy (buf, "\n------- End of Unsent Draft\n", sizeof(buf));
699 write (out, buf, strlen (buf));
700 if (rename (file, strncpy (buf, m_backup (file), sizeof(buf))) == NOTOK)
701 admonish (buf, "unable to rename %s to", file);
704 lseek (out, (off_t) 0, SEEK_SET);
705 dup2 (out, fileno (stdin));
707 /* create subject for error notification */
708 snprintf (buf, sizeof(buf), "send failed on %s",
710 "enclosed draft" : file);
712 execlp (mailproc, r1bindex (mailproc, '/'),
714 "-subject", buf, NULL);
715 fprintf (stderr, "unable to exec ");
719 default: /* no waiting... */
731 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
732 if (tfile == NULL) return NOTOK;
736 advise (NULL, "temporary file %s selected", tfile);
738 if (unlink (tfile) == NOTOK)
739 advise (tfile, "unable to remove");
746 anno (int fd, struct stat *st)
750 static char *cwd = NULL;
754 (stat (altmsg, &st2) == NOTOK
755 || st->st_mtime != st2.st_mtime
756 || st->st_dev != st2.st_dev
757 || st->st_ino != st2.st_ino)) {
759 admonish (NULL, "$mhaltmsg mismatch");
763 child_id = debugsw ? NOTOK : fork ();
765 case NOTOK: /* oops */
767 advise (NULL, "unable to fork, so doing annotations by hand...");
769 cwd = getcpy (pwd ());
772 /* block a few signals */
774 sigaddset (&set, SIGHUP);
775 sigaddset (&set, SIGINT);
776 sigaddset (&set, SIGQUIT);
777 sigaddset (&set, SIGTERM);
778 SIGPROCMASK (SIG_BLOCK, &set, &oset);
784 /* reset the signal mask */
785 SIGPROCMASK (SIG_SETMASK, &oset, &set);
790 default: /* no waiting... */
800 int fd2, fd3, msgnum;
801 char *cp, *folder, *maildir;
802 char buffer[BUFSIZ], **ap;
806 if ((folder = getenv ("mhfolder")) == NULL || *folder == 0) {
808 admonish (NULL, "$mhfolder not set");
811 maildir = m_maildir (folder);
812 if (chdir (maildir) == NOTOK) {
814 admonish (maildir, "unable to change directory to");
817 if (!(mp = folder_read (folder))) {
819 admonish (NULL, "unable to read folder %s", folder);
823 /* check for empty folder */
824 if (mp->nummsg == 0) {
826 admonish (NULL, "no messages in %s", folder);
830 if ((cp = getenv ("mhmessages")) == NULL || *cp == 0) {
832 admonish (NULL, "$mhmessages not set");
835 if (!debugsw /* MOBY HACK... */
837 && (fd3 = open ("/dev/null", O_RDWR)) != NOTOK
838 && (fd2 = dup (fileno (stderr))) != NOTOK) {
839 dup2 (fd3, fileno (stderr));
844 for (ap = brkstring (cp = getcpy (cp), " ", NULL); *ap; ap++)
848 dup2 (fd2, fileno (stderr));
849 if (mp->numsel == 0) {
851 admonish (NULL, "no messages to annotate");
855 lseek (fd, (off_t) 0, SEEK_SET);
856 if ((fp = fdopen (fd, "r")) == NULL) {
858 admonish (NULL, "unable to fdopen annotation list");
862 while (fgets (buffer, sizeof(buffer), fp) != NULL)
863 cp = add (buffer, cp);
867 advise (NULL, "annotate%s with %s: \"%s\"",
868 inplace ? " inplace" : "", annotext, cp);
869 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
870 if (is_selected(mp, msgnum)) {
872 advise (NULL, "annotate message %d", msgnum);
873 annotate (m_name (msgnum), annotext, cp, inplace, 1, -2, 0);
880 folder_free (mp); /* free folder/message structure */
885 armed_done (int status)
887 longjmp (env, status ? status : NOTOK);