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, *folder = 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);
128 } else if (*cp == '+' || *cp == '@') {
130 adios(EX_USAGE, NULL, "only one folder at a time!");
132 folder = mh_xstrdup(expandfol(cp));
136 files[nfiles++] = cp;
143 if (!nmsgs && !nfiles) {
144 msgs[nmsgs++] = seq_cur;
148 folder = folder ? folder : draftfolder;
149 maildir = toabsdir(folder);
150 if (chdir(maildir) == NOTOK) {
151 adios(EX_OSERR, maildir, "unable to change directory to");
153 if (!(mp = folder_read(folder))) {
154 adios(EX_IOERR, NULL, "unable to read folder %s",
157 if (mp->nummsg == 0) {
158 adios(EX_DATAERR, NULL, "no messages in folder %s",
161 /* parse all the msgranges/sequences and set SELECTED */
162 for (msgnum = 0; msgnum < nmsgs; msgnum++) {
163 if (!m_convert(mp, msgs[msgnum])) {
169 for (nmsgs = 0, msgnum = mp->lowsel;
170 msgnum <= mp->hghsel; msgnum++) {
171 if (is_selected(mp, msgnum)) {
172 files[nfiles++] = mh_xstrdup(m_name(msgnum));
173 unset_exists(mp, msgnum);
177 mp->msgflags |= SEQMOD;
181 if (!(cp = getenv("SIGNATURE")) || !*cp) {
182 if ((cp = context_find("signature")) && *cp) {
183 m_putenv("SIGNATURE", cp);
187 for (n = 0; n < nfiles; n++) {
188 if (stat(files[n], &st) == NOTOK) {
189 adios(EX_IOERR, files[n], "unable to stat draft file");
193 if (!(annotext = getenv("mhannotate")) || !*annotext) {
196 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
197 altmsg = NULL; /* used by dist interface - see below */
200 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
201 vec[vecp++] = "-dist";
202 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
203 adios(EX_IOERR, altmsg, "unable to open for reading");
206 distfile = mh_xstrdup(m_mktemp2(NULL, invo_name, NULL, NULL));
207 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
208 adios(EX_IOERR, distfile, "unable to open for writing");
210 cpydata(in, out, altmsg, distfile);
217 if (!altmsg || stat(altmsg, &st) == NOTOK) {
224 for (n=3; n<OPEN_MAX; n++) {
228 for (n = 0; n < nfiles; n++) {
229 switch (sendsbr(vec, vecp, files[n], &st)) {
245 ** message sending back-end
248 sendsbr(char **vec, int vecp, char *drft, struct stat *st)
251 char *original_draft;
254 ** Save the original name of the draft file. The name of the
255 ** draft file is changed to a temporary file containing the built
256 ** MIME message if there are attachments. We need the original
257 ** name so that it can be renamed after the message is sent.
259 original_draft = drft;
262 ** Convert the draft to a MIME message.
263 ** Use the mhbuild composition file for the draft if there was
264 ** a successful conversion because that now contains the MIME
265 ** message. A nice side effect of this is that it leaves the
266 ** original draft file untouched so that it can be retrieved
267 ** and modified if desired.
269 switch (attach(drft)) {
271 drft = composition_file_name;
282 ** Sign and encrypt the message as needed.
283 ** Use the mhbuild composition file for the draft if there was
284 ** a successful conversion because that now contains the MIME
285 ** message. A nice side effect of this is that it leaves the
286 ** original draft file untouched so that it can be retrieved
287 ** and modified if desired.
289 switch (signandenc(drft)) {
291 drft = composition_file_name;
301 if ((status = sendaux(vec, vecp, drft, st)) == OK) {
302 /* move original draft to +trash folder */
303 /* temporary close stdin, for refile not to ask */
306 if (execprogl("refile", "refile", "-file",
307 original_draft, "+trash",
308 (char *)NULL) != 0) {
309 advise(NULL, "unable to trash the draft");
321 /* Get rid of temporary files that we created for attachments. */
322 if (drft == composition_file_name) {
323 clean_up_temporary_files();
330 contains_non_ascii(char *str)
334 for (cp = str; *cp; cp++) {
343 attach(char *draft_file_name)
345 char buf[MAXPATHLEN + 6];
349 int non_ascii = 0; /* msg body or hdr contains non-ASCII chars */
350 int length; /* length of attachment header field name */
353 if (!(draft_file = fopen(draft_file_name, "r"))) {
354 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
357 /* We'll grow the buffer as needed. */
358 field = mh_xcalloc(field_size = 256, sizeof(char));
361 ** Scan the draft file for an attachment header field name.
363 length = strlen(attach_hdr);
365 while (get_line() != EOF && *field != '\0' && *field != '-') {
366 if (strncasecmp(field, attach_hdr, length)==0 &&
367 field[length] == ':') {
370 if (contains_non_ascii(field)) {
376 ** Look for at least one non-blank line in the body of the
377 ** message which indicates content in the body.
378 ** Check if body contains at least one non-blank (= not empty)
379 ** and if it contains any non-ASCII chars (= need MIME).
381 while (get_line() != EOF) {
382 if (contains_non_ascii(field)) {
385 for (p = field; *p; p++) {
390 if (has_body && non_ascii) {
391 break; /* that's been already enough information */
395 if (!has_attachment && non_ascii==0) {
396 /* We don't need to convert it to MIME. */
404 /* Make names for the temporary files. */
405 strncpy(body_file_name,
406 m_mktemp(toabsdir(invo_name), NULL, NULL),
407 sizeof (body_file_name));
408 strncpy(composition_file_name,
409 m_mktemp(toabsdir(invo_name), NULL, NULL),
410 sizeof (composition_file_name));
413 body_file = fopen(body_file_name, "w");
415 composition_file = fopen(composition_file_name, "w");
417 if ((has_body && !body_file) || !composition_file) {
418 clean_up_temporary_files();
419 adios(EX_IOERR, NULL, "unable to open all of the temporary files.");
422 /* Copy non-attachment header fields to the temp composition file. */
424 while (get_line() != EOF && *field && *field != '-') {
425 if (strncasecmp(field, attach_hdr, length) != 0 ||
426 field[length] != ':') {
427 fprintf(composition_file, "%s\n", field);
430 fputs("--------\n", composition_file);
433 /* Copy the message body to the temporary file. */
434 while ((c = getc(draft_file)) != EOF) {
439 /* Add a mhbuild MIME composition file line for the body */
440 /* charset will be discovered/guessed by mhbuild */
441 fprintf(composition_file, "#text/plain %s\n", body_file_name);
445 ** Now, go back to the beginning of the draft file and look for
446 ** header fields that specify attachments. Add a mhbuild MIME
447 ** composition file for each.
450 while (get_line() != EOF && *field && *field != '-') {
451 if (strncasecmp(field, attach_hdr, length) == 0 &&
452 field[length] == ':') {
453 p = trim(field+length+1);
455 /* forwarded message */
456 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
458 /* regular attachment */
459 make_mime_composition_file_entry(p);
463 fclose(composition_file);
465 /* We're ready to roll! */
466 if (execprogl("mhbuild", "mhbuild", composition_file_name,
467 (char *)NULL) != 0) {
469 clean_up_temporary_files();
472 /* Remove the automatically created backup of mhbuild. */
473 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
474 if (unlink(buf) == -1) {
475 advise(NULL, "unable to remove original composition file.");
482 signandenc(char *draft_file_name)
489 if (!(draft_file = fopen(draft_file_name, "r"))) {
490 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
493 /* We'll grow the buffer as needed. */
494 field = mh_xcalloc(field_size = 256, sizeof(char));
496 /* Scan the draft file for an attachment header field name. */
497 while (get_line() != EOF && *field != '\0' && *field != '-') {
498 if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 &&
499 field[strlen(sign_hdr)] == ':') {
502 if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 &&
503 field[strlen(enc_hdr)] == ':') {
507 if (!dosign && !doenc) {
511 strcpy(composition_file_name, draft_file_name);
513 /* We're ready to roll! */
515 ret = execprogl("mhsign", "mhsign", "-m", "-e",
516 draft_file_name, (char *)NULL);
518 ret = execprogl("mhsign", "mhsign", "-m",
519 draft_file_name, (char *)NULL);
525 /* Remove the automatically created backup of mhsign. */
526 snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
527 if (unlink(buf) == -1) {
528 advise(NULL, "unable to remove original draft file.");
535 clean_up_temporary_files(void)
537 unlink(body_file_name);
538 unlink(composition_file_name);
546 int c; /* current character */
547 int n; /* number of bytes in buffer */
551 ** Get a line from the input file, growing the field buffer as
552 ** needed. We do this so that we can fit an entire line in the
553 ** buffer making it easy to do a string comparison on both the
554 ** field name and the field body which might be a long path name.
556 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
557 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
558 ungetc(c, draft_file);
562 if (++n >= field_size - 1) {
563 field = mh_xrealloc(field, field_size += 256);
573 make_mime_composition_file_entry(char *file_name)
578 char content_type[BUFSIZ];
580 char *cmd = mimetypequeryproc;
583 for (np = m_defs; np; np = np->n_next) {
584 if (strcasecmp(np->n_name, mimetypequery)==0) {
589 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
591 if (!(fp = popen(cmdbuf, "r"))) {
592 clean_up_temporary_files();
593 adios(EX_IOERR, NULL, "unable to determine content type with `%s'",
596 if (fgets(content_type, sizeof content_type, fp) &&
597 (cp = strrchr(content_type, '\n'))) {
600 strcpy(content_type, "application/octet-stream");
601 admonish(NULL, "problems with `%s', using fall back type `%s'",
602 cmdbuf, content_type);
606 /* TODO: don't use access(2) because it checks for ruid, not euid */
607 if (access(file_name, R_OK) != 0) {
608 clean_up_temporary_files();
609 adios(EX_IOERR, NULL, "unable to access file `%s'", file_name);
612 /* Check for broken file(1). See man page mh-profile(5). */
613 for (cp=content_type; *cp; cp++) {
616 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);
618 } else if (*cp == ';') {
625 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
626 fprintf(composition_file,
627 "#%s; name=\"%s\" <> [%s] {attachment} %s\n",
628 content_type, cp, cp, file_name);
634 ** The back-end of the message sending back-end.
635 ** Annotate original message, and call `spost' to send message.
638 sendaux(char **vec, int vecp, char *drft, struct stat *st)
645 if (distfile && distout(drft, distfile, backup) == NOTOK) {
650 switch (child_id = fork()) {
652 /* oops -- fork error */
653 advise("fork", "unable to");
657 /* child process -- send it */
659 fprintf(stderr, "unable to exec ");
662 break; /* NOT REACHED */
665 /* parent process -- wait for it */
666 status = pidwait(child_id, NOTOK);
667 if (WIFEXITED(status) && WEXITSTATUS(status) == EX_OK) {
673 advise(NULL, "%s", strexit(status));
676 if (rename(backup, drft) == NOTOK) {
677 advise(drft, "unable to rename %s to",
685 return status ? NOTOK : status;
690 anno(struct stat *st)
699 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
700 st->st_mtime != st2.st_mtime ||
701 st->st_dev != st2.st_dev ||
702 st->st_ino != st2.st_ino)) {
704 admonish(NULL, "$mhaltmsg mismatch");
709 if (!(folder = getenv("mhfolder")) || !*folder) {
711 admonish(NULL, "$mhfolder not set");
715 if (!(msgs = getenv("mhmessages")) || !*msgs) {
717 admonish(NULL, "$mhmessages not set");
722 advise(NULL, "annotate as `%s': %s %s", annotext,
725 vec[vecp++] = "anno";
726 vec[vecp++] = "-comp";
727 vec[vecp++] = annotext;
728 snprintf(buf, sizeof buf, "+%s", folder);
731 while (isspace(*msgs)) {
734 for (cp=dp=msgs; *cp; cp++) {
736 while (isspace(*cp)) {
745 if (execprog(*vec, vec) != 0) {
746 advise(NULL, "unable to annotate");
754 if (WIFSIGNALED(status)) {
755 return "spost or sendmail killed by signal";
757 if (!WIFEXITED(status)) {
758 return "message not delivered to anyone";
760 switch (WEXITSTATUS(status)) {
762 return "Temporary error, maybe the MTA has queued the message";
764 return "message not delivered to anyone";