aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-12-04 14:22:43 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-12-04 14:22:43 +0000
commitacdbb7f65d72f038568e3e092558a8bcb7afdace (patch)
treebdd6e7820a4b091a1433caef08a0a28dd3becfd7
parentd296413cc4dcf1d445e3fceabd09f91c36a126ff (diff)
downloadrspamd-acdbb7f65d72f038568e3e092558a8bcb7afdace.tar.gz
rspamd-acdbb7f65d72f038568e3e092558a8bcb7afdace.zip
Add rspamd_config:register_regexp
-rw-r--r--src/lua/lua_config.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c
index ae7111f4a..b7d095ded 100644
--- a/src/lua/lua_config.c
+++ b/src/lua/lua_config.c
@@ -350,6 +350,21 @@ rspamd_config.SYMBOL = {
*/
LUA_FUNCTION_DEF (config, newindex);
+/***
+ * @method rspamd_config:register_regexp(params)
+ * Registers new re for further cached usage
+ * Params is the table with the follwoing fields (mandatory fields are marked with `*`):
+ * - `re`* : regular expression object
+ * - `type`*: type of regular expression:
+ * + `mime`: mime regexp
+ * + `header`: header regexp
+ * + `rawheader`: raw header expression
+ * + `body`: raw body regexp
+ * + `url`: url regexp
+ * - `header`: for header and rawheader regexp means the name of header
+ */
+LUA_FUNCTION_DEF (config, register_regexp);
+
static const struct luaL_reg configlib_m[] = {
LUA_INTERFACE_DEF (config, get_module_opt),
LUA_INTERFACE_DEF (config, get_mempool),
@@ -374,6 +389,7 @@ static const struct luaL_reg configlib_m[] = {
LUA_INTERFACE_DEF (config, get_api_version),
LUA_INTERFACE_DEF (config, get_key),
LUA_INTERFACE_DEF (config, add_condition),
+ LUA_INTERFACE_DEF (config, register_regexp),
{"__tostring", rspamd_lua_class_tostring},
{"__newindex", lua_config_newindex},
{NULL, NULL}
@@ -1531,6 +1547,67 @@ lua_config_add_condition (lua_State *L)
return 1;
}
+static gint
+lua_config_register_regexp (lua_State *L)
+{
+ struct rspamd_config *cfg = lua_check_config (L, 1);
+ struct rspamd_lua_regexp *re = NULL;
+ const gchar *type_str = NULL, *header_str = NULL;
+ gsize header_len = 0;
+ GError *err = NULL;
+ enum rspamd_re_type type = RSPAMD_RE_BODY;
+
+ /*
+ * - `re`* : regular expression object
+ * - `type`*: type of regular expression:
+ * + `mime`: mime regexp
+ * + `header`: header regexp
+ * + `rawheader`: raw header expression
+ * + `body`: raw body regexp
+ * + `url`: url regexp
+ * - `header`: for header and rawheader regexp means the name of header
+ */
+ if (cfg != NULL) {
+ if (!rspamd_lua_parse_table_arguments (L, 2, &err,
+ "re=*U{regexp};type=*S;header=V",
+ &re, &type_str, &header_len, &header_str)) {
+ msg_err_config ("cannot get parameters list: %e", err);
+
+ if (err) {
+ g_error_free (err);
+ }
+ }
+
+ if (strcmp (type_str, "header") == 0) {
+ type = RSPAMD_RE_HEADER;
+ }
+ else if (strcmp (type_str, "rawheader") == 0) {
+ type = RSPAMD_RE_RAWHEADER;
+ }
+ else if (strcmp (type_str, "mime") == 0) {
+ type = RSPAMD_RE_MIME;
+ }
+ else if (strcmp (type_str, "body") == 0) {
+ type = RSPAMD_RE_BODY;
+ }
+ else if (strcmp (type_str, "url") == 0) {
+ type = RSPAMD_RE_URL;
+ }
+
+ if ((type == RSPAMD_RE_HEADER || type == RSPAMD_RE_RAWHEADER)
+ && header_str == NULL) {
+ msg_err_config (
+ "header argument is mandatory for header/rawheader regexps");
+ }
+ else {
+ rspamd_re_cache_add (cfg->re_cache, re->re, type,
+ (gpointer) header_str, header_len);
+ }
+ }
+
+ return 0;
+}
+
struct lua_map_callback_data {
lua_State *L;
gint ref;