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 nmsgs = 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 (!nmsgs && !nfiles) {
131 msgs[nmsgs++] = seq_cur;
135 maildir = toabsdir(draftfolder);
136 if (chdir(maildir) == NOTOK) {
137 adios(maildir, "unable to change directory to");
139 if (!(mp = folder_read(draftfolder))) {
140 adios(NULL, "unable to read draft folder %s",
143 if (mp->nummsg == 0) {
144 adios(NULL, "no messages in draft folder %s",
147 /* parse all the msgranges/sequences and set SELECTED */
148 for (msgnum = 0; msgnum < nmsgs; msgnum++) {
149 if (!m_convert(mp, msgs[msgnum])) {
155 for (nmsgs = 0, msgnum = mp->lowsel;
156 msgnum <= mp->hghsel; msgnum++) {
157 if (is_selected(mp, msgnum)) {
158 files[nfiles++] = getcpy(m_name(msgnum));
159 unset_exists(mp, msgnum);
163 mp->msgflags |= SEQMOD;
167 if (!(cp = getenv("SIGNATURE")) || !*cp) {
168 if ((cp = context_find("signature")) && *cp) {
169 m_putenv("SIGNATURE", cp);
173 for (n = 0; n < nfiles; n++) {
174 if (stat(files[n], &st) == NOTOK) {
175 adios(files[n], "unable to stat draft file");
179 if (!(annotext = getenv("mhannotate")) || !*annotext) {
182 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
183 altmsg = NULL; /* used by dist interface - see below */
186 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
187 vec[vecp++] = "-dist";
188 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
189 adios(altmsg, "unable to open for reading");
192 distfile = getcpy(m_mktemp2(NULL, invo_name, NULL, NULL));
193 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
194 adios(distfile, "unable to open for writing");
196 cpydata(in, out, altmsg, distfile);
203 if (!altmsg || stat(altmsg, &st) == NOTOK) {
210 for (n=3; n<OPEN_MAX; n++) {
214 for (n = 0; n < nfiles; n++) {
215 switch (sendsbr(vec, vecp, files[n], &st)) {
232 ** message sending back-end
235 sendsbr(char **vec, int vecp, char *drft, struct stat *st)
239 char *original_draft;
242 ** Save the original name of the draft file. The name of the
243 ** draft file is changed to a temporary file containing the built
244 ** MIME message if there are attachments. We need the original
245 ** name so that it can be renamed after the message is sent.
247 original_draft = drft;
250 ** Convert the draft to a MIME message.
251 ** Use the mhbuild composition file for the draft if there was
252 ** a successful conversion because that now contains the MIME
253 ** message. A nice side effect of this is that it leaves the
254 ** original draft file untouched so that it can be retrieved
255 ** and modified if desired.
257 switch (attach(drft)) {
259 drft = composition_file_name;
270 ** Sign and encrypt the message as needed.
271 ** Use the mhbuild composition file for the draft if there was
272 ** a successful conversion because that now contains the MIME
273 ** message. A nice side effect of this is that it leaves the
274 ** original draft file untouched so that it can be retrieved
275 ** and modified if desired.
277 switch (signandenc(drft)) {
279 drft = composition_file_name;
290 switch (setjmp(env)) {
292 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
294 /* move original draft to +trash folder */
295 snprintf(buffer, sizeof buffer,
296 "</dev/null refile -file %s +trash",
298 if (system(buffer) != 0) {
299 advise(NULL, "unable to trash the draft");
314 /* Get rid of temporary files that we created for attachments. */
315 if (drft == composition_file_name) {
316 clean_up_temporary_files();
323 attach(char *draft_file_name)
325 char buf[MAXPATHLEN + 6];
329 int non_ascii; /* msg body contains non-ASCII chars */
330 int length; /* length of attachment header field name */
333 if (!(draft_file = fopen(draft_file_name, "r"))) {
334 adios(NULL, "can't open draft file `%s'.", draft_file_name);
337 /* We'll grow the buffer as needed. */
338 field = (char *)mh_xmalloc(field_size = 256);
341 ** Scan the draft file for an attachment header field name.
343 length = strlen(attach_hdr);
345 while (get_line() != EOF && *field != '\0' && *field != '-') {
346 if (strncasecmp(field, attach_hdr, length)==0 &&
347 field[length] == ':') {
353 ** Look for at least one non-blank line in the body of the
354 ** message which indicates content in the body.
355 ** Check if body contains at least one non-blank (= not empty)
356 ** and if it contains any non-ASCII chars (= need MIME).
360 while (get_line() != EOF) {
361 for (p = field; *p != '\0'; p++) {
362 if (*p != ' ' && *p != '\t') {
365 if (*p > 127 || *p < 0) {
369 if (has_body && non_ascii) {
370 break; /* that's been already enough information */
374 if (!has_attachment && non_ascii==0) {
375 /* We don't need to convert it to MIME. */
383 /* Make names for the temporary files. */
384 strncpy(body_file_name,
385 m_mktemp(toabsdir(invo_name), NULL, NULL),
386 sizeof (body_file_name));
387 strncpy(composition_file_name,
388 m_mktemp(toabsdir(invo_name), NULL, NULL),
389 sizeof (composition_file_name));
392 body_file = fopen(body_file_name, "w");
394 composition_file = fopen(composition_file_name, "w");
396 if ((has_body && !body_file) || !composition_file) {
397 clean_up_temporary_files();
398 adios(NULL, "unable to open all of the temporary files.");
401 /* Copy non-attachment header fields to the temp composition file. */
403 while (get_line() != EOF && *field && *field != '-') {
404 if (strncasecmp(field, attach_hdr, length) != 0 ||
405 field[length] != ':') {
406 fprintf(composition_file, "%s\n", field);
409 fputs("--------\n", composition_file);
412 /* Copy the message body to the temporary file. */
413 while ((c = getc(draft_file)) != EOF) {
418 /* Add a mhbuild MIME composition file line for the body */
419 /* charset will be discovered/guessed by mhbuild */
420 fprintf(composition_file, "#text/plain %s\n", body_file_name);
424 ** Now, go back to the beginning of the draft file and look for
425 ** header fields that specify attachments. Add a mhbuild MIME
426 ** composition file for each.
429 while (get_line() != EOF && *field && *field != '-') {
430 if (strncasecmp(field, attach_hdr, length) == 0 &&
431 field[length] == ':') {
432 p = trim(field+length+1);
434 /* forwarded message */
435 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
437 /* regular attachment */
438 make_mime_composition_file_entry(p);
442 fclose(composition_file);
444 /* We're ready to roll! */
445 sprintf(buf, "mhbuild %s", composition_file_name);
446 if (system(buf) != 0) {
448 clean_up_temporary_files();
451 /* Remove the automatically created backup of mhbuild. */
452 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
453 if (unlink(buf) == -1) {
454 advise(NULL, "unable to remove original composition file.");
461 signandenc(char *draft_file_name)
467 if (!(draft_file = fopen(draft_file_name, "r"))) {
468 adios(NULL, "can't open draft file `%s'.", draft_file_name);
471 /* We'll grow the buffer as needed. */
472 field = (char *)mh_xmalloc(field_size = 256);
474 /* Scan the draft file for an attachment header field name. */
475 while (get_line() != EOF && *field != '\0' && *field != '-') {
476 if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 &&
477 field[strlen(sign_hdr)] == ':') {
480 if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 &&
481 field[strlen(enc_hdr)] == ':') {
485 if (!dosign && !doenc) {
489 strcpy(composition_file_name, draft_file_name);
491 /* We're ready to roll! */
492 sprintf(buf, "mhsign -m%s '%s'", doenc ? " -e" : "",
494 if (system(buf) != 0) {
498 /* Remove the automatically created backup of mhsign. */
499 snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
500 if (unlink(buf) == -1) {
501 advise(NULL, "unable to remove original draft file.");
508 clean_up_temporary_files(void)
510 unlink(body_file_name);
511 unlink(composition_file_name);
519 int c; /* current character */
520 int n; /* number of bytes in buffer */
524 ** Get a line from the input file, growing the field buffer as
525 ** needed. We do this so that we can fit an entire line in the
526 ** buffer making it easy to do a string comparison on both the
527 ** field name and the field body which might be a long path name.
529 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
530 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
531 ungetc(c, draft_file);
535 if (++n >= field_size - 1) {
536 field = (char *)mh_xrealloc(field, field_size += 256);
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(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(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(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 adios("fork", "unable to");
627 break; /* NOT REACHED */
630 /* child process -- send it */
632 fprintf(stderr, "unable to exec ");
635 break; /* NOT REACHED */
638 /* parent process -- wait for it */
639 if ((status = pidwait(child_id, NOTOK)) == OK) {
645 advise(NULL, "message not delivered to anyone");
648 if (rename(backup, drft) == NOTOK) {
649 advise(drft, "unable to rename %s to",
662 anno(struct stat *st)
668 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
669 st->st_mtime != st2.st_mtime ||
670 st->st_dev != st2.st_dev ||
671 st->st_ino != st2.st_ino)) {
673 admonish(NULL, "$mhaltmsg mismatch");
678 if (!(folder = getenv("mhfolder")) || !*folder) {
680 admonish(NULL, "$mhfolder not set");
684 if (!(msgs = getenv("mhmessages")) || !*msgs) {
686 admonish(NULL, "$mhmessages not set");
691 advise(NULL, "annotate as `%s': %s %s", annotext,
694 snprintf(buf, sizeof buf, "anno -comp '%s' '+%s' %s",
695 annotext, folder, msgs);
696 if (system(buf) != 0) {
697 advise(NULL, "unable to annotate");
703 armed_done(int status)
705 longjmp(env, status ? status : NOTOK);