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 attach(char *draft_file_name)
325 char buf[MAXPATHLEN + 6];
329 int non_ascii; /* msg body contains non-ASCII chars */
330 int length; /* length of attachment header field name */
333 if (!(draft_file = fopen(draft_file_name, "r"))) {
334 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
337 /* We'll grow the buffer as needed. */
338 field = (char *)mh_xmalloc(field_size = 256);
341 ** Scan the draft file for an attachment header field name.
343 length = strlen(attach_hdr);
345 while (get_line() != EOF && *field != '\0' && *field != '-') {
346 if (strncasecmp(field, attach_hdr, length)==0 &&
347 field[length] == ':') {
353 ** Look for at least one non-blank line in the body of the
354 ** message which indicates content in the body.
355 ** Check if body contains at least one non-blank (= not empty)
356 ** and if it contains any non-ASCII chars (= need MIME).
360 while (get_line() != EOF) {
361 for (p = field; *p != '\0'; p++) {
362 if (*p != ' ' && *p != '\t') {
365 if (*p > 127 || *p < 0) {
369 if (has_body && non_ascii) {
370 break; /* that's been already enough information */
374 if (!has_attachment && non_ascii==0) {
375 /* We don't need to convert it to MIME. */
383 /* Make names for the temporary files. */
384 strncpy(body_file_name,
385 m_mktemp(toabsdir(invo_name), NULL, NULL),
386 sizeof (body_file_name));
387 strncpy(composition_file_name,
388 m_mktemp(toabsdir(invo_name), NULL, NULL),
389 sizeof (composition_file_name));
392 body_file = fopen(body_file_name, "w");
394 composition_file = fopen(composition_file_name, "w");
396 if ((has_body && !body_file) || !composition_file) {
397 clean_up_temporary_files();
398 adios(EX_IOERR, NULL, "unable to open all of the temporary files.");
401 /* Copy non-attachment header fields to the temp composition file. */
403 while (get_line() != EOF && *field && *field != '-') {
404 if (strncasecmp(field, attach_hdr, length) != 0 ||
405 field[length] != ':') {
406 fprintf(composition_file, "%s\n", field);
409 fputs("--------\n", composition_file);
412 /* Copy the message body to the temporary file. */
413 while ((c = getc(draft_file)) != EOF) {
418 /* Add a mhbuild MIME composition file line for the body */
419 /* charset will be discovered/guessed by mhbuild */
420 fprintf(composition_file, "#text/plain %s\n", body_file_name);
424 ** Now, go back to the beginning of the draft file and look for
425 ** header fields that specify attachments. Add a mhbuild MIME
426 ** composition file for each.
429 while (get_line() != EOF && *field && *field != '-') {
430 if (strncasecmp(field, attach_hdr, length) == 0 &&
431 field[length] == ':') {
432 p = trim(field+length+1);
434 /* forwarded message */
435 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
437 /* regular attachment */
438 make_mime_composition_file_entry(p);
442 fclose(composition_file);
444 /* We're ready to roll! */
445 if (execprogl("mhbuild", "mhbuild", composition_file_name,
446 (char *)NULL) != 0) {
448 clean_up_temporary_files();
451 /* Remove the automatically created backup of mhbuild. */
452 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
453 if (unlink(buf) == -1) {
454 advise(NULL, "unable to remove original composition file.");
461 signandenc(char *draft_file_name)
468 if (!(draft_file = fopen(draft_file_name, "r"))) {
469 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
472 /* We'll grow the buffer as needed. */
473 field = (char *)mh_xmalloc(field_size = 256);
475 /* Scan the draft file for an attachment header field name. */
476 while (get_line() != EOF && *field != '\0' && *field != '-') {
477 if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 &&
478 field[strlen(sign_hdr)] == ':') {
481 if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 &&
482 field[strlen(enc_hdr)] == ':') {
486 if (!dosign && !doenc) {
490 strcpy(composition_file_name, draft_file_name);
492 /* We're ready to roll! */
494 ret = execprogl("mhsign", "mhsign", "-m", "-e",
495 draft_file_name, (char *)NULL);
497 ret = execprogl("mhsign", "mhsign", "-m",
498 draft_file_name, (char *)NULL);
504 /* Remove the automatically created backup of mhsign. */
505 snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
506 if (unlink(buf) == -1) {
507 advise(NULL, "unable to remove original draft file.");
514 clean_up_temporary_files(void)
516 unlink(body_file_name);
517 unlink(composition_file_name);
525 int c; /* current character */
526 int n; /* number of bytes in buffer */
530 ** Get a line from the input file, growing the field buffer as
531 ** needed. We do this so that we can fit an entire line in the
532 ** buffer making it easy to do a string comparison on both the
533 ** field name and the field body which might be a long path name.
535 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
536 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
537 ungetc(c, draft_file);
541 if (++n >= field_size - 1) {
542 field = (char *)mh_xrealloc(field, field_size += 256);
552 make_mime_composition_file_entry(char *file_name)
557 char content_type[BUFSIZ];
559 char *cmd = mimetypequeryproc;
562 for (np = m_defs; np; np = np->n_next) {
563 if (strcasecmp(np->n_name, mimetypequery)==0) {
568 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
570 if (!(fp = popen(cmdbuf, "r"))) {
571 clean_up_temporary_files();
572 adios(EX_IOERR, NULL, "unable to determine content type with `%s'",
575 if (fgets(content_type, sizeof content_type, fp) &&
576 (cp = strrchr(content_type, '\n'))) {
579 strcpy(content_type, "application/octet-stream");
580 admonish(NULL, "problems with `%s', using fall back type `%s'",
581 cmdbuf, content_type);
585 /* TODO: don't use access(2) because it checks for ruid, not euid */
586 if (access(file_name, R_OK) != 0) {
587 clean_up_temporary_files();
588 adios(EX_IOERR, NULL, "unable to access file `%s'", file_name);
591 /* Check for broken file(1). See man page mh-profile(5). */
592 for (cp=content_type; *cp; cp++) {
595 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);
597 } else if (*cp == ';') {
604 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
605 fprintf(composition_file,
606 "#%s; name=\"%s\" <> [%s] {attachment} %s\n",
607 content_type, cp, cp, file_name);
613 ** The back-end of the message sending back-end.
614 ** Annotate original message, and call `spost' to send message.
617 sendaux(char **vec, int vecp, char *drft, struct stat *st)
624 if (distfile && distout(drft, distfile, backup) == NOTOK) {
629 switch (child_id = fork()) {
631 /* oops -- fork error */
632 advise("fork", "unable to");
636 /* child process -- send it */
638 fprintf(stderr, "unable to exec ");
641 break; /* NOT REACHED */
644 /* parent process -- wait for it */
645 status = pidwait(child_id, NOTOK);
646 if (WIFEXITED(status) && WEXITSTATUS(status) == EX_OK) {
652 advise(NULL, "%s", strexit(status));
655 if (rename(backup, drft) == NOTOK) {
656 advise(drft, "unable to rename %s to",
664 return status ? NOTOK : status;
669 anno(struct stat *st)
678 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
679 st->st_mtime != st2.st_mtime ||
680 st->st_dev != st2.st_dev ||
681 st->st_ino != st2.st_ino)) {
683 admonish(NULL, "$mhaltmsg mismatch");
688 if (!(folder = getenv("mhfolder")) || !*folder) {
690 admonish(NULL, "$mhfolder not set");
694 if (!(msgs = getenv("mhmessages")) || !*msgs) {
696 admonish(NULL, "$mhmessages not set");
701 advise(NULL, "annotate as `%s': %s %s", annotext,
704 vec[vecp++] = "anno";
705 vec[vecp++] = "-comp";
706 vec[vecp++] = annotext;
707 snprintf(buf, sizeof buf, "+%s", folder);
710 while (isspace(*msgs)) {
713 for (cp=dp=msgs; *cp; cp++) {
715 while (isspace(*cp)) {
724 if (execprog(*vec, vec) != 0) {
725 advise(NULL, "unable to annotate");
733 if (WIFSIGNALED(status)) {
734 return "spost or sendmail killed by signal";
736 if (!WIFEXITED(status)) {
737 return "message not delivered to anyone";
739 switch (WEXITSTATUS(status)) {
741 return "Temporary error, maybe the MTA has queued the message";
743 return "message not delivered to anyone";