+ }
+ }
+
+ context_save();
+ return status;
+}
+
+
+/*
+** message sending back-end
+*/
+static int
+sendsbr(char **vec, int vecp, char *drft, struct stat *st)
+{
+ int status, dupfd;
+ 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:
+ /* successfully MIMEified: use generate draft */
+ drft = composition_file_name;
+ break;
+
+ case NOTOK:
+ return (NOTOK);
+
+ case DONE:
+ /* already in MIME format: keep original draft */
+ 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;
+ }
+
+ if ((status = sendaux(vec, vecp, drft, st)) == OK) {
+ /* move original draft to +trash folder */
+ /* temporary close stdin, for refile not to ask */
+ dupfd = dup(0);
+ close(0);
+ if (execprogl("refile", "refile", "-file",
+ original_draft, "+trash",
+ (char *)NULL) != 0) {
+ advise(NULL, "unable to trash the draft");
+ }
+ dup2(dupfd, 0);
+ close(dupfd);
+ } else {
+ status = DONE;
+ }
+
+ 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)
+{
+ enum state state;
+ struct field f = {{0}};
+ int compnum;
+ int finished_header = 0;
+ char buf[MAXPATHLEN + 6];
+ size_t length = strlen(attach_hdr);
+ char *p;
+
+ if(distfile) {
+ return DONE;
+ }
+
+ if (!(draft_file = fopen(draft_file_name, "r"))) {
+ adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
+ }
+
+ /*
+ ** MIMEify
+ */
+
+ /* 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));
+
+ body_file = fopen(body_file_name, "w");
+ composition_file = fopen(composition_file_name, "w");
+
+ if (!body_file || !composition_file) {
+ clean_up_temporary_files();
+ adios(EX_IOERR, NULL, "unable to open all of the temporary files.");
+ }
+
+ /* Copy non-attachment header fields to the temp composition file. */
+ rewind(draft_file);
+ for (compnum = 1, state = FLD2; state == FLD2 || state == BODY2;) {
+ switch (state = m_getfld2(state, &f, draft_file)) {
+ case LENERR2:
+ state = FLD2;
+ case FLD2:
+ compnum++;
+ if (strncasecmp(f.name, VRSN_FIELD, sizeof(VRSN_FIELD) - 1)==0) {
+ /*
+ ** The draft is already in MIME format, thus
+ ** back out and use the original draft file.
+ */
+ clean_up_temporary_files();
+ return DONE;
+ }
+ if (strncasecmp(f.name, attach_hdr, length) != 0 ||
+ f.namelen != length) {
+ fprintf(composition_file, "%s:%s", f.name, f.value);
+ }
+ break;
+ case BODY2:
+ if (!finished_header) {
+ fputs("--------\n", composition_file);
+ finished_header++;
+ }
+ fputs(f.value, body_file);