]> source.dussan.org Git - rspamd.git/commitdiff
Implement direct checking.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 20 Dec 2014 13:40:35 +0000 (13:40 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 20 Dec 2014 13:40:35 +0000 (13:40 +0000)
src/fuzzy_storage.h
src/libserver/fuzzy_backend.c
src/libserver/fuzzy_backend.h

index 783280fc02b4e01b0a3584da42ae9bb899629b84..31921013fa459ac86e86d91be00471db7035f0c4 100644 (file)
@@ -35,7 +35,7 @@ struct rspamd_fuzzy_shingle_cmd {
 };
 
 struct rspamd_fuzzy_reply {
-       guint32 code;
+       guint32 value;
        gdouble prob;
 };
 
index c70afb4459988d775dfa5ae571ff8a04bcd15b0b..0b78b2ac05e6f04ced236712e6921d925281d6be 100644 (file)
@@ -107,7 +107,7 @@ static struct rspamd_fuzzy_stmts {
                .idx = RSPAMD_FUZZY_BACKEND_INSERT,
                .sql = "INSERT INTO digests(flag, digest, value, time) VALUES"
                                "(?1, ?2, ?3, ?4);",
-               .args = "STII",
+               .args = "SDII",
                .stmt = NULL,
                .result = SQLITE_DONE
        },
@@ -121,8 +121,8 @@ static struct rspamd_fuzzy_stmts {
        },
        {
                .idx = RSPAMD_FUZZY_BACKEND_CHECK,
-               .sql = "SELECT * FROM digests WHERE digest==?1 AND flag==?2;",
-               .args = "TI",
+               .sql = "SELECT value, time FROM digests WHERE digest==?1 AND flag==?2;",
+               .args = "DS",
                .stmt = NULL,
                .result = SQLITE_ROW
        },
@@ -135,8 +135,8 @@ static struct rspamd_fuzzy_stmts {
        },
        {
                .idx = RSPAMD_FUZZY_BACKEND_DELETE,
-               .sql = "",
-               .args = "",
+               .sql = "DELETE FROM digests WHERE digest==?1 AND flag==?2;",
+               .args = "DS",
                .stmt = NULL,
                .result = SQLITE_DONE
        }
@@ -205,7 +205,7 @@ rspamd_fuzzy_backend_run_stmt (struct rspamd_fuzzy_backend *bk, int idx, ...)
        for (i = 0; argtypes[i] != '\0'; i++) {
                switch (argtypes[i]) {
                case 'T':
-                       sqlite3_bind_text(stmt, i + 1, va_arg (ap, const char*), -1,
+                       sqlite3_bind_text (stmt, i + 1, va_arg (ap, const char*), -1,
                                        SQLITE_STATIC);
                        break;
                case 'I':
@@ -214,6 +214,11 @@ rspamd_fuzzy_backend_run_stmt (struct rspamd_fuzzy_backend *bk, int idx, ...)
                case 'S':
                        sqlite3_bind_int (stmt, i + 1, va_arg (ap, gint));
                        break;
+               case 'D':
+                       /* Special case for digests variable */
+                       sqlite3_bind_text (stmt, i + 1, va_arg (ap, const char*), 64,
+                                       SQLITE_STATIC);
+                       break;
                }
        }
 
@@ -471,9 +476,36 @@ rspamd_fuzzy_backend_open (const gchar *path, GError **err)
 
 struct rspamd_fuzzy_reply
 rspamd_fuzzy_backend_check (struct rspamd_fuzzy_backend *backend,
-               const struct rspamd_fuzzy_cmd *cmd)
+               const struct rspamd_fuzzy_cmd *cmd, gint64 expire)
 {
+       struct rspamd_fuzzy_reply rep = {0, 0.0};
+       const struct rspamd_fuzzy_shingle_cmd *shcmd;
+       int rc;
+       gint64 timestamp;
+
+       /* Try direct match first of all */
+       rc = rspamd_fuzzy_backend_run_stmt (backend, RSPAMD_FUZZY_BACKEND_CHECK,
+                       cmd->digest, cmd->flag);
+
+       if (rc == SQLITE_OK) {
+               timestamp = sqlite3_column_int64 (
+                               prepared_stmts[RSPAMD_FUZZY_BACKEND_CHECK].stmt, 1);
+               if (time (NULL) - timestamp > expire) {
+                       /* Expire element */
+                       rspamd_fuzzy_backend_run_stmt (backend, RSPAMD_FUZZY_BACKEND_DELETE,
+                               cmd->digest, cmd->flag);
+               }
+               else {
+                       rep.value = sqlite3_column_int64 (
+                               prepared_stmts[RSPAMD_FUZZY_BACKEND_CHECK].stmt, 0);
+                       rep.prob = 1.0;
+               }
+       }
+       else if (cmd->shingles_count > 0) {
+               /* Fuzzy match */
+       }
 
+       return rep;
 }
 
 gboolean
index 5322eff5024fa40536d6eb30e5b33ef3121f9231..61664c889c5eea1f78acd1d32e5ae7d621d8ded3 100644 (file)
@@ -46,7 +46,8 @@ struct rspamd_fuzzy_backend* rspamd_fuzzy_backend_open (const gchar *path,
  */
 struct rspamd_fuzzy_reply rspamd_fuzzy_backend_check (
                struct rspamd_fuzzy_backend *backend,
-               const struct rspamd_fuzzy_cmd *cmd);
+               const struct rspamd_fuzzy_cmd *cmd,
+               gint64 expire);
 
 /**
  * Add digest to the database