#include "fuzzy.h"
#include "expressions.h"
#include "html.h"
+#include "lua/lua_common.h"
gboolean rspamd_compare_encoding (struct worker_task *task, GList * args, void *unused);
gboolean rspamd_header_exists (struct worker_task *task, GList * args, void *unused);
call_expression_function (struct expression_function * func, struct worker_task * task)
{
struct _fl *selected, key;
+#ifdef RSPAMD_MAIN
+ gboolean res;
+#endif
key.name = func->name;
selected = bsearch (&key, list_ptr, functions_number, sizeof (struct _fl), fl_cmp);
if (selected == NULL) {
- msg_warn ("call to undefined function %s", key.name);
+ /* Try to check lua function */
+#ifdef RSPAMD_MAIN
+ if (! lua_call_expression_func (func->name, task, func->args, &res)) {
+ msg_warn ("call to undefined function %s", key.name);
+ return FALSE;
+ }
+ else {
+ return res;
+ }
+#else
return FALSE;
+#endif
}
return selected->func (task, func->args, selected->user_data);
*/
#include "lua_common.h"
+#include "../expressions.h"
/* Lua module init function */
#define MODULE_INIT_FUNC "module_init"
{
int result;
struct worker_task **ptask;
+ lua_State *L = task->cfg->lua_state;
lua_getglobal (L, function);
ptask = lua_newuserdata (L, sizeof (struct worker_task *));
lua_call_chain_filter (const char *function, struct worker_task *task, int *marks, unsigned int number)
{
int result, i;
+ lua_State *L = task->cfg->lua_state;
lua_getglobal (L, function);
return result;
}
+/* Call custom lua function in rspamd expression */
+gboolean
+lua_call_expression_func (const char *function, struct worker_task *task, GList *args, gboolean *res)
+{
+ lua_State *L = task->cfg->lua_state;
+ struct worker_task **ptask;
+ GList *cur;
+ struct expression_argument *arg;
+ int nargs = 0;
+
+ lua_getglobal (L, function);
+ ptask = lua_newuserdata (L, sizeof (struct worker_task *));
+ lua_setclass (L, "rspamd{task}", -1);
+ *ptask = task;
+
+ /* Now push all arguments */
+ cur = args;
+ while (cur) {
+ arg = get_function_arg (cur->data, task, FALSE);
+ if (arg) {
+ switch (arg->type) {
+ case EXPRESSION_ARGUMENT_NORMAL:
+ lua_pushstring (L, (const gchar *)arg->data);
+ break;
+ case EXPRESSION_ARGUMENT_BOOL:
+ lua_pushboolean (L, (gboolean) GPOINTER_TO_SIZE (arg->data));
+ break;
+ default:
+ msg_err ("cannot pass custom params to lua function");
+ return FALSE;
+ }
+ }
+ nargs ++;
+ cur = g_list_next (cur);
+ }
+
+ if (lua_pcall (L, nargs, 1, 0) != 0) {
+ msg_info ("call to %s failed", function);
+ return FALSE;
+ }
+
+ if (!lua_isboolean (L, -1)) {
+ msg_info ("function %s must return a boolean", function);
+ return FALSE;
+ }
+ *res = lua_toboolean (L, -1);
+
+ return TRUE;
+}
+
/*
* LUA custom consolidation function
*/
double res;
struct symbol *s = (struct symbol *)value;
struct consolidation_callback_data *data = (struct consolidation_callback_data *)arg;
+ lua_State *L = data->task->cfg->lua_state;
lua_getglobal (L, data->func);
int lua_call_filter (const char *function, struct worker_task *task);
int lua_call_chain_filter (const char *function, struct worker_task *task, int *marks, unsigned int number);
double lua_consolidation_func (struct worker_task *task, const char *metric_name, const char *function_name);
+gboolean lua_call_expression_func (const char *function, struct worker_task *task, GList *args, gboolean *res);
void add_luabuf (const char *line);
/* Classify functions */