diff options
Diffstat (limited to 'src/libstat/learn_cache')
-rw-r--r-- | src/libstat/learn_cache/learn_cache.h | 58 | ||||
-rw-r--r-- | src/libstat/learn_cache/redis_cache.c | 326 | ||||
-rw-r--r-- | src/libstat/learn_cache/sqlite3_cache.c | 251 |
3 files changed, 307 insertions, 328 deletions
diff --git a/src/libstat/learn_cache/learn_cache.h b/src/libstat/learn_cache/learn_cache.h index ad13ecf29..11a66fc09 100644 --- a/src/libstat/learn_cache/learn_cache.h +++ b/src/libstat/learn_cache/learn_cache.h @@ -19,7 +19,7 @@ #include "config.h" #include "ucl.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -33,46 +33,46 @@ struct rspamd_statfile; struct rspamd_stat_cache { const char *name; - gpointer (*init) (struct rspamd_stat_ctx *ctx, - struct rspamd_config *cfg, - struct rspamd_statfile *st, - const ucl_object_t *cf); + gpointer (*init)(struct rspamd_stat_ctx *ctx, + struct rspamd_config *cfg, + struct rspamd_statfile *st, + const ucl_object_t *cf); - gpointer (*runtime) (struct rspamd_task *task, - gpointer ctx, gboolean learn); + gpointer (*runtime)(struct rspamd_task *task, + gpointer ctx, gboolean learn); - gint (*check) (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime); + gint (*check)(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime); - gint (*learn) (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime); + gint (*learn)(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime); - void (*close) (gpointer ctx); + void (*close)(gpointer ctx); gpointer ctx; }; -#define RSPAMD_STAT_CACHE_DEF(name) \ - gpointer rspamd_stat_cache_##name##_init (struct rspamd_stat_ctx *ctx, \ - struct rspamd_config *cfg, \ - struct rspamd_statfile *st, \ - const ucl_object_t *cf); \ - gpointer rspamd_stat_cache_##name##_runtime (struct rspamd_task *task, \ - gpointer ctx, gboolean learn); \ - gint rspamd_stat_cache_##name##_check (struct rspamd_task *task, \ - gboolean is_spam, \ - gpointer runtime); \ - gint rspamd_stat_cache_##name##_learn (struct rspamd_task *task, \ - gboolean is_spam, \ - gpointer runtime); \ - void rspamd_stat_cache_##name##_close (gpointer ctx) +#define RSPAMD_STAT_CACHE_DEF(name) \ + gpointer rspamd_stat_cache_##name##_init(struct rspamd_stat_ctx *ctx, \ + struct rspamd_config *cfg, \ + struct rspamd_statfile *st, \ + const ucl_object_t *cf); \ + gpointer rspamd_stat_cache_##name##_runtime(struct rspamd_task *task, \ + gpointer ctx, gboolean learn); \ + gint rspamd_stat_cache_##name##_check(struct rspamd_task *task, \ + gboolean is_spam, \ + gpointer runtime); \ + gint rspamd_stat_cache_##name##_learn(struct rspamd_task *task, \ + gboolean is_spam, \ + gpointer runtime); \ + void rspamd_stat_cache_##name##_close(gpointer ctx) RSPAMD_STAT_CACHE_DEF(sqlite3); RSPAMD_STAT_CACHE_DEF(redis); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libstat/learn_cache/redis_cache.c b/src/libstat/learn_cache/redis_cache.c index 479623942..d5fe4ad48 100644 --- a/src/libstat/learn_cache/redis_cache.c +++ b/src/libstat/learn_cache/redis_cache.c @@ -52,78 +52,78 @@ struct rspamd_redis_cache_runtime { }; static GQuark -rspamd_stat_cache_redis_quark (void) +rspamd_stat_cache_redis_quark(void) { - return g_quark_from_static_string (M); + return g_quark_from_static_string(M); } static inline struct upstream_list * -rspamd_redis_get_servers (struct rspamd_redis_cache_ctx *ctx, - const gchar *what) +rspamd_redis_get_servers(struct rspamd_redis_cache_ctx *ctx, + const gchar *what) { lua_State *L = ctx->L; struct upstream_list *res; - lua_rawgeti (L, LUA_REGISTRYINDEX, ctx->conf_ref); - lua_pushstring (L, what); - lua_gettable (L, -2); - res = *((struct upstream_list**)lua_touserdata (L, -1)); - lua_settop (L, 0); + lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->conf_ref); + lua_pushstring(L, what); + lua_gettable(L, -2); + res = *((struct upstream_list **) lua_touserdata(L, -1)); + lua_settop(L, 0); return res; } static void -rspamd_redis_cache_maybe_auth (struct rspamd_redis_cache_ctx *ctx, - redisAsyncContext *redis) +rspamd_redis_cache_maybe_auth(struct rspamd_redis_cache_ctx *ctx, + redisAsyncContext *redis) { if (ctx->password) { - redisAsyncCommand (redis, NULL, NULL, "AUTH %s", ctx->password); + redisAsyncCommand(redis, NULL, NULL, "AUTH %s", ctx->password); } if (ctx->dbname) { - redisAsyncCommand (redis, NULL, NULL, "SELECT %s", ctx->dbname); + redisAsyncCommand(redis, NULL, NULL, "SELECT %s", ctx->dbname); } } /* Called on connection termination */ static void -rspamd_redis_cache_fin (gpointer data) +rspamd_redis_cache_fin(gpointer data) { struct rspamd_redis_cache_runtime *rt = data; redisAsyncContext *redis; rt->has_event = FALSE; - ev_timer_stop (rt->task->event_loop, &rt->timer_ev); + ev_timer_stop(rt->task->event_loop, &rt->timer_ev); if (rt->redis) { redis = rt->redis; rt->redis = NULL; /* This calls for all callbacks pending */ - redisAsyncFree (redis); + redisAsyncFree(redis); } } static void -rspamd_redis_cache_timeout (EV_P_ ev_timer *w, int revents) +rspamd_redis_cache_timeout(EV_P_ ev_timer *w, int revents) { struct rspamd_redis_cache_runtime *rt = - (struct rspamd_redis_cache_runtime *)w->data; + (struct rspamd_redis_cache_runtime *) w->data; struct rspamd_task *task; task = rt->task; - msg_err_task ("connection to redis server %s timed out", - rspamd_upstream_name (rt->selected)); - rspamd_upstream_fail (rt->selected, FALSE, "timeout"); + msg_err_task("connection to redis server %s timed out", + rspamd_upstream_name(rt->selected)); + rspamd_upstream_fail(rt->selected, FALSE, "timeout"); if (rt->has_event) { - rspamd_session_remove_event (task->s, rspamd_redis_cache_fin, rt); + rspamd_session_remove_event(task->s, rspamd_redis_cache_fin, rt); } } /* Called when we have checked the specified message id */ static void -rspamd_stat_cache_redis_get (redisAsyncContext *c, gpointer r, gpointer priv) +rspamd_stat_cache_redis_get(redisAsyncContext *c, gpointer r, gpointer priv) { struct rspamd_redis_cache_runtime *rt = priv; redisReply *reply = r; @@ -134,20 +134,20 @@ rspamd_stat_cache_redis_get (redisAsyncContext *c, gpointer r, gpointer priv) if (c->err == 0) { if (reply) { - if (G_LIKELY (reply->type == REDIS_REPLY_INTEGER)) { + if (G_LIKELY(reply->type == REDIS_REPLY_INTEGER)) { val = reply->integer; } else if (reply->type == REDIS_REPLY_STRING) { - rspamd_strtol (reply->str, reply->len, &val); + rspamd_strtol(reply->str, reply->len, &val); } else { if (reply->type == REDIS_REPLY_ERROR) { - msg_err_task ("cannot learn %s: redis error: \"%s\"", - rt->ctx->stcf->symbol, reply->str); + msg_err_task("cannot learn %s: redis error: \"%s\"", + rt->ctx->stcf->symbol, reply->str); } else if (reply->type != REDIS_REPLY_NIL) { - msg_err_task ("bad learned type for %s: %d", - rt->ctx->stcf->symbol, reply->type); + msg_err_task("bad learned type for %s: %d", + rt->ctx->stcf->symbol, reply->type); } val = 0; @@ -155,11 +155,12 @@ rspamd_stat_cache_redis_get (redisAsyncContext *c, gpointer r, gpointer priv) } if ((val > 0 && (task->flags & RSPAMD_TASK_FLAG_LEARN_SPAM)) || - (val < 0 && (task->flags & RSPAMD_TASK_FLAG_LEARN_HAM))) { + (val < 0 && (task->flags & RSPAMD_TASK_FLAG_LEARN_HAM))) { /* Already learned */ - msg_info_task ("<%s> has been already " - "learned as %s, ignore it", MESSAGE_FIELD (task, message_id), - (task->flags & RSPAMD_TASK_FLAG_LEARN_SPAM) ? "spam" : "ham"); + msg_info_task("<%s> has been already " + "learned as %s, ignore it", + MESSAGE_FIELD(task, message_id), + (task->flags & RSPAMD_TASK_FLAG_LEARN_SPAM) ? "spam" : "ham"); task->flags |= RSPAMD_TASK_FLAG_ALREADY_LEARNED; } else if (val != 0) { @@ -167,20 +168,20 @@ rspamd_stat_cache_redis_get (redisAsyncContext *c, gpointer r, gpointer priv) task->flags |= RSPAMD_TASK_FLAG_UNLEARN; } - rspamd_upstream_ok (rt->selected); + rspamd_upstream_ok(rt->selected); } else { - rspamd_upstream_fail (rt->selected, FALSE, c->errstr); + rspamd_upstream_fail(rt->selected, FALSE, c->errstr); } if (rt->has_event) { - rspamd_session_remove_event (task->s, rspamd_redis_cache_fin, rt); + rspamd_session_remove_event(task->s, rspamd_redis_cache_fin, rt); } } /* Called when we have learned the specified message id */ static void -rspamd_stat_cache_redis_set (redisAsyncContext *c, gpointer r, gpointer priv) +rspamd_stat_cache_redis_set(redisAsyncContext *c, gpointer r, gpointer priv) { struct rspamd_redis_cache_runtime *rt = priv; struct rspamd_task *task; @@ -189,19 +190,19 @@ rspamd_stat_cache_redis_set (redisAsyncContext *c, gpointer r, gpointer priv) if (c->err == 0) { /* XXX: we ignore results here */ - rspamd_upstream_ok (rt->selected); + rspamd_upstream_ok(rt->selected); } else { - rspamd_upstream_fail (rt->selected, FALSE, c->errstr); + rspamd_upstream_fail(rt->selected, FALSE, c->errstr); } if (rt->has_event) { - rspamd_session_remove_event (task->s, rspamd_redis_cache_fin, rt); + rspamd_session_remove_event(task->s, rspamd_redis_cache_fin, rt); } } static void -rspamd_stat_cache_redis_generate_id (struct rspamd_task *task) +rspamd_stat_cache_redis_generate_id(struct rspamd_task *task) { rspamd_cryptobox_hash_state_t st; rspamd_token_t *tok; @@ -210,98 +211,98 @@ rspamd_stat_cache_redis_generate_id (struct rspamd_task *task) gchar *b32out; gchar *user = NULL; - rspamd_cryptobox_hash_init (&st, NULL, 0); + rspamd_cryptobox_hash_init(&st, NULL, 0); - user = rspamd_mempool_get_variable (task->task_pool, "stat_user"); + user = rspamd_mempool_get_variable(task->task_pool, "stat_user"); /* Use dedicated hash space for per users cache */ if (user != NULL) { - rspamd_cryptobox_hash_update (&st, user, strlen (user)); + rspamd_cryptobox_hash_update(&st, user, strlen(user)); } - for (i = 0; i < task->tokens->len; i ++) { - tok = g_ptr_array_index (task->tokens, i); - rspamd_cryptobox_hash_update (&st, (guchar *)&tok->data, - sizeof (tok->data)); + for (i = 0; i < task->tokens->len; i++) { + tok = g_ptr_array_index(task->tokens, i); + rspamd_cryptobox_hash_update(&st, (guchar *) &tok->data, + sizeof(tok->data)); } - rspamd_cryptobox_hash_final (&st, out); + rspamd_cryptobox_hash_final(&st, out); - b32out = rspamd_mempool_alloc (task->task_pool, - sizeof (out) * 8 / 5 + 3); - i = rspamd_encode_base32_buf (out, sizeof (out), b32out, - sizeof (out) * 8 / 5 + 2, RSPAMD_BASE32_DEFAULT); + b32out = rspamd_mempool_alloc(task->task_pool, + sizeof(out) * 8 / 5 + 3); + i = rspamd_encode_base32_buf(out, sizeof(out), b32out, + sizeof(out) * 8 / 5 + 2, RSPAMD_BASE32_DEFAULT); if (i > 0) { /* Zero terminate */ b32out[i] = '\0'; } - rspamd_mempool_set_variable (task->task_pool, "words_hash", b32out, NULL); + rspamd_mempool_set_variable(task->task_pool, "words_hash", b32out, NULL); } gpointer -rspamd_stat_cache_redis_init (struct rspamd_stat_ctx *ctx, - struct rspamd_config *cfg, - struct rspamd_statfile *st, - const ucl_object_t *cf) +rspamd_stat_cache_redis_init(struct rspamd_stat_ctx *ctx, + struct rspamd_config *cfg, + struct rspamd_statfile *st, + const ucl_object_t *cf) { struct rspamd_redis_cache_ctx *cache_ctx; struct rspamd_statfile_config *stf = st->stcf; const ucl_object_t *obj; gboolean ret = FALSE; - lua_State *L = (lua_State *)cfg->lua_state; + lua_State *L = (lua_State *) cfg->lua_state; gint conf_ref = -1; - cache_ctx = g_malloc0 (sizeof (*cache_ctx)); + cache_ctx = g_malloc0(sizeof(*cache_ctx)); cache_ctx->timeout = REDIS_DEFAULT_TIMEOUT; cache_ctx->L = L; /* First search in backend configuration */ - obj = ucl_object_lookup (st->classifier->cfg->opts, "backend"); - if (obj != NULL && ucl_object_type (obj) == UCL_OBJECT) { - ret = rspamd_lua_try_load_redis (L, obj, cfg, &conf_ref); + obj = ucl_object_lookup(st->classifier->cfg->opts, "backend"); + if (obj != NULL && ucl_object_type(obj) == UCL_OBJECT) { + ret = rspamd_lua_try_load_redis(L, obj, cfg, &conf_ref); } /* Now try statfiles config */ if (!ret && stf->opts) { - ret = rspamd_lua_try_load_redis (L, stf->opts, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, stf->opts, cfg, &conf_ref); } /* Now try classifier config */ if (!ret && st->classifier->cfg->opts) { - ret = rspamd_lua_try_load_redis (L, st->classifier->cfg->opts, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, st->classifier->cfg->opts, cfg, &conf_ref); } /* Now try global redis settings */ if (!ret) { - obj = ucl_object_lookup (cfg->rcl_obj, "redis"); + obj = ucl_object_lookup(cfg->rcl_obj, "redis"); if (obj) { const ucl_object_t *specific_obj; - specific_obj = ucl_object_lookup (obj, "statistics"); + specific_obj = ucl_object_lookup(obj, "statistics"); if (specific_obj) { - ret = rspamd_lua_try_load_redis (L, - specific_obj, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, + specific_obj, cfg, &conf_ref); } else { - ret = rspamd_lua_try_load_redis (L, - obj, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, + obj, cfg, &conf_ref); } } } if (!ret) { - msg_err_config ("cannot init redis cache for %s", stf->symbol); - g_free (cache_ctx); + msg_err_config("cannot init redis cache for %s", stf->symbol); + g_free(cache_ctx); return NULL; } - obj = ucl_object_lookup (st->classifier->cfg->opts, "cache_key"); + obj = ucl_object_lookup(st->classifier->cfg->opts, "cache_key"); if (obj) { - cache_ctx->redis_object = ucl_object_tostring (obj); + cache_ctx->redis_object = ucl_object_tostring(obj); } else { cache_ctx->redis_object = DEFAULT_REDIS_KEY; @@ -310,41 +311,41 @@ rspamd_stat_cache_redis_init (struct rspamd_stat_ctx *ctx, cache_ctx->conf_ref = conf_ref; /* Check some common table values */ - lua_rawgeti (L, LUA_REGISTRYINDEX, conf_ref); + lua_rawgeti(L, LUA_REGISTRYINDEX, conf_ref); - lua_pushstring (L, "timeout"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { - cache_ctx->timeout = lua_tonumber (L, -1); + lua_pushstring(L, "timeout"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TNUMBER) { + cache_ctx->timeout = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "db"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - cache_ctx->dbname = rspamd_mempool_strdup (cfg->cfg_pool, - lua_tostring (L, -1)); + lua_pushstring(L, "db"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TSTRING) { + cache_ctx->dbname = rspamd_mempool_strdup(cfg->cfg_pool, + lua_tostring(L, -1)); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "password"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - cache_ctx->password = rspamd_mempool_strdup (cfg->cfg_pool, - lua_tostring (L, -1)); + lua_pushstring(L, "password"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TSTRING) { + cache_ctx->password = rspamd_mempool_strdup(cfg->cfg_pool, + lua_tostring(L, -1)); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_settop (L, 0); + lua_settop(L, 0); cache_ctx->stcf = stf; - return (gpointer)cache_ctx; + return (gpointer) cache_ctx; } gpointer -rspamd_stat_cache_redis_runtime (struct rspamd_task *task, - gpointer c, gboolean learn) +rspamd_stat_cache_redis_runtime(struct rspamd_task *task, + gpointer c, gboolean learn) { struct rspamd_redis_cache_ctx *ctx = c; struct rspamd_redis_cache_runtime *rt; @@ -352,120 +353,119 @@ rspamd_stat_cache_redis_runtime (struct rspamd_task *task, struct upstream_list *ups; rspamd_inet_addr_t *addr; - g_assert (ctx != NULL); + g_assert(ctx != NULL); if (task->tokens == NULL || task->tokens->len == 0) { return NULL; } if (learn) { - ups = rspamd_redis_get_servers (ctx, "write_servers"); + ups = rspamd_redis_get_servers(ctx, "write_servers"); if (!ups) { - msg_err_task ("no write servers defined for %s, cannot learn", - ctx->stcf->symbol); + msg_err_task("no write servers defined for %s, cannot learn", + ctx->stcf->symbol); return NULL; } - up = rspamd_upstream_get (ups, - RSPAMD_UPSTREAM_MASTER_SLAVE, - NULL, - 0); + up = rspamd_upstream_get(ups, + RSPAMD_UPSTREAM_MASTER_SLAVE, + NULL, + 0); } else { - ups = rspamd_redis_get_servers (ctx, "read_servers"); + ups = rspamd_redis_get_servers(ctx, "read_servers"); if (!ups) { - msg_err_task ("no read servers defined for %s, cannot check", - ctx->stcf->symbol); + msg_err_task("no read servers defined for %s, cannot check", + ctx->stcf->symbol); return NULL; } - up = rspamd_upstream_get (ups, - RSPAMD_UPSTREAM_ROUND_ROBIN, - NULL, - 0); + up = rspamd_upstream_get(ups, + RSPAMD_UPSTREAM_ROUND_ROBIN, + NULL, + 0); } if (up == NULL) { - msg_err_task ("no upstreams reachable"); + msg_err_task("no upstreams reachable"); return NULL; } - rt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*rt)); + rt = rspamd_mempool_alloc0(task->task_pool, sizeof(*rt)); rt->selected = up; rt->task = task; rt->ctx = ctx; - addr = rspamd_upstream_addr_next (up); - g_assert (addr != NULL); + addr = rspamd_upstream_addr_next(up); + g_assert(addr != NULL); - if (rspamd_inet_address_get_af (addr) == AF_UNIX) { - rt->redis = redisAsyncConnectUnix (rspamd_inet_address_to_string (addr)); + if (rspamd_inet_address_get_af(addr) == AF_UNIX) { + rt->redis = redisAsyncConnectUnix(rspamd_inet_address_to_string(addr)); } else { - rt->redis = redisAsyncConnect (rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr)); + rt->redis = redisAsyncConnect(rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr)); } if (rt->redis == NULL) { - msg_warn_task ("cannot connect to redis server %s: %s", - rspamd_inet_address_to_string_pretty (addr), - strerror (errno)); + msg_warn_task("cannot connect to redis server %s: %s", + rspamd_inet_address_to_string_pretty(addr), + strerror(errno)); return NULL; } else if (rt->redis->err != REDIS_OK) { - msg_warn_task ("cannot connect to redis server %s: %s", - rspamd_inet_address_to_string_pretty (addr), - rt->redis->errstr); - redisAsyncFree (rt->redis); + msg_warn_task("cannot connect to redis server %s: %s", + rspamd_inet_address_to_string_pretty(addr), + rt->redis->errstr); + redisAsyncFree(rt->redis); rt->redis = NULL; return NULL; } - redisLibevAttach (task->event_loop, rt->redis); + redisLibevAttach(task->event_loop, rt->redis); /* Now check stats */ rt->timer_ev.data = rt; - ev_timer_init (&rt->timer_ev, rspamd_redis_cache_timeout, - rt->ctx->timeout, 0.0); - rspamd_redis_cache_maybe_auth (ctx, rt->redis); + ev_timer_init(&rt->timer_ev, rspamd_redis_cache_timeout, + rt->ctx->timeout, 0.0); + rspamd_redis_cache_maybe_auth(ctx, rt->redis); if (!learn) { - rspamd_stat_cache_redis_generate_id (task); + rspamd_stat_cache_redis_generate_id(task); } return rt; } -gint -rspamd_stat_cache_redis_check (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime) +gint rspamd_stat_cache_redis_check(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime) { struct rspamd_redis_cache_runtime *rt = runtime; gchar *h; - if (rspamd_session_blocked (task->s)) { + if (rspamd_session_blocked(task->s)) { return RSPAMD_LEARN_IGNORE; } - h = rspamd_mempool_get_variable (task->task_pool, "words_hash"); + h = rspamd_mempool_get_variable(task->task_pool, "words_hash"); if (h == NULL) { return RSPAMD_LEARN_IGNORE; } - if (redisAsyncCommand (rt->redis, rspamd_stat_cache_redis_get, rt, - "HGET %s %s", - rt->ctx->redis_object, h) == REDIS_OK) { - rspamd_session_add_event (task->s, - rspamd_redis_cache_fin, - rt, - M); - ev_timer_start (rt->task->event_loop, &rt->timer_ev); + if (redisAsyncCommand(rt->redis, rspamd_stat_cache_redis_get, rt, + "HGET %s %s", + rt->ctx->redis_object, h) == REDIS_OK) { + rspamd_session_add_event(task->s, + rspamd_redis_cache_fin, + rt, + M); + ev_timer_start(rt->task->event_loop, &rt->timer_ev); rt->has_event = TRUE; } @@ -473,30 +473,29 @@ rspamd_stat_cache_redis_check (struct rspamd_task *task, return RSPAMD_LEARN_OK; } -gint -rspamd_stat_cache_redis_learn (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime) +gint rspamd_stat_cache_redis_learn(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime) { struct rspamd_redis_cache_runtime *rt = runtime; gchar *h; gint flag; - if (rt == NULL || rt->ctx == NULL || rspamd_session_blocked (task->s)) { + if (rt == NULL || rt->ctx == NULL || rspamd_session_blocked(task->s)) { return RSPAMD_LEARN_IGNORE; } - h = rspamd_mempool_get_variable (task->task_pool, "words_hash"); - g_assert (h != NULL); + h = rspamd_mempool_get_variable(task->task_pool, "words_hash"); + g_assert(h != NULL); flag = (task->flags & RSPAMD_TASK_FLAG_LEARN_SPAM) ? 1 : -1; - if (redisAsyncCommand (rt->redis, rspamd_stat_cache_redis_set, rt, - "HSET %s %s %d", - rt->ctx->redis_object, h, flag) == REDIS_OK) { - rspamd_session_add_event (task->s, - rspamd_redis_cache_fin, rt, M); - ev_timer_start (rt->task->event_loop, &rt->timer_ev); + if (redisAsyncCommand(rt->redis, rspamd_stat_cache_redis_set, rt, + "HSET %s %s %d", + rt->ctx->redis_object, h, flag) == REDIS_OK) { + rspamd_session_add_event(task->s, + rspamd_redis_cache_fin, rt, M); + ev_timer_start(rt->task->event_loop, &rt->timer_ev); rt->has_event = TRUE; } @@ -504,17 +503,16 @@ rspamd_stat_cache_redis_learn (struct rspamd_task *task, return RSPAMD_LEARN_OK; } -void -rspamd_stat_cache_redis_close (gpointer c) +void rspamd_stat_cache_redis_close(gpointer c) { - struct rspamd_redis_cache_ctx *ctx = (struct rspamd_redis_cache_ctx *)c; + struct rspamd_redis_cache_ctx *ctx = (struct rspamd_redis_cache_ctx *) c; lua_State *L; L = ctx->L; if (ctx->conf_ref) { - luaL_unref (L, LUA_REGISTRYINDEX, ctx->conf_ref); + luaL_unref(L, LUA_REGISTRYINDEX, ctx->conf_ref); } - g_free (ctx); + g_free(ctx); } diff --git a/src/libstat/learn_cache/sqlite3_cache.c b/src/libstat/learn_cache/sqlite3_cache.c index 755114367..d8ad20ad2 100644 --- a/src/libstat/learn_cache/sqlite3_cache.c +++ b/src/libstat/learn_cache/sqlite3_cache.c @@ -25,13 +25,13 @@ #include "libutil/sqlite_utils.h" static const char *create_tables_sql = - "" - "CREATE TABLE IF NOT EXISTS learns(" - "id INTEGER PRIMARY KEY," - "flag INTEGER NOT NULL," - "digest TEXT NOT NULL);" - "CREATE UNIQUE INDEX IF NOT EXISTS d ON learns(digest);" - ""; + "" + "CREATE TABLE IF NOT EXISTS learns(" + "id INTEGER PRIMARY KEY," + "flag INTEGER NOT NULL," + "digest TEXT NOT NULL);" + "CREATE UNIQUE INDEX IF NOT EXISTS d ON learns(digest);" + ""; #define SQLITE_CACHE_PATH RSPAMD_DBDIR "/learn_cache.sqlite" @@ -47,64 +47,49 @@ enum rspamd_stat_sqlite3_stmt_idx { }; static struct rspamd_sqlite3_prstmt prepared_stmts[RSPAMD_STAT_CACHE_MAX] = -{ - { - .idx = RSPAMD_STAT_CACHE_TRANSACTION_START_IM, - .sql = "BEGIN IMMEDIATE TRANSACTION;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, - { - .idx = RSPAMD_STAT_CACHE_TRANSACTION_START_DEF, - .sql = "BEGIN DEFERRED TRANSACTION;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, - { - .idx = RSPAMD_STAT_CACHE_TRANSACTION_COMMIT, - .sql = "COMMIT;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, - { - .idx = RSPAMD_STAT_CACHE_TRANSACTION_ROLLBACK, - .sql = "ROLLBACK;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, { - .idx = RSPAMD_STAT_CACHE_GET_LEARN, - .sql = "SELECT flag FROM learns WHERE digest=?1", - .args = "V", - .stmt = NULL, - .result = SQLITE_ROW, - .ret = "I" - }, - { - .idx = RSPAMD_STAT_CACHE_ADD_LEARN, - .sql = "INSERT INTO learns(digest, flag) VALUES (?1, ?2);", - .args = "VI", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, - { - .idx = RSPAMD_STAT_CACHE_UPDATE_LEARN, - .sql = "UPDATE learns SET flag=?1 WHERE digest=?2;", - .args = "IV", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - } -}; + {.idx = RSPAMD_STAT_CACHE_TRANSACTION_START_IM, + .sql = "BEGIN IMMEDIATE TRANSACTION;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_TRANSACTION_START_DEF, + .sql = "BEGIN DEFERRED TRANSACTION;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_TRANSACTION_COMMIT, + .sql = "COMMIT;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_TRANSACTION_ROLLBACK, + .sql = "ROLLBACK;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_GET_LEARN, + .sql = "SELECT flag FROM learns WHERE digest=?1", + .args = "V", + .stmt = NULL, + .result = SQLITE_ROW, + .ret = "I"}, + {.idx = RSPAMD_STAT_CACHE_ADD_LEARN, + .sql = "INSERT INTO learns(digest, flag) VALUES (?1, ?2);", + .args = "VI", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_UPDATE_LEARN, + .sql = "UPDATE learns SET flag=?1 WHERE digest=?2;", + .args = "IV", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}}; struct rspamd_stat_sqlite3_ctx { sqlite3 *db; @@ -112,10 +97,10 @@ struct rspamd_stat_sqlite3_ctx { }; gpointer -rspamd_stat_cache_sqlite3_init (struct rspamd_stat_ctx *ctx, - struct rspamd_config *cfg, - struct rspamd_statfile *st, - const ucl_object_t *cf) +rspamd_stat_cache_sqlite3_init(struct rspamd_stat_ctx *ctx, + struct rspamd_config *cfg, + struct rspamd_statfile *st, + const ucl_object_t *cf) { struct rspamd_stat_sqlite3_ctx *new = NULL; const ucl_object_t *elt; @@ -125,35 +110,35 @@ rspamd_stat_cache_sqlite3_init (struct rspamd_stat_ctx *ctx, GError *err = NULL; if (cf) { - elt = ucl_object_lookup_any (cf, "path", "file", NULL); + elt = ucl_object_lookup_any(cf, "path", "file", NULL); if (elt != NULL) { - path = ucl_object_tostring (elt); + path = ucl_object_tostring(elt); } } - rspamd_snprintf (dbpath, sizeof (dbpath), "%s", path); + rspamd_snprintf(dbpath, sizeof(dbpath), "%s", path); - sqlite = rspamd_sqlite3_open_or_create (cfg->cfg_pool, - dbpath, create_tables_sql, 0, &err); + sqlite = rspamd_sqlite3_open_or_create(cfg->cfg_pool, + dbpath, create_tables_sql, 0, &err); if (sqlite == NULL) { - msg_err ("cannot open sqlite3 cache: %e", err); - g_error_free (err); + msg_err("cannot open sqlite3 cache: %e", err); + g_error_free(err); err = NULL; } else { - new = g_malloc0 (sizeof (*new)); + new = g_malloc0(sizeof(*new)); new->db = sqlite; - new->prstmt = rspamd_sqlite3_init_prstmt (sqlite, prepared_stmts, - RSPAMD_STAT_CACHE_MAX, &err); + new->prstmt = rspamd_sqlite3_init_prstmt(sqlite, prepared_stmts, + RSPAMD_STAT_CACHE_MAX, &err); if (new->prstmt == NULL) { - msg_err ("cannot open sqlite3 cache: %e", err); - g_error_free (err); + msg_err("cannot open sqlite3 cache: %e", err); + g_error_free(err); err = NULL; - sqlite3_close (sqlite); - g_free (new); + sqlite3_close(sqlite); + g_free(new); new = NULL; } } @@ -162,17 +147,16 @@ rspamd_stat_cache_sqlite3_init (struct rspamd_stat_ctx *ctx, } gpointer -rspamd_stat_cache_sqlite3_runtime (struct rspamd_task *task, - gpointer ctx, gboolean learn) +rspamd_stat_cache_sqlite3_runtime(struct rspamd_task *task, + gpointer ctx, gboolean learn) { /* No need of runtime for this type of classifier */ return ctx; } -gint -rspamd_stat_cache_sqlite3_check (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime) +gint rspamd_stat_cache_sqlite3_check(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime) { struct rspamd_stat_sqlite3_ctx *ctx = runtime; rspamd_cryptobox_hash_state_t st; @@ -188,41 +172,41 @@ rspamd_stat_cache_sqlite3_check (struct rspamd_task *task, } if (ctx != NULL && ctx->db != NULL) { - out = rspamd_mempool_alloc (task->task_pool, rspamd_cryptobox_HASHBYTES); + out = rspamd_mempool_alloc(task->task_pool, rspamd_cryptobox_HASHBYTES); - rspamd_cryptobox_hash_init (&st, NULL, 0); + rspamd_cryptobox_hash_init(&st, NULL, 0); - user = rspamd_mempool_get_variable (task->task_pool, "stat_user"); + user = rspamd_mempool_get_variable(task->task_pool, "stat_user"); /* Use dedicated hash space for per users cache */ if (user != NULL) { - rspamd_cryptobox_hash_update (&st, user, strlen (user)); + rspamd_cryptobox_hash_update(&st, user, strlen(user)); } - for (i = 0; i < task->tokens->len; i ++) { - tok = g_ptr_array_index (task->tokens, i); - rspamd_cryptobox_hash_update (&st, (guchar *)&tok->data, - sizeof (tok->data)); + for (i = 0; i < task->tokens->len; i++) { + tok = g_ptr_array_index(task->tokens, i); + rspamd_cryptobox_hash_update(&st, (guchar *) &tok->data, + sizeof(tok->data)); } - rspamd_cryptobox_hash_final (&st, out); + rspamd_cryptobox_hash_final(&st, out); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_START_DEF); - rc = rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_GET_LEARN, (gint64)rspamd_cryptobox_HASHBYTES, - out, &flag); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_START_DEF); + rc = rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_GET_LEARN, (gint64) rspamd_cryptobox_HASHBYTES, + out, &flag); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); /* Save hash into variables */ - rspamd_mempool_set_variable (task->task_pool, "words_hash", out, NULL); + rspamd_mempool_set_variable(task->task_pool, "words_hash", out, NULL); if (rc == SQLITE_OK) { /* We have some existing record in the table */ if (!!flag == !!is_spam) { /* Already learned */ - msg_warn_task ("already seen stat hash: %*bs", - rspamd_cryptobox_HASHBYTES, out); + msg_warn_task("already seen stat hash: %*bs", + rspamd_cryptobox_HASHBYTES, out); return RSPAMD_LEARN_IGNORE; } else { @@ -235,17 +219,16 @@ rspamd_stat_cache_sqlite3_check (struct rspamd_task *task, return RSPAMD_LEARN_OK; } -gint -rspamd_stat_cache_sqlite3_learn (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime) +gint rspamd_stat_cache_sqlite3_learn(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime) { struct rspamd_stat_sqlite3_ctx *ctx = runtime; gboolean unlearn = !!(task->flags & RSPAMD_TASK_FLAG_UNLEARN); guchar *h; gint64 flag; - h = rspamd_mempool_get_variable (task->task_pool, "words_hash"); + h = rspamd_mempool_get_variable(task->task_pool, "words_hash"); if (h == NULL) { return RSPAMD_LEARN_IGNORE; @@ -255,39 +238,37 @@ rspamd_stat_cache_sqlite3_learn (struct rspamd_task *task, if (!unlearn) { /* Insert result new id */ - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_START_IM); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_ADD_LEARN, - (gint64)rspamd_cryptobox_HASHBYTES, h, flag); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_START_IM); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_ADD_LEARN, + (gint64) rspamd_cryptobox_HASHBYTES, h, flag); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); } else { - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_START_IM); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_UPDATE_LEARN, - flag, - (gint64)rspamd_cryptobox_HASHBYTES, h); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_START_IM); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_UPDATE_LEARN, + flag, + (gint64) rspamd_cryptobox_HASHBYTES, h); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); } - rspamd_sqlite3_sync (ctx->db, NULL, NULL); + rspamd_sqlite3_sync(ctx->db, NULL, NULL); return RSPAMD_LEARN_OK; } -void -rspamd_stat_cache_sqlite3_close (gpointer c) +void rspamd_stat_cache_sqlite3_close(gpointer c) { - struct rspamd_stat_sqlite3_ctx *ctx = (struct rspamd_stat_sqlite3_ctx *)c; + struct rspamd_stat_sqlite3_ctx *ctx = (struct rspamd_stat_sqlite3_ctx *) c; if (ctx != NULL) { - rspamd_sqlite3_close_prstmt (ctx->db, ctx->prstmt); - sqlite3_close (ctx->db); - g_free (ctx); + rspamd_sqlite3_close_prstmt(ctx->db, ctx->prstmt); + sqlite3_close(ctx->db); + g_free(ctx); } - } |