2 ** send.c -- send a composed message
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.
13 #include <h/signals.h>
19 #ifdef TIME_WITH_SYS_TIME
20 # include <sys/time.h>
23 # ifdef TM_IN_SYS_TIME
24 # include <sys/time.h>
30 int debugsw = 0; /* global */
35 char *annotext = NULL;
36 char *distfile = NULL;
40 /* name of temp file for body content */
41 static char body_file_name[MAXPATHLEN + 1];
42 /* name of mhbuild composition temporary file */
43 static char composition_file_name[MAXPATHLEN + 1];
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 static int sendsbr(char **, int, char *, struct stat *, int);
54 static void armed_done(int) NORETURN;
55 static void alert(char *, int);
56 static int tmp_fd(void);
57 static void anno(struct stat *);
58 static int sendaux(char **, int, char *, struct stat *);
59 static int attach(char *);
60 static void clean_up_temporary_files(void);
61 static int get_line(void);
62 static void make_mime_composition_file_entry(char *);
65 static struct swit switches[] = {
67 { "alias aliasfile", 0 },
95 main(int argc, char **argv)
97 int msgp = 0, nfiles = 0, distsw = 0, vecp = 1;
100 char *cp, *maildir = NULL;
101 char buf[BUFSIZ], **ap, **argp, **arguments;
102 char *msgs[MAXARGS], *vec[MAXARGS];
103 char *files[MAXARGS];
110 setlocale(LC_ALL, "");
112 invo_name = mhbasename(argv[0]);
114 /* read user profile/context */
117 arguments = getarguments(invo_name, argc, argv, 1);
120 vec[vecp++] = "-library";
121 vec[vecp++] = getcpy(toabsdir("+"));
123 while ((cp = *argp++)) {
125 switch (smatch(++cp, switches)) {
127 ambigsw(cp, switches);
130 adios(NULL, "-%s unknown\n", cp);
133 snprintf(buf, sizeof(buf),
134 "%s [file] [switches]",
136 print_help(buf, switches, 1);
139 print_version(invo_name);
166 debugsw++; /* fall */
174 if (!(cp = *argp++) || *cp == '-') {
175 adios(NULL, "missing argument to %s",
184 files[nfiles++] = cp;
191 /* check for "Aliasfile:" profile entry */
192 if ((cp = context_find("Aliasfile"))) {
195 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap; ap++) {
196 vec[vecp++] = "-alias";
197 vec[vecp++] = getcpy(etcpath(*ap));
201 if (!msgp && !nfiles) {
202 msgs[msgp++] = seq_cur;
204 maildir = toabsdir(draftfolder);
206 if (chdir(maildir) == NOTOK) {
207 adios(maildir, "unable to change directory to");
210 if (!(mp = folder_read(draftfolder))) {
211 adios(NULL, "unable to read draft folder %s", draftfolder);
213 if (mp->nummsg == 0) {
214 adios(NULL, "no messages in draft folder %s", draftfolder);
216 /* parse all the message ranges/sequences and set SELECTED */
217 for (msgnum = 0; msgnum < msgp; msgnum++) {
218 if (!m_convert(mp, msgs[msgnum])) {
224 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
225 if (is_selected(mp, msgnum)) {
226 files[nfiles++] = getcpy(m_name(msgnum));
227 unset_exists(mp, msgnum);
231 mp->msgflags |= SEQMOD;
234 if (!(cp = getenv("SIGNATURE")) || !*cp) {
235 if ((cp = context_find("signature")) && *cp) {
236 m_putenv("SIGNATURE", cp);
240 for (msgnum = 0; msgnum < nfiles; msgnum++) {
241 if (stat(files[msgnum], &st) == NOTOK) {
242 adios(files[msgnum], "unable to stat draft file");
246 if (!(annotext = getenv("mhannotate")) || !*annotext) {
249 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
250 altmsg = NULL; /* used by dist interface - see below */
253 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
254 vec[vecp++] = "-dist";
255 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
256 adios(altmsg, "unable to open for reading");
259 distfile = getcpy(m_mktemp2(NULL, invo_name, NULL, NULL));
260 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
261 adios(distfile, "unable to open for writing");
263 cpydata(in, out, altmsg, distfile);
270 if (!altmsg || stat(altmsg, &st) == NOTOK) {
279 vec[0] = mhbasename(postproc);
282 for (msgnum = 0; msgnum < nfiles; msgnum++) {
283 switch (sendsbr(vec, vecp, files[msgnum], &st, 1)) {
300 ** message sending back-end
303 sendsbr(char **vec, int vecp, char *drft, struct stat *st, int rename_drft)
307 char *original_draft;
308 char *p; /* string pointer for building file name */
311 ** Save the original name of the draft file. The name of the
312 ** draft file is changed to a temporary file containing the built
313 ** MIME message if there are attachments. We need the original
314 ** name so that it can be renamed after the message is sent.
316 original_draft = drft;
319 ** Convert the draft to a MIME message.
320 ** Use the mhbuild composition file for the draft if there was
321 ** a successful conversion because that now contains the MIME
322 ** message. A nice side effect of this is that it leaves the
323 ** original draft file untouched so that it can be retrieved
324 ** and modified if desired.
326 switch (attach(drft)) {
328 drft = composition_file_name;
339 switch (setjmp(env)) {
341 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
342 /* rename the original draft */
343 if (rename_drft && status == OK &&
344 rename(original_draft, strncpy(buffer,
345 m_backup(original_draft), sizeof(buffer)))
347 advise(buffer, "unable to rename %s to", drft);
362 ** Get rid of any temporary files that we created for attachments.
363 ** Also get rid of the renamed composition file that mhbuild
364 ** leaves as a turd. It looks confusing, but we use the body
365 ** file name to help build the renamed composition file name.
367 if (drft == composition_file_name) {
368 clean_up_temporary_files();
370 if (strlen(composition_file_name) >=
371 sizeof (composition_file_name) - 6) {
372 advise(NULL, "unable to remove original composition file.");
374 if (!(p = strrchr(composition_file_name, '/'))) {
375 p = composition_file_name;
379 strcpy(body_file_name, p);
381 strcpy(p, body_file_name);
384 unlink(composition_file_name);
392 attach(char *draft_file_name)
394 char buf[MAXPATHLEN + 6];
398 int non_ascii; /* msg body contains non-ASCII chars */
399 int length; /* length of attachment header field name */
402 if (!(draft_file = fopen(draft_file_name, "r"))) {
403 adios(NULL, "can't open draft file `%s'.", draft_file_name);
406 /* We'll grow the buffer as needed. */
407 field = (char *)mh_xmalloc(field_size = 256);
410 ** Scan the draft file for an attachment header field name.
412 length = strlen(attach_hdr);
414 while (get_line() != EOF && *field != '\0' && *field != '-') {
415 if (strncasecmp(field, attach_hdr, length)==0 &&
416 field[length] == ':') {
422 ** Look for at least one non-blank line in the body of the
423 ** message which indicates content in the body.
424 ** Check if body contains at least one non-blank (= not empty)
425 ** and if it contains any non-ASCII chars (= need MIME).
429 while (get_line() != EOF) {
430 for (p = field; *p != '\0'; p++) {
431 if (*p != ' ' && *p != '\t') {
434 if (*p > 127 || *p < 0) {
438 if (has_body && non_ascii) {
439 break; /* that's been already enough information */
443 if (!has_attachment && non_ascii==0) {
444 /* We don't need to convert it to MIME. */
452 /* Make names for the temporary files. */
453 strncpy(body_file_name,
454 m_mktemp(toabsdir(invo_name), NULL, NULL),
455 sizeof (body_file_name));
456 strncpy(composition_file_name,
457 m_mktemp(toabsdir(invo_name), NULL, NULL),
458 sizeof (composition_file_name));
461 body_file = fopen(body_file_name, "w");
463 composition_file = fopen(composition_file_name, "w");
465 if ((has_body && !body_file) || !composition_file) {
466 clean_up_temporary_files();
467 adios(NULL, "unable to open all of the temporary files.");
470 /* Copy non-attachment header fields to the temp composition file. */
472 while (get_line() != EOF && *field && *field != '-') {
473 if (strncasecmp(field, attach_hdr, length) != 0 ||
474 field[length] != ':') {
475 fprintf(composition_file, "%s\n", field);
478 fputs("--------\n", composition_file);
481 /* Copy the message body to the temporary file. */
482 while ((c = getc(draft_file)) != EOF) {
487 /* Add a mhbuild MIME composition file line for the body */
488 /* charset will be discovered/guessed by buildmimeproc */
489 fprintf(composition_file, "#text/plain %s\n", body_file_name);
493 ** Now, go back to the beginning of the draft file and look for
494 ** header fields that specify attachments. Add a mhbuild MIME
495 ** composition file for each.
498 while (get_line() != EOF && *field && *field != '-') {
499 if (strncasecmp(field, attach_hdr, length) == 0 &&
500 field[length] == ':') {
501 for (p = field+length+1; *p==' ' || *p=='\t'; p++) {
505 /* forwarded message */
506 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
508 /* regular attachment */
509 make_mime_composition_file_entry(p);
513 fclose(composition_file);
516 ** We're ready to roll! Run mhbuild on the composition file.
517 ** Note that mhbuild is in the context as buildmimeproc.
519 sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
520 if (system(buf) != 0) {
522 clean_up_temporary_files();
530 clean_up_temporary_files(void)
532 unlink(body_file_name);
533 unlink(composition_file_name);
541 int c; /* current character */
542 int n; /* number of bytes in buffer */
546 ** Get a line from the input file, growing the field buffer as
547 ** needed. We do this so that we can fit an entire line in the
548 ** buffer making it easy to do a string comparison on both the
549 ** field name and the field body which might be a long path name.
551 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
552 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
553 ungetc(c, draft_file);
557 if (++n >= field_size - 1) {
558 field = (char *)mh_xrealloc(field, field_size += 256);
568 make_mime_composition_file_entry(char *file_name)
573 char content_type[BUFSIZ];
575 char *cmd = mimetypequeryproc;
577 for (np = m_defs; np; np = np->n_next) {
578 if (strcasecmp(np->n_name, mimetypequery)==0) {
583 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
585 if (!(fp = popen(cmdbuf, "r"))) {
586 clean_up_temporary_files();
587 adios(NULL, "unable to determine content type with `%s'",
590 if (fgets(content_type, sizeof content_type, fp) &&
591 (cp = strrchr(content_type, '\n'))) {
594 strcpy(content_type, "application/octet-stream");
595 admonish(NULL, "problems with `%s', using fall back type `%s'",
596 cmdbuf, content_type);
600 /* TODO: don't use access(2) because it checks for ruid, not euid */
601 if (access(file_name, R_OK) != 0) {
602 clean_up_temporary_files();
603 adios(NULL, "unable to access file `%s'", file_name);
606 fprintf(composition_file, "#%s; name=\"%s\" <>{attachment}",
608 (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1);
610 fprintf(composition_file, " %s\n", file_name);
616 ** The back-end of the message sending back-end.
617 ** Annotate original message, and call `postproc' to send message.
620 sendaux(char **vec, int vecp, char *drft, struct stat *st)
627 ** fd collects the output of postproc, and is used for the
628 ** failure notice if we need to send one in alert().
630 fd = pushsw ? tmp_fd() : NOTOK;
633 if (distfile && distout(drft, distfile, backup) == NOTOK) {
638 switch (child_id = fork()) {
640 /* oops -- fork error */
641 adios("fork", "unable to");
642 break; /* NOT REACHED */
646 ** child process -- send it
648 ** If fd is ok, then we are pushing and fd points to temp
649 ** file, so capture anything on stdout and stderr there.
652 dup2(fd, fileno(stdout));
653 dup2(fd, fileno(stderr));
656 execvp(postproc, vec);
657 fprintf(stderr, "unable to exec ");
660 break; /* NOT REACHED */
663 /* parent process -- wait for it */
664 if ((status = pidwait(child_id, NOTOK)) == OK) {
670 ** If postproc failed, and we have good fd (which
671 ** means we pushed), then mail error message
672 ** (and possibly the draft) back to the user.
678 advise(NULL, "message not delivered to anyone");
682 if (rename(backup, drft) == NOTOK) {
683 advise(drft, "unable to rename %s to",
696 ** Mail error notification (and possibly a copy of the
697 ** message) back to the user, using the mailproc
700 alert(char *file, int out)
706 switch (child_id = fork()) {
708 /* oops -- fork error */
709 advise("fork", "unable to");
712 /* child process -- send it */
713 SIGNAL(SIGHUP, SIG_IGN);
714 SIGNAL(SIGINT, SIG_IGN);
715 SIGNAL(SIGQUIT, SIG_IGN);
716 SIGNAL(SIGTERM, SIG_IGN);
718 if ((in = open(file, O_RDONLY)) == NOTOK) {
719 admonish(file, "unable to re-open");
721 lseek(out, (off_t) 0, SEEK_END);
722 strncpy(buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
723 write(out, buf, strlen(buf));
724 strncpy(buf, "\n------- Unsent Draft\n\n", sizeof(buf));
725 write(out, buf, strlen(buf));
726 cpydgst(in, out, file, "temporary file");
728 strncpy(buf, "\n------- End of Unsent Draft\n", sizeof(buf));
729 write(out, buf, strlen(buf));
730 if (rename(file, strncpy(buf, m_backup(file), sizeof(buf))) == NOTOK) {
731 admonish(buf, "unable to rename %s to", file);
735 lseek(out, (off_t) 0, SEEK_SET);
736 dup2(out, fileno(stdin));
738 /* create subject for error notification */
739 snprintf(buf, sizeof(buf), "send failed on %s",
740 forwsw ? "enclosed draft" : file);
741 execlp(mailproc, mhbasename(mailproc), getusername(),
742 "-subject", buf, NULL);
743 fprintf(stderr, "unable to exec ");
747 default: /* no waiting... */
759 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
760 if (tfile == NULL) return NOTOK;
764 advise(NULL, "temporary file %s selected", tfile);
766 if (unlink(tfile) == NOTOK) {
767 advise(tfile, "unable to remove");
776 anno(struct stat *st)
782 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
783 st->st_mtime != st2.st_mtime ||
784 st->st_dev != st2.st_dev ||
785 st->st_ino != st2.st_ino)) {
787 admonish(NULL, "$mhaltmsg mismatch");
792 if (!(folder = getenv("mhfolder")) || !*folder) {
794 admonish(NULL, "$mhfolder not set");
798 if (!(msgs = getenv("mhmessages")) || !*msgs) {
800 admonish(NULL, "$mhmessages not set");
805 advise(NULL, "annotate as `%s': %s %s", annotext,
808 snprintf(buf, sizeof buf, "anno -comp '%s' '+%s' %s",
809 annotext, folder, msgs);
810 if (system(buf) != 0) {
811 advise(NULL, "unable to annotate");
817 armed_done(int status)
819 longjmp(env, status ? status : NOTOK);