simplify alias expansion a lot.
[apps/madmutt.git] / alias.c
diff --git a/alias.c b/alias.c
index 7282a8c..32b6df3 100644 (file)
--- 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