2 ** sendsbr.c -- routines to help WhatNow/Send along
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.
10 #include <h/signals.h>
18 #ifdef TIME_WITH_SYS_TIME
19 # include <sys/time.h>
22 # ifdef TM_IN_SYS_TIME
23 # include <sys/time.h>
29 int debugsw = 0; /* global */
35 char *altmsg = NULL; /* .. */
36 char *annotext = NULL;
37 char *distfile = NULL;
41 /* name of temp file for body content */
42 static char body_file_name[MAXPATHLEN + 1];
43 /* name of mhbuild composition temporary file */
44 static char composition_file_name[MAXPATHLEN + 1];
45 static int field_size; /* size of header field buffer */
46 static char *field; /* header field buffer */
47 static FILE *draft_file; /* draft file pointer */
48 static FILE *body_file; /* body file pointer */
49 static FILE *composition_file; /* composition file pointer */
52 ** external prototypes
54 int sendsbr(char **, int, char *, struct stat *, int);
55 char *getusername(void);
60 static void armed_done(int) NORETURN;
61 static void alert(char *, int);
62 static int tmp_fd(void);
63 static void anno(int, struct stat *);
64 static void annoaux(int);
65 static int sendaux(char **, int, char *, struct stat *);
67 static int attach(char *);
68 static void clean_up_temporary_files(void);
69 static int get_line(void);
70 static void make_mime_composition_file_entry(char *);
74 ** Entry point into (back-end) routines to send message.
78 sendsbr(char **vec, int vecp, char *drft, struct stat *st, int rename_drft)
82 char *original_draft; /* name of original draft file */
83 char *p; /* string pointer for building file name */
86 ** Save the original name of the draft file. The name of the
87 ** draft file is changed to a temporary file containing the built
88 ** MIME message if there are attachments. We need the original
89 ** name so that it can be renamed after the message is sent.
92 original_draft = drft;
95 ** Convert the draft to a MIME message.
96 ** Use the mhbuild composition file for the draft if there was
97 ** a successful conversion because that now contains the MIME
98 ** message. A nice side effect of this is that it leaves the
99 ** original draft file untouched so that it can be retrieved
100 ** and modified if desired.
102 switch (attach(drft)) {
104 drft = composition_file_name;
115 switch (setjmp(env)) {
117 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
118 /* rename the original draft */
119 if (rename_drft && status == OK &&
120 rename(original_draft, strncpy(buffer,
121 m_backup(original_draft), sizeof(buffer)))
123 advise(buffer, "unable to rename %s to", drft);
136 ** Get rid of any temporary files that we created for attachments.
137 ** Also get rid of the renamed composition file that mhbuild
138 ** leaves as a turd. It looks confusing, but we use the body
139 ** file name to help build the renamed composition file name.
142 if (drft == composition_file_name) {
143 clean_up_temporary_files();
145 if (strlen(composition_file_name) >=
146 sizeof (composition_file_name) - 6)
147 advise(NULL, "unable to remove original composition file.");
150 if ((p = strrchr(composition_file_name, '/')) == NULL)
151 p = composition_file_name;
155 strcpy(body_file_name, p);
157 strcpy(p, body_file_name);
160 unlink(composition_file_name);
168 attach(char *draft_file_name)
170 char buf[MAXPATHLEN + 6]; /* miscellaneous buffer */
171 int c; /* current character for body copy */
172 int has_attachment; /* draft has at least one attachment */
173 int has_body; /* draft has a message body */
174 int non_ascii; /* msg body contains non-ASCII chars */
175 int length; /* length of attachment header field name */
176 char *p; /* miscellaneous string pointer */
178 /* Open up the draft file. */
179 if ((draft_file = fopen(draft_file_name, "r")) == (FILE *)0)
180 adios(NULL, "can't open draft file `%s'.",
184 ** Allocate a buffer to hold the header components as they're read in.
185 ** This buffer might need to be quite large, so we grow it as needed.
187 field = (char *)mh_xmalloc(field_size = 256);
190 ** Scan the draft file for an attachment header field name.
191 ** The existence of one indicates that the
192 ** draft has attachments. Bail out if there are no attachments
193 ** because we're done. Read to the end of the headers even if
194 ** we have no attachments.
196 length = strlen(attach_hdr);
200 while (get_line() != EOF && *field != '\0' && *field != '-') {
201 if (strncasecmp(field, attach_hdr, length) == 0 &&
202 field[length] == ':') {
208 ** Look for at least one non-blank line in the body of the
209 ** message which indicates content in the body.
210 ** Check if body contains at least one non-blank (= not empty)
211 ** and if it contains any non-ASCII chars (= need MIME).
216 while (get_line() != EOF) {
217 for (p = field; *p != '\0'; p++) {
218 if (*p != ' ' && *p != '\t') {
221 if (*p > 127 || *p < 0) {
225 if (has_body && non_ascii)
230 ** Bail out if there are no attachments and only ASCII text.
231 ** This means we don't need to convert it to MIME.
233 if (!has_attachment && non_ascii==0) {
241 /* Make names for the temporary files. */
242 strncpy(body_file_name,
243 m_mktemp(toabsdir(invo_name), NULL, NULL),
244 sizeof (body_file_name));
245 strncpy(composition_file_name,
246 m_mktemp(toabsdir(invo_name), NULL, NULL),
247 sizeof (composition_file_name));
250 body_file = fopen(body_file_name, "w");
252 composition_file = fopen(composition_file_name, "w");
254 if ((has_body && !body_file) || !composition_file) {
255 clean_up_temporary_files();
256 adios(NULL, "unable to open all of the temporary files.");
260 ** Start at the beginning of the draft file. Copy all
261 ** non-attachment header fields to the temporary composition file.
262 ** Then add the dashed line separator.
265 while (get_line() != EOF && *field && *field != '-') {
266 if (strncasecmp(field, attach_hdr, length) != 0 ||
267 field[length] != ':') {
268 fprintf(composition_file, "%s\n", field);
271 fputs("--------\n", composition_file);
274 /* Copy the message body to the temporary file. */
275 while ((c = getc(draft_file)) != EOF) {
280 /* Add a mhbuild MIME composition file line for the body */
281 /* charset will be discovered/guessed by buildmimeproc */
282 fprintf(composition_file, "#text/plain %s\n", body_file_name);
286 ** Now, go back to the beginning of the draft file and look for
287 ** header fields that specify attachments. Add a mhbuild MIME
288 ** composition file for each.
291 while (get_line() != EOF && *field && *field != '-') {
292 if (strncasecmp(field, attach_hdr, length) == 0 &&
293 field[length] == ':') {
294 for (p = field+length+1; *p==' ' || *p=='\t'; p++) {
298 /* forwarded message */
299 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
301 make_mime_composition_file_entry(p);
306 fclose(composition_file);
309 ** We're ready to roll! Run mhbuild on the composition file.
310 ** Note that mhbuild is in the context as buildmimeproc.
312 sprintf(buf, "%s %s", buildmimeproc, composition_file_name);
314 if (system(buf) != 0) {
315 clean_up_temporary_files();
323 clean_up_temporary_files(void)
325 unlink(body_file_name);
326 unlink(composition_file_name);
334 int c; /* current character */
335 int n; /* number of bytes in buffer */
336 char *p; /* buffer pointer */
339 ** Get a line from the input file, growing the field buffer as
340 ** needed. We do this so that we can fit an entire line in the
341 ** buffer making it easy to do a string comparison on both the
342 ** field name and the field body which might be a long path name.
345 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
346 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
347 ungetc(c, draft_file);
352 if (++n >= field_size - 1) {
353 field = (char *)mh_xrealloc((void *)field, field_size += 256);
359 /* NUL-terminate the field. */
366 make_mime_composition_file_entry(char *file_name)
371 char content_type[BUFSIZ];
373 char *cmd = mimetypequeryproc;
375 for (np = m_defs; np; np = np->n_next) {
376 if (strcasecmp(np->n_name, mimetypequery)==0) {
381 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
383 if (!(fp = popen(cmdbuf, "r"))) {
384 clean_up_temporary_files();
385 adios(NULL, "unable to determine content type with `%s'",
388 if (fgets(content_type, sizeof content_type, fp) &&
389 (cp = strrchr(content_type, '\n'))) {
392 strcpy(content_type, "application/octet-stream");
393 admonish(NULL, "problems with `%s', using fall back type `%s'",
394 cmdbuf, content_type);
398 /* TODO: don't use access(2) because it checks for ruid, not euid */
399 if (access(file_name, R_OK) != 0) {
400 clean_up_temporary_files();
401 adios(NULL, "unable to access file `%s'", file_name);
404 fprintf(composition_file, "#%s; name=\"%s\" <>{attachment}",
406 (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1);
408 fprintf(composition_file, " %s\n", file_name);
414 ** Annotate original message, and
415 ** call `postproc' to send message.
419 sendaux(char **vec, int vecp, char *drft, struct stat *st)
422 int i, status, fd, fd2;
425 fd = pushsw ? tmp_fd() : NOTOK;
429 if (annotext && (fd2 = tmp_fd()) == NOTOK) {
430 admonish(NULL, "unable to create file for annotation list");
432 if (distfile && distout(drft, distfile, backup) == NOTOK)
436 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
441 /* oops -- fork error */
442 adios("fork", "unable to");
443 break; /* NOT REACHED */
447 ** child process -- send it
449 ** If fd is ok, then we are pushing and fd points to temp
450 ** file, so capture anything on stdout and stderr there.
453 dup2(fd, fileno(stdout));
454 dup2(fd, fileno(stderr));
457 execvp(postproc, vec);
458 fprintf(stderr, "unable to exec ");
461 break; /* NOT REACHED */
465 ** parent process -- wait for it
467 if ((status = pidwait(child_id, NOTOK)) == OK) {
468 if (annotext && fd2 != NOTOK)
472 ** If postproc failed, and we have good fd (which
473 ** means we pushed), then mail error message
474 ** (and possibly the draft) back to the user.
480 advise(NULL, "message not delivered to anyone");
482 if (annotext && fd2 != NOTOK)
486 if (rename(backup, drft) == NOTOK)
487 advise(drft, "unable to rename %s to",
499 ** Mail error notification (and possibly a copy of the
500 ** message) back to the user, using the mailproc
504 alert(char *file, int out)
510 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
515 /* oops -- fork error */
516 advise("fork", "unable to");
519 /* child process -- send it */
520 SIGNAL(SIGHUP, SIG_IGN);
521 SIGNAL(SIGINT, SIG_IGN);
522 SIGNAL(SIGQUIT, SIG_IGN);
523 SIGNAL(SIGTERM, SIG_IGN);
525 if ((in = open(file, O_RDONLY)) == NOTOK) {
526 admonish(file, "unable to re-open");
528 lseek(out, (off_t) 0, SEEK_END);
529 strncpy(buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
530 write(out, buf, strlen(buf));
531 strncpy(buf, "\n------- Unsent Draft\n\n", sizeof(buf));
532 write(out, buf, strlen(buf));
533 cpydgst(in, out, file, "temporary file");
535 strncpy(buf, "\n------- End of Unsent Draft\n", sizeof(buf));
536 write(out, buf, strlen(buf));
537 if (rename(file, strncpy(buf, m_backup(file), sizeof(buf))) == NOTOK)
538 admonish(buf, "unable to rename %s to", file);
541 lseek(out, (off_t) 0, SEEK_SET);
542 dup2(out, fileno(stdin));
544 /* create subject for error notification */
545 snprintf(buf, sizeof(buf), "send failed on %s",
546 forwsw ? "enclosed draft" : file);
548 execlp(mailproc, mhbasename(mailproc), getusername(),
549 "-subject", buf, NULL);
550 fprintf(stderr, "unable to exec ");
554 default: /* no waiting... */
566 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
567 if (tfile == NULL) return NOTOK;
571 advise(NULL, "temporary file %s selected", tfile);
573 if (unlink(tfile) == NOTOK)
574 advise(tfile, "unable to remove");
581 anno(int fd, struct stat *st)
585 static char *cwd = NULL;
588 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
589 st->st_mtime != st2.st_mtime ||
590 st->st_dev != st2.st_dev ||
591 st->st_ino != st2.st_ino)) {
593 admonish(NULL, "$mhaltmsg mismatch");
597 child_id = debugsw ? NOTOK : fork();
599 case NOTOK: /* oops */
601 advise(NULL, "unable to fork, so doing annotations by hand...");
606 /* block a few signals */
608 sigaddset(&set, SIGHUP);
609 sigaddset(&set, SIGINT);
610 sigaddset(&set, SIGQUIT);
611 sigaddset(&set, SIGTERM);
612 SIGPROCMASK(SIG_BLOCK, &set, &oset);
618 /* reset the signal mask */
619 SIGPROCMASK(SIG_SETMASK, &oset, &set);
624 default: /* no waiting... */
634 int fd2, fd3, msgnum;
635 char *cp, *folder, *maildir;
636 char buffer[BUFSIZ], **ap;
640 if ((folder = getenv("mhfolder")) == NULL || *folder == 0) {
642 admonish(NULL, "$mhfolder not set");
645 maildir = toabsdir(folder);
646 if (chdir(maildir) == NOTOK) {
648 admonish(maildir, "unable to change directory to");
651 if (!(mp = folder_read(folder))) {
653 admonish(NULL, "unable to read folder %s", folder);
657 /* check for empty folder */
658 if (mp->nummsg == 0) {
660 admonish(NULL, "no messages in %s", folder);
664 if ((cp = getenv("mhmessages")) == NULL || *cp == 0) {
666 admonish(NULL, "$mhmessages not set");
669 if (!debugsw /* MOBY HACK... */
671 && (fd3 = open("/dev/null", O_RDWR)) != NOTOK
672 && (fd2 = dup(fileno(stderr))) != NOTOK) {
673 dup2(fd3, fileno(stderr));
678 for (ap = brkstring(cp = getcpy(cp), " ", NULL); *ap; ap++)
682 dup2(fd2, fileno(stderr));
683 if (mp->numsel == 0) {
685 admonish(NULL, "no messages to annotate");
689 lseek(fd, (off_t) 0, SEEK_SET);
690 if ((fp = fdopen(fd, "r")) == NULL) {
692 admonish(NULL, "unable to fdopen annotation list");
696 while (fgets(buffer, sizeof(buffer), fp) != NULL)
697 cp = add(buffer, cp);
701 advise(NULL, "annotate%s with %s: \"%s\"",
702 inplace ? " inplace" : "", annotext, cp);
703 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
704 if (is_selected(mp, msgnum)) {
706 advise(NULL, "annotate message %d", msgnum);
707 annotate(m_name(msgnum), annotext, cp, inplace,
715 folder_free(mp); /* free folder/message structure */
720 armed_done(int status)
722 longjmp(env, status ? status : NOTOK);