Move initialization of alternate mailnames list into separate function. ---
Next revision of this patch, with shortened function names. --- sbr/addrsbr.c | 149 +++++++++++++++++++++++++--------------------------------- 1 file changed, 63 insertions(+), 86 deletions(-) diff --git a/sbr/addrsbr.c b/sbr/addrsbr.c index db5b42e..c966763 100644 --- a/sbr/addrsbr.c +++ b/sbr/addrsbr.c @@ -229,6 +229,66 @@ adrformat(struct mailname *mp) #define W_HOST (W_HBEG | W_HEND) #define WBITS "\020\01MBEG\02MEND\03HBEG\04HEND" +static void +sync_mtype(struct mailname *mp) +{ + char *cp = mp->m_mbox + strlen(mp->m_mbox) - 1; /* last symbol */ + + if (*cp == '*') { + mp->m_type |= W_MEND; + *cp = '\0'; + } + if (*mp->m_mbox == '*') { + mp->m_type |= W_MBEG; + mp->m_mbox++; + } + if (mp->m_host) { + char *cp2 = mp->m_host + strlen(mp->m_host) - 1; /* last symbol */ + if (*cp2 == '*') { + mp->m_type |= W_HEND; + *cp2 = '\0'; + } + if (*mp->m_host == '*') { + mp->m_type |= W_HBEG; + mp->m_host++; + } + } +} + +static struct mailname** +parse_ctx(struct mailname **mp, char *entry) +{ + char *data = context_find(entry); + char *cp; + + while ((cp = getname(data))) { + struct mailname *next = getm(cp, NULL, W_NIL, AD_NAME, NULL); + if (!next) { + advise(NULL, "fix context entry %s: invalid address %s", + entry, cp); + } + + sync_mtype(next); + *mp = next; + mp = &next; + } + return mp; +} + +static const struct mailname* +alt_mailnames() +{ + static struct mailname *mq = NULL; + + if (!mq) { + struct mailname **mp = &mq; + + mp = parse_ctx(mp, "alternate-mailboxes"); + mp = parse_ctx(mp, "Default-From"); + } + return mq; +} + /* ** Check if this is my address */ @@ -236,93 +296,11 @@ adrformat(struct mailname *mp) int ismymbox(struct mailname *np) { - int oops; int len, i; char *cp; char *pp; char buffer[BUFSIZ]; - struct mailname *mp; - static char *am = NULL; - static struct mailname mq; - - /* - ** If this is the first call, initialize - ** list of alternate mailboxes. - */ - if (am == NULL) { - mq.m_next = NULL; - mq.m_mbox = getusername(); - mp = &mq; - if ((am = context_find("alternate-mailboxes")) == NULL) { - am = getusername(); - } else { - oops = 0; - while ((cp = getname(am))) { - if ((mp->m_next = getm(cp, NULL, 0, AD_NAME, NULL)) == NULL) { - admonish(NULL, "illegal address: %s", cp); - oops++; - } else { - mp = mp->m_next; - mp->m_type = W_NIL; - if (*mp->m_mbox == '*') { - mp->m_type |= W_MBEG; - mp->m_mbox++; - } - if (*(cp = mp->m_mbox + strlen(mp->m_mbox) - 1) == '*') { - mp->m_type |= W_MEND; - *cp = '\0'; - } - if (mp->m_host) { - if (*mp->m_host == '*') { - mp->m_type |= W_HBEG; - mp->m_host++; - } - if (*(cp = mp->m_host + strlen(mp->m_host) - 1) == '*') { - mp->m_type |= W_HEND; - *cp = '\0'; - } - } - if ((cp = getenv("MHWDEBUG")) && *cp) { - fprintf(stderr, "mbox=\"%s\" host=\"%s\" %s\n", - mp->m_mbox, mp->m_host, - snprintb(buffer, sizeof(buffer), (unsigned) mp->m_type, WBITS)); - } - } - } - if (oops != 0) { - advise(NULL, "please fix the profile entry %s", - "alternate-mailboxes"); - } - } - - if ((cp = context_find("Default-From")) != NULL) { - int i = 0; - char *cp2; - oops = 0; - - while ((cp2 = getname(cp)) != NULL) { - i++; - if ((mp->m_next = getm(cp2, NULL, 0, AD_NAME, NULL)) == NULL) { - admonish(NULL, "illegal address: %s", cp); - oops++; - } else { - mp = mp->m_next; - if ((cp = getenv("MHWDEBUG")) && *cp) { - fprintf(stderr, "mbox=\"%s\" host=\"%s\" %s\n", - mp->m_mbox, mp->m_host, - snprintb(buffer, sizeof(buffer), (unsigned) mp->m_type, WBITS)); - } - } - - } - - if (oops != 0 || i < 1) { - advise(NULL, "please fix the profile entry %s", - "Default-From"); - } - - } - } + const struct mailname *mp; if (np == NULL) /* XXX */ return 0; @@ -336,7 +314,7 @@ ismymbox(struct mailname *np) case LOCALHOST: local_test: ; - if (!mh_strcasecmp(np->m_mbox, mq.m_mbox)) + if (!mh_strcasecmp(np->m_mbox, getusername())) return 1; break; @@ -348,8 +326,7 @@ local_test: ; ** Now scan through list of alternate ** mailboxes, and check for a match. */ - for (mp = &mq; mp->m_next;) { - mp = mp->m_next; + for (mp = alt_mailnames(); mp; mp = mp->m_next) { if (!np->m_mbox) continue; if ((len = strlen(cp = np->m_mbox))