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[] = {
76 char *version=VERSION;
79 main(int argc, char **argv)
81 int nmsgs = 0, nfiles = 0, distsw = 0, vecp = 1;
85 char *cp, *maildir = NULL, *folder = NULL;
86 char buf[BUFSIZ], **argp, **arguments;
87 char *msgs[MAXARGS], *vec[MAXARGS];
93 setlocale(LC_ALL, "");
94 invo_name = mhbasename(argv[0]);
96 /* read user profile/context */
99 arguments = getarguments(invo_name, argc, argv, 1);
102 while ((cp = *argp++)) {
104 switch (smatch(++cp, switches)) {
106 ambigsw(cp, switches);
109 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
112 snprintf(buf, sizeof(buf),
113 "%s [file] [switches]",
115 print_help(buf, switches, 1);
116 exit(argc == 2 ? EX_OK : EX_USAGE);
118 print_version(invo_name);
119 exit(argc == 2 ? EX_OK : EX_USAGE);
129 } else if (*cp == '+' || *cp == '@') {
131 adios(EX_USAGE, NULL, "only one folder at a time!");
133 folder = mh_xstrdup(expandfol(cp));
137 files[nfiles++] = cp;
144 if (!nmsgs && !nfiles) {
145 msgs[nmsgs++] = seq_cur;
149 folder = folder ? folder : draftfolder;
150 maildir = toabsdir(folder);
151 if (chdir(maildir) == NOTOK) {
152 adios(EX_OSERR, maildir, "unable to change directory to");
154 if (!(mp = folder_read(folder))) {
155 adios(EX_IOERR, NULL, "unable to read folder %s",
158 if (mp->nummsg == 0) {
159 adios(EX_DATAERR, NULL, "no messages in folder %s",
162 /* parse all the msgranges/sequences and set SELECTED */
163 for (msgnum = 0; msgnum < nmsgs; msgnum++) {
164 if (!m_convert(mp, msgs[msgnum])) {
170 for (nmsgs = 0, msgnum = mp->lowsel;
171 msgnum <= mp->hghsel; msgnum++) {
172 if (is_selected(mp, msgnum)) {
173 files[nfiles++] = mh_xstrdup(m_name(msgnum));
174 unset_exists(mp, msgnum);
178 mp->msgflags |= SEQMOD;
182 if (!(cp = getenv("SIGNATURE")) || !*cp) {
183 if ((cp = context_find("signature")) && *cp) {
184 m_putenv("SIGNATURE", cp);
188 for (n = 0; n < nfiles; n++) {
189 if (stat(files[n], &st) == NOTOK) {
190 adios(EX_IOERR, files[n], "unable to stat draft file");
194 if (!(annotext = getenv("mhannotate")) || !*annotext) {
197 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
198 altmsg = NULL; /* used by dist interface - see below */
201 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
202 vec[vecp++] = "-dist";
203 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
204 adios(EX_IOERR, altmsg, "unable to open for reading");
207 distfile = mh_xstrdup(m_mktemp2(NULL, invo_name, NULL, NULL));
208 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
209 adios(EX_IOERR, distfile, "unable to open for writing");
211 cpydata(in, out, altmsg, distfile);
218 if (!altmsg || stat(altmsg, &st) == NOTOK) {
225 for (n=3; n<OPEN_MAX; n++) {
229 for (n = 0; n < nfiles; n++) {
230 switch (sendsbr(vec, vecp, files[n], &st)) {
246 ** message sending back-end
249 sendsbr(char **vec, int vecp, char *drft, struct stat *st)
252 char *original_draft;
255 ** Save the original name of the draft file. The name of the
256 ** draft file is changed to a temporary file containing the built
257 ** MIME message if there are attachments. We need the original
258 ** name so that it can be renamed after the message is sent.
260 original_draft = drft;
263 ** Convert the draft to a MIME message.
264 ** Use the mhbuild composition file for the draft if there was
265 ** a successful conversion because that now contains the MIME
266 ** message. A nice side effect of this is that it leaves the
267 ** original draft file untouched so that it can be retrieved
268 ** and modified if desired.
270 switch (attach(drft)) {
272 /* successfully MIMEified: use generate draft */
273 drft = composition_file_name;
280 /* already in MIME format: keep original draft */
285 ** Sign and encrypt the message as needed.
286 ** Use the mhbuild composition file for the draft if there was
287 ** a successful conversion because that now contains the MIME
288 ** message. A nice side effect of this is that it leaves the
289 ** original draft file untouched so that it can be retrieved
290 ** and modified if desired.
292 switch (signandenc(drft)) {
294 drft = composition_file_name;
304 if ((status = sendaux(vec, vecp, drft, st)) == OK) {
305 /* move original draft to +trash folder */
306 /* temporary close stdin, for refile not to ask */
309 if (execprogl("refile", "refile", "-file",
310 original_draft, "+trash",
311 (char *)NULL) != 0) {
312 advise(NULL, "unable to trash the draft");
324 /* Get rid of temporary files that we created for attachments. */
325 if (drft == composition_file_name) {
326 clean_up_temporary_files();
333 attach(char *draft_file_name)
335 char buf[MAXPATHLEN + 6];
337 int length = strlen(attach_hdr);
344 if (!(draft_file = fopen(draft_file_name, "r"))) {
345 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
348 /* We'll grow the buffer as needed. */
349 field = mh_xcalloc(field_size = 256, sizeof(char));
355 /* Make names for the temporary files. */
356 strncpy(body_file_name,
357 m_mktemp(toabsdir(invo_name), NULL, NULL),
358 sizeof (body_file_name));
359 strncpy(composition_file_name,
360 m_mktemp(toabsdir(invo_name), NULL, NULL),
361 sizeof (composition_file_name));
363 body_file = fopen(body_file_name, "w");
364 composition_file = fopen(composition_file_name, "w");
366 if (!body_file || !composition_file) {
367 clean_up_temporary_files();
368 adios(EX_IOERR, NULL, "unable to open all of the temporary files.");
371 /* Copy non-attachment header fields to the temp composition file. */
373 while (get_line() != EOF && *field && *field != '-') {
374 if (strncasecmp(field, VRSN_FIELD, strlen(VRSN_FIELD))==0 &&
375 field[strlen(VRSN_FIELD)] == ':') {
377 ** The draft is already in MIME format, thus
378 ** back out and use the original draft file.
380 clean_up_temporary_files();
384 if (strncasecmp(field, attach_hdr, length) != 0 ||
385 field[length] != ':') {
386 fprintf(composition_file, "%s\n", field);
389 fputs("--------\n", composition_file);
391 /* Copy the message body to the temporary file. */
392 while ((c = getc(draft_file)) != EOF) {
397 /* Add a mhbuild MIME composition file line for the body */
398 /* charset will be discovered/guessed by mhbuild */
399 fprintf(composition_file, "#text/plain %s\n", body_file_name);
402 ** Now, go back to the beginning of the draft file and look for
403 ** header fields that specify attachments. Add a mhbuild MIME
404 ** composition file for each.
407 while (get_line() != EOF && *field && *field != '-') {
408 if (strncasecmp(field, attach_hdr, length) == 0 &&
409 field[length] == ':') {
410 p = trim(field+length+1);
412 /* forwarded message */
413 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
415 /* regular attachment */
416 make_mime_composition_file_entry(p);
420 fclose(composition_file);
422 /* We're ready to roll! */
423 if (execprogl("mhbuild", "mhbuild", composition_file_name,
424 (char *)NULL) != 0) {
426 clean_up_temporary_files();
429 /* Remove the automatically created backup of mhbuild. */
430 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
431 if (unlink(buf) == -1) {
432 advise(NULL, "unable to remove original composition file.");
439 signandenc(char *draft_file_name)
450 if (!(draft_file = fopen(draft_file_name, "r"))) {
451 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
454 /* We'll grow the buffer as needed. */
455 field = mh_xcalloc(field_size = 256, sizeof(char));
457 /* Scan the draft file for an attachment header field name. */
458 while (get_line() != EOF && *field != '\0' && *field != '-') {
459 if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 &&
460 field[strlen(sign_hdr)] == ':') {
463 if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 &&
464 field[strlen(enc_hdr)] == ':') {
468 if (!dosign && !doenc) {
472 strcpy(composition_file_name, draft_file_name);
474 /* We're ready to roll! */
476 ret = execprogl("mhsign", "mhsign", "-m", "-e",
477 draft_file_name, (char *)NULL);
479 ret = execprogl("mhsign", "mhsign", "-m",
480 draft_file_name, (char *)NULL);
486 /* Remove the automatically created backup of mhsign. */
487 snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
488 if (unlink(buf) == -1) {
489 advise(NULL, "unable to remove original draft file.");
496 clean_up_temporary_files(void)
498 unlink(body_file_name);
499 unlink(composition_file_name);
507 int c; /* current character */
508 int n; /* number of bytes in buffer */
512 ** Get a line from the input file, growing the field buffer as
513 ** needed. We do this so that we can fit an entire line in the
514 ** buffer making it easy to do a string comparison on both the
515 ** field name and the field body which might be a long path name.
517 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
518 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
519 ungetc(c, draft_file);
523 if (++n >= field_size - 1) {
524 field = mh_xrealloc(field, field_size += 256);
534 make_mime_composition_file_entry(char *file_name)
539 char content_type[BUFSIZ];
541 char *cmd = mimetypequeryproc;
544 for (np = m_defs; np; np = np->n_next) {
545 if (strcasecmp(np->n_name, mimetypequery)==0) {
550 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
552 if (!(fp = popen(cmdbuf, "r"))) {
553 clean_up_temporary_files();
554 adios(EX_IOERR, NULL, "unable to determine content type with `%s'",
557 if (fgets(content_type, sizeof content_type, fp) &&
558 (cp = strrchr(content_type, '\n'))) {
561 strcpy(content_type, "application/octet-stream");
562 admonish(NULL, "problems with `%s', using fall back type `%s'",
563 cmdbuf, content_type);
567 /* TODO: don't use access(2) because it checks for ruid, not euid */
568 if (access(file_name, R_OK) != 0) {
569 clean_up_temporary_files();
570 adios(EX_IOERR, NULL, "unable to access file `%s'", file_name);
573 /* Check for broken file(1). See man page mh-profile(5). */
574 for (cp=content_type; *cp; cp++) {
577 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);
579 } else if (*cp == ';') {
586 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
587 fprintf(composition_file,
588 "#%s; name=\"%s\" <> [%s] {attachment} %s\n",
589 content_type, cp, cp, file_name);
595 ** The back-end of the message sending back-end.
596 ** Annotate original message, and call `spost' to send message.
599 sendaux(char **vec, int vecp, char *drft, struct stat *st)
606 if (distfile && distout(drft, distfile, backup) == NOTOK) {
611 switch (child_id = fork()) {
613 /* oops -- fork error */
614 advise("fork", "unable to");
618 /* child process -- send it */
620 fprintf(stderr, "unable to exec ");
623 break; /* NOT REACHED */
626 /* parent process -- wait for it */
627 status = pidwait(child_id, NOTOK);
628 if (WIFEXITED(status) && WEXITSTATUS(status) == EX_OK) {
634 advise(NULL, "%s", strexit(status));
637 if (rename(backup, drft) == NOTOK) {
638 advise(drft, "unable to rename %s to",
645 return status ? NOTOK : status;
650 anno(struct stat *st)
659 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
660 st->st_mtime != st2.st_mtime ||
661 st->st_dev != st2.st_dev ||
662 st->st_ino != st2.st_ino)) {
664 admonish(NULL, "$mhaltmsg mismatch");
669 if (!(folder = getenv("mhfolder")) || !*folder) {
671 admonish(NULL, "$mhfolder not set");
675 if (!(msgs = getenv("mhmessages")) || !*msgs) {
677 admonish(NULL, "$mhmessages not set");
682 advise(NULL, "annotate as `%s': %s %s", annotext,
685 vec[vecp++] = "anno";
686 vec[vecp++] = "-comp";
687 vec[vecp++] = annotext;
688 snprintf(buf, sizeof buf, "+%s", folder);
691 while (isspace(*msgs)) {
694 for (cp=dp=msgs; *cp; cp++) {
696 while (isspace(*cp)) {
705 if (execprog(*vec, vec) != 0) {
706 advise(NULL, "unable to annotate");
714 if (WIFSIGNALED(status)) {
715 return "spost or sendmail killed by signal";
717 if (!WIFEXITED(status)) {
718 return "message not delivered to anyone";
720 switch (WEXITSTATUS(status)) {
722 return "Temporary error, maybe the MTA has queued the message";
724 return "message not delivered to anyone";