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>
20 #ifdef HAVE_SYS_TIME_H
21 # include <sys/time.h>
25 int debugsw = 0; /* global */
27 char *annotext = NULL;
28 char *distfile = NULL;
30 /* name of temp file for body content */
31 static char body_file_name[MAXPATHLEN + 1];
32 /* name of mhbuild composition temporary file */
33 static char composition_file_name[MAXPATHLEN + 1];
34 static int field_size; /* size of header field buffer */
35 static char *field; /* header field buffer */
36 static FILE *draft_file; /* draft file pointer */
37 static FILE *body_file; /* body file pointer */
38 static FILE *composition_file; /* composition file pointer */
43 static int sendsbr(char **, int, char *, struct stat *);
44 static void anno(struct stat *);
45 static int sendaux(char **, int, char *, struct stat *);
46 static int attach(char *);
47 static int signandenc(char *);
48 static void clean_up_temporary_files(void);
49 static int get_line(void);
50 static void make_mime_composition_file_entry(char *);
51 static char* strexit(int status);
54 static struct swit switches[] = {
70 main(int argc, char **argv)
72 int nmsgs = 0, nfiles = 0, distsw = 0, vecp = 1;
76 char *cp, *maildir = NULL;
77 char buf[BUFSIZ], **argp, **arguments;
78 char *msgs[MAXARGS], *vec[MAXARGS];
84 setlocale(LC_ALL, "");
85 invo_name = mhbasename(argv[0]);
87 /* read user profile/context */
90 arguments = getarguments(invo_name, argc, argv, 1);
93 while ((cp = *argp++)) {
95 switch (smatch(++cp, switches)) {
97 ambigsw(cp, switches);
100 adios(NULL, "-%s unknown\n", cp);
103 snprintf(buf, sizeof(buf),
104 "%s [file] [switches]",
106 print_help(buf, switches, 1);
109 print_version(invo_name);
122 files[nfiles++] = cp;
129 if (!nmsgs && !nfiles) {
130 msgs[nmsgs++] = seq_cur;
134 maildir = toabsdir(draftfolder);
135 if (chdir(maildir) == NOTOK) {
136 adios(maildir, "unable to change directory to");
138 if (!(mp = folder_read(draftfolder))) {
139 adios(NULL, "unable to read draft folder %s",
142 if (mp->nummsg == 0) {
143 adios(NULL, "no messages in draft folder %s",
146 /* parse all the msgranges/sequences and set SELECTED */
147 for (msgnum = 0; msgnum < nmsgs; msgnum++) {
148 if (!m_convert(mp, msgs[msgnum])) {
154 for (nmsgs = 0, msgnum = mp->lowsel;
155 msgnum <= mp->hghsel; msgnum++) {
156 if (is_selected(mp, msgnum)) {
157 files[nfiles++] = getcpy(m_name(msgnum));
158 unset_exists(mp, msgnum);
162 mp->msgflags |= SEQMOD;
166 if (!(cp = getenv("SIGNATURE")) || !*cp) {
167 if ((cp = context_find("signature")) && *cp) {
168 m_putenv("SIGNATURE", cp);
172 for (n = 0; n < nfiles; n++) {
173 if (stat(files[n], &st) == NOTOK) {
174 adios(files[n], "unable to stat draft file");
178 if (!(annotext = getenv("mhannotate")) || !*annotext) {
181 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
182 altmsg = NULL; /* used by dist interface - see below */
185 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
186 vec[vecp++] = "-dist";
187 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
188 adios(altmsg, "unable to open for reading");
191 distfile = getcpy(m_mktemp2(NULL, invo_name, NULL, NULL));
192 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
193 adios(distfile, "unable to open for writing");
195 cpydata(in, out, altmsg, distfile);
202 if (!altmsg || stat(altmsg, &st) == NOTOK) {
209 for (n=3; n<OPEN_MAX; n++) {
213 for (n = 0; n < nfiles; n++) {
214 switch (sendsbr(vec, vecp, files[n], &st)) {
230 ** message sending back-end
233 sendsbr(char **vec, int vecp, char *drft, struct stat *st)
236 char *original_draft;
239 ** Save the original name of the draft file. The name of the
240 ** draft file is changed to a temporary file containing the built
241 ** MIME message if there are attachments. We need the original
242 ** name so that it can be renamed after the message is sent.
244 original_draft = drft;
247 ** Convert the draft to a MIME message.
248 ** Use the mhbuild composition file for the draft if there was
249 ** a successful conversion because that now contains the MIME
250 ** message. A nice side effect of this is that it leaves the
251 ** original draft file untouched so that it can be retrieved
252 ** and modified if desired.
254 switch (attach(drft)) {
256 drft = composition_file_name;
267 ** Sign and encrypt the message as needed.
268 ** Use the mhbuild composition file for the draft if there was
269 ** a successful conversion because that now contains the MIME
270 ** message. A nice side effect of this is that it leaves the
271 ** original draft file untouched so that it can be retrieved
272 ** and modified if desired.
274 switch (signandenc(drft)) {
276 drft = composition_file_name;
286 switch (sendaux(vec, vecp, drft, st)) {
288 /* move original draft to +trash folder */
289 /* temporary close stdin, for refile not to ask */
292 if (execprogl("refile", "refile", "-file",
293 original_draft, "+trash",
294 (char *)NULL) != 0) {
295 advise(NULL, "unable to trash the draft");
310 /* Get rid of temporary files that we created for attachments. */
311 if (drft == composition_file_name) {
312 clean_up_temporary_files();
319 attach(char *draft_file_name)
321 char buf[MAXPATHLEN + 6];
325 int non_ascii; /* msg body contains non-ASCII chars */
326 int length; /* length of attachment header field name */
329 if (!(draft_file = fopen(draft_file_name, "r"))) {
330 adios(NULL, "can't open draft file `%s'.", draft_file_name);
333 /* We'll grow the buffer as needed. */
334 field = (char *)mh_xmalloc(field_size = 256);
337 ** Scan the draft file for an attachment header field name.
339 length = strlen(attach_hdr);
341 while (get_line() != EOF && *field != '\0' && *field != '-') {
342 if (strncasecmp(field, attach_hdr, length)==0 &&
343 field[length] == ':') {
349 ** Look for at least one non-blank line in the body of the
350 ** message which indicates content in the body.
351 ** Check if body contains at least one non-blank (= not empty)
352 ** and if it contains any non-ASCII chars (= need MIME).
356 while (get_line() != EOF) {
357 for (p = field; *p != '\0'; p++) {
358 if (*p != ' ' && *p != '\t') {
361 if (*p > 127 || *p < 0) {
365 if (has_body && non_ascii) {
366 break; /* that's been already enough information */
370 if (!has_attachment && non_ascii==0) {
371 /* We don't need to convert it to MIME. */
379 /* Make names for the temporary files. */
380 strncpy(body_file_name,
381 m_mktemp(toabsdir(invo_name), NULL, NULL),
382 sizeof (body_file_name));
383 strncpy(composition_file_name,
384 m_mktemp(toabsdir(invo_name), NULL, NULL),
385 sizeof (composition_file_name));
388 body_file = fopen(body_file_name, "w");
390 composition_file = fopen(composition_file_name, "w");
392 if ((has_body && !body_file) || !composition_file) {
393 clean_up_temporary_files();
394 adios(NULL, "unable to open all of the temporary files.");
397 /* Copy non-attachment header fields to the temp composition file. */
399 while (get_line() != EOF && *field && *field != '-') {
400 if (strncasecmp(field, attach_hdr, length) != 0 ||
401 field[length] != ':') {
402 fprintf(composition_file, "%s\n", field);
405 fputs("--------\n", composition_file);
408 /* Copy the message body to the temporary file. */
409 while ((c = getc(draft_file)) != EOF) {
414 /* Add a mhbuild MIME composition file line for the body */
415 /* charset will be discovered/guessed by mhbuild */
416 fprintf(composition_file, "#text/plain %s\n", body_file_name);
420 ** Now, go back to the beginning of the draft file and look for
421 ** header fields that specify attachments. Add a mhbuild MIME
422 ** composition file for each.
425 while (get_line() != EOF && *field && *field != '-') {
426 if (strncasecmp(field, attach_hdr, length) == 0 &&
427 field[length] == ':') {
428 p = trim(field+length+1);
430 /* forwarded message */
431 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
433 /* regular attachment */
434 make_mime_composition_file_entry(p);
438 fclose(composition_file);
440 /* We're ready to roll! */
441 if (execprogl("mhbuild", "mhbuild", composition_file_name,
442 (char *)NULL) != 0) {
444 clean_up_temporary_files();
447 /* Remove the automatically created backup of mhbuild. */
448 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
449 if (unlink(buf) == -1) {
450 advise(NULL, "unable to remove original composition file.");
457 signandenc(char *draft_file_name)
464 if (!(draft_file = fopen(draft_file_name, "r"))) {
465 adios(NULL, "can't open draft file `%s'.", draft_file_name);
468 /* We'll grow the buffer as needed. */
469 field = (char *)mh_xmalloc(field_size = 256);
471 /* Scan the draft file for an attachment header field name. */
472 while (get_line() != EOF && *field != '\0' && *field != '-') {
473 if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 &&
474 field[strlen(sign_hdr)] == ':') {
477 if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 &&
478 field[strlen(enc_hdr)] == ':') {
482 if (!dosign && !doenc) {
486 strcpy(composition_file_name, draft_file_name);
488 /* We're ready to roll! */
490 ret = execprogl("mhsign", "mhsign", "-m", "-e",
491 draft_file_name, (char *)NULL);
493 ret = execprogl("mhsign", "mhsign", "-m",
494 draft_file_name, (char *)NULL);
500 /* Remove the automatically created backup of mhsign. */
501 snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
502 if (unlink(buf) == -1) {
503 advise(NULL, "unable to remove original draft file.");
510 clean_up_temporary_files(void)
512 unlink(body_file_name);
513 unlink(composition_file_name);
521 int c; /* current character */
522 int n; /* number of bytes in buffer */
526 ** Get a line from the input file, growing the field buffer as
527 ** needed. We do this so that we can fit an entire line in the
528 ** buffer making it easy to do a string comparison on both the
529 ** field name and the field body which might be a long path name.
531 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
532 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
533 ungetc(c, draft_file);
537 if (++n >= field_size - 1) {
538 field = (char *)mh_xrealloc(field, field_size += 256);
548 make_mime_composition_file_entry(char *file_name)
553 char content_type[BUFSIZ];
555 char *cmd = mimetypequeryproc;
558 for (np = m_defs; np; np = np->n_next) {
559 if (strcasecmp(np->n_name, mimetypequery)==0) {
564 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
566 if (!(fp = popen(cmdbuf, "r"))) {
567 clean_up_temporary_files();
568 adios(NULL, "unable to determine content type with `%s'",
571 if (fgets(content_type, sizeof content_type, fp) &&
572 (cp = strrchr(content_type, '\n'))) {
575 strcpy(content_type, "application/octet-stream");
576 admonish(NULL, "problems with `%s', using fall back type `%s'",
577 cmdbuf, content_type);
581 /* TODO: don't use access(2) because it checks for ruid, not euid */
582 if (access(file_name, R_OK) != 0) {
583 clean_up_temporary_files();
584 adios(NULL, "unable to access file `%s'", file_name);
587 /* Check for broken file(1). See man page mh-profile(5). */
588 for (cp=content_type; *cp; cp++) {
591 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);
593 } else if (*cp == ';') {
600 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
601 fprintf(composition_file,
602 "#%s; name=\"%s\" <> [%s] {attachment} %s\n",
603 content_type, cp, cp, file_name);
609 ** The back-end of the message sending back-end.
610 ** Annotate original message, and call `spost' to send message.
613 sendaux(char **vec, int vecp, char *drft, struct stat *st)
620 if (distfile && distout(drft, distfile, backup) == NOTOK) {
625 switch (child_id = fork()) {
627 /* oops -- fork error */
628 advise("fork", "unable to");
632 /* child process -- send it */
634 fprintf(stderr, "unable to exec ");
637 break; /* NOT REACHED */
640 /* parent process -- wait for it */
641 status = pidwait(child_id, NOTOK);
642 if (WIFEXITED(status) && WEXITSTATUS(status) == EX_OK) {
648 advise(NULL, "%s", strexit(status));
651 if (rename(backup, drft) == NOTOK) {
652 advise(drft, "unable to rename %s to",
660 return status ? NOTOK : status;
665 anno(struct stat *st)
674 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
675 st->st_mtime != st2.st_mtime ||
676 st->st_dev != st2.st_dev ||
677 st->st_ino != st2.st_ino)) {
679 admonish(NULL, "$mhaltmsg mismatch");
684 if (!(folder = getenv("mhfolder")) || !*folder) {
686 admonish(NULL, "$mhfolder not set");
690 if (!(msgs = getenv("mhmessages")) || !*msgs) {
692 admonish(NULL, "$mhmessages not set");
697 advise(NULL, "annotate as `%s': %s %s", annotext,
700 vec[vecp++] = "anno";
701 vec[vecp++] = "-comp";
702 vec[vecp++] = annotext;
703 snprintf(buf, sizeof buf, "+%s", folder);
706 while (isspace(*msgs)) {
709 for (cp=dp=msgs; *cp; cp++) {
711 while (isspace(*cp)) {
720 if (execprog(*vec, vec) != 0) {
721 advise(NULL, "unable to annotate");
729 if (WIFSIGNALED(status)) {
730 return "spost or sendmail killed by signal";
732 if (!WIFEXITED(status)) {
733 return "message not delivered to anyone";
735 switch (WEXITSTATUS(status)) {
737 return "Temporary error, maybe the MTA has queued the message";
739 return "message not delivered to anyone";