fix bug if multible mbox in From-field
[mmh] / uip / spost.c
index ac2843f..163c1e6 100644 (file)
@@ -115,6 +115,7 @@ static char *tmpfil;
 static char *subject = NULL;  /* the subject field for BCC'ing */
 static char fccs[BUFSIZ] = "";
 struct mailname *bccs = NULL;  /* list of the bcc recipients */
+struct mailname *sender = NULL;
 
 static struct headers *hdrtab;  /* table for the message we're doing */
 static FILE *out;  /* output (temp) file */
@@ -415,16 +416,38 @@ putfmt(char *name, char *str, FILE *out)
        if (hdr->set & MFRM) {
 
                struct mailname *mp = NULL;
+               struct mailname *my = NULL;
+               unsigned int fromcnt = 0;
 
                /* This is need because the addresse parser hold global state */
                ismymbox(NULL);
 
                while ((cp = getname(str)) != NULL) {
+                       fromcnt++;
                        mp = getm(cp, NULL, 0, AD_NAME, NULL);
                        if (ismymbox(mp)) {
                                msgflags |= MFMM;
+                               if (my == NULL) {
+                                       my = mp;
+                               } else {
+                                       mnfree(mp);
+                                       mp = NULL;
+                               }
+                       } else {
+                               mnfree(mp);
+                               mp = NULL;
                        }
                }
+
+               if (fromcnt > 1) {
+                       sender = my;
+               } else {
+                       mnfree(my);
+               }
+
+               free(cp);
+               cp = NULL;
+
        }
 
        if (hdr->flags & HSUB) {
@@ -449,7 +472,9 @@ finish_headers(FILE *out)
                fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
        }
 
-       if ((cp = context_find("Default-From")) != NULL) {
+       if (sender != NULL) {
+               snprintf(signature, sizeof(signature), "%s", sender->m_text);
+       } else if ((cp = context_find("Default-From")) != NULL) {
                snprintf(signature, sizeof(signature), "%s", cp);
        } else {
                snprintf(from, sizeof(from), "%s@%s", getusername(), LocalName());
@@ -466,7 +491,7 @@ finish_headers(FILE *out)
                ** Add a Sender: header because the From: header could
                ** be fake or contain multiple addresses.
                */
-               if (!(msgflags & MFMM)) {
+               if (!(msgflags & MFMM) || sender != NULL) {
                        fprintf(out, "%sSender: %s\n", resentstr, signature);
                }
        }