more moves and dead code
[apps/madmutt.git] / lib-mime / rfc822address.c
index 9009d40..6c11e63 100644 (file)
  * please see the file GPL in the top level source directory.
  */
 
-#include <string.h>
-#include <ctype.h>
-#include <stdlib.h>
-
-#include <lib-lib/mem.h>
-#include <lib-lib/str.h>
-#include <lib-lib/ascii.h>
-#include <lib-lib/macros.h>
+#include <lib-lib/lib-lib.h>
 
 #include "mutt_idna.h"
 
-void address_wipe(address_t *addr)
-{
-    p_delete(&addr->personal);
-    p_delete(&addr->mailbox);
-    address_delete(&addr->next);
-}
-
-
 void rfc822_qualify(address_t *addr, const char *host)
 {
     char *p;
@@ -59,7 +44,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();
 
@@ -69,7 +54,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;
 
@@ -81,6 +66,26 @@ address_t *address_list_dup(address_t *addr)
     return res;
 }
 
+/* given a list of addresses, return a list of unique addresses */
+void address_list_uniq(address_t *a)
+{
+    for (; a; a = a->next) {
+        address_t **b = &a->next;
+
+        if (!a->mailbox)
+            continue;
+
+        while (*b) {
+            if ((*b)->mailbox && !ascii_strcasecmp((*b)->mailbox, a->mailbox))
+            {
+                address_t *pop = address_list_pop(b);
+                address_delete(&pop);
+            } else {
+                b = &(*b)->next;
+            }
+        }
+    }
+}
 
 /****************************************************************************/
 /* Parsing functions                                                        */
@@ -250,7 +255,7 @@ parse_address(const char *s, static_buf *comment, address_t *cur)
     return s;
 }
 
-address_t **rfc822_eotoken(address_t **last, static_buf *phrase, static_buf *comment)
+static address_t **rfc822_eotoken(address_t **last, static_buf *phrase, static_buf *comment)
 {
     if (phrase->len) {
         const char *s;
@@ -258,7 +263,7 @@ address_t **rfc822_eotoken(address_t **last, static_buf *phrase, static_buf *com
 
         s = parse_address(phrase->buf, comment, cur);
         if (s && *s && *s != ',' && *s != ';') {
-            address_delete(&cur);
+            address_list_wipe(&cur);
             return last;
         }
 
@@ -289,7 +294,7 @@ address_t *rfc822_parse_adrlist(address_t *top, const char *s)
                 stbuf_append_sp(&phrase);
             s = next_phrase(s, &phrase);
             if (!s) {
-                address_delete(&top);
+                address_list_wipe(&top);
                 return NULL;
             }
             continue;
@@ -298,7 +303,7 @@ address_t *rfc822_parse_adrlist(address_t *top, const char *s)
             stbuf_append_sp(&comment);
             s = parse_comment(s + 1, &comment);
             if (!s) {
-                address_delete(&top);
+                address_list_wipe(&top);
                 return NULL;
             }
             continue;
@@ -313,8 +318,8 @@ address_t *rfc822_parse_adrlist(address_t *top, const char *s)
 
             s = parse_address(skipspaces(s + 1), &comment, cur);
             if (!s || *s != '>' || !cur->mailbox) {
-                address_delete(&top);
-                address_delete(&cur);
+                address_list_wipe(&top);
+                address_list_wipe(&cur);
                 return NULL;
             }
 
@@ -470,3 +475,22 @@ rfc822_write_address(char *buf, ssize_t buflen, address_t *addr, int display)
     return pos;
 }
 
+address_t *mutt_parse_adrlist(address_t *p, const char *s)
+{
+    /* check for a simple whitespace separated list of addresses */
+    char *q = strpbrk(s, "\"<>():;,\\");
+    char tmp[HUGE_STRING];
+
+    if (q)
+        return rfc822_parse_adrlist(p, s);
+
+    m_strcpy(tmp, sizeof(tmp), s);
+    q = tmp;
+    while ((q = strtok(q, " \t"))) {
+        p = rfc822_parse_adrlist(p, q);
+        q = NULL;
+    }
+
+    return p;
+}
+