main.c
changeset 6 90dceae3ed1f
parent 3 a5f864d4207f
child 7 eb6d89bf1fbf
--- a/main.c	Mon Feb 23 23:23:42 2009 +0200
+++ b/main.c	Tue Feb 24 09:14:00 2009 +0200
@@ -46,11 +46,13 @@
 /// dopath
 /// Loads lua file from default location.
 /// A: string (filename, without ".lua")
+/// R: string (error message, optional)
 static int lua_global_dopath (lua_State *L)
 {
 	const char *name = luaL_checkstring (L, 1);
 	size_t      size = lua_objlen (L, 1);
 	char       *path;
+	int         ret = 0;
 	if (!strncmp (name + size - 4, ".lua", 4))
 		path = mcabber_config_filename (name);
 	else {
@@ -59,13 +61,16 @@
 		g_free (fname);
 	}
 
-	if (luaL_loadfile (L, path))
+	if (ret = luaL_loadfile (L, path))
 		scr_LogPrint (LPRINT_LOGNORM, "lua: Unable to compile file %s: %s", path, lua_tostring (L, -1));
-	else if (lua_pcall (lua, 0, LUA_MULTRET, 0))
+	else if (ret = lua_pcall (lua, 0, LUA_MULTRET, 0))
 		scr_LogPrint(LPRINT_LOGNORM, "lua: Runtime error in file %s: %s", path, lua_tostring (L, -1));
+	g_free (path);
 
-	g_free (path);
-	return 0;
+	if (ret)
+		return 1;
+	else
+		return 0;
 }
 
 /// main.config_file
@@ -533,14 +538,13 @@
 }
 #endif
 
-#ifndef LUA_HOOK_NAME
-#define LUA_HOOK_NAME ( "hook_handler" )
-#endif
-
 static void lua_hook (hk_arg_t *args, lua_State *L)
 {
 	hk_arg_t *arg = args;
-	lua_getglobal (lua, LUA_HOOK_NAME);
+	const char *hook = settings_opt_get ("lua_hook_function");
+	if (!hook)
+		return;
+	lua_getglobal (lua, hook);
 	if (!lua_isfunction (lua, -1)) {
 		lua_pop (lua, 1);
 		return;
@@ -553,7 +557,7 @@
 		arg++;
 	}
 	if (lua_pcall (lua, 1, 0, 0)) {
-		scr_LogPrint (LPRINT_NORMAL, "lua: Error in hook_handler: %s", lua_tostring (lua, -1));
+		scr_LogPrint (LPRINT_NORMAL, "lua: Error in hook handler: %s", lua_tostring (lua, -1));
 		lua_pop (lua, 1);
 	}
 }
@@ -626,13 +630,14 @@
 
 	hk_add_handler ((hk_handler_t) lua_hook, lua);
 
-	lua_getglobal (lua, "hook_start");
-	if (!lua_isfunction (lua, -1))
-		lua_pop (lua, 1);
-	else if (lua_pcall (lua, 0, 0, 0)) {
-		scr_LogPrint (LPRINT_NORMAL, "lua: Error in hook_start: %s", lua_tostring (lua, -1));
-		lua_pop (lua, 1);
+	{
+		hk_arg_t args[] = {
+			{ "hook", "hook-start" },
+			{ NULL,   NULL         },
+		};
+		lua_hook (args, lua);
 	}
+
 	return NULL;
 }
 
@@ -655,13 +660,11 @@
 void g_module_unload (GModule *module)
 {
 	if (lua) {
-		lua_getglobal (lua, "hook_quit");
-		if (!lua_isfunction (lua, -1))
-			lua_pop (lua, 1);
-		else if (lua_pcall (lua, 0, 0, 0)) {
-			scr_LogPrint (LPRINT_NORMAL, "lua: Error in hook_quit: %s", lua_tostring (lua, -1));
-			lua_pop (lua, 1);
-		}
+		hk_arg_t args[] = {
+			{ "hook", "hook-quit" },
+			{ NULL,   NULL        },
+		};
+		lua_hook (args, lua);
 
 		hk_del_handler ((hk_handler_t) lua_hook, lua);