]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Add lua method to get tld for a host
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 21 Dec 2015 09:39:28 +0000 (09:39 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 21 Dec 2015 09:39:28 +0000 (09:39 +0000)
src/lua/lua_util.c

index 65afa8470b20578a53d6a9bf9e3aff744633cfe6..7f89234c6c8754715a99ed5e566b1c54d606cfcf 100644 (file)
@@ -27,6 +27,7 @@
 #include "html.h"
 #include "cfg_rcl.h"
 #include "tokenizers/tokenizers.h"
+#include "libserver/url.h"
 #include <math.h>
 
 /***
@@ -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)
 {