yesno in C, no config_file
authorMyhailo Danylenko <isbear@ukrpost.net>
Mon, 16 Mar 2009 05:31:24 +0200
changeset 31 54957980a83a
parent 30 2cf24aced294
child 32 524fde5be49a
yesno in C, no config_file
TODO
examples/beep.lua
examples/lua.rc
examples/mcabberrc.lua
examples/urls.lua
examples/xep0163.lua
main.c
--- a/TODO	Mon Mar 16 04:43:24 2009 +0200
+++ b/TODO	Mon Mar 16 05:31:24 2009 +0200
@@ -13,4 +13,5 @@
 think about how tune notification should act on start/quit
 clarify situation with disco, maybe it is due to this we are unable to receive pep events
 lm debug should be more flexible
+file option (usual option get but with home expanding)?
 
--- a/examples/beep.lua	Mon Mar 16 04:43:24 2009 +0200
+++ b/examples/beep.lua	Mon Mar 16 05:31:24 2009 +0200
@@ -4,7 +4,7 @@
 
 main.command ( 'beep',
 	function ( args )
-		local enable = yesno ( args )
+		local enable = main.yesno ( args )
 		if enable ~= nil then
 			beep_enable = enable
 		end
--- a/examples/lua.rc	Mon Mar 16 04:43:24 2009 +0200
+++ b/examples/lua.rc	Mon Mar 16 05:31:24 2009 +0200
@@ -1,11 +1,11 @@
+
+# initialization file to load
+set lua_init_filename    = ~/.mcabber/mcabberrc.lua
 
 # lua function name to be called on hooks invocation
 set lua_hook_function    = hook_handler
 
-# initialization file to load
-set lua_init_filename    = ~/.mcabber/mcabberrc.lua
-
-# lua-loudmouth debug output be shown in log
+# enable lua-loudmouth debug output to be shown in log
 set lua_lm_debug         = 0
 
 # show notifications for pep events
@@ -14,5 +14,11 @@
 # XXX jobs file
 set lua_jobs_file        = /home/isbear/.mcabber/saved_jobs.lua
 
+# XXX url file
+set lua_url_file         = /home/isbear/.mcabber/urls.log
+
+# XXX additional path to allow non-system loading of lua-lm
+set lua_extra_include    = /home/isbear/.mcabber
+
 load mcabber-lua
 
--- a/examples/mcabberrc.lua	Mon Mar 16 04:43:24 2009 +0200
+++ b/examples/mcabberrc.lua	Mon Mar 16 05:31:24 2009 +0200
@@ -77,8 +77,10 @@
 
 -- This is a hack to allow loading of lm.lua and loudmouth.so from ~/.mcabber
 -- instead of installing them system-wide
-package.path = main.config_file ( '?.lua' ) .. ';' .. package.path
-package.cpath = main.config_file ( '?.so' ) .. ';' .. package.cpath
+if main.option ( 'lua_extra_include' ) then
+	package.path = main.option ( 'lua_extra_include' ) .. '/?.lua;' .. package.path
+	package.cpath = main.option ( 'lua_extra_include' ) .. '/?.so;' .. package.cpath
+end
 
 require 'lm'
 
@@ -93,7 +95,7 @@
 	end
 end
 
--- This is for debugging purposes, for real reloading need to quote and bracket keys.
+-- This is for debugging purposes, not for reloading. For that see jobs.
 function table_to_string ( tab, pre )
 	local prefix = pre or ""
 	local tbls, jk = "", ""
@@ -102,7 +104,7 @@
 		if type ( val ) == 'table' then
 			tbls = string.format ( "%s  %s%s = %s,\n", tbls, prefix, tostring(key), table_to_string ( val, "  " .. prefix ) )
 		else
-			jk = string.format ( "%s %s = %q,", jk, tostring(key), tostring(val) )
+			jk = string.format ( "%s %s = \"%s\",", jk, tostring(key), tostring(val) )
 		end
 	end
 
@@ -126,28 +128,17 @@
 	return false
 end
 
+-- XXX to C
 boolean_cid = main.add_category { 'enable', 'disable', 'yes', 'no', 'true', 'false', 'on', 'off' }
 
-function yesno ( value )
-	if value == 'enable' or value == 'yes' or value == 'true' or value == 'on' or value == true then
-		return true
-	elseif value == 'disable' or value == 'no' or value == 'false' or value == 'off' or value == false then
-		return false
-	else
-		return nil
-	end
-end
-
 -- COMMANDS
 
 -- Help strings should not contain command, only arguments. This is necessary to support soft aliases.
 commands_help = {
 	post      = "filename\n\nSends file as a message. Just shorthand.",
-	s         = "status [message]\n\nSets your status, but takes into account mpd (if enabled).",
 	beep      = "[enable|disable|on|off|yes|no|true|false]\n\nEnables or disables beeping on all messages.\nIf state is omitted, prints current state.",
 	cmd       = "shell_command\n\nRuns shell command in background and sends output to current buddy.\nWorks asynchroneously, and may break long output in the middle of line",
 	exthelp   = "[command]\n\nPrints help for a given command, or list of available help topics.",
-	reload    = "\n\nJust a shorthand to reload lua config file. Note, that for now this discards all changes to configuration, open forms, transferred files.",
 	['join!'] = "\n\nForcibly joins to current buddy. Just saves you typing of full room name (that can be quite long) in a case of a non-bookmarked rooms.",
 	count     = "\n\nPrints number of resources of current buddy. Useful to determine member count of large room.",
 	toggle    = "\n\nToggles away/online status.",
@@ -157,10 +148,6 @@
 	function ( args )
 		main.run ( 'say_to -f ' .. args .. ' .' )
 	end, 'filename' )
-main.command ( 's',
-	function ( args )
-		main.run ( ('status %s %s'):format ( args, mpd_getstatus () ) )
-	end, 'status' )
 main.command ( 'cmd',
 	function ( args )
 		local to = main.current_buddy ()
@@ -189,10 +176,6 @@
 			print ( "For built-in mcabber commands see /help" )
 		end
 	end, 'cmd' )
-main.command ( 'reload',
-	function ( args )
-		dofile ( main.config_file ( 'mcabberrc.lua' ) )
-	end )
 main.command ( 'join!',
 	function ( args )
 		main.run ( 'room join ' .. main.current_buddy () )
--- a/examples/urls.lua	Mon Mar 16 04:43:24 2009 +0200
+++ b/examples/urls.lua	Mon Mar 16 05:31:24 2009 +0200
@@ -1,6 +1,6 @@
 -- SAVING URLS TO FILE
 
-url_file = main.config_file ( 'urls.log' )
+url_file = main.option ( 'lua_url_file' )
 
 hooks_d['hook-message-in'].urls =
 	function ( args )
--- a/examples/xep0163.lua	Mon Mar 16 04:43:24 2009 +0200
+++ b/examples/xep0163.lua	Mon Mar 16 05:31:24 2009 +0200
@@ -117,7 +117,7 @@
 	function ( conn, mess )
 		local e = mess:child ( 'event' )
 		if e and e:attribute ( 'xmlns' ) == 'http://jabber.org/protocol/pubsub#event' then
-			local enable = yesno ( main.option ( 'lua_pep_notification' ) )
+			local enable = main.yesno ( main.option ( 'lua_pep_notification' ) )
 			if enable == false then
 				return true
 			end
@@ -256,7 +256,7 @@
 
 main.command ( 'tune',
 	function ( args )
-		local enable = yesno ( args )
+		local enable = main.yesno ( args )
 		if enable == nil then
 			if tune_enabled then
 				print ( "Tune notifications enabled" )
--- a/main.c	Mon Mar 16 04:43:24 2009 +0200
+++ b/main.c	Mon Mar 16 05:31:24 2009 +0200
@@ -76,21 +76,40 @@
 		return 0;
 }
 
-/// main.config_file
-/// Adds mcabber default config location path to config file name.
-/// Note: deprecated, use dopath.
-/// XXX: should we use g_filename_from_utf8?
-/// A: string (filename)
-/// R: string (full path)
-static int lua_main_config_file (lua_State *L)
+/// yes or no ansvers
+/// G:
+static const string2enum_t lua_yesno[] = {
+	{ "enable",  1 },
+	{ "disable", 0 },
+	{ "true",    1 },
+	{ "false",   0 },
+	{ "on",      1 },
+	{ "off",     0 },
+	{ "yes",     1 },
+	{ "no",      0 },
+	{ "1",       1 },
+	{ "0",       0 },
+	{ NULL,     -1 },
+};
+
+/// main.yesno
+/// According to yes or no ansvers returns true or false.
+/// If ansver is not recognized, returns nil.
+/// A: anything (string expected)
+/// R: boolean or nil
+static int lua_main_yesno (lua_State *L)
 {
-	char *home = mcabber_config_filename (luaL_checkstring (L, 1));
-	if (!home) {
-		lua_pushstring (L, "Cannot find home dir!");
-		lua_error (L);
-	}
-	lua_pushstring (L, home);
-	g_free (home);
+	int type = lua_type (L, 1);
+	if (type == LUA_TSTRING) {
+		int ret = luaL_checkenum (L, 1, lua_yesno);
+		if (ret == -1)
+			lua_pushnil (L);
+		else
+			lua_pushboolean (L, ret);
+	} else if (type == LUA_TNUMBER)
+		lua_pushboolean (L, lua_tointeger (L, 1));
+	else if (type != LUA_TBOOLEAN)
+		lua_pushnil (L);
 	return 1;
 }
 
@@ -996,7 +1015,7 @@
 #define reg(NAME)                   \
 	{ #NAME, lua_main_##NAME },
 static const luaL_Reg lua_reg_main[] = {
-	reg ( config_file )
+	reg ( yesno )
 	reg ( connection )
 	reg ( log )
 	reg ( option )