diff options
Diffstat (limited to 'src/libmime/email_addr.c')
-rw-r--r-- | src/libmime/email_addr.c | 253 |
1 files changed, 127 insertions, 126 deletions
diff --git a/src/libmime/email_addr.c b/src/libmime/email_addr.c index 38a4732c5..0af7388a8 100644 --- a/src/libmime/email_addr.c +++ b/src/libmime/email_addr.c @@ -21,7 +21,7 @@ #include "smtp_parsers.h" static void -rspamd_email_address_unescape (struct rspamd_email_address *addr) +rspamd_email_address_unescape(struct rspamd_email_address *addr) { const char *h, *end; char *t, *d; @@ -30,7 +30,7 @@ rspamd_email_address_unescape (struct rspamd_email_address *addr) return; } - d = g_malloc (addr->user_len); + d = g_malloc(addr->user_len); t = d; h = addr->user; end = h + addr->user_len; @@ -39,7 +39,7 @@ rspamd_email_address_unescape (struct rspamd_email_address *addr) if (*h != '\\') { *t++ = *h; } - h ++; + h++; } addr->user = d; @@ -48,7 +48,7 @@ rspamd_email_address_unescape (struct rspamd_email_address *addr) } struct rspamd_email_address * -rspamd_email_address_from_smtp (const gchar *str, guint len) +rspamd_email_address_from_smtp(const gchar *str, guint len) { struct rspamd_email_address addr, *ret; gsize nlen; @@ -57,24 +57,24 @@ rspamd_email_address_from_smtp (const gchar *str, guint len) return NULL; } - rspamd_smtp_addr_parse (str, len, &addr); + rspamd_smtp_addr_parse(str, len, &addr); if (addr.flags & RSPAMD_EMAIL_ADDR_VALID) { - ret = g_malloc (sizeof (*ret)); - memcpy (ret, &addr, sizeof (addr)); + ret = g_malloc(sizeof(*ret)); + memcpy(ret, &addr, sizeof(addr)); if ((ret->flags & RSPAMD_EMAIL_ADDR_QUOTED) && ret->addr[0] == '"') { if (ret->flags & RSPAMD_EMAIL_ADDR_HAS_BACKSLASH) { /* We also need to unquote user */ - rspamd_email_address_unescape (ret); + rspamd_email_address_unescape(ret); } /* We need to unquote addr */ nlen = ret->domain_len + ret->user_len + 2; - ret->addr = g_malloc (nlen + 1); - ret->addr_len = rspamd_snprintf ((char *)ret->addr, nlen, "%*s@%*s", - (gint)ret->user_len, ret->user, - (gint)ret->domain_len, ret->domain); + ret->addr = g_malloc(nlen + 1); + ret->addr_len = rspamd_snprintf((char *) ret->addr, nlen, "%*s@%*s", + (gint) ret->user_len, ret->user, + (gint) ret->domain_len, ret->domain); ret->flags |= RSPAMD_EMAIL_ADDR_ADDR_ALLOCATED; } @@ -84,36 +84,35 @@ rspamd_email_address_from_smtp (const gchar *str, guint len) return NULL; } -void -rspamd_email_address_free (struct rspamd_email_address *addr) +void rspamd_email_address_free(struct rspamd_email_address *addr) { if (addr) { if (addr->flags & RSPAMD_EMAIL_ADDR_ADDR_ALLOCATED) { - g_free ((void *) addr->addr); + g_free((void *) addr->addr); } if (addr->flags & RSPAMD_EMAIL_ADDR_USER_ALLOCATED) { - g_free ((void *) addr->user); + g_free((void *) addr->user); } - g_free (addr); + g_free(addr); } } static inline void -rspamd_email_address_add (rspamd_mempool_t *pool, - GPtrArray *ar, - struct rspamd_email_address *addr, - GString *name) +rspamd_email_address_add(rspamd_mempool_t *pool, + GPtrArray *ar, + struct rspamd_email_address *addr, + GString *name) { struct rspamd_email_address *elt; guint nlen; - elt = g_malloc0 (sizeof (*elt)); - rspamd_mempool_notify_alloc (pool, sizeof (*elt)); + elt = g_malloc0(sizeof(*elt)); + rspamd_mempool_notify_alloc(pool, sizeof(*elt)); if (addr != NULL) { - memcpy (elt, addr, sizeof (*addr)); + memcpy(elt, addr, sizeof(*addr)); } else { elt->addr = ""; @@ -127,43 +126,43 @@ rspamd_email_address_add (rspamd_mempool_t *pool, if ((elt->flags & RSPAMD_EMAIL_ADDR_QUOTED) && elt->addr[0] == '"') { if (elt->flags & RSPAMD_EMAIL_ADDR_HAS_BACKSLASH) { /* We also need to unquote user */ - rspamd_email_address_unescape (elt); + rspamd_email_address_unescape(elt); } /* We need to unquote addr */ nlen = elt->domain_len + elt->user_len + 2; - elt->addr = g_malloc (nlen + 1); - rspamd_mempool_notify_alloc (pool, nlen + 1); - elt->addr_len = rspamd_snprintf ((char *)elt->addr, nlen, "%*s@%*s", - (gint)elt->user_len, elt->user, - (gint)elt->domain_len, elt->domain); + elt->addr = g_malloc(nlen + 1); + rspamd_mempool_notify_alloc(pool, nlen + 1); + elt->addr_len = rspamd_snprintf((char *) elt->addr, nlen, "%*s@%*s", + (gint) elt->user_len, elt->user, + (gint) elt->domain_len, elt->domain); elt->flags |= RSPAMD_EMAIL_ADDR_ADDR_ALLOCATED; } if (name->len > 0) { - rspamd_gstring_strip (name, " \t\v"); - elt->name = rspamd_mime_header_decode (pool, name->str, name->len, NULL); + rspamd_gstring_strip(name, " \t\v"); + elt->name = rspamd_mime_header_decode(pool, name->str, name->len, NULL); } - rspamd_mempool_notify_alloc (pool, name->len); - g_ptr_array_add (ar, elt); + rspamd_mempool_notify_alloc(pool, name->len); + g_ptr_array_add(ar, elt); } /* * Tries to parse an email address that doesn't conform RFC */ static gboolean -rspamd_email_address_parse_heuristic (const char *data, size_t len, - struct rspamd_email_address *addr) +rspamd_email_address_parse_heuristic(const char *data, size_t len, + struct rspamd_email_address *addr) { const gchar *p = data, *at = NULL, *end = data + len; gboolean ret = FALSE; - memset (addr, 0, sizeof (*addr)); + memset(addr, 0, sizeof(*addr)); if (*p == '<' && len > 1) { /* Angled address */ - addr->addr_len = rspamd_memcspn (p + 1, ">", len - 1); + addr->addr_len = rspamd_memcspn(p + 1, ">", len - 1); addr->addr = p + 1; addr->raw = p; addr->raw_len = len; @@ -182,7 +181,7 @@ rspamd_email_address_parse_heuristic (const char *data, size_t len, } if (ret) { - at = rspamd_memrchr (p, '@', len); + at = rspamd_memrchr(p, '@', len); if (at != NULL && at + 1 < end) { addr->domain = at + 1; @@ -191,7 +190,7 @@ rspamd_email_address_parse_heuristic (const char *data, size_t len, addr->user_len = at - p; } - if (rspamd_str_has_8bit (p, len)) { + if (rspamd_str_has_8bit(p, len)) { addr->flags |= RSPAMD_EMAIL_ADDR_HAS_8BIT; } } @@ -200,36 +199,36 @@ rspamd_email_address_parse_heuristic (const char *data, size_t len, } static inline int -rspamd_email_address_check_and_add (const gchar *start, gsize len, - GPtrArray *res, - rspamd_mempool_t *pool, - GString *ns, - gint max_elements) +rspamd_email_address_check_and_add(const gchar *start, gsize len, + GPtrArray *res, + rspamd_mempool_t *pool, + GString *ns, + gint max_elements) { struct rspamd_email_address addr; - g_assert (res != NULL); + g_assert(res != NULL); if (max_elements > 0 && res->len >= max_elements) { - msg_info_pool_check ("reached maximum number of elements %d when adding %v", - max_elements, - ns); + msg_info_pool_check("reached maximum number of elements %d when adding %v", + max_elements, + ns); return -1; } /* The whole email is likely address */ - memset (&addr, 0, sizeof (addr)); - rspamd_smtp_addr_parse (start, len, &addr); + memset(&addr, 0, sizeof(addr)); + rspamd_smtp_addr_parse(start, len, &addr); if (addr.flags & RSPAMD_EMAIL_ADDR_VALID) { - rspamd_email_address_add (pool, res, &addr, ns); + rspamd_email_address_add(pool, res, &addr, ns); } else { /* Try heuristic */ - if (rspamd_email_address_parse_heuristic (start, - len, &addr)) { - rspamd_email_address_add (pool, res, &addr, ns); + if (rspamd_email_address_parse_heuristic(start, + len, &addr)) { + rspamd_email_address_add(pool, res, &addr, ns); return 1; } @@ -242,10 +241,10 @@ rspamd_email_address_check_and_add (const gchar *start, gsize len, } GPtrArray * -rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, - guint len, - GPtrArray *src, - gint max_elements) +rspamd_email_address_from_mime(rspamd_mempool_t *pool, const gchar *hdr, + guint len, + GPtrArray *src, + gint max_elements) { GPtrArray *res = src; gboolean seen_at = FALSE, seen_obrace = FALSE; @@ -258,23 +257,24 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, parse_quoted, parse_addr, skip_spaces - } state = parse_name, next_state = parse_name; + } state = parse_name, + next_state = parse_name; if (res == NULL) { - res = g_ptr_array_sized_new (2); - rspamd_mempool_add_destructor (pool, rspamd_email_address_list_destroy, - res); + res = g_ptr_array_sized_new(2); + rspamd_mempool_add_destructor(pool, rspamd_email_address_list_destroy, + res); } else if (max_elements > 0 && res->len >= max_elements) { - msg_info_pool_check ("reached maximum number of elements %d", max_elements); + msg_info_pool_check("reached maximum number of elements %d", max_elements); return res; } - ns = g_string_sized_new (len); - cpy = g_string_sized_new (len); + ns = g_string_sized_new(len); + cpy = g_string_sized_new(len); - rspamd_mempool_add_destructor (pool, rspamd_gstring_free_hard, cpy); + rspamd_mempool_add_destructor(pool, rspamd_gstring_free_hard, cpy); /* First, we need to remove all comments as they are terrible */ obraces = 0; @@ -284,7 +284,7 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, if (state == parse_name) { if (*p == '\\') { if (obraces == 0) { - g_string_append_c (cpy, *p); + g_string_append_c(cpy, *p); } p++; @@ -294,11 +294,11 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, state = parse_quoted; } else if (*p == '(') { - obraces ++; /* To avoid ) itself being copied */ + obraces++; /* To avoid ) itself being copied */ } else if (*p == ')') { - ebraces ++; - p ++; + ebraces++; + p++; } if (obraces == ebraces) { @@ -308,13 +308,13 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, } if (p < end && obraces == 0) { - g_string_append_c (cpy, *p); + g_string_append_c(cpy, *p); } } else { /* Quoted elt */ if (*p == '\\') { - g_string_append_c (cpy, *p); + g_string_append_c(cpy, *p); p++; } else { @@ -324,7 +324,7 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, } if (p < end) { - g_string_append_c (cpy, *p); + g_string_append_c(cpy, *p); } } @@ -347,15 +347,15 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, t = p - 1; - while (t > c && g_ascii_isspace (*t)) { - t --; - nspaces ++; + while (t > c && g_ascii_isspace(*t)) { + t--; + nspaces++; } - g_string_append_len (ns, c, t - c + 1); + g_string_append_len(ns, c, t - c + 1); if (nspaces > 0) { - g_string_append_c (ns, ' '); + g_string_append_c(ns, ' '); } } @@ -366,11 +366,11 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, if (p > c) { t = p - 1; - while (t > c && g_ascii_isspace (*t)) { - t --; + while (t > c && g_ascii_isspace(*t)) { + t--; } - g_string_append_len (ns, c, t - c + 1); + g_string_append_len(ns, c, t - c + 1); } c = p; @@ -384,23 +384,23 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, */ t = p - 1; - while (t > c && g_ascii_isspace (*t)) { - t --; + while (t > c && g_ascii_isspace(*t)) { + t--; } - int check = rspamd_email_address_check_and_add (c, t - c + 1, - res, pool, ns, max_elements); + int check = rspamd_email_address_check_and_add(c, t - c + 1, + res, pool, ns, max_elements); if (check == 0 && res->len == 0) { /* Insert fake address */ - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } else if (check != 1) { goto end; } /* Cleanup for the next use */ - g_string_set_size (ns, 0); + g_string_set_size(ns, 0); seen_at = FALSE; } @@ -411,24 +411,24 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, seen_at = TRUE; } - p ++; + p++; break; case parse_quoted: if (*p == '\\') { if (p > c) { - g_string_append_len (ns, c, p - c); + g_string_append_len(ns, c, p - c); } - p ++; + p++; c = p; } else if (*p == '"') { if (p > c) { - g_string_append_len (ns, c, p - c); + g_string_append_len(ns, c, p - c); } - if (p + 1 < end && g_ascii_isspace (p[1])) { - g_string_append_c (ns, ' '); + if (p + 1 < end && g_ascii_isspace(p[1])) { + g_string_append_c(ns, ' '); } state = skip_spaces; @@ -440,22 +440,22 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, else if (*p == '<') { seen_obrace = TRUE; } - p ++; + p++; break; case parse_addr: if (*p == '>') { - int check = rspamd_email_address_check_and_add (c, p - c + 1, - res, pool, ns, max_elements); + int check = rspamd_email_address_check_and_add(c, p - c + 1, + res, pool, ns, max_elements); if (check == 0 && res->len == 0) { /* Insert a fake address */ - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } else if (check != 1) { goto end; } /* Cleanup for the next use */ - g_string_set_size (ns, 0); + g_string_set_size(ns, 0); seen_at = FALSE; state = skip_spaces; next_state = parse_name; @@ -463,15 +463,15 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, else if (*p == '@') { seen_at = TRUE; } - p ++; + p++; break; case skip_spaces: - if (!g_ascii_isspace (*p)) { + if (!g_ascii_isspace(*p)) { c = p; state = next_state; } else { - p ++; + p++; } break; } @@ -482,42 +482,43 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, case parse_name: /* Assume the whole header as name (bad thing) */ if (p > c) { - while (p > c && g_ascii_isspace (*p)) { - p --; + while (p > c && g_ascii_isspace(*p)) { + p--; } if (p > c) { if (seen_at) { /* The whole email is likely address */ - int check = rspamd_email_address_check_and_add (c, p - c, - res, pool, ns, max_elements); + int check = rspamd_email_address_check_and_add(c, p - c, + res, pool, ns, max_elements); if (check == 0 && res->len == 0) { /* Insert a fake address */ - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } else if (check != 1) { goto end; } - } else { + } + else { /* No @ seen */ - g_string_append_len (ns, c, p - c); + g_string_append_len(ns, c, p - c); if (res->len == 0) { - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } } } else if (res->len == 0) { - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } } break; case parse_addr: if (p > c) { - if (rspamd_email_address_check_and_add (c, p - c, - res, pool, ns, max_elements) == 0) { + if (rspamd_email_address_check_and_add(c, p - c, + res, pool, ns, max_elements) == 0) { if (res->len == 0) { - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } } } @@ -526,12 +527,12 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, /* Unfinished quoted string or a comment */ /* If we have seen obrace + at, then we still can try to resolve address */ if (seen_at && seen_obrace) { - p = rspamd_memrchr (cpy->str, '<', cpy->len); - g_assert (p != NULL); - if (rspamd_email_address_check_and_add (p, end - p, - res, pool, ns, max_elements) == 0) { + p = rspamd_memrchr(cpy->str, '<', cpy->len); + g_assert(p != NULL); + if (rspamd_email_address_check_and_add(p, end - p, + res, pool, ns, max_elements) == 0) { if (res->len == 0) { - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } } } @@ -541,22 +542,22 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, break; } end: - rspamd_mempool_notify_alloc (pool, cpy->len); - g_string_free (ns, TRUE); + rspamd_mempool_notify_alloc(pool, cpy->len); + g_string_free(ns, TRUE); return res; } -void -rspamd_email_address_list_destroy (gpointer ptr) +void rspamd_email_address_list_destroy(gpointer ptr) { GPtrArray *ar = ptr; guint i; struct rspamd_email_address *addr; - PTR_ARRAY_FOREACH (ar, i, addr) { - rspamd_email_address_free (addr); + PTR_ARRAY_FOREACH(ar, i, addr) + { + rspamd_email_address_free(addr); } - g_ptr_array_free (ar, TRUE); + g_ptr_array_free(ar, TRUE); }
\ No newline at end of file |