]> source.dussan.org Git - rspamd.git/commitdiff
* Make lua api object oriented
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Thu, 3 Sep 2009 15:18:55 +0000 (19:18 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Thu, 3 Sep 2009 15:18:55 +0000 (19:18 +0400)
src/lua/lua_common.c
src/lua/lua_common.h
src/lua/lua_config.c
src/lua/lua_message.c
src/lua/lua_task.c

index 907f7e6b58cb4f47b8915142f8364cdbc7055e24..0480ea1b6ef466c344ab78ec7c3ed8fb5f9e90b4 100644 (file)
 #define MODULE_INIT_FUNC "module_init"
 
 lua_State *L = NULL;
+const luaL_reg null_reg[] = {
+    {NULL, NULL}
+};
 
 /* Logger methods */
-LUA_FUNCTION_DEF(logger, err);
-LUA_FUNCTION_DEF(logger, warn);
-LUA_FUNCTION_DEF(logger, info);
-LUA_FUNCTION_DEF(logger, debug);
+LUA_FUNCTION_DEF(logger, _err);
+LUA_FUNCTION_DEF(logger, _warn);
+LUA_FUNCTION_DEF(logger, _info);
+LUA_FUNCTION_DEF(logger, _debug);
 
 static const struct luaL_reg loggerlib_m[] = {
-    LUA_INTERFACE_DEF(logger, err),
-    LUA_INTERFACE_DEF(logger, warn),
-    LUA_INTERFACE_DEF(logger, info),
-    LUA_INTERFACE_DEF(logger, debug),
+    LUA_INTERFACE_DEF(logger, _err),
+    LUA_INTERFACE_DEF(logger, _warn),
+    LUA_INTERFACE_DEF(logger, _info),
+    LUA_INTERFACE_DEF(logger, _debug),
     {NULL, NULL}
 };
 
@@ -89,7 +92,7 @@ lua_set_table_index (lua_State *L, const char *index, const char *value)
 
 /*** Logger interface ***/
 static int
-lua_logger_err (lua_State *L)
+lua_logger__err (lua_State *L)
 {
     const char *msg;
     msg = luaL_checkstring (L, 2);
@@ -98,7 +101,7 @@ lua_logger_err (lua_State *L)
 }
 
 static int
-lua_logger_warn (lua_State *L)
+lua_logger__warn (lua_State *L)
 {
     const char *msg;
     msg = luaL_checkstring (L, 2);
@@ -107,7 +110,7 @@ lua_logger_warn (lua_State *L)
 }
 
 static int
-lua_logger_info (lua_State *L)
+lua_logger__info (lua_State *L)
 {
     const char *msg;
     msg = luaL_checkstring (L, 2);
@@ -116,7 +119,7 @@ lua_logger_info (lua_State *L)
 }
 
 static int
-lua_logger_debug (lua_State *L)
+lua_logger__debug (lua_State *L)
 {
     const char *msg;
     msg = luaL_checkstring (L, 2);
@@ -127,11 +130,24 @@ lua_logger_debug (lua_State *L)
 
 /*** Init functions ***/
 
+int
+luaopen_rspamd (lua_State *L)
+{
+       luaL_openlib(L, "rspamd", null_reg, 0);
+    /* make version string available to scripts */
+       lua_pushstring(L, "_VERSION");
+       lua_pushstring(L, RVERSION);
+    lua_rawset(L, -3);
+    
+       return 1;
+}
+
 int
 luaopen_logger (lua_State *L)
 {
-    lua_newclass (L, "Rspamd.logger", loggerlib_m);
-       luaL_openlib (L, "logger", loggerlib_m, 0);
+
+    lua_newclass (L, "rspamd{logger}", loggerlib_m);
+       luaL_openlib (L, NULL, null_reg, 0);
 
     return 1;
 }
@@ -142,13 +158,14 @@ init_lua ()
        if (L == NULL) {
                L = lua_open ();
                luaL_openlibs (L);
-
-               luaopen_task (L);
-               luaopen_message (L);
-        luaopen_logger (L);
-        luaopen_config (L);
-        luaopen_metric (L);
-        luaopen_textpart (L);
+        
+        (void)luaopen_rspamd (L);
+        (void)luaopen_logger (L);
+        (void)luaopen_config (L);
+        (void)luaopen_metric (L);
+               (void)luaopen_task (L);
+        (void)luaopen_textpart (L);
+               (void)luaopen_message (L);
        }
 }
 
@@ -172,7 +189,7 @@ init_lua_filters (struct config_file *cfg)
 
                        /* Call module init function */
                        pcfg = lua_newuserdata (L, sizeof (struct config_file *));
-                       lua_setclass (L, "Rspamd.config", -1);
+                       lua_setclass (L, "rspamd{config}", -1);
                        *pcfg = cfg;
             lua_setglobal (L, "rspamd_config");
                        /* do the call (1 arguments, 1 result) */
@@ -194,7 +211,7 @@ lua_call_filter (const char *function, struct worker_task *task)
 
        lua_getglobal (L, function);
        ptask = lua_newuserdata (L, sizeof (struct worker_task *));
-       lua_setclass (L, "Rspamd.task", -1);
+       lua_setclass (L, "rspamd{task}", -1);
        *ptask = task;
        
        if (lua_pcall (L, 1, 1, 0) != 0) {
index bcb69a009856fb697a7a6d8bb827702654835c40..886960ab3ca5405ae7fa55dbf38cbb4ab5c9aace 100644 (file)
@@ -12,6 +12,8 @@
 #define LUA_FUNCTION_DEF(class, name) static int lua_##class##_##name(lua_State *L)
 #define LUA_INTERFACE_DEF(class, name) { #name, lua_##class##_##name }
 
+extern const luaL_reg null_reg[];
+
 void lua_newclass (lua_State *L, const char *classname, const struct luaL_reg *func);
 void lua_setclass (lua_State *L, const char *classname, int objidx);
 void lua_set_table_index (lua_State *L, const char *index, const char *value);
index c385d24f74501ac1731eb28f733e8f9b78bb1805..0879cf835d61fa825d617fb78ef42d96f5727412 100644 (file)
@@ -48,7 +48,7 @@ static const struct luaL_reg metriclib_m[] = {
 static struct config_file *
 lua_check_config (lua_State *L)
 {
-       void *ud = luaL_checkudata (L, 1, "Rspamd.config");
+       void *ud = luaL_checkudata (L, 1, "rspamd{config}");
        luaL_argcheck (L, ud != NULL, 1, "'config' expected");
        return *((struct config_file **)ud);
 }
@@ -56,7 +56,7 @@ lua_check_config (lua_State *L)
 static struct metric *
 lua_check_metric (lua_State *L)
 {
-       void *ud = luaL_checkudata (L, 1, "Rspamd.metric");
+       void *ud = luaL_checkudata (L, 1, "rspamd{metric}");
        luaL_argcheck (L, ud != NULL, 1, "'metric' expected");
        return *((struct metric **)ud);
 }
@@ -128,7 +128,7 @@ lua_config_get_metric (lua_State *L)
         metric = g_hash_table_lookup (cfg->metrics, name);
         if (metric) {
             pmetric = lua_newuserdata (L, sizeof (struct metric *));
-                   lua_setclass (L, "Rspamd.metric", -1);
+                   lua_setclass (L, "rspamd{metric}", -1);
                    *pmetric = metric;
                        return 1;
         }
@@ -154,7 +154,7 @@ lua_metric_symbol_callback (struct worker_task *task, gpointer ud)
 
        lua_getglobal (cd->L, cd->name);
        ptask = lua_newuserdata (cd->L, sizeof (struct worker_task *));
-       lua_setclass (cd->L, "Rspamd.task", -1);
+       lua_setclass (cd->L, "rspamd{task}", -1);
        *ptask = task;
 
        if (lua_pcall(cd->L, 1, 1, 0) != 0) {
@@ -187,8 +187,8 @@ lua_metric_register_symbol (lua_State *L)
 int
 luaopen_config (lua_State *L)
 {
-    lua_newclass (L, "Rspamd.config", configlib_m);
-       luaL_openlib (L, "config", configlib_m, 0);
+    lua_newclass (L, "rspamd{config}", configlib_m);
+       luaL_openlib (L, NULL, null_reg, 0);
 
     return 1;
 }
@@ -196,8 +196,8 @@ luaopen_config (lua_State *L)
 int
 luaopen_metric (lua_State *L)
 {
-    lua_newclass (L, "Rspamd.metric", configlib_m);
-       luaL_openlib (L, "metric", configlib_m, 0);
+    lua_newclass (L, "rspamd{metric}", configlib_m);
+       luaL_openlib (L, NULL, null_reg, 0);
 
     return 1;
 }
index 60daf357bf4ac25147b913a83b0c235b3eab6c33..a99fa070c1d5c69ed76e8685a1795fb1b5ca57f2 100644 (file)
@@ -87,7 +87,7 @@ static const struct luaL_reg msglib_m[] = {
 static GMimeMessage *
 lua_check_message (lua_State *L)
 {
-       void *ud = luaL_checkudata (L, 1, "Rspamd.message");
+       void *ud = luaL_checkudata (L, 1, "rspamd{message}");
        luaL_argcheck (L, ud != NULL, 1, "'message' expected");
        return *((GMimeMessage **)ud);
 }
@@ -166,8 +166,8 @@ lua_message_set_header (lua_State *L)
 int
 luaopen_message (lua_State *L)
 {
-       lua_newclass (L, "Rspamd.message", msglib_m);
-       luaL_openlib (L, "message", msglib_m, 0);
+       lua_newclass (L, "rspamd{message}", msglib_m);
+       luaL_openlib (L, NULL, null_reg, 0);
     
        return 1;
 }
index 2f55f0d718f4949123e227604559df0734f013dc..e8957214479e7851d5ac502ede8d0f2809da406f 100644 (file)
@@ -58,7 +58,7 @@ static const struct luaL_reg textpartlib_m[] = {
 static struct worker_task *
 lua_check_task (lua_State *L) 
 {
-       void *ud = luaL_checkudata (L, 1, "Rspamd.task");
+       void *ud = luaL_checkudata (L, 1, "rspamd{task}");
        luaL_argcheck (L, ud != NULL, 1, "'task' expected");
        return *((struct worker_task **)ud);
 }
@@ -66,7 +66,7 @@ lua_check_task (lua_State *L)
 static struct mime_text_part *
 lua_check_textpart (lua_State *L)
 {
-       void *ud = luaL_checkudata (L, 1, "Rspamd.textpart");
+       void *ud = luaL_checkudata (L, 1, "rspamd{textpart}");
        luaL_argcheck (L, ud != NULL, 1, "'textpart' expected");
        return *((struct mime_text_part **)ud);
 }
@@ -81,7 +81,7 @@ lua_task_get_message (lua_State *L)
        if (task != NULL) {
                /* XXX write handler for message object */
                pmsg = lua_newuserdata (L, sizeof (GMimeMessage *));
-               lua_setclass (L, "Rspamd.message", -1);
+               lua_setclass (L, "rspamd{message}", -1);
                *pmsg = task->message;
        }
        return 1;
@@ -135,7 +135,7 @@ lua_task_get_text_parts (lua_State *L)
                while (cur) {
                        part = cur->data;
                        ppart = lua_newuserdata (L, sizeof (struct mime_text_part *));
-                       lua_setclass (L, "Rspamd.textpart", -1);
+                       lua_setclass (L, "rspamd{textpart}", -1);
                        *ppart = part;
                        cur = g_list_next (cur);
                }
@@ -209,8 +209,8 @@ lua_textpart_get_fuzzy (lua_State *L)
 int
 luaopen_task (lua_State *L)
 {
-       lua_newclass (L, "Rspamd.task", tasklib_m);
-       luaL_openlib (L, "task", tasklib_m, 0);
+       lua_newclass (L, "rspamd{task}", tasklib_m);
+       luaL_openlib (L, NULL, null_reg, 0);
     
        return 1;
 }
@@ -218,8 +218,8 @@ luaopen_task (lua_State *L)
 int
 luaopen_textpart (lua_State *L)
 {
-       lua_newclass (L, "Rspamd.textpart", textpartlib_m);
-       luaL_openlib (L, "textpart", textpartlib_m, 0);
+       lua_newclass (L, "rspamd{textpart}", textpartlib_m);
+       luaL_openlib (L, NULL, null_reg, 0);
     
        return 1;
 }