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>
24 #ifdef HAVE_SYS_TIME_H
25 # include <sys/time.h>
29 #ifdef HAVE_SYS_PARAM_H
30 # include <sys/param.h>
33 int debugsw = 0; /* global */
35 char *annotext = NULL;
36 char *distfile = NULL;
38 /* name of temp file for body content */
39 static char body_file_name[MAXPATHLEN + 1];
40 /* name of mhbuild composition temporary file */
41 static char composition_file_name[MAXPATHLEN + 1];
42 static int field_size; /* size of header field buffer */
43 static char *field; /* header field buffer */
44 static FILE *draft_file; /* draft file pointer */
45 static FILE *body_file; /* body file pointer */
46 static FILE *composition_file; /* composition file pointer */
51 static int sendsbr(char **, int, char *, struct stat *);
52 static void anno(struct stat *);
53 static int sendaux(char **, int, char *, struct stat *);
54 static int attach(char *);
55 static int signandenc(char *);
56 static void clean_up_temporary_files(void);
57 static int get_line(void);
58 static void make_mime_composition_file_entry(char *);
59 static char* strexit(int status);
62 static struct swit switches[] = {
78 main(int argc, char **argv)
80 int nmsgs = 0, nfiles = 0, distsw = 0, vecp = 1;
84 char *cp, *maildir = NULL;
85 char buf[BUFSIZ], **argp, **arguments;
86 char *msgs[MAXARGS], *vec[MAXARGS];
92 setlocale(LC_ALL, "");
93 invo_name = mhbasename(argv[0]);
95 /* read user profile/context */
98 arguments = getarguments(invo_name, argc, argv, 1);
101 while ((cp = *argp++)) {
103 switch (smatch(++cp, switches)) {
105 ambigsw(cp, switches);
108 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
111 snprintf(buf, sizeof(buf),
112 "%s [file] [switches]",
114 print_help(buf, switches, 1);
115 exit(argc == 2 ? EX_OK : EX_USAGE);
117 print_version(invo_name);
118 exit(argc == 2 ? EX_OK : EX_USAGE);
130 files[nfiles++] = cp;
137 if (!nmsgs && !nfiles) {
138 msgs[nmsgs++] = seq_cur;
142 maildir = toabsdir(draftfolder);
143 if (chdir(maildir) == NOTOK) {
144 adios(EX_OSERR, maildir, "unable to change directory to");
146 if (!(mp = folder_read(draftfolder))) {
147 adios(EX_IOERR, NULL, "unable to read draft folder %s",
150 if (mp->nummsg == 0) {
151 adios(EX_DATAERR, NULL, "no messages in draft folder %s",
154 /* parse all the msgranges/sequences and set SELECTED */
155 for (msgnum = 0; msgnum < nmsgs; msgnum++) {
156 if (!m_convert(mp, msgs[msgnum])) {
162 for (nmsgs = 0, msgnum = mp->lowsel;
163 msgnum <= mp->hghsel; msgnum++) {
164 if (is_selected(mp, msgnum)) {
165 files[nfiles++] = getcpy(m_name(msgnum));
166 unset_exists(mp, msgnum);
170 mp->msgflags |= SEQMOD;
174 if (!(cp = getenv("SIGNATURE")) || !*cp) {
175 if ((cp = context_find("signature")) && *cp) {
176 m_putenv("SIGNATURE", cp);
180 for (n = 0; n < nfiles; n++) {
181 if (stat(files[n], &st) == NOTOK) {
182 adios(EX_IOERR, files[n], "unable to stat draft file");
186 if (!(annotext = getenv("mhannotate")) || !*annotext) {
189 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
190 altmsg = NULL; /* used by dist interface - see below */
193 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
194 vec[vecp++] = "-dist";
195 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
196 adios(EX_IOERR, altmsg, "unable to open for reading");
199 distfile = getcpy(m_mktemp2(NULL, invo_name, NULL, NULL));
200 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
201 adios(EX_IOERR, distfile, "unable to open for writing");
203 cpydata(in, out, altmsg, distfile);
210 if (!altmsg || stat(altmsg, &st) == NOTOK) {
217 for (n=3; n<OPEN_MAX; n++) {
221 for (n = 0; n < nfiles; n++) {
222 switch (sendsbr(vec, vecp, files[n], &st)) {
238 ** message sending back-end
241 sendsbr(char **vec, int vecp, char *drft, struct stat *st)
244 char *original_draft;
247 ** Save the original name of the draft file. The name of the
248 ** draft file is changed to a temporary file containing the built
249 ** MIME message if there are attachments. We need the original
250 ** name so that it can be renamed after the message is sent.
252 original_draft = drft;
255 ** Convert the draft to a MIME message.
256 ** Use the mhbuild composition file for the draft if there was
257 ** a successful conversion because that now contains the MIME
258 ** message. A nice side effect of this is that it leaves the
259 ** original draft file untouched so that it can be retrieved
260 ** and modified if desired.
262 switch (attach(drft)) {
264 drft = composition_file_name;
275 ** Sign and encrypt the message as needed.
276 ** Use the mhbuild composition file for the draft if there was
277 ** a successful conversion because that now contains the MIME
278 ** message. A nice side effect of this is that it leaves the
279 ** original draft file untouched so that it can be retrieved
280 ** and modified if desired.
282 switch (signandenc(drft)) {
284 drft = composition_file_name;
294 if ((status = sendaux(vec, vecp, drft, st)) == OK) {
295 /* move original draft to +trash folder */
296 /* temporary close stdin, for refile not to ask */
299 if (execprogl("refile", "refile", "-file",
300 original_draft, "+trash",
301 (char *)NULL) != 0) {
302 advise(NULL, "unable to trash the draft");
314 /* Get rid of temporary files that we created for attachments. */
315 if (drft == composition_file_name) {
316 clean_up_temporary_files();
323 contains_non_ascii(char *str)
327 for (cp = str; *cp; cp++) {
328 if (*cp > 127 || *cp < 0) {
336 attach(char *draft_file_name)
338 char buf[MAXPATHLEN + 6];
342 int non_ascii = 0; /* msg body or hdr contains non-ASCII chars */
343 int length; /* length of attachment header field name */
346 if (!(draft_file = fopen(draft_file_name, "r"))) {
347 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
350 /* We'll grow the buffer as needed. */
351 field = (char *)mh_xmalloc(field_size = 256);
354 ** Scan the draft file for an attachment header field name.
356 length = strlen(attach_hdr);
358 while (get_line() != EOF && *field != '\0' && *field != '-') {
359 if (strncasecmp(field, attach_hdr, length)==0 &&
360 field[length] == ':') {
363 if (contains_non_ascii(field)) {
369 ** Look for at least one non-blank line in the body of the
370 ** message which indicates content in the body.
371 ** Check if body contains at least one non-blank (= not empty)
372 ** and if it contains any non-ASCII chars (= need MIME).
374 while (get_line() != EOF) {
375 if (contains_non_ascii(field)) {
378 for (p = field; *p; p++) {
383 if (has_body && non_ascii) {
384 break; /* that's been already enough information */
388 if (!has_attachment && non_ascii==0) {
389 /* We don't need to convert it to MIME. */
397 /* Make names for the temporary files. */
398 strncpy(body_file_name,
399 m_mktemp(toabsdir(invo_name), NULL, NULL),
400 sizeof (body_file_name));
401 strncpy(composition_file_name,
402 m_mktemp(toabsdir(invo_name), NULL, NULL),
403 sizeof (composition_file_name));
406 body_file = fopen(body_file_name, "w");
408 composition_file = fopen(composition_file_name, "w");
410 if ((has_body && !body_file) || !composition_file) {
411 clean_up_temporary_files();
412 adios(EX_IOERR, NULL, "unable to open all of the temporary files.");
415 /* Copy non-attachment header fields to the temp composition file. */
417 while (get_line() != EOF && *field && *field != '-') {
418 if (strncasecmp(field, attach_hdr, length) != 0 ||
419 field[length] != ':') {
420 fprintf(composition_file, "%s\n", field);
423 fputs("--------\n", composition_file);
426 /* Copy the message body to the temporary file. */
427 while ((c = getc(draft_file)) != EOF) {
432 /* Add a mhbuild MIME composition file line for the body */
433 /* charset will be discovered/guessed by mhbuild */
434 fprintf(composition_file, "#text/plain %s\n", body_file_name);
438 ** Now, go back to the beginning of the draft file and look for
439 ** header fields that specify attachments. Add a mhbuild MIME
440 ** composition file for each.
443 while (get_line() != EOF && *field && *field != '-') {
444 if (strncasecmp(field, attach_hdr, length) == 0 &&
445 field[length] == ':') {
446 p = trim(field+length+1);
448 /* forwarded message */
449 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
451 /* regular attachment */
452 make_mime_composition_file_entry(p);
456 fclose(composition_file);
458 /* We're ready to roll! */
459 if (execprogl("mhbuild", "mhbuild", composition_file_name,
460 (char *)NULL) != 0) {
462 clean_up_temporary_files();
465 /* Remove the automatically created backup of mhbuild. */
466 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
467 if (unlink(buf) == -1) {
468 advise(NULL, "unable to remove original composition file.");
475 signandenc(char *draft_file_name)
482 if (!(draft_file = fopen(draft_file_name, "r"))) {
483 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
486 /* We'll grow the buffer as needed. */
487 field = (char *)mh_xmalloc(field_size = 256);
489 /* Scan the draft file for an attachment header field name. */
490 while (get_line() != EOF && *field != '\0' && *field != '-') {
491 if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 &&
492 field[strlen(sign_hdr)] == ':') {
495 if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 &&
496 field[strlen(enc_hdr)] == ':') {
500 if (!dosign && !doenc) {
504 strcpy(composition_file_name, draft_file_name);
506 /* We're ready to roll! */
508 ret = execprogl("mhsign", "mhsign", "-m", "-e",
509 draft_file_name, (char *)NULL);
511 ret = execprogl("mhsign", "mhsign", "-m",
512 draft_file_name, (char *)NULL);
518 /* Remove the automatically created backup of mhsign. */
519 snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
520 if (unlink(buf) == -1) {
521 advise(NULL, "unable to remove original draft file.");
528 clean_up_temporary_files(void)
530 unlink(body_file_name);
531 unlink(composition_file_name);
539 int c; /* current character */
540 int n; /* number of bytes in buffer */
544 ** Get a line from the input file, growing the field buffer as
545 ** needed. We do this so that we can fit an entire line in the
546 ** buffer making it easy to do a string comparison on both the
547 ** field name and the field body which might be a long path name.
549 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
550 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
551 ungetc(c, draft_file);
555 if (++n >= field_size - 1) {
556 field = (char *)mh_xrealloc(field, field_size += 256);
566 make_mime_composition_file_entry(char *file_name)
571 char content_type[BUFSIZ];
573 char *cmd = mimetypequeryproc;
576 for (np = m_defs; np; np = np->n_next) {
577 if (strcasecmp(np->n_name, mimetypequery)==0) {
582 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
584 if (!(fp = popen(cmdbuf, "r"))) {
585 clean_up_temporary_files();
586 adios(EX_IOERR, NULL, "unable to determine content type with `%s'",
589 if (fgets(content_type, sizeof content_type, fp) &&
590 (cp = strrchr(content_type, '\n'))) {
593 strcpy(content_type, "application/octet-stream");
594 admonish(NULL, "problems with `%s', using fall back type `%s'",
595 cmdbuf, content_type);
599 /* TODO: don't use access(2) because it checks for ruid, not euid */
600 if (access(file_name, R_OK) != 0) {
601 clean_up_temporary_files();
602 adios(EX_IOERR, NULL, "unable to access file `%s'", file_name);
605 /* Check for broken file(1). See man page mh-profile(5). */
606 for (cp=content_type; *cp; cp++) {
609 adios(EX_SOFTWARE, NULL, "Sorry, your Mime-Type-Query command (%s) is broken.\n\tThe output misses a semicolon before the whitespace.\n\tOutput was: %s", cmd, content_type);
611 } else if (*cp == ';') {
618 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
619 fprintf(composition_file,
620 "#%s; name=\"%s\" <> [%s] {attachment} %s\n",
621 content_type, cp, cp, file_name);
627 ** The back-end of the message sending back-end.
628 ** Annotate original message, and call `spost' to send message.
631 sendaux(char **vec, int vecp, char *drft, struct stat *st)
638 if (distfile && distout(drft, distfile, backup) == NOTOK) {
643 switch (child_id = fork()) {
645 /* oops -- fork error */
646 advise("fork", "unable to");
650 /* child process -- send it */
652 fprintf(stderr, "unable to exec ");
655 break; /* NOT REACHED */
658 /* parent process -- wait for it */
659 status = pidwait(child_id, NOTOK);
660 if (WIFEXITED(status) && WEXITSTATUS(status) == EX_OK) {
666 advise(NULL, "%s", strexit(status));
669 if (rename(backup, drft) == NOTOK) {
670 advise(drft, "unable to rename %s to",
678 return status ? NOTOK : status;
683 anno(struct stat *st)
692 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
693 st->st_mtime != st2.st_mtime ||
694 st->st_dev != st2.st_dev ||
695 st->st_ino != st2.st_ino)) {
697 admonish(NULL, "$mhaltmsg mismatch");
702 if (!(folder = getenv("mhfolder")) || !*folder) {
704 admonish(NULL, "$mhfolder not set");
708 if (!(msgs = getenv("mhmessages")) || !*msgs) {
710 admonish(NULL, "$mhmessages not set");
715 advise(NULL, "annotate as `%s': %s %s", annotext,
718 vec[vecp++] = "anno";
719 vec[vecp++] = "-comp";
720 vec[vecp++] = annotext;
721 snprintf(buf, sizeof buf, "+%s", folder);
724 while (isspace(*msgs)) {
727 for (cp=dp=msgs; *cp; cp++) {
729 while (isspace(*cp)) {
738 if (execprog(*vec, vec) != 0) {
739 advise(NULL, "unable to annotate");
747 if (WIFSIGNALED(status)) {
748 return "spost or sendmail killed by signal";
750 if (!WIFEXITED(status)) {
751 return "message not delivered to anyone";
753 switch (WEXITSTATUS(status)) {
755 return "Temporary error, maybe the MTA has queued the message";
757 return "message not delivered to anyone";