make address functions a lot better, and less error prone.
authorPierre Habouzit <madcoder@debian.org>
Thu, 2 Nov 2006 22:11:19 +0000 (23:11 +0100)
committerPierre Habouzit <madcoder@debian.org>
Thu, 2 Nov 2006 22:11:19 +0000 (23:11 +0100)
reuse SLIST inlines.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
alias.c
crypt-gpgme.c
lib-mime/mime.h
lib-mime/rfc822.c
mbox.c
pgp.c
query.c
send.c
smime.c

diff --git a/alias.c b/alias.c
index c148c53..9be40dc 100644 (file)
--- a/alias.c
+++ b/alias.c
@@ -79,7 +79,7 @@ static address_t *mutt_expand_aliases_r (address_t * a, LIST ** expn)
           u->data = m_strdup(a->mailbox);
           u->next = *expn;
           *expn = u;
-          w = rfc822_cpy_adr (t);
+          w = address_list_dup (t);
           w = mutt_expand_aliases_r (w, expn);
           if (head)
             last->next = w;
index b44a9ba..32efdb7 100644 (file)
@@ -3688,7 +3688,7 @@ static char *find_keys (address_t * to, address_t * cc, address_t * bcc,
       abort ();
     }
 
-    *last = rfc822_cpy_adr (p);
+    *last = address_list_dup (p);
     while (*last)
       last = &((*last)->next);
   }
index 34e1b0f..269b0d0 100644 (file)
@@ -32,6 +32,7 @@
 #include <stdlib.h>
 
 #include <lib-lib/mem.h>
+#include <lib-lib/list.h>
 
 /* Content-Type */
 enum {
@@ -111,12 +112,14 @@ void address_wipe(address_t *);
 
 DO_NEW(address_t, address);
 DO_DELETE(address_t, address);
+DO_SLIST(address_t, address);
+
+address_t *address_dup(address_t *addr);
+address_t *address_list_dup(address_t *addr);
 
 void rfc822_qualify(address_t *, const char *);
+
 address_t *rfc822_parse_adrlist(address_t *, const char *s);
-address_t *rfc822_cpy_adr(address_t * addr);
-address_t *rfc822_cpy_adr_real(address_t * addr);
-address_t *rfc822_append(address_t ** a, address_t * b);
 void rfc822_write_address(char *, size_t, address_t *, int);
 void rfc822_write_address_single(char *, size_t, address_t *, int);
 void rfc822_cat(char *, size_t, const char *, const char *);
index 674d681..e73b408 100644 (file)
 
 void address_wipe(address_t *addr)
 {
-    address_delete(&addr->next);
     p_delete(&addr->personal);
     p_delete(&addr->mailbox);
+    address_delete(&addr->next);
+}
+
+
+void rfc822_qualify(address_t *addr, const char *host)
+{
+    char *p;
+
+    for (; addr; addr = addr->next) {
+        if (!addr->group && addr->mailbox && strchr(addr->mailbox, '@') == NULL) {
+            p = p_new(char, m_strlen(addr->mailbox) + m_strlen(host) + 2);
+            sprintf(p, "%s@%s", addr->mailbox, host);        /* __SPRINTF_CHECKED__ */
+            p_delete(&addr->mailbox);
+            addr->mailbox = p;
+        }
+    }
+}
+
+address_t *address_dup(address_t *addr)
+{
+    address_t *res = address_new();
+
+    res->personal = m_strdup(addr->personal);
+    res->mailbox  = m_strdup(addr->mailbox);
+    res->group    = addr->group;
+    return res;
 }
 
+address_t *address_list_dup(address_t *addr)
+{
+    address_t *res = NULL, **resp = &res;
+
+    for (; addr; addr = addr->next) {
+        *resp = address_dup(addr);
+        resp = &(*resp)->next;
+    }
+
+    return res;
+}
 
 #define terminate_string(a, b, c) do { if ((b) < (c)) a[(b)] = 0; else \
        a[(c)] = 0; } while (0)
@@ -446,19 +482,6 @@ address_t *rfc822_parse_adrlist (address_t * top, const char *s)
   return top;
 }
 
-void rfc822_qualify (address_t * addr, const char *host)
-{
-  char *p;
-
-  for (; addr; addr = addr->next)
-    if (!addr->group && addr->mailbox && strchr (addr->mailbox, '@') == NULL) {
-      p = p_new(char, m_strlen(addr->mailbox) + m_strlen(host) + 2);
-      sprintf (p, "%s@%s", addr->mailbox, host);        /* __SPRINTF_CHECKED__ */
-      p_delete(&addr->mailbox);
-      addr->mailbox = p;
-    }
-}
-
 void
 rfc822_cat (char *buf, size_t buflen, const char *value, const char *specials)
 {
@@ -641,47 +664,3 @@ done:
   *pbuf = 0;
 }
 
-/* this should be rfc822_cpy_adr */
-address_t *rfc822_cpy_adr_real (address_t * addr)
-{
-  address_t *p = address_new ();
-
-  p->personal = m_strdup(addr->personal);
-  p->mailbox = m_strdup(addr->mailbox);
-  p->group = addr->group;
-  return p;
-}
-
-/* this should be rfc822_cpy_adrlist */
-address_t *rfc822_cpy_adr (address_t * addr)
-{
-  address_t *top = NULL, *last = NULL;
-
-  for (; addr; addr = addr->next) {
-    if (last) {
-      last->next = rfc822_cpy_adr_real (addr);
-      last = last->next;
-    }
-    else
-      top = last = rfc822_cpy_adr_real (addr);
-  }
-  return top;
-}
-
-/* append list 'b' to list 'a' and return the last element in the new list */
-address_t *rfc822_append (address_t ** a, address_t * b)
-{
-  address_t *tmp = *a;
-
-  while (tmp && tmp->next)
-    tmp = tmp->next;
-  if (!b)
-    return tmp;
-  if (tmp)
-    tmp->next = rfc822_cpy_adr (b);
-  else
-    tmp = *a = rfc822_cpy_adr (b);
-  while (tmp && tmp->next)
-    tmp = tmp->next;
-  return tmp;
-}
diff --git a/mbox.c b/mbox.c
index 46d7291..7207ea7 100644 (file)
--- a/mbox.c
+++ b/mbox.c
@@ -203,7 +203,7 @@ static int mmdf_parse_mailbox (CONTEXT * ctx)
           rfc822_parse_adrlist (hdr->env->return_path, return_path);
 
       if (!hdr->env->from)
-        hdr->env->from = rfc822_cpy_adr (hdr->env->return_path);
+        hdr->env->from = address_list_dup (hdr->env->return_path);
 
       ctx->msgcount++;
     }
@@ -359,7 +359,7 @@ static int mbox_parse_mailbox (CONTEXT * ctx)
           rfc822_parse_adrlist (curhdr->env->return_path, return_path);
 
       if (!curhdr->env->from)
-        curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path);
+        curhdr->env->from = address_list_dup (curhdr->env->return_path);
 
       lines = 0;
     }
diff --git a/pgp.c b/pgp.c
index 0848077..ab582c3 100644 (file)
--- a/pgp.c
+++ b/pgp.c
@@ -1106,7 +1106,7 @@ char *pgp_findKeys (address_t * to, address_t * cc, address_t * bcc)
       abort ();
     }
 
-    *last = rfc822_cpy_adr (p);
+    *last = address_list_dup (p);
     while (*last)
       last = &((*last)->next);
   }
diff --git a/query.c b/query.c
index 1aa7b8a..398f3b4 100644 (file)
--- a/query.c
+++ b/query.c
@@ -61,7 +61,7 @@ static address_t *result_to_addr (QUERY * r)
 {
   static address_t *tmp;
 
-  tmp = rfc822_cpy_adr (r->addr);
+  tmp = address_list_dup (r->addr);
 
   if (!tmp->next && !tmp->personal)
     tmp->personal = m_strdup(r->name);
@@ -380,10 +380,7 @@ static void query_menu (char *buf, size_t buflen, QUERY * results, int retbuf)
 
           for (i = 0; i < menu->max; i++)
             if (QueryTable[i].tagged) {
-              address_t *a = result_to_addr (QueryTable[i].data);
-
-              rfc822_append (&naddr, a);
-              address_delete (&a);
+              address_list_append(&naddr, result_to_addr(QueryTable[i].data));
             }
 
           mutt_create_alias (NULL, naddr);
@@ -412,10 +409,7 @@ static void query_menu (char *buf, size_t buflen, QUERY * results, int retbuf)
         else {
           for (i = 0; i < menu->max; i++)
             if (QueryTable[i].tagged) {
-              address_t *a = result_to_addr (QueryTable[i].data);
-
-              rfc822_append (&msg->env->to, a);
-              address_delete (&a);
+              address_list_append(&msg->env->to, result_to_addr(QueryTable[i].data));
             }
         }
         ci_send_message (0, msg, NULL, Context, NULL);
diff --git a/send.c b/send.c
index f98e32b..4ffad14 100644 (file)
--- a/send.c
+++ b/send.c
@@ -170,11 +170,11 @@ static address_t *find_mailing_lists (address_t * t, address_t * c)
     for (; t; t = t->next) {
       if (mutt_is_mail_list (t) && !t->group) {
         if (top) {
-          ptr->next = rfc822_cpy_adr_real (t);
+          ptr->next = address_dup (t);
           ptr = ptr->next;
         }
         else
-          ptr = top = rfc822_cpy_adr_real (t);
+          ptr = top = address_dup (t);
       }
     }
   }
@@ -473,10 +473,9 @@ static int include_reply (CONTEXT * ctx, HEADER * cur, FILE * out)
 static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
 {
   char prompt[STRING];
-  address_t *tmp;
 
   if (flags && env->mail_followup_to && hmfupto == M_YES) {
-    rfc822_append (to, env->mail_followup_to);
+    address_list_append(to, address_list_dup(env->mail_followup_to));
     return 0;
   }
 
@@ -494,9 +493,7 @@ static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
                               _("Message came from a mailing list. List-reply to mailing list?")))
     {
     case M_YES:
-      tmp = find_mailing_lists (env->to, env->cc);
-      rfc822_append (to, tmp);
-      address_delete (&tmp);
+      address_list_append(to, find_mailing_lists (env->to, env->cc));
       return 0;
     case -1:
       return -1;                /* abort */
@@ -505,7 +502,7 @@ static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
 
   if (!option (OPTREPLYSELF) && mutt_addr_is_user (env->from)) {
     /* mail is from the user, assume replying to recipients */
-    rfc822_append (to, env->to);
+    address_list_append(to, address_list_dup(env->to));
   }
   else if (env->reply_to) {
     if ((mutt_addrcmp (env->from, env->reply_to) && !env->reply_to->next) ||
@@ -521,7 +518,7 @@ static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
        * in his From header.
        * 
        */
-      rfc822_append (to, env->from);
+      address_list_append(to, address_list_dup(env->from));
     }
     else if (!(mutt_addrcmp (env->from, env->reply_to) &&
                !env->reply_to->next) && quadoption (OPT_REPLYTO) != M_YES) {
@@ -534,11 +531,11 @@ static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
                 env->reply_to->mailbox, env->reply_to->next ? ",..." : "");
       switch (query_quadoption (OPT_REPLYTO, prompt)) {
       case M_YES:
-        rfc822_append (to, env->reply_to);
+        address_list_append(to, address_list_dup(env->reply_to));
         break;
 
       case M_NO:
-        rfc822_append (to, env->from);
+        address_list_append(to, address_list_dup(env->from));
         break;
 
       default:
@@ -546,10 +543,10 @@ static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
       }
     }
     else
-      rfc822_append (to, env->reply_to);
+      address_list_append(to, address_list_dup(env->reply_to));
   }
   else
-    rfc822_append (to, env->from);
+    address_list_append(to, address_list_dup(env->from));
 
   return (0);
 }
@@ -557,7 +554,6 @@ static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
 int mutt_fetch_recips (ENVELOPE * out, ENVELOPE * in, int flags)
 {
   char prompt[STRING];
-  address_t *tmp;
   int hmfupto = -1;
 
   if ((flags & (SENDLISTREPLY | SENDGROUPREPLY)) && in->mail_followup_to) {
@@ -570,9 +566,7 @@ int mutt_fetch_recips (ENVELOPE * out, ENVELOPE * in, int flags)
   }
 
   if (flags & SENDLISTREPLY) {
-    tmp = find_mailing_lists (in->to, in->cc);
-    rfc822_append (&out->to, tmp);
-    address_delete (&tmp);
+    address_list_append(&out->to, find_mailing_lists(in->to, in->cc));
 
     if (in->mail_followup_to && hmfupto == M_YES &&
         default_to (&out->cc, in, flags & SENDLISTREPLY, hmfupto) == -1)
@@ -583,10 +577,10 @@ int mutt_fetch_recips (ENVELOPE * out, ENVELOPE * in, int flags)
       return (-1);              /* abort */
 
     if ((flags & SENDGROUPREPLY)
-        && (!in->mail_followup_to || hmfupto != M_YES)) {
-      /* if(!mutt_addr_is_user(in->to)) */
-      rfc822_append (&out->cc, in->to);
-      rfc822_append (&out->cc, in->cc);
+        && (!in->mail_followup_to || hmfupto != M_YES))
+    {
+      address_t **tmp = address_list_append(&out->cc, address_list_dup(in->to));
+      address_list_append(tmp, address_list_dup(in->cc));
     }
   }
   return 0;
@@ -881,7 +875,6 @@ static int generate_body (FILE * tempfp,        /* stream for outgoing message *
 
 void mutt_set_followup_to (ENVELOPE * e)
 {
-  address_t *t = NULL;
   address_t *from;
 
   /* 
@@ -901,13 +894,14 @@ void mutt_set_followup_to (ENVELOPE * e)
 
   if (!e->mail_followup_to) {
     if (mutt_is_list_cc (0, e->to, e->cc)) {
+      address_t **tmp;
       /* 
        * this message goes to known mailing lists, so create a proper
        * mail-followup-to header
        */
 
-      t = rfc822_append (&e->mail_followup_to, e->to);
-      rfc822_append (&t, e->cc);
+      tmp = address_list_append(&e->mail_followup_to, address_list_dup(e->to));
+      address_list_append(tmp, address_list_dup(e->cc));
     }
 
     /* remove ourselves from the mail-followup-to header */
@@ -922,22 +916,19 @@ void mutt_set_followup_to (ENVELOPE * e)
 
     if (e->mail_followup_to && !mutt_is_list_recipient (0, e->to, e->cc)) {
       if (e->reply_to)
-        from = rfc822_cpy_adr (e->reply_to);
+        from = address_list_dup (e->reply_to);
       else if (e->from)
-        from = rfc822_cpy_adr (e->from);
+        from = address_list_dup (e->from);
       else
         from = mutt_default_from ();
 
       if (from) {
-        /* Normally, this loop will not even be entered. */
-        for (t = from; t && t->next; t = t->next);
-
-        t->next = e->mail_followup_to;  /* t cannot be NULL at this point. */
+        address_list_append(&from->next, e->mail_followup_to);
         e->mail_followup_to = from;
       }
     }
 
-    e->mail_followup_to = mutt_remove_duplicates (e->mail_followup_to);
+    e->mail_followup_to = mutt_remove_duplicates(e->mail_followup_to);
 
   }
 }
@@ -963,7 +954,7 @@ static address_t *set_reverse_name (ENVELOPE * env)
   if (!tmp && mutt_addr_is_user (env->from))
     tmp = env->from;
   if (tmp) {
-    tmp = rfc822_cpy_adr_real (tmp);
+    tmp = address_dup (tmp);
     if (!option (OPTREVREAL))
       p_delete(&tmp->personal);
     if (!tmp->personal)
@@ -983,7 +974,7 @@ address_t *mutt_default_from (void)
    */
 
   if (From)
-    adr = rfc822_cpy_adr_real (From);
+    adr = address_dup (From);
   else if (option (OPTUSEDOMAIN)) {
     adr = address_new ();
     adr->mailbox = p_new(char, m_strlen(Username) + m_strlen(fqdn) + 2);
diff --git a/smime.c b/smime.c
index 0c9d435..1e173f6 100644 (file)
--- a/smime.c
+++ b/smime.c
@@ -756,7 +756,7 @@ char *smime_findKeys (address_t * to, address_t * cc, address_t * bcc)
       abort ();
     }
 
-    *last = rfc822_cpy_adr (p);
+    *last = address_list_dup (p);
     while (*last)
       last = &((*last)->next);
   }