- msgs[msgp++] = cp;
- }
- }
-
- /*
- * check for "Aliasfile:" profile entry
- */
- if ((cp = context_find ("Aliasfile"))) {
- char *dp = NULL;
-
- for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
- vec[vecp++] = "-alias";
- vec[vecp++] = *ap;
- }
- }
-
- if (dfolder == NULL) {
- if (msgp == 0) {
-#ifdef WHATNOW
- if ((cp = getenv ("mhdraft")) && *cp) {
- msgs[msgp++] = cp;
- goto go_to_it;
- }
-#endif /* WHATNOW */
- msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
- if (stat (msgs[0], &st) == NOTOK)
- adios (msgs[0], "unable to stat draft file");
- cp = concat ("Use \"", msgs[0], "\"? ", NULL);
- for (status = LISTDSW; status != YESW;) {
- if (!(argp = getans (cp, anyl)))
- done (1);
- switch (status = smatch (*argp, anyl)) {
- case NOSW:
- done (0);
- case YESW:
+ distfile = NULL;
+ }
+
+ if (!altmsg || stat(altmsg, &st) == NOTOK) {
+ st.st_mtime = 0;
+ st.st_dev = 0;
+ st.st_ino = 0;
+ }
+ status = 0;
+ vec[0] = "spost";
+ for (n=3; n<OPEN_MAX; n++) {
+ close(n);
+ }
+
+ for (n = 0; n < nfiles; n++) {
+ switch (sendsbr(vec, vecp, files[n], &st)) {
+ case DONE:
+ exit(++status);
+ case NOTOK:
+ status++; /* fall */
+ case OK:
+ break;
+ }
+ }
+
+ 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);
+ }