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>
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
26 int debugsw = 0; /* global */
28 char *annotext = NULL;
29 char *distfile = NULL;
33 /* name of temp file for body content */
34 static char body_file_name[MAXPATHLEN + 1];
35 /* name of mhbuild composition temporary file */
36 static char composition_file_name[MAXPATHLEN + 1];
37 static int field_size; /* size of header field buffer */
38 static char *field; /* header field buffer */
39 static FILE *draft_file; /* draft file pointer */
40 static FILE *body_file; /* body file pointer */
41 static FILE *composition_file; /* composition file pointer */
46 static int sendsbr(char **, int, char *, struct stat *);
47 static void armed_done(int) NORETURN;
48 static void anno(struct stat *);
49 static int sendaux(char **, int, char *, struct stat *);
50 static int attach(char *);
51 static int signandenc(char *);
52 static void clean_up_temporary_files(void);
53 static int get_line(void);
54 static void make_mime_composition_file_entry(char *);
55 static char* strexit(int status);
58 static struct swit switches[] = {
74 main(int argc, char **argv)
76 int nmsgs = 0, nfiles = 0, distsw = 0, vecp = 1;
80 char *cp, *maildir = NULL;
81 char buf[BUFSIZ], **argp, **arguments;
82 char *msgs[MAXARGS], *vec[MAXARGS];
88 setlocale(LC_ALL, "");
89 invo_name = mhbasename(argv[0]);
91 /* read user profile/context */
94 arguments = getarguments(invo_name, argc, argv, 1);
97 while ((cp = *argp++)) {
99 switch (smatch(++cp, switches)) {
101 ambigsw(cp, switches);
104 adios(NULL, "-%s unknown\n", cp);
107 snprintf(buf, sizeof(buf),
108 "%s [file] [switches]",
110 print_help(buf, switches, 1);
113 print_version(invo_name);
126 files[nfiles++] = cp;
133 if (!nmsgs && !nfiles) {
134 msgs[nmsgs++] = seq_cur;
138 maildir = toabsdir(draftfolder);
139 if (chdir(maildir) == NOTOK) {
140 adios(maildir, "unable to change directory to");
142 if (!(mp = folder_read(draftfolder))) {
143 adios(NULL, "unable to read draft folder %s",
146 if (mp->nummsg == 0) {
147 adios(NULL, "no messages in draft folder %s",
150 /* parse all the msgranges/sequences and set SELECTED */
151 for (msgnum = 0; msgnum < nmsgs; msgnum++) {
152 if (!m_convert(mp, msgs[msgnum])) {
158 for (nmsgs = 0, msgnum = mp->lowsel;
159 msgnum <= mp->hghsel; msgnum++) {
160 if (is_selected(mp, msgnum)) {
161 files[nfiles++] = getcpy(m_name(msgnum));
162 unset_exists(mp, msgnum);
166 mp->msgflags |= SEQMOD;
170 if (!(cp = getenv("SIGNATURE")) || !*cp) {
171 if ((cp = context_find("signature")) && *cp) {
172 m_putenv("SIGNATURE", cp);
176 for (n = 0; n < nfiles; n++) {
177 if (stat(files[n], &st) == NOTOK) {
178 adios(files[n], "unable to stat draft file");
182 if (!(annotext = getenv("mhannotate")) || !*annotext) {
185 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
186 altmsg = NULL; /* used by dist interface - see below */
189 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
190 vec[vecp++] = "-dist";
191 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
192 adios(altmsg, "unable to open for reading");
195 distfile = getcpy(m_mktemp2(NULL, invo_name, NULL, NULL));
196 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
197 adios(distfile, "unable to open for writing");
199 cpydata(in, out, altmsg, distfile);
206 if (!altmsg || stat(altmsg, &st) == NOTOK) {
213 for (n=3; n<OPEN_MAX; n++) {
217 for (n = 0; n < nfiles; n++) {
218 switch (sendsbr(vec, vecp, files[n], &st)) {
235 ** message sending back-end
238 sendsbr(char **vec, int vecp, char *drft, struct stat *st)
241 char *original_draft;
244 ** Save the original name of the draft file. The name of the
245 ** draft file is changed to a temporary file containing the built
246 ** MIME message if there are attachments. We need the original
247 ** name so that it can be renamed after the message is sent.
249 original_draft = drft;
252 ** Convert the draft to a MIME message.
253 ** Use the mhbuild composition file for the draft if there was
254 ** a successful conversion because that now contains the MIME
255 ** message. A nice side effect of this is that it leaves the
256 ** original draft file untouched so that it can be retrieved
257 ** and modified if desired.
259 switch (attach(drft)) {
261 drft = composition_file_name;
272 ** Sign and encrypt the message as needed.
273 ** Use the mhbuild composition file for the draft if there was
274 ** a successful conversion because that now contains the MIME
275 ** message. A nice side effect of this is that it leaves the
276 ** original draft file untouched so that it can be retrieved
277 ** and modified if desired.
279 switch (signandenc(drft)) {
281 drft = composition_file_name;
292 switch (setjmp(env)) {
294 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
296 /* move original draft to +trash folder */
297 /* temporary close stdin, for refile not to ask */
300 if (execprogl("refile", "refile", "-file",
301 original_draft, "+trash",
302 (char *)NULL) != 0) {
303 advise(NULL, "unable to trash the draft");
320 /* Get rid of temporary files that we created for attachments. */
321 if (drft == composition_file_name) {
322 clean_up_temporary_files();
329 attach(char *draft_file_name)
331 char buf[MAXPATHLEN + 6];
335 int non_ascii; /* msg body contains non-ASCII chars */
336 int length; /* length of attachment header field name */
339 if (!(draft_file = fopen(draft_file_name, "r"))) {
340 adios(NULL, "can't open draft file `%s'.", draft_file_name);
343 /* We'll grow the buffer as needed. */
344 field = (char *)mh_xmalloc(field_size = 256);
347 ** Scan the draft file for an attachment header field name.
349 length = strlen(attach_hdr);
351 while (get_line() != EOF && *field != '\0' && *field != '-') {
352 if (strncasecmp(field, attach_hdr, length)==0 &&
353 field[length] == ':') {
359 ** Look for at least one non-blank line in the body of the
360 ** message which indicates content in the body.
361 ** Check if body contains at least one non-blank (= not empty)
362 ** and if it contains any non-ASCII chars (= need MIME).
366 while (get_line() != EOF) {
367 for (p = field; *p != '\0'; p++) {
368 if (*p != ' ' && *p != '\t') {
371 if (*p > 127 || *p < 0) {
375 if (has_body && non_ascii) {
376 break; /* that's been already enough information */
380 if (!has_attachment && non_ascii==0) {
381 /* We don't need to convert it to MIME. */
389 /* Make names for the temporary files. */
390 strncpy(body_file_name,
391 m_mktemp(toabsdir(invo_name), NULL, NULL),
392 sizeof (body_file_name));
393 strncpy(composition_file_name,
394 m_mktemp(toabsdir(invo_name), NULL, NULL),
395 sizeof (composition_file_name));
398 body_file = fopen(body_file_name, "w");
400 composition_file = fopen(composition_file_name, "w");
402 if ((has_body && !body_file) || !composition_file) {
403 clean_up_temporary_files();
404 adios(NULL, "unable to open all of the temporary files.");
407 /* Copy non-attachment header fields to the temp composition file. */
409 while (get_line() != EOF && *field && *field != '-') {
410 if (strncasecmp(field, attach_hdr, length) != 0 ||
411 field[length] != ':') {
412 fprintf(composition_file, "%s\n", field);
415 fputs("--------\n", composition_file);
418 /* Copy the message body to the temporary file. */
419 while ((c = getc(draft_file)) != EOF) {
424 /* Add a mhbuild MIME composition file line for the body */
425 /* charset will be discovered/guessed by mhbuild */
426 fprintf(composition_file, "#text/plain %s\n", body_file_name);
430 ** Now, go back to the beginning of the draft file and look for
431 ** header fields that specify attachments. Add a mhbuild MIME
432 ** composition file for each.
435 while (get_line() != EOF && *field && *field != '-') {
436 if (strncasecmp(field, attach_hdr, length) == 0 &&
437 field[length] == ':') {
438 p = trim(field+length+1);
440 /* forwarded message */
441 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
443 /* regular attachment */
444 make_mime_composition_file_entry(p);
448 fclose(composition_file);
450 /* We're ready to roll! */
451 if (execprogl("mhbuild", "mhbuild", composition_file_name,
452 (char *)NULL) != 0) {
454 clean_up_temporary_files();
457 /* Remove the automatically created backup of mhbuild. */
458 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
459 if (unlink(buf) == -1) {
460 advise(NULL, "unable to remove original composition file.");
467 signandenc(char *draft_file_name)
474 if (!(draft_file = fopen(draft_file_name, "r"))) {
475 adios(NULL, "can't open draft file `%s'.", draft_file_name);
478 /* We'll grow the buffer as needed. */
479 field = (char *)mh_xmalloc(field_size = 256);
481 /* Scan the draft file for an attachment header field name. */
482 while (get_line() != EOF && *field != '\0' && *field != '-') {
483 if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 &&
484 field[strlen(sign_hdr)] == ':') {
487 if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 &&
488 field[strlen(enc_hdr)] == ':') {
492 if (!dosign && !doenc) {
496 strcpy(composition_file_name, draft_file_name);
498 /* We're ready to roll! */
500 ret = execprogl("mhsign", "mhsign", "-m", "-e",
501 draft_file_name, (char *)NULL);
503 ret = execprogl("mhsign", "mhsign", "-m",
504 draft_file_name, (char *)NULL);
510 /* Remove the automatically created backup of mhsign. */
511 snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
512 if (unlink(buf) == -1) {
513 advise(NULL, "unable to remove original draft file.");
520 clean_up_temporary_files(void)
522 unlink(body_file_name);
523 unlink(composition_file_name);
531 int c; /* current character */
532 int n; /* number of bytes in buffer */
536 ** Get a line from the input file, growing the field buffer as
537 ** needed. We do this so that we can fit an entire line in the
538 ** buffer making it easy to do a string comparison on both the
539 ** field name and the field body which might be a long path name.
541 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
542 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
543 ungetc(c, draft_file);
547 if (++n >= field_size - 1) {
548 field = (char *)mh_xrealloc(field, field_size += 256);
558 make_mime_composition_file_entry(char *file_name)
563 char content_type[BUFSIZ];
565 char *cmd = mimetypequeryproc;
568 for (np = m_defs; np; np = np->n_next) {
569 if (strcasecmp(np->n_name, mimetypequery)==0) {
574 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
576 if (!(fp = popen(cmdbuf, "r"))) {
577 clean_up_temporary_files();
578 adios(NULL, "unable to determine content type with `%s'",
581 if (fgets(content_type, sizeof content_type, fp) &&
582 (cp = strrchr(content_type, '\n'))) {
585 strcpy(content_type, "application/octet-stream");
586 admonish(NULL, "problems with `%s', using fall back type `%s'",
587 cmdbuf, content_type);
591 /* TODO: don't use access(2) because it checks for ruid, not euid */
592 if (access(file_name, R_OK) != 0) {
593 clean_up_temporary_files();
594 adios(NULL, "unable to access file `%s'", file_name);
597 /* Check for broken file(1). See man page mh-profile(5). */
598 for (cp=content_type; *cp; cp++) {
601 adios(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);
603 } else if (*cp == ';') {
610 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
611 fprintf(composition_file,
612 "#%s; name=\"%s\" <> [%s] {attachment} %s\n",
613 content_type, cp, cp, file_name);
619 ** The back-end of the message sending back-end.
620 ** Annotate original message, and call `spost' to send message.
623 sendaux(char **vec, int vecp, char *drft, struct stat *st)
630 if (distfile && distout(drft, distfile, backup) == NOTOK) {
635 switch (child_id = fork()) {
637 /* oops -- fork error */
638 adios("fork", "unable to");
639 break; /* NOT REACHED */
642 /* child process -- send it */
644 fprintf(stderr, "unable to exec ");
647 break; /* NOT REACHED */
650 /* parent process -- wait for it */
651 status = pidwait(child_id, NOTOK);
652 if (WIFEXITED(status) && WEXITSTATUS(status) == EX_OK) {
658 advise(NULL, "%s", strexit(status));
661 if (rename(backup, drft) == NOTOK) {
662 advise(drft, "unable to rename %s to",
675 anno(struct stat *st)
684 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
685 st->st_mtime != st2.st_mtime ||
686 st->st_dev != st2.st_dev ||
687 st->st_ino != st2.st_ino)) {
689 admonish(NULL, "$mhaltmsg mismatch");
694 if (!(folder = getenv("mhfolder")) || !*folder) {
696 admonish(NULL, "$mhfolder not set");
700 if (!(msgs = getenv("mhmessages")) || !*msgs) {
702 admonish(NULL, "$mhmessages not set");
707 advise(NULL, "annotate as `%s': %s %s", annotext,
710 vec[vecp++] = "anno";
711 vec[vecp++] = "-comp";
712 vec[vecp++] = annotext;
713 snprintf(buf, sizeof buf, "+%s", folder);
716 while (isspace(*msgs)) {
719 for (cp=dp=msgs; *cp; cp++) {
721 while (isspace(*cp)) {
730 if (execprog(*vec, vec) != 0) {
731 advise(NULL, "unable to annotate");
739 if (WIFSIGNALED(status)) {
740 return "spost or sendmail killed by signal";
742 if (!WIFEXITED(status)) {
743 return "message not delivered to anyone";
745 switch (WEXITSTATUS(status)) {
747 return "Temporary error, maybe the MTA has queued the message";
749 return "message not delivered to anyone";
755 armed_done(int status)
757 longjmp(env, status ? status : NOTOK);