lua.c
changeset 113 3d4f0415c8f8
parent 112 15f4c09ffb86
child 115 47bed161e3b0
equal deleted inserted replaced
112:15f4c09ffb86 113:3d4f0415c8f8
     1 
     1 
     2 /* Copyright 2009 Myhailo Danylenko
     2 /* Copyright 2009,2010 Myhailo Danylenko
       
     3  * Copyright 2011 Mikael Berthe
     3 
     4 
     4 This file is part of mcabber-lua.
     5 This file is part of mcabber-lua.
     5 
     6 
     6 mcabber-lua is free software: you can redistribute it and/or modify
     7 mcabber-lua is free software: you can redistribute it and/or modify
     7 it under the terms of the GNU General Public License as published by
     8 it under the terms of the GNU General Public License as published by
    60 	"Provides command /lua" )
    61 	"Provides command /lua" )
    61 #endif
    62 #endif
    62 
    63 
    63 static module_info_t info_lua_experimental = {
    64 static module_info_t info_lua_experimental = {
    64 	.branch      = "experimental",
    65 	.branch      = "experimental",
    65 	.api         = 27,
    66 	.api         = 34,
    66 	.version     = PROJECT_VERSION,
    67 	.version     = PROJECT_VERSION,
    67 	.description = DESCRIPTION,
    68 	.description = DESCRIPTION,
    68 	.requires    = NULL,
    69 	.requires    = NULL,
    69 	.init        = mlua_init,
    70 	.init        = mlua_init,
    70 	.uninit      = mlua_uninit,
    71 	.uninit      = mlua_uninit,
    71 	.next        = NULL,
    72 	.next        = NULL,
    72 };
    73 };
    73 
    74 
    74 static module_info_t info_lua_dev = {
    75 static module_info_t info_lua_dev = {
    75 	.branch      = "dev",
    76 	.branch      = "dev",
    76 	.api         = 13,
    77 	.api         = 20,
    77 	.version     = PROJECT_VERSION,
    78 	.version     = PROJECT_VERSION,
    78 	.description = DESCRIPTION,
    79 	.description = DESCRIPTION,
    79 	.requires    = NULL,
    80 	.requires    = NULL,
    80 	.init        = mlua_init,
    81 	.init        = mlua_init,
    81 	.uninit      = mlua_uninit,
    82 	.uninit      = mlua_uninit,
    82 	.next        = &info_lua_experimental,
    83 	.next        = &info_lua_experimental,
    83 };
    84 };
    84 
    85 
    85 module_info_t info_lua = {
    86 module_info_t info_lua_0_10_0 = {
    86 	.branch      = "0.10.0",
    87 	.branch      = "0.10.0",
    87 	.api         = 1,
    88 	.api         = 1,
    88 	.version     = PROJECT_VERSION,
    89 	.version     = PROJECT_VERSION,
    89 	.description = DESCRIPTION,
    90 	.description = DESCRIPTION,
    90 	.requires    = NULL,
    91 	.requires    = NULL,
    91 	.init        = mlua_init,
    92 	.init        = mlua_init,
    92 	.uninit      = mlua_uninit,
    93 	.uninit      = mlua_uninit,
    93 	.next        = &info_lua_dev,
    94 	.next        = &info_lua_dev,
    94 };
    95 };
       
    96 
       
    97 module_info_t info_lua = {
       
    98 	.branch      = "0.10.1",
       
    99 	.api         = 1,
       
   100 	.version     = PROJECT_VERSION,
       
   101 	.description = DESCRIPTION,
       
   102 	.requires    = NULL,
       
   103 	.init        = mlua_init,
       
   104 	.uninit      = mlua_uninit,
       
   105 	.next        = &info_lua_0_10_0,
       
   106 };
       
   107 
       
   108 #ifdef MCABBER_API_HAVE_CMD_ID
       
   109 static gpointer lua_cmdid;
       
   110 #endif
    95 
   111 
    96 // global lua state object, necessary for uninitialization function
   112 // global lua state object, necessary for uninitialization function
    97 static lua_State *lua = NULL;
   113 static lua_State *lua = NULL;
    98 
   114 
    99 // caller sould g_free result
   115 // caller sould g_free result
   802 #define MLUA_YESNO_POS ( 21 )
   818 #define MLUA_YESNO_POS ( 21 )
   803 
   819 
   804 typedef struct {
   820 typedef struct {
   805 	int        cbref;
   821 	int        cbref;
   806 	int        parse_args;
   822 	int        parse_args;
   807 #ifdef HAVE_CMD_ID
   823 #ifdef MCABBER_API_HAVE_CMD_ID
   808 	gpointer   cmid;
   824 	gpointer   cmid;
   809 #else
   825 #else
   810 	int        nameref;
   826 	int        nameref;
   811 #endif
   827 #endif
   812 	int        selfref;
   828 	int        selfref;
  1057 				}
  1073 				}
  1058 			} else
  1074 			} else
  1059 				cid = luaL_checkenum (L, 4, lua_completion_type);
  1075 				cid = luaL_checkenum (L, 4, lua_completion_type);
  1060 		}
  1076 		}
  1061 	}
  1077 	}
  1062 	
  1078 
  1063 	cb = lua_newuserdata (L, sizeof (lua_command_t));
  1079 	cb = lua_newuserdata (L, sizeof (lua_command_t));
  1064 	luaL_getmetatable (L, "mcabber.command");
  1080 	luaL_getmetatable (L, "mcabber.command");
  1065 	lua_setmetatable (L, -2);
  1081 	lua_setmetatable (L, -2);
  1066 	lua_pushvalue (L, 2);
  1082 	lua_pushvalue (L, 2);
  1067 	cb -> cbref      = luaL_ref (L, LUA_REGISTRYINDEX);
  1083 	cb -> cbref      = luaL_ref (L, LUA_REGISTRYINDEX);
  1068 	lua_pushvalue (L, -1);
  1084 	lua_pushvalue (L, -1);
  1069 	cb -> selfref    = luaL_ref (L, LUA_REGISTRYINDEX);
  1085 	cb -> selfref    = luaL_ref (L, LUA_REGISTRYINDEX);
  1070 	cb -> parse_args = parse;
  1086 	cb -> parse_args = parse;
  1071 	cb -> L          = L;
  1087 	cb -> L          = L;
  1072 #ifdef HAVE_CMD_ID
  1088 #ifdef MCABBER_API_HAVE_CMD_ID
  1073 	cb -> cmid       = cmd_add (name, "", cid, 0, (void (*) (char *p)) lua_main_command_handler, cb);
  1089 	cb -> cmid       = cmd_add (name, "", cid, 0, (void (*) (char *p)) lua_main_command_handler, cb);
  1074 #else
  1090 #else
  1075 	lua_pushvalue (L, 1);
  1091 	lua_pushvalue (L, 1);
  1076 	cb -> nameref    = luaL_ref (L, LUA_REGISTRYINDEX);
  1092 	cb -> nameref    = luaL_ref (L, LUA_REGISTRYINDEX);
  1077 	cmd_add (name, "", cid, 0, (void (*) (char *p)) lua_main_command_handler, cb);
  1093 	cmd_add (name, "", cid, 0, (void (*) (char *p)) lua_main_command_handler, cb);
  1080 	return 1;
  1096 	return 1;
  1081 }
  1097 }
  1082 
  1098 
  1083 static void lua_mcabber_unregister_command (lua_State *L, lua_command_t *cb)
  1099 static void lua_mcabber_unregister_command (lua_State *L, lua_command_t *cb)
  1084 {
  1100 {
  1085 #ifdef HAVE_CMD_ID
  1101 #ifdef MCABBER_API_HAVE_CMD_ID
  1086 	if (cb -> cmid)
  1102 	if (cb -> cmid)
  1087 		cmd_del (cb -> cmid);
  1103 		cmd_del (cb -> cmid);
  1088 #else
  1104 #else
  1089 	const char *name;
  1105 	const char *name;
  1090 
  1106 
  1117 static int lua_mcabber_command_gc (lua_State *L)
  1133 static int lua_mcabber_command_gc (lua_State *L)
  1118 {
  1134 {
  1119 	lua_command_t *cb = luaL_checkudata (L, 1, "mcabber.command");
  1135 	lua_command_t *cb = luaL_checkudata (L, 1, "mcabber.command");
  1120 	luaL_argcheck (L, cb != NULL, 1, "mcabber command object expected");
  1136 	luaL_argcheck (L, cb != NULL, 1, "mcabber command object expected");
  1121 	lua_mcabber_unregister_command (L, cb);
  1137 	lua_mcabber_unregister_command (L, cb);
  1122 #ifndef HAVE_CMD_ID
  1138 #ifndef MCABBER_API_HAVE_CMD_ID
  1123 	if (cb -> nameref != LUA_NOREF)
  1139 	if (cb -> nameref != LUA_NOREF)
  1124 		luaL_unref (L, LUA_REGISTRYINDEX, cb -> nameref);
  1140 		luaL_unref (L, LUA_REGISTRYINDEX, cb -> nameref);
  1125 #endif
  1141 #endif
  1126 	if (cb -> cbref != LUA_NOREF)
  1142 	if (cb -> cbref != LUA_NOREF)
  1127 		luaL_unref (L, LUA_REGISTRYINDEX, cb -> cbref);
  1143 		luaL_unref (L, LUA_REGISTRYINDEX, cb -> cbref);
  1745 				++word;
  1761 				++word;
  1746 			}
  1762 			}
  1747 		}
  1763 		}
  1748 	}
  1764 	}
  1749 
  1765 
       
  1766 #ifdef MCABBER_API_HAVE_CMD_ID
       
  1767 	lua_cmdid = cmd_add ("lua", "Evaluate lua string", 0, 0, (void (*) (char *p)) do_lua, lua);
       
  1768 #else
  1750 	cmd_add ("lua", "Evaluate lua string", 0, 0, (void (*) (char *p)) do_lua, lua);
  1769 	cmd_add ("lua", "Evaluate lua string", 0, 0, (void (*) (char *p)) do_lua, lua);
       
  1770 #endif
  1751 
  1771 
  1752 #ifdef LLM_LOG_HANDLER
  1772 #ifdef LLM_LOG_HANDLER
  1753 	// FIXME: this should not be here.
  1773 	// FIXME: this should not be here.
  1754 	lua_lm_log_handler_id = g_log_set_handler ("lua-lm", G_LOG_LEVEL_MASK, (GLogFunc) lua_lm_log_handler, NULL);
  1774 	lua_lm_log_handler_id = g_log_set_handler ("lua-lm", G_LOG_LEVEL_MASK, (GLogFunc) lua_lm_log_handler, NULL);
  1755 #endif
  1775 #endif
  1757 	lua_hook_init    (lua);
  1777 	lua_hook_init    (lua);
  1758 	lua_command_init (lua);
  1778 	lua_command_init (lua);
  1759 
  1779 
  1760 	{
  1780 	{
  1761 		char *initfile = expand_filename (settings_opt_get ("lua_init_filename"));
  1781 		char *initfile = expand_filename (settings_opt_get ("lua_init_filename"));
  1762 		
  1782 
  1763 		if (!initfile)
  1783 		if (!initfile)
  1764 			scr_log_print (LPRINT_LOGNORM, "lua: Cannot determine config file name");
  1784 			scr_log_print (LPRINT_LOGNORM, "lua: Cannot determine config file name");
  1765 		else {
  1785 		else {
  1766 			if (luaL_loadfile(lua, initfile)) {
  1786 			if (luaL_loadfile(lua, initfile)) {
  1767 				scr_log_print (LPRINT_LOGNORM, "lua: Unable to compile rc file: %s", lua_tostring (lua, -1));
  1787 				scr_log_print (LPRINT_LOGNORM, "lua: Unable to compile rc file: %s", lua_tostring (lua, -1));
  1851 
  1871 
  1852 		g_slist_foreach (lua_added_features, (GFunc) lua_features_destroy, NULL);
  1872 		g_slist_foreach (lua_added_features, (GFunc) lua_features_destroy, NULL);
  1853 		g_slist_free (lua_added_features);
  1873 		g_slist_free (lua_added_features);
  1854 		lua_added_features = NULL;
  1874 		lua_added_features = NULL;
  1855 
  1875 
       
  1876 #ifdef MCABBER_API_HAVE_CMD_ID
       
  1877 		cmd_del (lua_cmdid);
       
  1878 		lua_cmdid = NULL;
       
  1879 #else
  1856 		cmd_del ("lua");
  1880 		cmd_del ("lua");
       
  1881 #endif
  1857 
  1882 
  1858 		lua_close (lua);
  1883 		lua_close (lua);
  1859 		lua = NULL;
  1884 		lua = NULL;
  1860 
  1885 
  1861 		g_slist_foreach (lua_added_categories, (GFunc) lua_categories_destroy, NULL);
  1886 		g_slist_foreach (lua_added_categories, (GFunc) lua_categories_destroy, NULL);