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 FILE *draft_file; /* draft file pointer */
43 static FILE *body_file; /* body file pointer */
44 static FILE *composition_file; /* composition file pointer */
49 static int sendsbr(char **, int, char *, struct stat *);
50 static void anno(struct stat *);
51 static int sendaux(char **, int, char *, struct stat *);
52 static int attach(char *);
53 static int signandenc(char *);
54 static void clean_up_temporary_files(void);
55 static void make_mime_composition_file_entry(char *);
56 static char* strexit(int status);
59 static struct swit switches[] = {
73 char *version=VERSION;
76 main(int argc, char **argv)
78 int nmsgs = 0, nfiles = 0, distsw = 0, vecp = 1;
82 char *cp, *maildir = NULL, *folder = NULL;
83 char buf[BUFSIZ], **argp, **arguments;
84 char *msgs[MAXARGS], *vec[MAXARGS];
90 setlocale(LC_ALL, "");
91 invo_name = mhbasename(argv[0]);
93 /* read user profile/context */
96 arguments = getarguments(invo_name, argc, argv, 1);
99 while ((cp = *argp++)) {
101 switch (smatch(++cp, switches)) {
103 ambigsw(cp, switches);
106 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
109 snprintf(buf, sizeof(buf),
110 "%s [file] [switches]",
112 print_help(buf, switches, 1);
113 exit(argc == 2 ? EX_OK : EX_USAGE);
115 print_version(invo_name);
116 exit(argc == 2 ? EX_OK : EX_USAGE);
126 } else if (*cp == '+' || *cp == '@') {
128 adios(EX_USAGE, NULL, "only one folder at a time!");
130 folder = mh_xstrdup(expandfol(cp));
134 files[nfiles++] = cp;
141 if (!nmsgs && !nfiles) {
142 msgs[nmsgs++] = seq_cur;
146 folder = folder ? folder : draftfolder;
147 maildir = toabsdir(folder);
148 if (chdir(maildir) == NOTOK) {
149 adios(EX_OSERR, maildir, "unable to change directory to");
151 if (!(mp = folder_read(folder))) {
152 adios(EX_IOERR, NULL, "unable to read folder %s",
155 if (mp->nummsg == 0) {
156 adios(EX_DATAERR, NULL, "no messages in folder %s",
159 /* parse all the msgranges/sequences and set SELECTED */
160 for (msgnum = 0; msgnum < nmsgs; msgnum++) {
161 if (!m_convert(mp, msgs[msgnum])) {
166 for (nmsgs = 0, msgnum = mp->lowsel;
167 msgnum <= mp->hghsel; msgnum++) {
168 if (is_selected(mp, msgnum)) {
169 files[nfiles++] = mh_xstrdup(m_name(msgnum));
170 unset_exists(mp, msgnum);
174 mp->msgflags |= SEQMOD;
178 if (!(cp = getenv("SIGNATURE")) || !*cp) {
179 if ((cp = context_find("signature")) && *cp) {
180 m_putenv("SIGNATURE", cp);
184 for (n = 0; n < nfiles; n++) {
185 if (stat(files[n], &st) == NOTOK) {
186 adios(EX_IOERR, files[n], "unable to stat draft file");
190 if (!(annotext = getenv("mhannotate")) || !*annotext) {
193 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
194 altmsg = NULL; /* used by dist interface - see below */
197 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
198 vec[vecp++] = "-dist";
199 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
200 adios(EX_IOERR, altmsg, "unable to open for reading");
203 distfile = mh_xstrdup(m_mktemp2(NULL, invo_name, NULL, NULL));
204 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
205 adios(EX_IOERR, distfile, "unable to open for writing");
207 cpydata(in, out, altmsg, distfile);
214 if (!altmsg || stat(altmsg, &st) == NOTOK) {
221 for (n=3; n<OPEN_MAX; n++) {
225 for (n = 0; n < nfiles; n++) {
226 switch (sendsbr(vec, vecp, files[n], &st)) {
242 ** message sending back-end
245 sendsbr(char **vec, int vecp, char *drft, struct stat *st)
248 char *original_draft;
251 ** Save the original name of the draft file. The name of the
252 ** draft file is changed to a temporary file containing the built
253 ** MIME message if there are attachments. We need the original
254 ** name so that it can be renamed after the message is sent.
256 original_draft = drft;
259 ** Convert the draft to a MIME message.
260 ** Use the mhbuild composition file for the draft if there was
261 ** a successful conversion because that now contains the MIME
262 ** message. A nice side effect of this is that it leaves the
263 ** original draft file untouched so that it can be retrieved
264 ** and modified if desired.
266 switch (attach(drft)) {
268 /* successfully MIMEified: use generate draft */
269 drft = composition_file_name;
276 /* already in MIME format: keep original draft */
281 ** Sign and encrypt the message as needed.
282 ** Use the mhbuild composition file for the draft if there was
283 ** a successful conversion because that now contains the MIME
284 ** message. A nice side effect of this is that it leaves the
285 ** original draft file untouched so that it can be retrieved
286 ** and modified if desired.
288 switch (signandenc(drft)) {
290 drft = composition_file_name;
300 if ((status = sendaux(vec, vecp, drft, st)) == OK) {
301 /* move original draft to +trash folder */
302 /* temporary close stdin, for refile not to ask */
305 if (execprogl("refile", "refile", "-file",
306 original_draft, "+trash",
307 (char *)NULL) != 0) {
308 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)
332 struct field f = {{0}};
334 int finished_header = 0;
335 char buf[MAXPATHLEN + 6];
336 size_t length = strlen(attach_hdr);
343 if (!(draft_file = fopen(draft_file_name, "r"))) {
344 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
351 /* Make names for the temporary files. */
352 strncpy(body_file_name,
353 m_mktemp(toabsdir(invo_name), NULL, NULL),
354 sizeof (body_file_name));
355 strncpy(composition_file_name,
356 m_mktemp(toabsdir(invo_name), NULL, NULL),
357 sizeof (composition_file_name));
359 body_file = fopen(body_file_name, "w");
360 composition_file = fopen(composition_file_name, "w");
362 if (!body_file || !composition_file) {
363 clean_up_temporary_files();
364 adios(EX_IOERR, NULL, "unable to open all of the temporary files.");
367 /* Copy non-attachment header fields to the temp composition file. */
369 for (compnum = 1, state = FLD2; state == FLD2 || state == BODY2;) {
370 switch (state = m_getfld2(state, &f, draft_file)) {
375 if (strncasecmp(f.name, VRSN_FIELD, sizeof(VRSN_FIELD) - 1)==0) {
377 ** The draft is already in MIME format, thus
378 ** back out and use the original draft file.
380 clean_up_temporary_files();
383 if (strncasecmp(f.name, attach_hdr, length) != 0 ||
384 f.namelen != length) {
385 fprintf(composition_file, "%s:%s", f.name, f.value);
389 if (!finished_header) {
390 fputs("--------\n", composition_file);
393 fputs(f.value, body_file);
398 adios(EX_DATAERR, NULL, "message format error in component #%d", compnum);
400 adios(EX_IOERR, NULL, "error reading draft file: %s", draft_file_name);
402 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
407 /* Add a mhbuild MIME composition file line for the body */
408 /* charset will be discovered/guessed by mhbuild */
409 fprintf(composition_file, "#text/plain %s\n", body_file_name);
412 ** Now, go back to the beginning of the draft file and look for
413 ** header fields that specify attachments. Add a mhbuild MIME
414 ** composition file for each.
417 for (compnum = 1, state = FLD2; state == FLD2;) {
418 switch (state = m_getfld2(state, &f, draft_file)) {
423 if (strncasecmp(f.name, attach_hdr, length) == 0 &&
424 f.namelen == length) {
427 /* forwarded message */
428 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
430 /* regular attachment */
431 make_mime_composition_file_entry(p);
439 adios(EX_DATAERR, NULL, "message format error in component #%d", compnum);
441 adios(EX_IOERR, NULL, "error reading draft file: %s", draft_file_name);
443 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
446 fclose(composition_file);
448 /* We're ready to roll! */
449 if (execprogl("mhbuild", "mhbuild", composition_file_name,
450 (char *)NULL) != 0) {
452 clean_up_temporary_files();
455 /* Remove the automatically created backup of mhbuild. */
456 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
457 if (unlink(buf) == -1) {
458 advise(NULL, "unable to remove original composition file.");
465 signandenc(char *draft_file_name)
469 struct field f = {{0}};
479 if (!(draft_file = fopen(draft_file_name, "r"))) {
480 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
483 /* Scan the draft file for an attachment header field name. */
484 for (compnum = 1, state = FLD2; state == FLD2;) {
485 switch (state = m_getfld2(state, &f, draft_file)) {
490 if (strcasecmp(f.name, sign_hdr)==0) {
493 if (strcasecmp(f.name, enc_hdr)==0) {
501 adios(EX_DATAERR, NULL, "message format error in component #%d", compnum);
503 adios(EX_IOERR, NULL, "error reading draft file: %s", draft_file_name);
505 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
509 if (!dosign && !doenc) {
513 strcpy(composition_file_name, draft_file_name);
515 /* We're ready to roll! */
517 ret = execprogl("mhsign", "mhsign", "-m", "-e",
518 draft_file_name, (char *)NULL);
520 ret = execprogl("mhsign", "mhsign", "-m",
521 draft_file_name, (char *)NULL);
527 /* Remove the automatically created backup of mhsign. */
528 snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
529 if (unlink(buf) == -1) {
530 advise(NULL, "unable to remove original draft file.");
537 clean_up_temporary_files(void)
539 unlink(body_file_name);
540 unlink(composition_file_name);
546 make_mime_composition_file_entry(char *file_name)
551 char content_type[BUFSIZ];
553 char *cmd = mimetypequeryproc;
556 for (np = m_defs; np; np = np->n_next) {
557 if (strcasecmp(np->n_name, mimetypequery)==0) {
562 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
564 if (!(fp = popen(cmdbuf, "r"))) {
565 clean_up_temporary_files();
566 adios(EX_IOERR, NULL, "unable to determine content type with `%s'",
569 if (fgets(content_type, sizeof content_type, fp) &&
570 (cp = strrchr(content_type, '\n'))) {
573 strcpy(content_type, "application/octet-stream");
574 admonish(NULL, "problems with `%s', using fall back type `%s'",
575 cmdbuf, content_type);
579 /* TODO: don't use access(2) because it checks for ruid, not euid */
580 if (access(file_name, R_OK) != 0) {
581 clean_up_temporary_files();
582 adios(EX_IOERR, NULL, "unable to access file `%s'", file_name);
585 /* Check for broken file(1). See man page mh-profile(5). */
586 for (cp=content_type; *cp; cp++) {
589 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);
591 } else if (*cp == ';') {
598 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
599 fprintf(composition_file,
600 "#%s; name=\"%s\" <> [%s] {attachment} %s\n",
601 content_type, cp, cp, file_name);
607 ** The back-end of the message sending back-end.
608 ** Annotate original message, and call `spost' to send message.
611 sendaux(char **vec, int vecp, char *drft, struct stat *st)
618 if (distfile && distout(drft, distfile, backup) == NOTOK) {
623 switch (child_id = fork()) {
625 /* oops -- fork error */
626 advise("fork", "unable to");
630 /* child process -- send it */
632 fprintf(stderr, "unable to exec ");
635 break; /* NOT REACHED */
638 /* parent process -- wait for it */
639 status = pidwait(child_id, NOTOK);
640 if (WIFEXITED(status) && WEXITSTATUS(status) == EX_OK) {
646 advise(NULL, "%s", strexit(status));
649 if (rename(backup, drft) == NOTOK) {
650 advise(drft, "unable to rename %s to",
657 return status ? NOTOK : status;
662 anno(struct stat *st)
671 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
672 st->st_mtime != st2.st_mtime ||
673 st->st_dev != st2.st_dev ||
674 st->st_ino != st2.st_ino)) {
676 admonish(NULL, "$mhaltmsg mismatch");
681 if (!(folder = getenv("mhfolder")) || !*folder) {
683 admonish(NULL, "$mhfolder not set");
687 if (!(msgs = getenv("mhmessages")) || !*msgs) {
689 admonish(NULL, "$mhmessages not set");
694 advise(NULL, "annotate as `%s': %s %s", annotext,
697 vec[vecp++] = "anno";
698 vec[vecp++] = "-comp";
699 vec[vecp++] = annotext;
700 snprintf(buf, sizeof buf, "+%s", folder);
703 while (isspace(*msgs)) {
706 for (cp=dp=msgs; *cp; cp++) {
708 while (isspace(*cp)) {
717 if (execprog(*vec, vec) != 0) {
718 advise(NULL, "unable to annotate");
726 if (WIFSIGNALED(status)) {
727 return "spost or sendmail killed by signal";
729 if (!WIFEXITED(status)) {
730 return "message not delivered to anyone";
732 switch (WEXITSTATUS(status)) {
734 return "Temporary error, maybe the MTA has queued the message";
736 return "message not delivered to anyone";