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 static char body_file_name[MAXPATHLEN + 1]; /* name of temporary file for body content */
43 static char composition_file_name[MAXPATHLEN + 1]; /* name of mhbuild composition temporary file */
44 static int field_size; /* size of header field buffer */
45 static char *field; /* header field buffer */
46 static FILE *draft_file; /* draft file pointer */
47 static FILE *body_file; /* body file pointer */
48 static FILE *composition_file; /* composition file pointer */
53 int sendsbr (char **, int, char *, struct stat *, int, char *, int);
54 char *getusername (void);
59 static void armed_done (int) NORETURN;
60 static void alert (char *, int);
61 static int tmp_fd (void);
62 static void anno (int, struct stat *);
63 static void annoaux (int);
64 static int sendaux (char **, int, char *, struct stat *);
66 static int attach(char *, char *, int);
67 static void clean_up_temporary_files(void);
68 static int get_line(void);
69 static void make_mime_composition_file_entry(char *, int);
73 * Entry point into (back-end) routines to send message.
77 sendsbr (char **vec, int vecp, char *drft, struct stat *st,
78 int rename_drft, char *attachment_header_field_name, int attachformat)
81 char buffer[BUFSIZ], file[BUFSIZ];
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 * There might be attachments if a header field name for
96 * attachments is supplied. 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.
104 if (attachment_header_field_name != (char *)0) {
105 switch (attach(attachment_header_field_name, drft, attachformat)) {
107 drft = composition_file_name;
119 switch (setjmp (env)) {
122 * If given -push and -unique (which is undocumented), then
123 * rename the draft file. I'm not quite sure why.
125 if (pushsw && unique) {
126 char *cp = m_mktemp2(drft, invo_name, NULL, NULL);
128 adios ("sendsbr", "unable to create temporary file");
130 if (rename (drft, strncpy(file, cp, sizeof(file))) == NOTOK)
131 adios (file, "unable to rename %s to", drft);
135 status = sendaux (vec, vecp, drft, st) ? NOTOK : OK;
137 /* rename the original draft */
138 if (rename_drft && status == OK &&
139 rename (original_draft, strncpy (buffer, m_backup (original_draft), sizeof(buffer))) == NOTOK)
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) >= sizeof (composition_file_name) - 6)
163 advise((char *)0, "unable to remove original composition file.");
166 if ((p = strrchr(composition_file_name, '/')) == (char *)0)
167 p = composition_file_name;
171 (void)strcpy(body_file_name, p);
173 (void)strcpy(p, body_file_name);
174 (void)strcat(p, ".orig");
176 (void)unlink(composition_file_name);
184 attach(char *attachment_header_field_name, 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 length; /* length of attachment header field name */
192 char *p; /* miscellaneous string pointer */
195 * Open up the draft file.
198 if ((draft_file = fopen(draft_file_name, "r")) == (FILE *)0)
199 adios((char *)0, "can't open draft file `%s'.", draft_file_name);
202 * Allocate a buffer to hold the header components as they're read in.
203 * This buffer might need to be quite large, so we grow it as needed.
206 field = (char *)mh_xmalloc(field_size = 256);
209 * Scan the draft file for a header field name that matches the
210 * -attach argument. The existence of one indicates that the
211 * draft has attachments. Bail out if there are no attachments
212 * because we're done. Read to the end of the headers even if
213 * we have no attachments.
216 length = strlen(attachment_header_field_name);
220 while (get_line() != EOF && *field != '\0' && *field != '-')
221 if (strncasecmp(field, attachment_header_field_name, length) == 0 && field[length] == ':')
224 if (has_attachment == 0)
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.
235 while (get_line() != EOF) {
236 for (p = field; *p != '\0'; p++) {
237 if (*p != ' ' && *p != '\t') {
248 * Make names for the temporary files.
251 (void)strncpy(body_file_name,
252 m_mktemp(m_maildir(invo_name), NULL, NULL),
253 sizeof (body_file_name));
254 (void)strncpy(composition_file_name,
255 m_mktemp(m_maildir(invo_name), NULL, NULL),
256 sizeof (composition_file_name));
259 body_file = fopen(body_file_name, "w");
261 composition_file = fopen(composition_file_name, "w");
263 if ((has_body && body_file == (FILE *)0) || composition_file == (FILE *)0) {
264 clean_up_temporary_files();
265 adios((char *)0, "unable to open all of the temporary files.");
269 * Start at the beginning of the draft file. Copy all
270 * non-attachment header fields to the temporary composition file.
271 * Then add the dashed line separator.
276 while (get_line() != EOF && *field != '\0' && *field != '-')
277 if (strncasecmp(field, attachment_header_field_name, length) != 0 || field[length] != ':')
278 (void)fprintf(composition_file, "%s\n", field);
280 (void)fputs("--------\n", composition_file);
283 * Copy the message body to a temporary file.
287 while ((c = getc(draft_file)) != EOF)
290 (void)fclose(body_file);
294 * Add a mhbuild MIME composition file line for the body if
299 make_mime_composition_file_entry(body_file_name, attachformat);
302 * Now, go back to the beginning of the draft file and look for
303 * header fields that specify attachments. Add a mhbuild MIME
304 * composition file for each.
309 while (get_line() != EOF && *field != '\0' && *field != '-') {
310 if (strncasecmp(field, attachment_header_field_name, length) == 0 && field[length] == ':') {
311 for (p = field + length + 1; *p == ' ' || *p == '\t'; p++)
314 make_mime_composition_file_entry(p, attachformat);
318 (void)fclose(composition_file);
321 * We're ready to roll! Run mhbuild on the composition file.
322 * Note that mhbuild is in the context as buildmimeproc.
325 (void)sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
327 if (system(buf) != 0) {
328 clean_up_temporary_files();
336 clean_up_temporary_files(void)
338 (void)unlink(body_file_name);
339 (void)unlink(composition_file_name);
347 int c; /* current character */
348 int n; /* number of bytes in buffer */
349 char *p; /* buffer pointer */
352 * Get a line from the input file, growing the field buffer as
353 * needed. We do this so that we can fit an entire line in the
354 * buffer making it easy to do a string comparison on both the
355 * field name and the field body which might be a long path name.
358 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
359 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
360 (void)ungetc(c, draft_file);
365 if (++n >= field_size - 1) {
366 field = (char *)mh_xrealloc((void *)field, field_size += 256);
373 * NUL-terminate the field..
382 make_mime_composition_file_entry(char *file_name, int attachformat)
384 int binary; /* binary character found flag */
385 int c; /* current character */
386 char cmd[MAXPATHLEN + 6]; /* file command buffer */
387 char *content_type; /* mime content type */
388 FILE *fp; /* content and pipe file pointer */
389 struct node *np; /* context scan node pointer */
390 char *p; /* miscellaneous string pointer */
391 struct stat st; /* file status buffer */
393 content_type = (char *)0;
396 * Check the file name for a suffix. Scan the context for that
397 * suffix on a mhshow-suffix- entry. We use these entries to
398 * be compatible with mhshow, and there's no reason to make the
399 * user specify each suffix twice. Context entries of the form
400 * "mhshow-suffix-contenttype" in the name have the suffix in
401 * the field, including the dot.
404 if ((p = strrchr(file_name, '.')) != (char *)0) {
405 for (np = m_defs; np; np = np->n_next) {
406 if (strncasecmp(np->n_name, "mhshow-suffix-", 14) == 0 && mh_strcasecmp(p, np->n_field) == 0) {
407 content_type = np->n_name + 14;
414 * No content type was found, either because there was no matching
415 * entry in the context or because the file name has no suffix.
416 * Open the file and check for non-ASCII characters. Choose the
417 * content type based on this check.
420 if (content_type == (char *)0) {
421 if ((fp = fopen(file_name, "r")) == (FILE *)0) {
422 clean_up_temporary_files();
423 adios((char *)0, "unable to access file \"%s\"", file_name);
428 while ((c = getc(fp)) != EOF) {
429 if (c > 127 || c < 0) {
437 content_type = binary ? "application/octet-stream" : "text/plain";
441 * Make sure that the attachment file exists and is readable.
442 * Append a mhbuild directive to the draft file. This starts with
443 * the content type. Append a file name attribute and a private
444 * x-unix-mode attribute. Also append a description obtained
445 * (if possible) by running the "file" command on the file.
448 if (stat(file_name, &st) == -1 || access(file_name, R_OK) != 0) {
449 clean_up_temporary_files();
450 adios((char *)0, "unable to access file \"%s\"", file_name);
453 switch (attachformat) {
455 /* Insert name, file mode, and Content-Id. */
456 (void)fprintf(composition_file, "#%s; name=\"%s\"; x-unix-mode=0%.3ho",
457 content_type, ((p = strrchr(file_name, '/')) == (char *)0) ? file_name : p + 1, (unsigned short)(st.st_mode & 0777));
459 if (strlen(file_name) > MAXPATHLEN) {
460 clean_up_temporary_files();
461 adios((char *)0, "attachment file name `%s' too long.", file_name);
464 (void)sprintf(cmd, "file '%s'", file_name);
466 if ((fp = popen(cmd, "r")) != (FILE *)0 && fgets(cmd, sizeof (cmd), fp) != (char *)0) {
467 *strchr(cmd, '\n') = '\0';
470 * The output of the "file" command is of the form
474 * Strip off the "file:" and subsequent white space.
477 for (p = cmd; *p != '\0'; p++) {
479 for (p++; *p != '\0'; p++) {
488 /* Insert Content-Description. */
489 (void)fprintf(composition_file, " [ %s ]", p);
496 if (stringdex (m_maildir(invo_name), file_name) == 0) {
498 * Content had been placed by send into a temp file.
499 * Don't generate Content-Disposition header, because
500 * it confuses Microsoft Outlook, Build 10.0.6626, at
503 (void) fprintf (composition_file, "#%s <>", content_type);
505 /* Suppress Content-Id, insert simple Content-Disposition. */
506 (void) fprintf (composition_file,
507 "#%s; name=\"%s\" <>{attachment}",
509 ((p = strrchr(file_name, '/')) == (char *)0) ?
515 if (stringdex (m_maildir(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 (void) fprintf (composition_file, "#%s <>", content_type);
525 * Suppress Content-Id, insert Content-Disposition
526 * with modification date.
528 (void) fprintf (composition_file,
529 "#%s; name=\"%s\" <>{attachment; modification-date=\"%s\"}",
531 ((p = strrchr(file_name, '/')) == (char *)0) ?
532 file_name : p + 1, dtime (&st.st_mtime, 0));
537 adios ((char *)0, "unsupported attachformat %d", attachformat);
541 * Finish up with the file name.
544 (void)fprintf(composition_file, " %s\n", file_name);
550 * Annotate original message, and
551 * call `postproc' to send message.
555 sendaux (char **vec, int vecp, char *drft, struct stat *st)
558 int i, status, fd, fd2;
559 char backup[BUFSIZ], buf[BUFSIZ];
561 fd = pushsw ? tmp_fd () : NOTOK;
566 if ((fd2 = tmp_fd ()) != NOTOK) {
567 vec[vecp++] = "-idanno";
568 snprintf (buf, sizeof(buf), "%d", fd2);
571 admonish (NULL, "unable to create file for annotation list");
574 if (distfile && distout (drft, distfile, backup) == NOTOK)
578 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
583 /* oops -- fork error */
584 adios ("fork", "unable to");
585 break; /* NOT REACHED */
589 * child process -- send it
591 * If fd is ok, then we are pushing and fd points to temp
592 * file, so capture anything on stdout and stderr there.
595 dup2 (fd, fileno (stdout));
596 dup2 (fd, fileno (stderr));
599 execvp (postproc, vec);
600 fprintf (stderr, "unable to exec ");
603 break; /* NOT REACHED */
607 * parent process -- wait for it
609 if ((status = pidwait(child_id, NOTOK)) == OK) {
610 if (annotext && fd2 != NOTOK)
614 * If postproc failed, and we have good fd (which means
615 * we pushed), then mail error message (and possibly the
616 * draft) back to the user.
622 advise (NULL, "message not delivered to anyone");
624 if (annotext && fd2 != NOTOK)
628 if (rename (backup, drft) == NOTOK)
629 advise (drft, "unable to rename %s to", backup);
640 * Mail error notification (and possibly a copy of the
641 * message) back to the user, using the mailproc
645 alert (char *file, int out)
651 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
656 /* oops -- fork error */
657 advise ("fork", "unable to");
660 /* child process -- send it */
661 SIGNAL (SIGHUP, SIG_IGN);
662 SIGNAL (SIGINT, SIG_IGN);
663 SIGNAL (SIGQUIT, SIG_IGN);
664 SIGNAL (SIGTERM, SIG_IGN);
666 if ((in = open (file, O_RDONLY)) == NOTOK) {
667 admonish (file, "unable to re-open");
669 lseek (out, (off_t) 0, SEEK_END);
670 strncpy (buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
671 write (out, buf, strlen (buf));
672 strncpy (buf, "\n------- Unsent Draft\n\n", sizeof(buf));
673 write (out, buf, strlen (buf));
674 cpydgst (in, out, file, "temporary file");
676 strncpy (buf, "\n------- End of Unsent Draft\n", sizeof(buf));
677 write (out, buf, strlen (buf));
678 if (rename (file, strncpy (buf, m_backup (file), sizeof(buf))) == NOTOK)
679 admonish (buf, "unable to rename %s to", file);
682 lseek (out, (off_t) 0, SEEK_SET);
683 dup2 (out, fileno (stdin));
685 /* create subject for error notification */
686 snprintf (buf, sizeof(buf), "send failed on %s",
687 forwsw ? "enclosed draft" : file);
689 execlp (mailproc, r1bindex (mailproc, '/'), getusername (),
690 "-subject", buf, NULL);
691 fprintf (stderr, "unable to exec ");
695 default: /* no waiting... */
707 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
708 if (tfile == NULL) return NOTOK;
712 advise (NULL, "temporary file %s selected", tfile);
714 if (unlink (tfile) == NOTOK)
715 advise (tfile, "unable to remove");
722 anno (int fd, struct stat *st)
726 static char *cwd = NULL;
730 (stat (altmsg, &st2) == NOTOK
731 || st->st_mtime != st2.st_mtime
732 || st->st_dev != st2.st_dev
733 || st->st_ino != st2.st_ino)) {
735 admonish (NULL, "$mhaltmsg mismatch");
739 child_id = debugsw ? NOTOK : fork ();
741 case NOTOK: /* oops */
744 "unable to fork, so doing annotations by hand...");
746 cwd = getcpy (pwd ());
749 /* block a few signals */
751 sigaddset (&set, SIGHUP);
752 sigaddset (&set, SIGINT);
753 sigaddset (&set, SIGQUIT);
754 sigaddset (&set, SIGTERM);
755 SIGPROCMASK (SIG_BLOCK, &set, &oset);
761 /* reset the signal mask */
762 SIGPROCMASK (SIG_SETMASK, &oset, &set);
767 default: /* no waiting... */
777 int fd2, fd3, msgnum;
778 char *cp, *folder, *maildir;
779 char buffer[BUFSIZ], **ap;
783 if ((folder = getenv ("mhfolder")) == NULL || *folder == 0) {
785 admonish (NULL, "$mhfolder not set");
788 maildir = m_maildir (folder);
789 if (chdir (maildir) == NOTOK) {
791 admonish (maildir, "unable to change directory to");
794 if (!(mp = folder_read (folder))) {
796 admonish (NULL, "unable to read folder %s", folder);
800 /* check for empty folder */
801 if (mp->nummsg == 0) {
803 admonish (NULL, "no messages in %s", folder);
807 if ((cp = getenv ("mhmessages")) == NULL || *cp == 0) {
809 admonish (NULL, "$mhmessages not set");
812 if (!debugsw /* MOBY HACK... */
814 && (fd3 = open ("/dev/null", O_RDWR)) != NOTOK
815 && (fd2 = dup (fileno (stderr))) != NOTOK) {
816 dup2 (fd3, fileno (stderr));
821 for (ap = brkstring (cp = getcpy (cp), " ", NULL); *ap; ap++)
825 dup2 (fd2, fileno (stderr));
826 if (mp->numsel == 0) {
828 admonish (NULL, "no messages to annotate");
832 lseek (fd, (off_t) 0, SEEK_SET);
833 if ((fp = fdopen (fd, "r")) == NULL) {
835 admonish (NULL, "unable to fdopen annotation list");
839 while (fgets (buffer, sizeof(buffer), fp) != NULL)
840 cp = add (buffer, cp);
844 advise (NULL, "annotate%s with %s: \"%s\"",
845 inplace ? " inplace" : "", annotext, cp);
846 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
847 if (is_selected(mp, msgnum)) {
849 advise (NULL, "annotate message %d", msgnum);
850 annotate (m_name (msgnum), annotext, cp, inplace, 1, -2, 0);
857 folder_free (mp); /* free folder/message structure */
862 armed_done (int status)
864 longjmp (env, status ? status : NOTOK);