]> source.dussan.org Git - rspamd.git/commitdiff
Add generic routine to get task's recipient
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 11 Jan 2016 14:12:35 +0000 (14:12 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 11 Jan 2016 14:12:35 +0000 (14:12 +0000)
src/libserver/task.c
src/libserver/task.h
src/libstat/backends/redis_backend.c
src/libstat/backends/sqlite3_backend.c

index 2f79b540b47b8cec022608188606b578c0ad842c..657fcdc11863ad64e1e09b4063c90e59a15b9426 100644 (file)
@@ -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)
 {
index d64d6461c04cf4793f49fd4d787e07dd9b69af58..45e8826db083bac45d6f9bdb3afecb965a42825b 100644 (file)
@@ -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
index f1857871e8556b043b8c9398aa27227f7bb02745..9de80cedbdae2f7d91395e8a1c291aebdf7dff92 100644 (file)
@@ -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':
index 3e05bee808cd59f973aec448c6705a1797878bf4..ab1d24a719cc1c1d430d6d2564e8c59cc524f65a 100644 (file)
@@ -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 */