From 8cdf6a7b1640ca4c850ea51fa6a48e1f9ae40a71 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 11 Jan 2016 14:12:35 +0000 Subject: [PATCH] Add generic routine to get task's recipient --- src/libserver/task.c | 34 ++++++++++++++++++++++++++ src/libserver/task.h | 10 ++++++++ src/libstat/backends/redis_backend.c | 29 +++++----------------- src/libstat/backends/sqlite3_backend.c | 23 +---------------- 4 files changed, 51 insertions(+), 45 deletions(-) diff --git a/src/libserver/task.c b/src/libserver/task.c index 2f79b540b..657fcdc11 100644 --- a/src/libserver/task.c +++ b/src/libserver/task.c @@ -552,6 +552,40 @@ rspamd_task_get_sender (struct rspamd_task *task) #endif } +const gchar * +rspamd_task_get_principal_recipient (struct rspamd_task *task) +{ + InternetAddress *iaelt = NULL; + + if (task->deliver_to) { + return task->deliver_to; + } + +#ifdef GMIME24 + InternetAddressMailbox *imb; + + if (task->rcpt_envelope != NULL) { + iaelt = internet_address_list_get_address (task->rcpt_envelope, 0); + } + else if (task->rcpt_mime != NULL) { + iaelt = internet_address_list_get_address (task->rcpt_mime, 0); + } + imb = INTERNET_ADDRESS_IS_MAILBOX(iaelt) ? + INTERNET_ADDRESS_MAILBOX (iaelt) : NULL; + + return (imb ? internet_address_mailbox_get_addr (imb) : NULL); +#else + if (task->rcpt_envelope != NULL) { + iaelt = internet_address_list_get_address (task->rcpt_envelope); + } + else if (task->rcpt_mime != NULL) { + iaelt = internet_address_list_get_address (task->rcpt_mime); + } + + return (iaelt != NULL ? internet_address_get_addr (iaelt) : NULL); +#endif +} + gboolean rspamd_task_add_recipient (struct rspamd_task *task, const gchar *rcpt) { diff --git a/src/libserver/task.h b/src/libserver/task.h index d64d6461c..45e8826db 100644 --- a/src/libserver/task.h +++ b/src/libserver/task.h @@ -248,6 +248,16 @@ gboolean rspamd_task_process (struct rspamd_task *task, guint stages); */ const gchar *rspamd_task_get_sender (struct rspamd_task *task); +/** + * Return addresses in the following precendence: + * - deliver to + * - the first smtp recipient + * - the first mime recipient + * @param task + * @return + */ +const gchar *rspamd_task_get_principal_recipient (struct rspamd_task *task); + /** * Add a recipient for a task * @param task task object diff --git a/src/libstat/backends/redis_backend.c b/src/libstat/backends/redis_backend.c index f1857871e..9de80cedb 100644 --- a/src/libstat/backends/redis_backend.c +++ b/src/libstat/backends/redis_backend.c @@ -108,9 +108,6 @@ rspamd_redis_expand_object (const gchar *pattern, { gsize tlen = 0; const gchar *p = pattern, *elt; - InternetAddressList *ia; - InternetAddress *iaelt; - InternetAddressMailbox *imb; gchar *d, *end; enum { just_char, @@ -153,17 +150,10 @@ rspamd_redis_expand_object (const gchar *pattern, } break; case 'r': - ia = GET_TASK_ELT (task, rcpt_envelope); - if (ia != NULL) { - iaelt = internet_address_list_get_address (ia, 0); - imb = INTERNET_ADDRESS_IS_MAILBOX (iaelt) ? - INTERNET_ADDRESS_MAILBOX (iaelt) : NULL; + elt = rspamd_task_get_principal_recipient (task); - elt = (imb ? internet_address_mailbox_get_addr (imb) : NULL); - - if (elt) { - tlen += strlen (elt); - } + if (elt) { + tlen += strlen (elt); } break; case 'l': @@ -246,17 +236,10 @@ rspamd_redis_expand_object (const gchar *pattern, } break; case 'r': - ia = GET_TASK_ELT (task, rcpt_envelope); - if (ia != NULL) { - iaelt = internet_address_list_get_address (ia, 0); - imb = INTERNET_ADDRESS_IS_MAILBOX (iaelt) ? - INTERNET_ADDRESS_MAILBOX (iaelt) : NULL; + elt = rspamd_task_get_principal_recipient (task); - elt = (imb ? internet_address_mailbox_get_addr (imb) : NULL); - - if (elt) { - d += rspamd_strlcpy (d, elt, end - d); - } + if (elt) { + d += rspamd_strlcpy (d, elt, end - d); } break; case 'l': diff --git a/src/libstat/backends/sqlite3_backend.c b/src/libstat/backends/sqlite3_backend.c index 3e05bee80..ab1d24a71 100644 --- a/src/libstat/backends/sqlite3_backend.c +++ b/src/libstat/backends/sqlite3_backend.c @@ -303,33 +303,12 @@ rspamd_sqlite3_get_user (struct rspamd_stat_sqlite3_db *db, gint64 id = 0; /* Default user is 0 */ gint rc, err_idx; const gchar *user = NULL; - const InternetAddress *ia; struct rspamd_task **ptask; lua_State *L = db->L; GString *tb; if (db->cbref_user == -1) { - if (task->deliver_to != NULL) { - /* Use deliver-to value if presented */ - user = task->deliver_to; - } - if (task->user != NULL) { - /* Use user value if presented */ - user = task->user; - } - else if (task->rcpt_envelope != NULL) { - /* Check envelope recipients */ - if (internet_address_list_length (task->rcpt_envelope) == 1) { - /* XXX: we support now merely single recipient statistics */ - ia = internet_address_list_get_address (task->rcpt_envelope, 0); - - if (ia != NULL) { - user = internet_address_mailbox_get_addr ( - INTERNET_ADDRESS_MAILBOX (ia)); - } - } - } - /* XXX: We ignore now mime recipients as they could be easily forged */ + user = rspamd_task_get_principal_recipient (task); } else { /* Execute lua function to get userdata */ -- 2.39.5