+ }
+ }
+
+ context_save();
+ done(status);
+ return 1;
+}
+
+
+/*
+** message sending back-end
+*/
+static int
+sendsbr(char **vec, int vecp, char *drft, struct stat *st)
+{
+ int status;
+ char buffer[BUFSIZ];
+ char *original_draft;
+
+ /*
+ ** Save the original name of the draft file. The name of the
+ ** draft file is changed to a temporary file containing the built
+ ** MIME message if there are attachments. We need the original
+ ** name so that it can be renamed after the message is sent.
+ */
+ original_draft = drft;
+
+ /*
+ ** Convert the draft to a MIME message.
+ ** Use the mhbuild composition file for the draft if there was
+ ** a successful conversion because that now contains the MIME
+ ** message. A nice side effect of this is that it leaves the
+ ** original draft file untouched so that it can be retrieved
+ ** and modified if desired.
+ */
+ switch (attach(drft)) {
+ case OK:
+ drft = composition_file_name;
+ break;
+
+ case NOTOK:
+ return (NOTOK);
+
+ case DONE:
+ break;
+ }
+
+ /*
+ ** Sign and encrypt the message as needed.
+ ** Use the mhbuild composition file for the draft if there was
+ ** a successful conversion because that now contains the MIME
+ ** message. A nice side effect of this is that it leaves the
+ ** original draft file untouched so that it can be retrieved
+ ** and modified if desired.
+ */
+ switch (signandenc(drft)) {
+ case OK:
+ drft = composition_file_name;
+ break;
+
+ case NOTOK:
+ return (NOTOK);
+
+ case DONE:
+ break;
+ }
+
+ done=armed_done;
+ switch (setjmp(env)) {
+ case OK:
+ status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
+ if (status == OK) {
+ /* move original draft to +trash folder */
+ snprintf(buffer, sizeof buffer,
+ "</dev/null refile -file %s +trash",
+ original_draft);
+ if (system(buffer) != 0) {
+ advise(NULL, "unable to trash the draft");
+ }
+ }
+ break;
+
+ default:
+ status = DONE;
+ break;
+ }
+
+ done=exit;
+ if (distfile) {
+ unlink(distfile);
+ }
+
+ /* Get rid of temporary files that we created for attachments. */
+ if (drft == composition_file_name) {
+ clean_up_temporary_files();
+ }
+
+ return status;
+}
+
+static int
+attach(char *draft_file_name)
+{
+ char buf[MAXPATHLEN + 6];
+ int c;
+ int has_attachment;
+ int has_body;
+ int non_ascii; /* msg body contains non-ASCII chars */
+ int length; /* length of attachment header field name */
+ char *p;
+
+ if (!(draft_file = fopen(draft_file_name, "r"))) {
+ adios(NULL, "can't open draft file `%s'.", draft_file_name);
+ }
+
+ /* We'll grow the buffer as needed. */
+ field = (char *)mh_xmalloc(field_size = 256);
+
+ /*
+ ** Scan the draft file for an attachment header field name.
+ */
+ length = strlen(attach_hdr);
+ has_attachment = 0;
+ while (get_line() != EOF && *field != '\0' && *field != '-') {
+ if (strncasecmp(field, attach_hdr, length)==0 &&
+ field[length] == ':') {
+ has_attachment = 1;
+ }
+ }
+
+ /*
+ ** Look for at least one non-blank line in the body of the
+ ** message which indicates content in the body.
+ ** Check if body contains at least one non-blank (= not empty)
+ ** and if it contains any non-ASCII chars (= need MIME).
+ */
+ has_body = 0;
+ non_ascii = 0;
+ while (get_line() != EOF) {
+ for (p = field; *p != '\0'; p++) {
+ if (*p != ' ' && *p != '\t') {
+ has_body = 1;
+ }
+ if (*p > 127 || *p < 0) {
+ non_ascii = 1;
+ }
+ }
+ if (has_body && non_ascii) {
+ break; /* that's been already enough information */
+ }
+ }
+
+ if (!has_attachment && non_ascii==0) {
+ /* We don't need to convert it to MIME. */
+ return DONE;
+ }
+
+ /*
+ ** Else: mimify
+ */
+
+ /* Make names for the temporary files. */
+ strncpy(body_file_name,
+ m_mktemp(toabsdir(invo_name), NULL, NULL),
+ sizeof (body_file_name));
+ strncpy(composition_file_name,
+ m_mktemp(toabsdir(invo_name), NULL, NULL),
+ sizeof (composition_file_name));
+
+ if (has_body) {
+ body_file = fopen(body_file_name, "w");
+ }
+ composition_file = fopen(composition_file_name, "w");
+
+ if ((has_body && !body_file) || !composition_file) {
+ clean_up_temporary_files();
+ adios(NULL, "unable to open all of the temporary files.");
+ }
+
+ /* Copy non-attachment header fields to the temp composition file. */
+ rewind(draft_file);
+ while (get_line() != EOF && *field && *field != '-') {
+ if (strncasecmp(field, attach_hdr, length) != 0 ||
+ field[length] != ':') {
+ fprintf(composition_file, "%s\n", field);
+ }
+ }
+ fputs("--------\n", composition_file);
+
+ if (has_body) {
+ /* Copy the message body to the temporary file. */
+ while ((c = getc(draft_file)) != EOF) {
+ putc(c, body_file);
+ }
+ fclose(body_file);
+
+ /* Add a mhbuild MIME composition file line for the body */
+ /* charset will be discovered/guessed by mhbuild */
+ fprintf(composition_file, "#text/plain %s\n", body_file_name);
+ }
+
+ /*
+ ** Now, go back to the beginning of the draft file and look for
+ ** header fields that specify attachments. Add a mhbuild MIME
+ ** composition file for each.
+ */
+ rewind(draft_file);
+ while (get_line() != EOF && *field && *field != '-') {
+ if (strncasecmp(field, attach_hdr, length) == 0 &&
+ field[length] == ':') {
+ for (p = field+length+1; *p==' ' || *p=='\t'; p++) {
+ continue;
+ }
+ if (*p == '+') {
+ /* forwarded message */
+ fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
+ } else {
+ /* regular attachment */
+ make_mime_composition_file_entry(p);
+ }
+ }
+ }
+ fclose(composition_file);
+
+ /* We're ready to roll! */
+ sprintf(buf, "mhbuild %s", composition_file_name);
+ if (system(buf) != 0) {
+ /* some problem */
+ clean_up_temporary_files();
+ return (NOTOK);
+ }
+ /* Remove the automatically created backup of mhbuild. */
+ snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
+ if (unlink(buf) == -1) {
+ advise(NULL, "unable to remove original composition file.");
+ }
+
+ return (OK);
+}
+
+static int
+signandenc(char *draft_file_name)
+{
+ char buf[BUFSIZ];
+ int dosign = 0;
+ int doenc = 0;
+
+ if (!(draft_file = fopen(draft_file_name, "r"))) {
+ adios(NULL, "can't open draft file `%s'.", draft_file_name);
+ }
+
+ /* We'll grow the buffer as needed. */
+ field = (char *)mh_xmalloc(field_size = 256);
+
+ /* Scan the draft file for an attachment header field name. */
+ while (get_line() != EOF && *field != '\0' && *field != '-') {
+ if (strncasecmp(field, sign_hdr, strlen(sign_hdr))==0 &&
+ field[strlen(sign_hdr)] == ':') {
+ dosign = 1;
+ }
+ if (strncasecmp(field, enc_hdr, strlen(enc_hdr))==0 &&
+ field[strlen(enc_hdr)] == ':') {
+ doenc = 1;
+ }
+ }
+ if (!dosign && !doenc) {
+ return DONE;
+ }
+
+ strcpy(composition_file_name, draft_file_name);
+
+ /* We're ready to roll! */
+ sprintf(buf, "mhsign -m%s '%s'", doenc ? " -e" : "",
+ draft_file_name);
+ if (system(buf) != 0) {
+ /* some problem */
+ return (NOTOK);
+ }
+ /* Remove the automatically created backup of mhsign. */
+ snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
+ if (unlink(buf) == -1) {
+ advise(NULL, "unable to remove original draft file.");
+ }
+
+ return (OK);
+}
+
+static void
+clean_up_temporary_files(void)
+{
+ unlink(body_file_name);
+ unlink(composition_file_name);
+
+ return;
+}
+
+static int
+get_line(void)
+{
+ int c; /* current character */
+ int n; /* number of bytes in buffer */
+ char *p;
+
+ /*
+ ** Get a line from the input file, growing the field buffer as
+ ** needed. We do this so that we can fit an entire line in the
+ ** buffer making it easy to do a string comparison on both the
+ ** field name and the field body which might be a long path name.
+ */
+ for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
+ if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
+ ungetc(c, draft_file);
+ c = '\n';