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 if ((status = sendaux(vec, vecp, drft, st)) == OK) {
287 /* move original draft to +trash folder */
288 /* temporary close stdin, for refile not to ask */
291 if (execprogl("refile", "refile", "-file",
292 original_draft, "+trash",
293 (char *)NULL) != 0) {
294 advise(NULL, "unable to trash the draft");
306 /* Get rid of temporary files that we created for attachments. */
307 if (drft == composition_file_name) {
308 clean_up_temporary_files();
315 attach(char *draft_file_name)
317 char buf[MAXPATHLEN + 6];
321 int non_ascii; /* msg body contains non-ASCII chars */
322 int length; /* length of attachment header field name */
325 if (!(draft_file = fopen(draft_file_name, "r"))) {
326 adios(NULL, "can't open draft file `%s'.", draft_file_name);
329 /* We'll grow the buffer as needed. */
330 field = (char *)mh_xmalloc(field_size = 256);
333 ** Scan the draft file for an attachment header field name.
335 length = strlen(attach_hdr);
337 while (get_line() != EOF && *field != '\0' && *field != '-') {
338 if (strncasecmp(field, attach_hdr, length)==0 &&
339 field[length] == ':') {
345 ** Look for at least one non-blank line in the body of the
346 ** message which indicates content in the body.
347 ** Check if body contains at least one non-blank (= not empty)
348 ** and if it contains any non-ASCII chars (= need MIME).
352 while (get_line() != EOF) {
353 for (p = field; *p != '\0'; p++) {
354 if (*p != ' ' && *p != '\t') {
357 if (*p > 127 || *p < 0) {
361 if (has_body && non_ascii) {
362 break; /* that's been already enough information */
366 if (!has_attachment && non_ascii==0) {
367 /* We don't need to convert it to MIME. */
375 /* Make names for the temporary files. */
376 strncpy(body_file_name,
377 m_mktemp(toabsdir(invo_name), NULL, NULL),
378 sizeof (body_file_name));
379 strncpy(composition_file_name,
380 m_mktemp(toabsdir(invo_name), NULL, NULL),
381 sizeof (composition_file_name));
384 body_file = fopen(body_file_name, "w");
386 composition_file = fopen(composition_file_name, "w");
388 if ((has_body && !body_file) || !composition_file) {
389 clean_up_temporary_files();
390 adios(NULL, "unable to open all of the temporary files.");
393 /* Copy non-attachment header fields to the temp composition file. */
395 while (get_line() != EOF && *field && *field != '-') {
396 if (strncasecmp(field, attach_hdr, length) != 0 ||
397 field[length] != ':') {
398 fprintf(composition_file, "%s\n", field);
401 fputs("--------\n", composition_file);
404 /* Copy the message body to the temporary file. */
405 while ((c = getc(draft_file)) != EOF) {
410 /* Add a mhbuild MIME composition file line for the body */
411 /* charset will be discovered/guessed by mhbuild */
412 fprintf(composition_file, "#text/plain %s\n", body_file_name);
416 ** Now, go back to the beginning of the draft file and look for
417 ** header fields that specify attachments. Add a mhbuild MIME
418 ** composition file for each.
421 while (get_line() != EOF && *field && *field != '-') {
422 if (strncasecmp(field, attach_hdr, length) == 0 &&
423 field[length] == ':') {
424 p = trim(field+length+1);
426 /* forwarded message */
427 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
429 /* regular attachment */
430 make_mime_composition_file_entry(p);
434 fclose(composition_file);
436 /* We're ready to roll! */
437 if (execprogl("mhbuild", "mhbuild", composition_file_name,
438 (char *)NULL) != 0) {
440 clean_up_temporary_files();
443 /* Remove the automatically created backup of mhbuild. */
444 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
445 if (unlink(buf) == -1) {
446 advise(NULL, "unable to remove original composition file.");
453 signandenc(char *draft_file_name)
460 if (!(draft_file = fopen(draft_file_name, "r"))) {
461 adios(NULL, "can't open draft file `%s'.", draft_file_name);
464 /* We'll grow the buffer as needed. */
465 field = (char *)mh_xmalloc(field_size = 256);
467 /* Scan the draft file for an attachment header field name. */
468 while (get_line() != EOF && *field != '\0' && *field != '-') {
469 if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 &&
470 field[strlen(sign_hdr)] == ':') {
473 if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 &&
474 field[strlen(enc_hdr)] == ':') {
478 if (!dosign && !doenc) {
482 strcpy(composition_file_name, draft_file_name);
484 /* We're ready to roll! */
486 ret = execprogl("mhsign", "mhsign", "-m", "-e",
487 draft_file_name, (char *)NULL);
489 ret = execprogl("mhsign", "mhsign", "-m",
490 draft_file_name, (char *)NULL);
496 /* Remove the automatically created backup of mhsign. */
497 snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
498 if (unlink(buf) == -1) {
499 advise(NULL, "unable to remove original draft file.");
506 clean_up_temporary_files(void)
508 unlink(body_file_name);
509 unlink(composition_file_name);
517 int c; /* current character */
518 int n; /* number of bytes in buffer */
522 ** Get a line from the input file, growing the field buffer as
523 ** needed. We do this so that we can fit an entire line in the
524 ** buffer making it easy to do a string comparison on both the
525 ** field name and the field body which might be a long path name.
527 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
528 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
529 ungetc(c, draft_file);
533 if (++n >= field_size - 1) {
534 field = (char *)mh_xrealloc(field, field_size += 256);
544 make_mime_composition_file_entry(char *file_name)
549 char content_type[BUFSIZ];
551 char *cmd = mimetypequeryproc;
554 for (np = m_defs; np; np = np->n_next) {
555 if (strcasecmp(np->n_name, mimetypequery)==0) {
560 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
562 if (!(fp = popen(cmdbuf, "r"))) {
563 clean_up_temporary_files();
564 adios(NULL, "unable to determine content type with `%s'",
567 if (fgets(content_type, sizeof content_type, fp) &&
568 (cp = strrchr(content_type, '\n'))) {
571 strcpy(content_type, "application/octet-stream");
572 admonish(NULL, "problems with `%s', using fall back type `%s'",
573 cmdbuf, content_type);
577 /* TODO: don't use access(2) because it checks for ruid, not euid */
578 if (access(file_name, R_OK) != 0) {
579 clean_up_temporary_files();
580 adios(NULL, "unable to access file `%s'", file_name);
583 /* Check for broken file(1). See man page mh-profile(5). */
584 for (cp=content_type; *cp; cp++) {
587 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);
589 } else if (*cp == ';') {
596 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
597 fprintf(composition_file,
598 "#%s; name=\"%s\" <> [%s] {attachment} %s\n",
599 content_type, cp, cp, file_name);
605 ** The back-end of the message sending back-end.
606 ** Annotate original message, and call `spost' to send message.
609 sendaux(char **vec, int vecp, char *drft, struct stat *st)
616 if (distfile && distout(drft, distfile, backup) == NOTOK) {
621 switch (child_id = fork()) {
623 /* oops -- fork error */
624 advise("fork", "unable to");
628 /* child process -- send it */
630 fprintf(stderr, "unable to exec ");
633 break; /* NOT REACHED */
636 /* parent process -- wait for it */
637 status = pidwait(child_id, NOTOK);
638 if (WIFEXITED(status) && WEXITSTATUS(status) == EX_OK) {
644 advise(NULL, "%s", strexit(status));
647 if (rename(backup, drft) == NOTOK) {
648 advise(drft, "unable to rename %s to",
656 return status ? NOTOK : status;
661 anno(struct stat *st)
670 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
671 st->st_mtime != st2.st_mtime ||
672 st->st_dev != st2.st_dev ||
673 st->st_ino != st2.st_ino)) {
675 admonish(NULL, "$mhaltmsg mismatch");
680 if (!(folder = getenv("mhfolder")) || !*folder) {
682 admonish(NULL, "$mhfolder not set");
686 if (!(msgs = getenv("mhmessages")) || !*msgs) {
688 admonish(NULL, "$mhmessages not set");
693 advise(NULL, "annotate as `%s': %s %s", annotext,
696 vec[vecp++] = "anno";
697 vec[vecp++] = "-comp";
698 vec[vecp++] = annotext;
699 snprintf(buf, sizeof buf, "+%s", folder);
702 while (isspace(*msgs)) {
705 for (cp=dp=msgs; *cp; cp++) {
707 while (isspace(*cp)) {
716 if (execprog(*vec, vec) != 0) {
717 advise(NULL, "unable to annotate");
725 if (WIFSIGNALED(status)) {
726 return "spost or sendmail killed by signal";
728 if (!WIFEXITED(status)) {
729 return "message not delivered to anyone";
731 switch (WEXITSTATUS(status)) {
733 return "Temporary error, maybe the MTA has queued the message";
735 return "message not delivered to anyone";