]> source.dussan.org Git - rspamd.git/commitdiff
* Add support of whitelists in settings (by Anton Nekhoroshin)
authorcebka@lenovo-laptop <cebka@lenovo-laptop>
Wed, 30 Dec 2009 12:54:48 +0000 (15:54 +0300)
committercebka@lenovo-laptop <cebka@lenovo-laptop>
Wed, 30 Dec 2009 12:54:48 +0000 (15:54 +0300)
src/settings.c
src/settings.h

index 6de04a7ebd53215bd41aa81de10af7883f638038..cd7167eddc88153e26a743e6dbb618940bdf02db 100644 (file)
@@ -53,6 +53,9 @@ settings_free (gpointer data)
        if (s->reject_scores) {
                g_hash_table_destroy (s->reject_scores);
        }
+       if (s->whitelist) {
+               g_hash_table_destroy (s->whitelist);
+       }
        g_free (s);
 }
 
@@ -101,7 +104,7 @@ void
 json_fin_cb (memory_pool_t * pool, struct map_cb_data *data)
 {
        struct json_buf                *jb;
-       int                             nelts, i;
+       int                             nelts, i, n, a;
        json_t                         *js, *cur_elt, *cur_nm, *it_val;
        json_error_t                    je;
        struct rspamd_settings         *cur_settings;
@@ -154,6 +157,7 @@ json_fin_cb (memory_pool_t * pool, struct map_cb_data *data)
                cur_settings->metric_scores = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
                cur_settings->reject_scores = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
                cur_settings->factors = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+               cur_settings->whitelist = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
                cur_settings->statfile_alias = NULL;
                cur_settings->want_spam = FALSE;
 
@@ -217,6 +221,18 @@ json_fin_cb (memory_pool_t * pool, struct map_cb_data *data)
                                json_it = json_object_iter_next(cur_nm, json_it);
                        }
                }
+               /* Whitelist object */
+               cur_nm = json_object_get (cur_elt, "whitelist");
+               if (cur_nm != NULL && json_is_array (cur_nm)) {
+                       n = json_array_size(cur_nm);
+                       for(a = 0; a < n; a++) {
+                               it_val = json_array_get(cur_nm, a);
+                               if (it_val && json_is_string (it_val)) {
+                                       g_hash_table_insert (cur_settings->whitelist, g_strdup (json_string_value (it_val)), g_strdup (json_string_value (it_val)));
+                               }
+                   
+                       }
+               }
                /* Want spam */
                cur_nm = json_object_get (cur_elt, "want_spam");
                if (cur_nm != NULL) {
@@ -302,6 +318,29 @@ check_setting (struct worker_task *task, struct rspamd_settings **user_settings,
        return FALSE;
 }
 
+static                         gboolean
+check_whitelist(struct worker_task *task, struct rspamd_settings *s)
+{
+       char *src_email = NULL, *src_domain = NULL;
+
+       if (task->from != NULL) {
+               src_email = task->from;
+       } else {
+               return FALSE;
+       }
+
+       src_domain = strchr (src_email, '@');
+       if(src_domain != NULL) {
+               src_domain++;
+       }
+
+       if (((g_hash_table_lookup (s->whitelist, src_email) != NULL) ||
+                       ( (src_domain != NULL) && (g_hash_table_lookup (s->whitelist, src_domain) != NULL)) )) {
+               return TRUE;
+       }
+       return FALSE;
+}
+
 gboolean
 check_metric_settings (struct worker_task * task, struct metric * metric, double *score, double *rscore)
 {
@@ -312,7 +351,11 @@ check_metric_settings (struct worker_task * task, struct metric * metric, double
 
        if (check_setting (task, &us, &ds)) {
                if (us != NULL) {
-                       /* First search in user's settings */
+                       /* First look in user white list */
+                       if (check_whitelist(task, us)) {
+                               *score = DEFAULT_REJECT_SCORE;
+                               return TRUE;
+                       }
                        if ((rs = g_hash_table_lookup (us->reject_scores, metric->name)) != NULL) {
                                *rscore = *rs;
                        }
@@ -330,6 +373,10 @@ check_metric_settings (struct worker_task * task, struct metric * metric, double
                        }
                }
                else if (ds != NULL) {
+                       if (check_whitelist(task, ds)) {
+                               *score = DEFAULT_REJECT_SCORE;
+                               return TRUE;
+                       }
                        if ((rs = g_hash_table_lookup (ds->reject_scores, metric->name)) != NULL) {
                                *rscore = *rs;
                        }
index 58e877570bda593e19d1649740590504f63d1df3..1f7ee743f305902c4c20fe6a3faa0346573c6462 100644 (file)
@@ -8,6 +8,7 @@ struct rspamd_settings {
        GHashTable *metric_scores;                      /**< hash table of metric require scores for this setting               */
        GHashTable *reject_scores;                      /**< hash table of metric reject scores for this setting                */
        GHashTable *factors;                            /**< hash table of new factors for this setting                 */
+       GHashTable *whitelist;                          /**< hash table of whitelist for this setting                   */
        char *statfile_alias;                           /**< alias for statfile used                                                    */
        gboolean want_spam;                                     /**< if true disable rspamd checks                                              */
 };