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, *folder = 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);
128 } else if (*cp == '+' || *cp == '@') {
130 adios(EX_USAGE, NULL, "only one folder at a time!");
132 folder = mh_xstrdup(expandfol(cp));
136 files[nfiles++] = cp;
143 if (!nmsgs && !nfiles) {
144 msgs[nmsgs++] = seq_cur;
148 folder = folder ? folder : draftfolder;
149 maildir = toabsdir(folder);
150 if (chdir(maildir) == NOTOK) {
151 adios(EX_OSERR, maildir, "unable to change directory to");
153 if (!(mp = folder_read(folder))) {
154 adios(EX_IOERR, NULL, "unable to read folder %s",
157 if (mp->nummsg == 0) {
158 adios(EX_DATAERR, NULL, "no messages in folder %s",
161 /* parse all the msgranges/sequences and set SELECTED */
162 for (msgnum = 0; msgnum < nmsgs; msgnum++) {
163 if (!m_convert(mp, msgs[msgnum])) {
169 for (nmsgs = 0, msgnum = mp->lowsel;
170 msgnum <= mp->hghsel; msgnum++) {
171 if (is_selected(mp, msgnum)) {
172 files[nfiles++] = mh_xstrdup(m_name(msgnum));
173 unset_exists(mp, msgnum);
177 mp->msgflags |= SEQMOD;
181 if (!(cp = getenv("SIGNATURE")) || !*cp) {
182 if ((cp = context_find("signature")) && *cp) {
183 m_putenv("SIGNATURE", cp);
187 for (n = 0; n < nfiles; n++) {
188 if (stat(files[n], &st) == NOTOK) {
189 adios(EX_IOERR, files[n], "unable to stat draft file");
193 if (!(annotext = getenv("mhannotate")) || !*annotext) {
196 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
197 altmsg = NULL; /* used by dist interface - see below */
200 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
201 vec[vecp++] = "-dist";
202 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
203 adios(EX_IOERR, altmsg, "unable to open for reading");
206 distfile = mh_xstrdup(m_mktemp2(NULL, invo_name, NULL, NULL));
207 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
208 adios(EX_IOERR, distfile, "unable to open for writing");
210 cpydata(in, out, altmsg, distfile);
217 if (!altmsg || stat(altmsg, &st) == NOTOK) {
224 for (n=3; n<OPEN_MAX; n++) {
228 for (n = 0; n < nfiles; n++) {
229 switch (sendsbr(vec, vecp, files[n], &st)) {
245 ** message sending back-end
248 sendsbr(char **vec, int vecp, char *drft, struct stat *st)
251 char *original_draft;
254 ** Save the original name of the draft file. The name of the
255 ** draft file is changed to a temporary file containing the built
256 ** MIME message if there are attachments. We need the original
257 ** name so that it can be renamed after the message is sent.
259 original_draft = drft;
262 ** Convert the draft to a MIME message.
263 ** Use the mhbuild composition file for the draft if there was
264 ** a successful conversion because that now contains the MIME
265 ** message. A nice side effect of this is that it leaves the
266 ** original draft file untouched so that it can be retrieved
267 ** and modified if desired.
269 switch (attach(drft)) {
271 /* successfully MIMEified: use generate draft */
272 drft = composition_file_name;
279 /* already in MIME format: keep original draft */
284 ** Sign and encrypt the message as needed.
285 ** Use the mhbuild composition file for the draft if there was
286 ** a successful conversion because that now contains the MIME
287 ** message. A nice side effect of this is that it leaves the
288 ** original draft file untouched so that it can be retrieved
289 ** and modified if desired.
291 switch (signandenc(drft)) {
293 drft = composition_file_name;
303 if ((status = sendaux(vec, vecp, drft, st)) == OK) {
304 /* move original draft to +trash folder */
305 /* temporary close stdin, for refile not to ask */
308 if (execprogl("refile", "refile", "-file",
309 original_draft, "+trash",
310 (char *)NULL) != 0) {
311 advise(NULL, "unable to trash the draft");
323 /* Get rid of temporary files that we created for attachments. */
324 if (drft == composition_file_name) {
325 clean_up_temporary_files();
332 attach(char *draft_file_name)
334 char buf[MAXPATHLEN + 6];
336 int length = strlen(attach_hdr);
339 if (!(draft_file = fopen(draft_file_name, "r"))) {
340 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
343 /* We'll grow the buffer as needed. */
344 field = mh_xcalloc(field_size = 256, sizeof(char));
350 /* Make names for the temporary files. */
351 strncpy(body_file_name,
352 m_mktemp(toabsdir(invo_name), NULL, NULL),
353 sizeof (body_file_name));
354 strncpy(composition_file_name,
355 m_mktemp(toabsdir(invo_name), NULL, NULL),
356 sizeof (composition_file_name));
358 body_file = fopen(body_file_name, "w");
359 composition_file = fopen(composition_file_name, "w");
361 if (!body_file || !composition_file) {
362 clean_up_temporary_files();
363 adios(EX_IOERR, NULL, "unable to open all of the temporary files.");
366 /* Copy non-attachment header fields to the temp composition file. */
368 while (get_line() != EOF && *field && *field != '-') {
369 if (strncasecmp(field, VRSN_FIELD, strlen(VRSN_FIELD))==0 &&
370 field[strlen(VRSN_FIELD)] == ':') {
372 ** The draft is already in MIME format, thus
373 ** back out and use the original draft file.
375 clean_up_temporary_files();
379 if (strncasecmp(field, attach_hdr, length) != 0 ||
380 field[length] != ':') {
381 fprintf(composition_file, "%s\n", field);
384 fputs("--------\n", composition_file);
386 /* Copy the message body to the temporary file. */
387 while ((c = getc(draft_file)) != EOF) {
392 /* Add a mhbuild MIME composition file line for the body */
393 /* charset will be discovered/guessed by mhbuild */
394 fprintf(composition_file, "#text/plain %s\n", body_file_name);
397 ** Now, go back to the beginning of the draft file and look for
398 ** header fields that specify attachments. Add a mhbuild MIME
399 ** composition file for each.
402 while (get_line() != EOF && *field && *field != '-') {
403 if (strncasecmp(field, attach_hdr, length) == 0 &&
404 field[length] == ':') {
405 p = trim(field+length+1);
407 /* forwarded message */
408 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
410 /* regular attachment */
411 make_mime_composition_file_entry(p);
415 fclose(composition_file);
417 /* We're ready to roll! */
418 if (execprogl("mhbuild", "mhbuild", composition_file_name,
419 (char *)NULL) != 0) {
421 clean_up_temporary_files();
424 /* Remove the automatically created backup of mhbuild. */
425 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
426 if (unlink(buf) == -1) {
427 advise(NULL, "unable to remove original composition file.");
434 signandenc(char *draft_file_name)
441 if (!(draft_file = fopen(draft_file_name, "r"))) {
442 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
445 /* We'll grow the buffer as needed. */
446 field = mh_xcalloc(field_size = 256, sizeof(char));
448 /* Scan the draft file for an attachment header field name. */
449 while (get_line() != EOF && *field != '\0' && *field != '-') {
450 if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 &&
451 field[strlen(sign_hdr)] == ':') {
454 if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 &&
455 field[strlen(enc_hdr)] == ':') {
459 if (!dosign && !doenc) {
463 strcpy(composition_file_name, draft_file_name);
465 /* We're ready to roll! */
467 ret = execprogl("mhsign", "mhsign", "-m", "-e",
468 draft_file_name, (char *)NULL);
470 ret = execprogl("mhsign", "mhsign", "-m",
471 draft_file_name, (char *)NULL);
477 /* Remove the automatically created backup of mhsign. */
478 snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
479 if (unlink(buf) == -1) {
480 advise(NULL, "unable to remove original draft file.");
487 clean_up_temporary_files(void)
489 unlink(body_file_name);
490 unlink(composition_file_name);
498 int c; /* current character */
499 int n; /* number of bytes in buffer */
503 ** Get a line from the input file, growing the field buffer as
504 ** needed. We do this so that we can fit an entire line in the
505 ** buffer making it easy to do a string comparison on both the
506 ** field name and the field body which might be a long path name.
508 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
509 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
510 ungetc(c, draft_file);
514 if (++n >= field_size - 1) {
515 field = mh_xrealloc(field, field_size += 256);
525 make_mime_composition_file_entry(char *file_name)
530 char content_type[BUFSIZ];
532 char *cmd = mimetypequeryproc;
535 for (np = m_defs; np; np = np->n_next) {
536 if (strcasecmp(np->n_name, mimetypequery)==0) {
541 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
543 if (!(fp = popen(cmdbuf, "r"))) {
544 clean_up_temporary_files();
545 adios(EX_IOERR, NULL, "unable to determine content type with `%s'",
548 if (fgets(content_type, sizeof content_type, fp) &&
549 (cp = strrchr(content_type, '\n'))) {
552 strcpy(content_type, "application/octet-stream");
553 admonish(NULL, "problems with `%s', using fall back type `%s'",
554 cmdbuf, content_type);
558 /* TODO: don't use access(2) because it checks for ruid, not euid */
559 if (access(file_name, R_OK) != 0) {
560 clean_up_temporary_files();
561 adios(EX_IOERR, NULL, "unable to access file `%s'", file_name);
564 /* Check for broken file(1). See man page mh-profile(5). */
565 for (cp=content_type; *cp; cp++) {
568 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);
570 } else if (*cp == ';') {
577 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
578 fprintf(composition_file,
579 "#%s; name=\"%s\" <> [%s] {attachment} %s\n",
580 content_type, cp, cp, file_name);
586 ** The back-end of the message sending back-end.
587 ** Annotate original message, and call `spost' to send message.
590 sendaux(char **vec, int vecp, char *drft, struct stat *st)
597 if (distfile && distout(drft, distfile, backup) == NOTOK) {
602 switch (child_id = fork()) {
604 /* oops -- fork error */
605 advise("fork", "unable to");
609 /* child process -- send it */
611 fprintf(stderr, "unable to exec ");
614 break; /* NOT REACHED */
617 /* parent process -- wait for it */
618 status = pidwait(child_id, NOTOK);
619 if (WIFEXITED(status) && WEXITSTATUS(status) == EX_OK) {
625 advise(NULL, "%s", strexit(status));
628 if (rename(backup, drft) == NOTOK) {
629 advise(drft, "unable to rename %s to",
636 return status ? NOTOK : status;
641 anno(struct stat *st)
650 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
651 st->st_mtime != st2.st_mtime ||
652 st->st_dev != st2.st_dev ||
653 st->st_ino != st2.st_ino)) {
655 admonish(NULL, "$mhaltmsg mismatch");
660 if (!(folder = getenv("mhfolder")) || !*folder) {
662 admonish(NULL, "$mhfolder not set");
666 if (!(msgs = getenv("mhmessages")) || !*msgs) {
668 admonish(NULL, "$mhmessages not set");
673 advise(NULL, "annotate as `%s': %s %s", annotext,
676 vec[vecp++] = "anno";
677 vec[vecp++] = "-comp";
678 vec[vecp++] = annotext;
679 snprintf(buf, sizeof buf, "+%s", folder);
682 while (isspace(*msgs)) {
685 for (cp=dp=msgs; *cp; cp++) {
687 while (isspace(*cp)) {
696 if (execprog(*vec, vec) != 0) {
697 advise(NULL, "unable to annotate");
705 if (WIFSIGNALED(status)) {
706 return "spost or sendmail killed by signal";
708 if (!WIFEXITED(status)) {
709 return "message not delivered to anyone";
711 switch (WEXITSTATUS(status)) {
713 return "Temporary error, maybe the MTA has queued the message";
715 return "message not delivered to anyone";