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>
19 #ifdef HAVE_SYS_TIME_H
20 # include <sys/time.h>
24 int debugsw = 0; /* global */
26 char *annotext = NULL;
27 char *distfile = NULL;
31 /* name of temp file for body content */
32 static char body_file_name[MAXPATHLEN + 1];
33 /* name of mhbuild composition temporary file */
34 static char composition_file_name[MAXPATHLEN + 1];
35 static int field_size; /* size of header field buffer */
36 static char *field; /* header field buffer */
37 static FILE *draft_file; /* draft file pointer */
38 static FILE *body_file; /* body file pointer */
39 static FILE *composition_file; /* composition file pointer */
44 static int sendsbr(char **, int, char *, struct stat *);
45 static void armed_done(int) NORETURN;
46 static void anno(struct stat *);
47 static int sendaux(char **, int, char *, struct stat *);
48 static int attach(char *);
49 static int signandenc(char *);
50 static void clean_up_temporary_files(void);
51 static int get_line(void);
52 static void make_mime_composition_file_entry(char *);
55 static struct swit switches[] = {
71 main(int argc, char **argv)
73 int msgp = 0, nfiles = 0, distsw = 0, vecp = 1;
77 char *cp, *maildir = NULL;
78 char buf[BUFSIZ], **argp, **arguments;
79 char *msgs[MAXARGS], *vec[MAXARGS];
85 setlocale(LC_ALL, "");
86 invo_name = mhbasename(argv[0]);
88 /* read user profile/context */
91 arguments = getarguments(invo_name, argc, argv, 1);
94 while ((cp = *argp++)) {
96 switch (smatch(++cp, switches)) {
98 ambigsw(cp, switches);
101 adios(NULL, "-%s unknown\n", cp);
104 snprintf(buf, sizeof(buf),
105 "%s [file] [switches]",
107 print_help(buf, switches, 1);
110 print_version(invo_name);
123 files[nfiles++] = cp;
130 if (!msgp && !nfiles) {
131 msgs[msgp++] = seq_cur;
133 maildir = toabsdir(draftfolder);
135 if (chdir(maildir) == NOTOK) {
136 adios(maildir, "unable to change directory to");
139 if (!(mp = folder_read(draftfolder))) {
140 adios(NULL, "unable to read draft folder %s", draftfolder);
142 if (mp->nummsg == 0) {
143 adios(NULL, "no messages in draft folder %s", draftfolder);
145 /* parse all the message ranges/sequences and set SELECTED */
146 for (msgnum = 0; msgnum < msgp; msgnum++) {
147 if (!m_convert(mp, msgs[msgnum])) {
153 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
154 if (is_selected(mp, msgnum)) {
155 files[nfiles++] = getcpy(m_name(msgnum));
156 unset_exists(mp, msgnum);
160 mp->msgflags |= SEQMOD;
163 if (!(cp = getenv("SIGNATURE")) || !*cp) {
164 if ((cp = context_find("signature")) && *cp) {
165 m_putenv("SIGNATURE", cp);
169 for (msgnum = 0; msgnum < nfiles; msgnum++) {
170 if (stat(files[msgnum], &st) == NOTOK) {
171 adios(files[msgnum], "unable to stat draft file");
175 if (!(annotext = getenv("mhannotate")) || !*annotext) {
178 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
179 altmsg = NULL; /* used by dist interface - see below */
182 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
183 vec[vecp++] = "-dist";
184 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
185 adios(altmsg, "unable to open for reading");
188 distfile = getcpy(m_mktemp2(NULL, invo_name, NULL, NULL));
189 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
190 adios(distfile, "unable to open for writing");
192 cpydata(in, out, altmsg, distfile);
199 if (!altmsg || stat(altmsg, &st) == NOTOK) {
206 for (n=3; n<OPEN_MAX; n++) {
210 for (msgnum = 0; msgnum < nfiles; msgnum++) {
211 switch (sendsbr(vec, vecp, files[msgnum], &st)) {
228 ** message sending back-end
231 sendsbr(char **vec, int vecp, char *drft, struct stat *st)
235 char *original_draft;
238 ** Save the original name of the draft file. The name of the
239 ** draft file is changed to a temporary file containing the built
240 ** MIME message if there are attachments. We need the original
241 ** name so that it can be renamed after the message is sent.
243 original_draft = drft;
246 ** Convert the draft to a MIME message.
247 ** Use the mhbuild composition file for the draft if there was
248 ** a successful conversion because that now contains the MIME
249 ** message. A nice side effect of this is that it leaves the
250 ** original draft file untouched so that it can be retrieved
251 ** and modified if desired.
253 switch (attach(drft)) {
255 drft = composition_file_name;
266 ** Sign and encrypt the message as needed.
267 ** Use the mhbuild composition file for the draft if there was
268 ** a successful conversion because that now contains the MIME
269 ** message. A nice side effect of this is that it leaves the
270 ** original draft file untouched so that it can be retrieved
271 ** and modified if desired.
273 switch (signandenc(drft)) {
275 drft = composition_file_name;
286 switch (setjmp(env)) {
288 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
290 /* move original draft to +trash folder */
291 snprintf(buffer, sizeof buffer,
292 "</dev/null refile -file %s +trash",
294 if (system(buffer) != 0) {
295 advise(NULL, "unable to trash the draft");
310 /* Get rid of temporary files that we created for attachments. */
311 if (drft == composition_file_name) {
312 clean_up_temporary_files();
319 attach(char *draft_file_name)
321 char buf[MAXPATHLEN + 6];
325 int non_ascii; /* msg body contains non-ASCII chars */
326 int length; /* length of attachment header field name */
329 if (!(draft_file = fopen(draft_file_name, "r"))) {
330 adios(NULL, "can't open draft file `%s'.", draft_file_name);
333 /* We'll grow the buffer as needed. */
334 field = (char *)mh_xmalloc(field_size = 256);
337 ** Scan the draft file for an attachment header field name.
339 length = strlen(attach_hdr);
341 while (get_line() != EOF && *field != '\0' && *field != '-') {
342 if (strncasecmp(field, attach_hdr, length)==0 &&
343 field[length] == ':') {
349 ** Look for at least one non-blank line in the body of the
350 ** message which indicates content in the body.
351 ** Check if body contains at least one non-blank (= not empty)
352 ** and if it contains any non-ASCII chars (= need MIME).
356 while (get_line() != EOF) {
357 for (p = field; *p != '\0'; p++) {
358 if (*p != ' ' && *p != '\t') {
361 if (*p > 127 || *p < 0) {
365 if (has_body && non_ascii) {
366 break; /* that's been already enough information */
370 if (!has_attachment && non_ascii==0) {
371 /* We don't need to convert it to MIME. */
379 /* Make names for the temporary files. */
380 strncpy(body_file_name,
381 m_mktemp(toabsdir(invo_name), NULL, NULL),
382 sizeof (body_file_name));
383 strncpy(composition_file_name,
384 m_mktemp(toabsdir(invo_name), NULL, NULL),
385 sizeof (composition_file_name));
388 body_file = fopen(body_file_name, "w");
390 composition_file = fopen(composition_file_name, "w");
392 if ((has_body && !body_file) || !composition_file) {
393 clean_up_temporary_files();
394 adios(NULL, "unable to open all of the temporary files.");
397 /* Copy non-attachment header fields to the temp composition file. */
399 while (get_line() != EOF && *field && *field != '-') {
400 if (strncasecmp(field, attach_hdr, length) != 0 ||
401 field[length] != ':') {
402 fprintf(composition_file, "%s\n", field);
405 fputs("--------\n", composition_file);
408 /* Copy the message body to the temporary file. */
409 while ((c = getc(draft_file)) != EOF) {
414 /* Add a mhbuild MIME composition file line for the body */
415 /* charset will be discovered/guessed by mhbuild */
416 fprintf(composition_file, "#text/plain %s\n", body_file_name);
420 ** Now, go back to the beginning of the draft file and look for
421 ** header fields that specify attachments. Add a mhbuild MIME
422 ** composition file for each.
425 while (get_line() != EOF && *field && *field != '-') {
426 if (strncasecmp(field, attach_hdr, length) == 0 &&
427 field[length] == ':') {
428 p = trim(field+length+1);
430 /* forwarded message */
431 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
433 /* regular attachment */
434 make_mime_composition_file_entry(p);
438 fclose(composition_file);
440 /* We're ready to roll! */
441 sprintf(buf, "mhbuild %s", composition_file_name);
442 if (system(buf) != 0) {
444 clean_up_temporary_files();
447 /* Remove the automatically created backup of mhbuild. */
448 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
449 if (unlink(buf) == -1) {
450 advise(NULL, "unable to remove original composition file.");
457 signandenc(char *draft_file_name)
463 if (!(draft_file = fopen(draft_file_name, "r"))) {
464 adios(NULL, "can't open draft file `%s'.", draft_file_name);
467 /* We'll grow the buffer as needed. */
468 field = (char *)mh_xmalloc(field_size = 256);
470 /* Scan the draft file for an attachment header field name. */
471 while (get_line() != EOF && *field != '\0' && *field != '-') {
472 if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 &&
473 field[strlen(sign_hdr)] == ':') {
476 if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 &&
477 field[strlen(enc_hdr)] == ':') {
481 if (!dosign && !doenc) {
485 strcpy(composition_file_name, draft_file_name);
487 /* We're ready to roll! */
488 sprintf(buf, "mhsign -m%s '%s'", doenc ? " -e" : "",
490 if (system(buf) != 0) {
494 /* Remove the automatically created backup of mhsign. */
495 snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
496 if (unlink(buf) == -1) {
497 advise(NULL, "unable to remove original draft file.");
504 clean_up_temporary_files(void)
506 unlink(body_file_name);
507 unlink(composition_file_name);
515 int c; /* current character */
516 int n; /* number of bytes in buffer */
520 ** Get a line from the input file, growing the field buffer as
521 ** needed. We do this so that we can fit an entire line in the
522 ** buffer making it easy to do a string comparison on both the
523 ** field name and the field body which might be a long path name.
525 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
526 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
527 ungetc(c, draft_file);
531 if (++n >= field_size - 1) {
532 field = (char *)mh_xrealloc(field, field_size += 256);
542 make_mime_composition_file_entry(char *file_name)
547 char content_type[BUFSIZ];
549 char *cmd = mimetypequeryproc;
552 for (np = m_defs; np; np = np->n_next) {
553 if (strcasecmp(np->n_name, mimetypequery)==0) {
558 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
560 if (!(fp = popen(cmdbuf, "r"))) {
561 clean_up_temporary_files();
562 adios(NULL, "unable to determine content type with `%s'",
565 if (fgets(content_type, sizeof content_type, fp) &&
566 (cp = strrchr(content_type, '\n'))) {
569 strcpy(content_type, "application/octet-stream");
570 admonish(NULL, "problems with `%s', using fall back type `%s'",
571 cmdbuf, content_type);
575 /* TODO: don't use access(2) because it checks for ruid, not euid */
576 if (access(file_name, R_OK) != 0) {
577 clean_up_temporary_files();
578 adios(NULL, "unable to access file `%s'", file_name);
581 /* Check for broken file(1). See man page mh-profile(5). */
582 for (cp=content_type; *cp; cp++) {
585 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);
587 } else if (*cp == ';') {
594 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
595 fprintf(composition_file,
596 "#%s; name=\"%s\" <> [%s] {attachment} %s\n",
597 content_type, cp, cp, file_name);
603 ** The back-end of the message sending back-end.
604 ** Annotate original message, and call `spost' to send message.
607 sendaux(char **vec, int vecp, char *drft, struct stat *st)
614 if (distfile && distout(drft, distfile, backup) == NOTOK) {
619 switch (child_id = fork()) {
621 /* oops -- fork error */
622 adios("fork", "unable to");
623 break; /* NOT REACHED */
626 /* child process -- send it */
628 fprintf(stderr, "unable to exec ");
631 break; /* NOT REACHED */
634 /* parent process -- wait for it */
635 if ((status = pidwait(child_id, NOTOK)) == OK) {
641 advise(NULL, "message not delivered to anyone");
644 if (rename(backup, drft) == NOTOK) {
645 advise(drft, "unable to rename %s to",
658 anno(struct stat *st)
664 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
665 st->st_mtime != st2.st_mtime ||
666 st->st_dev != st2.st_dev ||
667 st->st_ino != st2.st_ino)) {
669 admonish(NULL, "$mhaltmsg mismatch");
674 if (!(folder = getenv("mhfolder")) || !*folder) {
676 admonish(NULL, "$mhfolder not set");
680 if (!(msgs = getenv("mhmessages")) || !*msgs) {
682 admonish(NULL, "$mhmessages not set");
687 advise(NULL, "annotate as `%s': %s %s", annotext,
690 snprintf(buf, sizeof buf, "anno -comp '%s' '+%s' %s",
691 annotext, folder, msgs);
692 if (system(buf) != 0) {
693 advise(NULL, "unable to annotate");
699 armed_done(int status)
701 longjmp(env, status ? status : NOTOK);