Example #1
0
static gint
lua_url_all (lua_State *L)
{
	rspamd_mempool_t *pool = rspamd_lua_check_mempool (L, 1);
	const gchar *text;
	size_t length;

	if (pool == NULL) {
		lua_pushnil (L);
	}
	else {
		text = luaL_checklstring (L, 2, &length);

		if (text != NULL) {
			lua_newtable (L);
			rspamd_url_find_multiple (pool, text, length, FALSE, NULL,
					lua_url_table_inserter, L);

		}
		else {
			lua_pushnil (L);
		}
	}

	return 1;
}
Example #2
0
/***
 * @function url.create([mempool,] str)
 * @param {rspamd_mempool} memory pool for URL, e.g. `task:get_mempool()`
 * @param {string} text that contains URL (can also contain other stuff)
 * @return {url} new url object that exists as long as the corresponding mempool exists
 */
static gint
lua_url_create (lua_State *L)
{
	rspamd_mempool_t *pool;
	const gchar *text;
	size_t length;
	gboolean own_pool = FALSE;

	if (lua_type (L, 1) == LUA_TUSERDATA) {
		pool = rspamd_lua_check_mempool (L, 1);
		text = luaL_checklstring (L, 2, &length);
	}
	else {
		own_pool = TRUE;
		pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "url");
		text = luaL_checklstring (L, 1, &length);
	}

	if (pool == NULL || text == NULL) {
		if (own_pool && pool) {
			rspamd_mempool_delete (pool);
		}

		return luaL_error (L, "invalid arguments");
	}
	else {
		rspamd_url_find_single (pool, text, length, FALSE,
				lua_url_single_inserter, L);

		if (lua_type (L, -1) != LUA_TUSERDATA) {
			/* URL is actually not found */
			lua_pushnil (L);
		}
	}

	if (own_pool && pool) {
		rspamd_mempool_delete (pool);
	}

	return 1;
}
Example #3
0
static gint
lua_expr_create (lua_State *L)
{
    struct lua_expression *e, **pe;
    const char *line;
    gsize len;
    GError *err = NULL;
    rspamd_mempool_t *pool;

    /* Check sanity of the arguments */
    if (lua_type (L, 1) != LUA_TSTRING || lua_type (L, 2) != LUA_TTABLE ||
            rspamd_lua_check_mempool (L, 3) == NULL) {
        msg_info ("bad arguments to lua_expr_create");
        lua_pushnil (L);
        lua_pushstring (L, "bad arguments");
    }
    else {
        line = lua_tolstring (L, 1, &len);
        pool = rspamd_lua_check_mempool (L, 3);

        /* Check callbacks */
        lua_pushvalue (L, 2);
        lua_pushnumber (L, 1);
        lua_gettable (L, -2);

        if (lua_type (L, -1) != LUA_TFUNCTION) {
            lua_pop (L, 2);
            lua_pushnil (L);
            lua_pushstring (L, "bad parse callback");

            return 2;
        }

        lua_pop (L, 1);

        lua_pushnumber (L, 2);
        lua_gettable (L, -2);

        if (lua_type (L, -1) != LUA_TFUNCTION) {
            lua_pop (L, 2);
            lua_pushnil (L);
            lua_pushstring (L, "bad process callback");

            return 2;
        }

        lua_pop (L, 1);

        /* Table is still on the top of stack */

        e = rspamd_mempool_alloc (pool, sizeof (*e));
        e->L = L;
        e->pool = pool;

        lua_pushnumber (L, 1);
        lua_gettable (L, -2);
        e->parse_idx = luaL_ref (L, LUA_REGISTRYINDEX);

        lua_pushnumber (L, 2);
        lua_gettable (L, -2);
        e->process_idx = luaL_ref (L, LUA_REGISTRYINDEX);
        lua_pop (L, 1); /* Table */

        if (!rspamd_parse_expression (line, len, &lua_atom_subr, e, pool, &err,
                                      &e->expr)) {
            lua_pushnil (L);
            lua_pushstring (L, err->message);
            g_error_free (err);

            return 2;
        }

        pe = lua_newuserdata (L, sizeof (struct lua_expression *));
        rspamd_lua_setclass (L, "rspamd{expr}", -1);
        *pe = e;
        lua_pushnil (L);
    }

    return 2;
}
Example #4
0
static gint
lua_expr_create (lua_State *L)
{
	LUA_TRACE_POINT;
	struct lua_expression *e, **pe;
	const char *line;
	gsize len;
	gboolean no_process = FALSE;
	GError *err = NULL;
	rspamd_mempool_t *pool;

	/* Check sanity of the arguments */
	if (lua_type (L, 1) != LUA_TSTRING ||
			(lua_type (L, 2) != LUA_TTABLE && lua_type (L, 2) != LUA_TFUNCTION) ||
			rspamd_lua_check_mempool (L, 3) == NULL) {
		msg_info ("bad arguments to lua_expr_create");
		lua_pushnil (L);
		lua_pushstring (L, "bad arguments");
	}
	else {
		line = lua_tolstring (L, 1, &len);
		pool = rspamd_lua_check_mempool (L, 3);

		e = rspamd_mempool_alloc (pool, sizeof (*e));
		e->L = L;
		e->pool = pool;

		/* Check callbacks */
		if (lua_istable (L, 2)) {
			lua_pushvalue (L, 2);
			lua_pushnumber (L, 1);
			lua_gettable (L, -2);

			if (lua_type (L, -1) != LUA_TFUNCTION) {
				lua_pop (L, 2);
				lua_pushnil (L);
				lua_pushstring (L, "bad parse callback");

				return 2;
			}

			lua_pop (L, 1);

			lua_pushnumber (L, 2);
			lua_gettable (L, -2);

			if (lua_type (L, -1) != LUA_TFUNCTION) {
				if (lua_type (L, -1) != LUA_TNIL && lua_type (L, -1) != LUA_TNONE) {
					lua_pop (L, 2);
					lua_pushnil (L);
					lua_pushstring (L, "bad process callback");

					return 2;
				}
				else {
					no_process = TRUE;
				}
			}

			lua_pop (L, 1);
			/* Table is still on the top of stack */

			lua_pushnumber (L, 1);
			lua_gettable (L, -2);
			e->parse_idx = luaL_ref (L, LUA_REGISTRYINDEX);

			if (!no_process) {
				lua_pushnumber (L, 2);
				lua_gettable (L, -2);
				e->process_idx = luaL_ref (L, LUA_REGISTRYINDEX);
			}
			else {
				e->process_idx = -1;
			}

			lua_pop (L, 1); /* Table */
		}
		else {
			/* Process function is just a function, not a table */
			lua_pushvalue (L, 2);
			e->parse_idx = luaL_ref (L, LUA_REGISTRYINDEX);
			e->process_idx = -1;
		}

		if (!rspamd_parse_expression (line, len, &lua_atom_subr, e, pool, &err,
				&e->expr)) {
			lua_pushnil (L);
			lua_pushstring (L, err->message);
			g_error_free (err);
			lua_expr_dtor (e);

			return 2;
		}

		rspamd_mempool_add_destructor (pool, lua_expr_dtor, e);
		pe = lua_newuserdata (L, sizeof (struct lua_expression *));
		rspamd_lua_setclass (L, "rspamd{expr}", -1);
		*pe = e;
		lua_pushnil (L);
	}

	return 2;
}