From: Pierre Habouzit Date: Mon, 13 Nov 2006 00:12:50 +0000 (+0100) Subject: simplify alias expansion a lot. X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=commitdiff_plain;h=0f6739c5be4203bec0fa32962e6ab13349da703b simplify alias expansion a lot. Signed-off-by: Pierre Habouzit --- diff --git a/alias.c b/alias.c index 7282a8c..32b6df3 100644 --- a/alias.c +++ b/alias.c @@ -50,7 +50,7 @@ void alias_wipe(alias_t *a) { alias_delete(&a->next); } -address_t *alias_lookup(alias_t *list, const char *s) +const address_t *alias_lookup(const alias_t *list, const char *s) { while (list) { if (!m_strcasecmp(s, list->name)) @@ -277,100 +277,84 @@ void mutt_create_alias(ENVELOPE *cur, address_t *iadr) } } -/************* READ MARK *********************/ - -static address_t *mutt_expand_aliases_r (address_t * a, LIST ** expn) +static address_t *mutt_expand_aliases_r(address_t *a, LIST **expn) { - address_t *head = NULL, *last = NULL, *t, *w; - LIST *u; - char i; - const char *fqdn; - - while (a) { - if (!a->group && !a->personal && a->mailbox - && strchr (a->mailbox, '@') == NULL) { - t = alias_lookup(Aliases, a->mailbox); - - if (t) { - i = 0; - for (u = *expn; u; u = u->next) { - if (m_strcmp(a->mailbox, u->data) == 0) { /* alias already found */ - debug_print(1, ("loop in alias found for '%s'\n", a->mailbox)); - i = 1; - break; - } - } - - if (!i) { - u = p_new(LIST, 1); - u->data = m_strdup(a->mailbox); - u->next = *expn; - *expn = u; - w = address_list_dup (t); - w = mutt_expand_aliases_r (w, expn); - if (head) - last->next = w; - else - head = last = w; - while (last && last->next) - last = last->next; + address_t *pop, *head = NULL; + address_t **last = &head; + + while ((pop = address_list_pop(&a))) { + if (!pop->group && !pop->personal + && pop->mailbox && !strchr(pop->mailbox, '@')) + { + const address_t *t = alias_lookup(Aliases, pop->mailbox); + + if (t) { + LIST *u; + + for (u = *expn; u; u = u->next) { + if (!m_strcmp(pop->mailbox, u->data)) { /* alias already found */ + address_delete(&pop); + continue; + } + } + + /* save the fact we saw it */ + u = mutt_new_list(); + u->data = m_strdup(pop->mailbox); + u->next = *expn; + *expn = u; + address_delete(&pop); + + /* recurse */ + last = address_list_last(last); + *last = mutt_expand_aliases_r(address_list_dup(t), expn); + continue; + } else { + struct passwd *pw = getpwnam(pop->mailbox); + + if (pw) { + char namebuf[STRING]; + mutt_gecos_name(namebuf, sizeof(namebuf), pw, GecosMask.rx); + m_strreplace(&pop->personal, namebuf); + } + } } - t = a; - a = a->next; - t->next = NULL; - address_delete (&t); - continue; - } - else { - struct passwd *pw = getpwnam (a->mailbox); - - if (pw) { - char namebuf[STRING]; - mutt_gecos_name(namebuf, sizeof (namebuf), pw, GecosMask.rx); - m_strreplace(&a->personal, namebuf); - } - } + last = address_list_append(last, pop); } - if (head) { - last->next = a; - last = last->next; + if (option(OPTUSEDOMAIN)) { + /* now qualify all local addresses */ + const char *fqdn = mutt_fqdn(1); + if (fqdn) + rfc822_qualify(head, fqdn); } - else - head = last = a; - a = a->next; - last->next = NULL; - } - if (option (OPTUSEDOMAIN) && (fqdn = mutt_fqdn (1))) { - /* now qualify all local addresses */ - rfc822_qualify (head, fqdn); - } - - return (head); + return head; } -address_t *mutt_expand_aliases (address_t * a) +address_t *mutt_expand_aliases(address_t *a) { - address_t *t; - LIST *expn = NULL; /* previously expanded aliases to avoid loops */ + address_t *t; + LIST *expn = NULL; /* previously expanded aliases to avoid loops */ - t = mutt_expand_aliases_r (a, &expn); - mutt_free_list (&expn); - return (mutt_remove_duplicates (t)); + t = mutt_expand_aliases_r(a, &expn); + mutt_free_list(&expn); + return mutt_remove_duplicates(t); } -void mutt_expand_aliases_env (ENVELOPE * env) +void mutt_expand_aliases_env(ENVELOPE *env) { - env->from = mutt_expand_aliases (env->from); - env->to = mutt_expand_aliases (env->to); - env->cc = mutt_expand_aliases (env->cc); - env->bcc = mutt_expand_aliases (env->bcc); - env->reply_to = mutt_expand_aliases (env->reply_to); - env->mail_followup_to = mutt_expand_aliases (env->mail_followup_to); + env->from = mutt_expand_aliases(env->from); + env->to = mutt_expand_aliases(env->to); + env->cc = mutt_expand_aliases(env->cc); + env->bcc = mutt_expand_aliases(env->bcc); + env->reply_to = mutt_expand_aliases(env->reply_to); + env->mail_followup_to = mutt_expand_aliases(env->mail_followup_to); } +/************* READ MARK *********************/ + /* * This routine looks to see if the user has an alias defined for the given diff --git a/alias.h b/alias.h index ec6afec..0c9a307 100644 --- a/alias.h +++ b/alias.h @@ -28,15 +28,15 @@ DO_NEW(alias_t, alias); DO_DELETE(alias_t, alias); DO_SLIST(alias_t, alias); -address_t *alias_lookup(alias_t *list, const char *s); +const address_t *alias_lookup(const alias_t *list, const char *s); int mutt_addr_is_user(address_t *); address_t *mutt_get_address(ENVELOPE *, const char **); void mutt_create_alias(ENVELOPE *, address_t *); -address_t *mutt_expand_aliases (address_t *); -void mutt_expand_aliases_env (ENVELOPE *); -address_t *alias_reverse_lookup (address_t *); -int mutt_alias_complete (char *, size_t); -void mutt_alias_menu (char *, size_t, alias_t *); +address_t *mutt_expand_aliases(address_t *); +void mutt_expand_aliases_env(ENVELOPE *); +address_t *alias_reverse_lookup(address_t *); +int mutt_alias_complete(char *, size_t); +void mutt_alias_menu(char *, size_t, alias_t *); #endif /* !_MUTT_ALIAS_H */ diff --git a/lib-mime/mime.h b/lib-mime/mime.h index d2b6fd5..f8e6fe8 100644 --- a/lib-mime/mime.h +++ b/lib-mime/mime.h @@ -76,9 +76,9 @@ int mutt_count_body_parts (HEADER *hdr, int flags); /*** addresses ***/ -address_t *mutt_parse_adrlist (address_t *, const char *); -address_t *address_dup(address_t *addr); -address_t *address_list_dup(address_t *addr); +address_t *mutt_parse_adrlist(address_t *, const char *); +address_t *address_dup(const address_t *addr); +address_t *address_list_dup(const address_t *addr); void rfc822_qualify(address_t *, const char *); address_t *rfc822_parse_adrlist(address_t *, const char *s); diff --git a/lib-mime/rfc822address.c b/lib-mime/rfc822address.c index e4bf976..0b06c05 100644 --- a/lib-mime/rfc822address.c +++ b/lib-mime/rfc822address.c @@ -51,7 +51,7 @@ void rfc822_qualify(address_t *addr, const char *host) } } -address_t *address_dup(address_t *addr) +address_t *address_dup(const address_t *addr) { address_t *res = address_new(); @@ -61,7 +61,7 @@ address_t *address_dup(address_t *addr) return res; } -address_t *address_list_dup(address_t *addr) +address_t *address_list_dup(const address_t *addr) { address_t *res = NULL, **resp = &res;