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);
343 if (!(draft_file = fopen(draft_file_name, "r"))) {
344 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
347 /* We'll grow the buffer as needed. */
348 field = mh_xcalloc(field_size = 256, sizeof(char));
354 /* Make names for the temporary files. */
355 strncpy(body_file_name,
356 m_mktemp(toabsdir(invo_name), NULL, NULL),
357 sizeof (body_file_name));
358 strncpy(composition_file_name,
359 m_mktemp(toabsdir(invo_name), NULL, NULL),
360 sizeof (composition_file_name));
362 body_file = fopen(body_file_name, "w");
363 composition_file = fopen(composition_file_name, "w");
365 if (!body_file || !composition_file) {
366 clean_up_temporary_files();
367 adios(EX_IOERR, NULL, "unable to open all of the temporary files.");
370 /* Copy non-attachment header fields to the temp composition file. */
372 while (get_line() != EOF && *field && *field != '-') {
373 if (strncasecmp(field, VRSN_FIELD, strlen(VRSN_FIELD))==0 &&
374 field[strlen(VRSN_FIELD)] == ':') {
376 ** The draft is already in MIME format, thus
377 ** back out and use the original draft file.
379 clean_up_temporary_files();
383 if (strncasecmp(field, attach_hdr, length) != 0 ||
384 field[length] != ':') {
385 fprintf(composition_file, "%s\n", field);
388 fputs("--------\n", composition_file);
390 /* Copy the message body to the temporary file. */
391 while ((c = getc(draft_file)) != EOF) {
396 /* Add a mhbuild MIME composition file line for the body */
397 /* charset will be discovered/guessed by mhbuild */
398 fprintf(composition_file, "#text/plain %s\n", body_file_name);
401 ** Now, go back to the beginning of the draft file and look for
402 ** header fields that specify attachments. Add a mhbuild MIME
403 ** composition file for each.
406 while (get_line() != EOF && *field && *field != '-') {
407 if (strncasecmp(field, attach_hdr, length) == 0 &&
408 field[length] == ':') {
409 p = trim(field+length+1);
411 /* forwarded message */
412 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
414 /* regular attachment */
415 make_mime_composition_file_entry(p);
419 fclose(composition_file);
421 /* We're ready to roll! */
422 if (execprogl("mhbuild", "mhbuild", composition_file_name,
423 (char *)NULL) != 0) {
425 clean_up_temporary_files();
428 /* Remove the automatically created backup of mhbuild. */
429 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
430 if (unlink(buf) == -1) {
431 advise(NULL, "unable to remove original composition file.");
438 signandenc(char *draft_file_name)
449 if (!(draft_file = fopen(draft_file_name, "r"))) {
450 adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
453 /* We'll grow the buffer as needed. */
454 field = mh_xcalloc(field_size = 256, sizeof(char));
456 /* Scan the draft file for an attachment header field name. */
457 while (get_line() != EOF && *field != '\0' && *field != '-') {
458 if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 &&
459 field[strlen(sign_hdr)] == ':') {
462 if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 &&
463 field[strlen(enc_hdr)] == ':') {
467 if (!dosign && !doenc) {
471 strcpy(composition_file_name, draft_file_name);
473 /* We're ready to roll! */
475 ret = execprogl("mhsign", "mhsign", "-m", "-e",
476 draft_file_name, (char *)NULL);
478 ret = execprogl("mhsign", "mhsign", "-m",
479 draft_file_name, (char *)NULL);
485 /* Remove the automatically created backup of mhsign. */
486 snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
487 if (unlink(buf) == -1) {
488 advise(NULL, "unable to remove original draft file.");
495 clean_up_temporary_files(void)
497 unlink(body_file_name);
498 unlink(composition_file_name);
506 int c; /* current character */
507 int n; /* number of bytes in buffer */
511 ** Get a line from the input file, growing the field buffer as
512 ** needed. We do this so that we can fit an entire line in the
513 ** buffer making it easy to do a string comparison on both the
514 ** field name and the field body which might be a long path name.
516 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
517 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
518 ungetc(c, draft_file);
522 if (++n >= field_size - 1) {
523 field = mh_xrealloc(field, field_size += 256);
533 make_mime_composition_file_entry(char *file_name)
538 char content_type[BUFSIZ];
540 char *cmd = mimetypequeryproc;
543 for (np = m_defs; np; np = np->n_next) {
544 if (strcasecmp(np->n_name, mimetypequery)==0) {
549 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
551 if (!(fp = popen(cmdbuf, "r"))) {
552 clean_up_temporary_files();
553 adios(EX_IOERR, NULL, "unable to determine content type with `%s'",
556 if (fgets(content_type, sizeof content_type, fp) &&
557 (cp = strrchr(content_type, '\n'))) {
560 strcpy(content_type, "application/octet-stream");
561 admonish(NULL, "problems with `%s', using fall back type `%s'",
562 cmdbuf, content_type);
566 /* TODO: don't use access(2) because it checks for ruid, not euid */
567 if (access(file_name, R_OK) != 0) {
568 clean_up_temporary_files();
569 adios(EX_IOERR, NULL, "unable to access file `%s'", file_name);
572 /* Check for broken file(1). See man page mh-profile(5). */
573 for (cp=content_type; *cp; cp++) {
576 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);
578 } else if (*cp == ';') {
585 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
586 fprintf(composition_file,
587 "#%s; name=\"%s\" <> [%s] {attachment} %s\n",
588 content_type, cp, cp, file_name);
594 ** The back-end of the message sending back-end.
595 ** Annotate original message, and call `spost' to send message.
598 sendaux(char **vec, int vecp, char *drft, struct stat *st)
605 if (distfile && distout(drft, distfile, backup) == NOTOK) {
610 switch (child_id = fork()) {
612 /* oops -- fork error */
613 advise("fork", "unable to");
617 /* child process -- send it */
619 fprintf(stderr, "unable to exec ");
622 break; /* NOT REACHED */
625 /* parent process -- wait for it */
626 status = pidwait(child_id, NOTOK);
627 if (WIFEXITED(status) && WEXITSTATUS(status) == EX_OK) {
633 advise(NULL, "%s", strexit(status));
636 if (rename(backup, drft) == NOTOK) {
637 advise(drft, "unable to rename %s to",
644 return status ? NOTOK : status;
649 anno(struct stat *st)
658 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
659 st->st_mtime != st2.st_mtime ||
660 st->st_dev != st2.st_dev ||
661 st->st_ino != st2.st_ino)) {
663 admonish(NULL, "$mhaltmsg mismatch");
668 if (!(folder = getenv("mhfolder")) || !*folder) {
670 admonish(NULL, "$mhfolder not set");
674 if (!(msgs = getenv("mhmessages")) || !*msgs) {
676 admonish(NULL, "$mhmessages not set");
681 advise(NULL, "annotate as `%s': %s %s", annotext,
684 vec[vecp++] = "anno";
685 vec[vecp++] = "-comp";
686 vec[vecp++] = annotext;
687 snprintf(buf, sizeof buf, "+%s", folder);
690 while (isspace(*msgs)) {
693 for (cp=dp=msgs; *cp; cp++) {
695 while (isspace(*cp)) {
704 if (execprog(*vec, vec) != 0) {
705 advise(NULL, "unable to annotate");
713 if (WIFSIGNALED(status)) {
714 return "spost or sendmail killed by signal";
716 if (!WIFEXITED(status)) {
717 return "message not delivered to anyone";
719 switch (WEXITSTATUS(status)) {
721 return "Temporary error, maybe the MTA has queued the message";
723 return "message not delivered to anyone";