From: Vsevolod Stakhov Date: Mon, 21 Dec 2015 09:39:28 +0000 (+0000) Subject: Add lua method to get tld for a host X-Git-Tag: 1.1.0~235 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2d840d3532cf0f72b022e1bcc2afb2ed6ea7e17b;p=thirdparty%2Frspamd.git Add lua method to get tld for a host --- diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 65afa8470b..7f89234c6c 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -27,6 +27,7 @@ #include "html.h" #include "cfg_rcl.h" #include "tokenizers/tokenizers.h" +#include "libserver/url.h" #include /*** @@ -142,6 +143,15 @@ LUA_FUNCTION_DEF (util, is_uppercase); */ LUA_FUNCTION_DEF (util, humanize_number); +/** + * @function util.get_tld(host) + * Returns tld for the specified host + * + * @param {string} host hostname + * @return {string} tld part of hostname or the full hostname + */ +LUA_FUNCTION_DEF (util, get_tld); + static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, create_event_base), LUA_INTERFACE_DEF (util, load_rspamd_config), @@ -157,6 +167,7 @@ static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, fold_header), LUA_INTERFACE_DEF (util, is_uppercase), LUA_INTERFACE_DEF (util, humanize_number), + LUA_INTERFACE_DEF (util, get_tld), {NULL, NULL} }; @@ -701,6 +712,30 @@ lua_util_humanize_number (lua_State *L) return 1; } +static gint +lua_util_get_tld (lua_State *L) +{ + const gchar *host; + gsize hostlen; + rspamd_ftok_t tld; + + host = luaL_checklstring (L, 1, &hostlen); + + if (host) { + if (!rspamd_url_find_tld (host, hostlen, &tld)) { + lua_pushlstring (L, host, hostlen); + } + else { + lua_pushlstring (L, tld.begin, tld.len); + } + } + else { + lua_pushnil (L); + } + + return 1; +} + static gint lua_load_util (lua_State * L) {