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 for (p = field+length+1; *p==' ' || *p=='\t'; p++) {
432 /* forwarded message */
433 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
435 /* regular attachment */
436 make_mime_composition_file_entry(p);
440 fclose(composition_file);
442 /* We're ready to roll! */
443 sprintf(buf, "mhbuild %s", composition_file_name);
444 if (system(buf) != 0) {
446 clean_up_temporary_files();
449 /* Remove the automatically created backup of mhbuild. */
450 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
451 if (unlink(buf) == -1) {
452 advise(NULL, "unable to remove original composition file.");
459 signandenc(char *draft_file_name)
465 if (!(draft_file = fopen(draft_file_name, "r"))) {
466 adios(NULL, "can't open draft file `%s'.", draft_file_name);
469 /* We'll grow the buffer as needed. */
470 field = (char *)mh_xmalloc(field_size = 256);
472 /* Scan the draft file for an attachment header field name. */
473 while (get_line() != EOF && *field != '\0' && *field != '-') {
474 if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 &&
475 field[strlen(sign_hdr)] == ':') {
478 if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 &&
479 field[strlen(enc_hdr)] == ':') {
483 if (!dosign && !doenc) {
487 strcpy(composition_file_name, draft_file_name);
489 /* We're ready to roll! */
490 sprintf(buf, "mhsign -m%s '%s'", doenc ? " -e" : "",
492 if (system(buf) != 0) {
496 /* Remove the automatically created backup of mhsign. */
497 snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
498 if (unlink(buf) == -1) {
499 advise(NULL, "unable to remove original draft file.");
506 clean_up_temporary_files(void)
508 unlink(body_file_name);
509 unlink(composition_file_name);
517 int c; /* current character */
518 int n; /* number of bytes in buffer */
522 ** Get a line from the input file, growing the field buffer as
523 ** needed. We do this so that we can fit an entire line in the
524 ** buffer making it easy to do a string comparison on both the
525 ** field name and the field body which might be a long path name.
527 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
528 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
529 ungetc(c, draft_file);
533 if (++n >= field_size - 1) {
534 field = (char *)mh_xrealloc(field, field_size += 256);
544 make_mime_composition_file_entry(char *file_name)
549 char content_type[BUFSIZ];
551 char *cmd = mimetypequeryproc;
553 for (np = m_defs; np; np = np->n_next) {
554 if (strcasecmp(np->n_name, mimetypequery)==0) {
559 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
561 if (!(fp = popen(cmdbuf, "r"))) {
562 clean_up_temporary_files();
563 adios(NULL, "unable to determine content type with `%s'",
566 if (fgets(content_type, sizeof content_type, fp) &&
567 (cp = strrchr(content_type, '\n'))) {
570 strcpy(content_type, "application/octet-stream");
571 admonish(NULL, "problems with `%s', using fall back type `%s'",
572 cmdbuf, content_type);
576 /* TODO: don't use access(2) because it checks for ruid, not euid */
577 if (access(file_name, R_OK) != 0) {
578 clean_up_temporary_files();
579 adios(NULL, "unable to access file `%s'", file_name);
582 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
583 fprintf(composition_file, "#%s; name=\"%s\" <> [%s] {attachment}",
584 content_type, cp, cp);
586 fprintf(composition_file, " %s\n", file_name);
592 ** The back-end of the message sending back-end.
593 ** Annotate original message, and call `spost' to send message.
596 sendaux(char **vec, int vecp, char *drft, struct stat *st)
603 if (distfile && distout(drft, distfile, backup) == NOTOK) {
608 switch (child_id = fork()) {
610 /* oops -- fork error */
611 adios("fork", "unable to");
612 break; /* NOT REACHED */
615 /* child process -- send it */
617 fprintf(stderr, "unable to exec ");
620 break; /* NOT REACHED */
623 /* parent process -- wait for it */
624 if ((status = pidwait(child_id, NOTOK)) == OK) {
630 advise(NULL, "message not delivered to anyone");
633 if (rename(backup, drft) == NOTOK) {
634 advise(drft, "unable to rename %s to",
647 anno(struct stat *st)
653 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
654 st->st_mtime != st2.st_mtime ||
655 st->st_dev != st2.st_dev ||
656 st->st_ino != st2.st_ino)) {
658 admonish(NULL, "$mhaltmsg mismatch");
663 if (!(folder = getenv("mhfolder")) || !*folder) {
665 admonish(NULL, "$mhfolder not set");
669 if (!(msgs = getenv("mhmessages")) || !*msgs) {
671 admonish(NULL, "$mhmessages not set");
676 advise(NULL, "annotate as `%s': %s %s", annotext,
679 snprintf(buf, sizeof buf, "anno -comp '%s' '+%s' %s",
680 annotext, folder, msgs);
681 if (system(buf) != 0) {
682 advise(NULL, "unable to annotate");
688 armed_done(int status)
690 longjmp(env, status ? status : NOTOK);