main.fileoption
authorMyhailo Danylenko <isbear@ukrpost.net>
Mon, 16 Mar 2009 06:12:55 +0200
changeset 33 db5396037b43
parent 32 524fde5be49a
child 34 8206d7cb1447
main.fileoption
TODO
examples/jobs.lua
examples/lua.rc
examples/mcabberrc.lua
examples/urls.lua
main.c
--- a/TODO	Mon Mar 16 05:58:10 2009 +0200
+++ b/TODO	Mon Mar 16 06:12:55 2009 +0200
@@ -13,5 +13,4 @@
 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/jobs.lua	Mon Mar 16 05:58:10 2009 +0200
+++ b/examples/jobs.lua	Mon Mar 16 06:12:55 2009 +0200
@@ -3,10 +3,10 @@
 delayed_jobs = {}
 
 -- may fail
-dopath 'saved_jobs.lua'
+dofile ( main.fileoption 'lua_jobs_file' )
 
 function save_jobs ()
-	local h = io.open ( main.option ( 'lua_jobs_file' ), "w" )
+	local h = io.open ( main.fileoption 'lua_jobs_file', "w" )
 	if not h then
 		print ( 'Cannot open jobs file for writing!' )
 		return
--- a/examples/lua.rc	Mon Mar 16 05:58:10 2009 +0200
+++ b/examples/lua.rc	Mon Mar 16 06:12:55 2009 +0200
@@ -12,13 +12,13 @@
 set lua_pep_notification = enable
 
 # XXX jobs file
-set lua_jobs_file        = /home/isbear/.mcabber/saved_jobs.lua
+set lua_jobs_file        = ~/.mcabber/saved_jobs.lua
 
 # XXX url file
-set lua_url_file         = /home/isbear/.mcabber/urls.log
+set lua_url_file         = ~/.mcabber/urls.log
 
 # XXX additional path to allow non-system loading of lua-lm
-set lua_extra_include    = /home/isbear/.mcabber
+set lua_extra_include    = ~/.mcabber
 
 load mcabber-lua
 
--- a/examples/mcabberrc.lua	Mon Mar 16 05:58:10 2009 +0200
+++ b/examples/mcabberrc.lua	Mon Mar 16 06:12:55 2009 +0200
@@ -78,8 +78,8 @@
 -- This is a hack to allow loading of lm.lua and loudmouth.so from ~/.mcabber
 -- instead of installing them system-wide
 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
+	package.path = main.fileoption ( 'lua_extra_include' ) .. '/?.lua;' .. package.path
+	package.cpath = main.fileoption ( 'lua_extra_include' ) .. '/?.so;' .. package.cpath
 end
 
 require 'lm'
--- a/examples/urls.lua	Mon Mar 16 05:58:10 2009 +0200
+++ b/examples/urls.lua	Mon Mar 16 06:12:55 2009 +0200
@@ -1,11 +1,9 @@
 -- SAVING URLS TO FILE
 
-url_file = main.option ( 'lua_url_file' )
-
 hooks_d['hook-message-in'].urls =
 	function ( args )
 		for url in args.message:gmatch ( "https?://[%w%p]+" ) do
-			fd = io.open ( url_file, "a" )
+			fd = io.open ( main.fileoption 'lua_url_file', "a" )
 			if fd then
 				fd:write ( url .. "\n" )
 				fd:close ()
--- a/main.c	Mon Mar 16 05:58:10 2009 +0200
+++ b/main.c	Mon Mar 16 06:12:55 2009 +0200
@@ -56,7 +56,7 @@
 	size_t      size = lua_objlen (L, 1);
 	char       *path;
 	int         ret = 0;
-	if (!strncmp (name + size - 4, ".lua", 4))
+	if (size > 4 && !strncmp (name + size - 4, ".lua", 4))
 		path = mcabber_config_filename (name);
 	else {
 		char *fname = g_strconcat (name, ".lua", NULL);
@@ -276,6 +276,21 @@
 	}
 }
 
+/// main.fileoption
+/// Gets option, expanding it as filename.
+/// A: string (option name)
+/// R: string (expanded option value) or nil
+static int lua_main_fileoption (lua_State *L)
+{
+	char *fname = expand_filename (settings_opt_get (luaL_checkstring (L, 1)));
+	if (fname) {
+		lua_pushstring (L, fname);
+		g_free (fname);
+	} else
+		lua_pushnil (L);
+	return 1;
+}
+
 /// main.connection
 /// Returns lightuserdata of mcabber's loudmouth connection.
 /// This can be very useful with lua-loudmouth, and not much otherwise.
@@ -1023,6 +1038,7 @@
 	reg ( option )
 	reg ( alias )
 	reg ( binding )
+	reg ( fileoption )
 	reg ( add_feature )
 	reg ( del_feature )
 	reg ( parse_args )