]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Feature] Add rspamd_version function to LUA API
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 1 Mar 2016 12:27:02 +0000 (12:27 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 1 Mar 2016 12:27:02 +0000 (12:27 +0000)
config.h.in
src/libserver/cfg_rcl.c

index 7e407ba64646685e0e1a56674e00ff19675ad88d..db1a63d9753584c469bb868e59aad4db97991ab7 100644 (file)
 #define RSPAMD_WWWDIR "${WWWDIR}"
 #define RSPAMD_PREFIX "${CMAKE_INSTALL_PREFIX}"
 
+#define RSPAMD_VERSION_MAJOR "${RSPAMD_VERSION_MAJOR}"
+#define RSPAMD_VERSION_MINOR "${RSPAMD_VERSION_MINOR}"
+#define RSPAMD_VERSION_PATCH "${RSPAMD_VERSION_PATCH}"
+
+#define RSPAMD_VERSION_MAJOR_NUM ${RSPAMD_VERSION_MAJOR_NUM}
+#define RSPAMD_VERSION_MINOR_NUM ${RSPAMD_VERSION_MINOR_NUM}
+#define RSPAMD_VERSION_PATCH_NUM ${RSPAMD_VERSION_PATCH_NUM}
+
+#define RSPAMD_VERSION_BRANCH "${RSPAMD_VERSION_MAJOR}.${RSPAMD_VERSION_MINOR}"
 
 #if defined(GIT_VERSION) && GIT_VERSION == 1
 # define RVERSION         "${RSPAMD_VERSION}"
+# define RSPAMD_VERSION_FULL         "${RSPAMD_VERSION}_${RSPAMD_ID}"
 # define RID              "${RSPAMD_ID}"
 # define RSPAMD_VERSION_NUM 0x${RSPAMD_VERSION_MAJOR_NUM}${RSPAMD_VERSION_MINOR_NUM}${RSPAMD_VERSION_PATCH_NUM}${RSPAMD_ID}ULL
 #else
+# define RSPAMD_VERSION_FULL         "${RSPAMD_VERSION}"
 # define RVERSION          "${RSPAMD_VERSION}"
 # define RSPAMD_VERSION_NUM 0x${RSPAMD_VERSION_MAJOR_NUM}${RSPAMD_VERSION_MINOR_NUM}${RSPAMD_VERSION_PATCH_NUM}0000000ULL
 # define RID "release"
index 1481a3ebeb75a9b40e3bbcf7ccc27f13e8bfdb38..497e5cf9be73a55a46a1984f5166e880073ca730 100644 (file)
@@ -692,6 +692,145 @@ rspamd_rcl_worker_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
 #define RSPAMD_PREFIX_INDEX "PREFIX"
 #define RSPAMD_VERSION_INDEX "VERSION"
 
+static gint
+rspamd_rcl_cmp_components (const gchar *comp1, const gchar *comp2)
+{
+       guint v1, v2;
+
+       v1 = strtoul (comp1, NULL, 10);
+       v2 = strtoul (comp2, NULL, 10);
+
+       return v1 - v2;
+}
+
+static int
+rspamd_rcl_lua_version_cmp (lua_State *L)
+{
+       const gchar *ver;
+       gchar **components;
+       gint ret = 0;
+
+       if (lua_type (L, 2) == LUA_TSTRING) {
+               ver = lua_tostring (L, 2);
+
+               components = g_strsplit_set (ver, ".-_", -1);
+
+               if (!components) {
+                       return luaL_error (L, "invalid arguments to 'cmp': %s", ver);
+               }
+
+               if (components[0]) {
+                       ret = rspamd_rcl_cmp_components (components[0], RSPAMD_VERSION_MAJOR);
+               }
+
+               if (ret) {
+                       goto set;
+               }
+
+               if (components[1]) {
+                       ret = rspamd_rcl_cmp_components (components[1], RSPAMD_VERSION_MAJOR);
+               }
+
+               if (ret) {
+                       goto set;
+               }
+
+               if (components[2]) {
+                       ret = rspamd_rcl_cmp_components (components[2], RSPAMD_VERSION_PATCH);
+               }
+
+               /*
+                * XXX: we don't compare git releases assuming that it is meaningless
+                */
+       }
+       else {
+               return luaL_error (L, "invalid arguments to 'cmp'");
+       }
+
+set:
+       g_strfreev (components);
+       lua_pushnumber (L, ret);
+
+       return 1;
+}
+
+static int
+rspamd_rcl_lua_version_numeric (lua_State *L)
+{
+       static gint64 version_num = RSPAMD_VERSION_NUM;
+       const gchar *type;
+
+       if (lua_gettop (L) >= 2 && lua_type (L, 1) == LUA_TSTRING) {
+               type = lua_tostring (L, 1);
+               if (g_ascii_strcasecmp (type, "short") == 0) {
+                       version_num = RSPAMD_VERSION_MAJOR_NUM * 1000 +
+                                       RSPAMD_VERSION_MINOR_NUM * 100 + RSPAMD_VERSION_PATCH_NUM * 10;
+               }
+               else if (g_ascii_strcasecmp (type, "main") == 0) {
+                       version_num = RSPAMD_VERSION_MAJOR_NUM * 1000 +
+                                               RSPAMD_VERSION_MINOR_NUM * 100;
+               }
+               else if (g_ascii_strcasecmp (type, "major") == 0) {
+                       version_num = RSPAMD_VERSION_MAJOR_NUM;
+               }
+               else if (g_ascii_strcasecmp (type, "minor") == 0) {
+                       version_num = RSPAMD_VERSION_MINOR_NUM;
+               }
+               else if (g_ascii_strcasecmp (type, "patch") == 0) {
+                       version_num = RSPAMD_VERSION_PATCH_NUM;
+               }
+       }
+
+       lua_pushnumber (L, version_num);
+
+       return 1;
+}
+
+static int
+rspamd_rcl_lua_version (lua_State *L)
+{
+       const gchar *result = NULL, *type;
+
+       if (lua_gettop (L) == 0) {
+               result = RVERSION;
+       }
+       else if (lua_gettop (L) >= 1 && lua_type (L, 1) == LUA_TSTRING) {
+               /* We got something like string */
+               type = lua_tostring (L, 1);
+
+               if (g_ascii_strcasecmp (type, "short") == 0) {
+                       result = RSPAMD_VERSION_MAJOR
+                                       "." RSPAMD_VERSION_MINOR
+                                       "." RSPAMD_VERSION_PATCH;
+               }
+               else if (g_ascii_strcasecmp (type, "main") == 0) {
+                       result = RSPAMD_VERSION_MAJOR "." RSPAMD_VERSION_MINOR;
+               }
+               else if (g_ascii_strcasecmp (type, "major") == 0) {
+                       result = RSPAMD_VERSION_MAJOR;
+               }
+               else if (g_ascii_strcasecmp (type, "minor") == 0) {
+                       result = RSPAMD_VERSION_MINOR;
+               }
+               else if (g_ascii_strcasecmp (type, "patch") == 0) {
+                       result = RSPAMD_VERSION_PATCH;
+               }
+               else if (g_ascii_strcasecmp (type, "id") == 0) {
+                       result = RID;
+               }
+               else if (g_ascii_strcasecmp (type, "num") == 0) {
+                       return rspamd_rcl_lua_version_numeric (L);
+               }
+               else if (g_ascii_strcasecmp (type, "cmp") == 0) {
+                       return rspamd_rcl_lua_version_cmp (L);
+               }
+       }
+
+       lua_pushstring (L, result);
+
+       return 1;
+}
+
 static void
 rspamd_rcl_set_lua_globals (struct rspamd_config *cfg, lua_State *L,
                GHashTable *vars)
@@ -726,6 +865,12 @@ rspamd_rcl_set_lua_globals (struct rspamd_config *cfg, lua_State *L,
                lua_setglobal (L, "classifiers");
        }
 
+       lua_getglobal (L, "rspamd_version");
+       if (lua_isnil (L, -1)) {
+               lua_pushcfunction (L, rspamd_rcl_lua_version);
+               lua_setglobal (L, "rspamd_version");
+       }
+
        pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *));
        rspamd_lua_setclass (L, "rspamd{config}", -1);
        *pcfg = cfg;