Enum completion type
authorMyhailo Danylenko <isbear@ukrpost.net>
Thu, 26 Feb 2009 01:05:02 +0200
changeset 9 c2517f8bf647
parent 8 fc9060b9b7cc
child 10 73f4c12b6ffb
Enum completion type * small improvements around * built-in completion types made enum
examples/marking.lua
examples/mcabberrc.lua
examples/room_priv.lua
examples/transports.lua
examples/xep0030.lua
examples/xep0077.lua
examples/xep0146.lua
main.c
--- a/examples/marking.lua	Wed Feb 25 22:58:34 2009 +0200
+++ b/examples/marking.lua	Thu Feb 26 01:05:02 2009 +0200
@@ -68,4 +68,9 @@
 -- TODO: check if ins already bound
 main.run ( 'bind 331 = lua mark_toggle ()' )
 
+hooks_d['hook-quit'].mark =
+	function ( args )
+		main.run ( 'bind 331 =' )
+	end
+
 -- vim: se ts=4: --
--- a/examples/mcabberrc.lua	Wed Feb 25 22:58:34 2009 +0200
+++ b/examples/mcabberrc.lua	Thu Feb 26 01:05:02 2009 +0200
@@ -30,33 +30,50 @@
 
 NOTES
 
-Most hooks get one incoming parameter - hash table with some fields in it.
-This allows further extending and adding arguments without breaking
-previous implementations.
-
 Ibb uses own iq handler. This is the laziest way to implement this.
 
 BUILTINS
 
-print (global) - prints to log
-dofile (global) - loads lua file from default mcabber location
+print (global)   - prints to log/status/logwindow
+dofile (global)  - loads lua file from default mcabber location
 
 main methods:
-- run           - run literal mcabber command
-- beep          - beep
-- log           - print to log w/specified priority
-- print_info    - print info into specified buffer
-- config_file   - format full file name from relative to mcabber's config dir
-- status        - get current user status and message
-- roster        - get list of roster jids (rooms, buddies and agents)
-- current_buddy - get jid of current buddy
-- buddy_info    - get table with info about jid and its resources
-- connection    - get lightuserdata for mcabber's lm connection
-- timer         - run function periodically
-- bgread        - run command and read it's output in background
-- add_feature   - add string to feature list (for disco#info)
-- del_feature   - delete string from feature list
-- command       - adds/removes mcabber command
+- run            - run literal mcabber command
+- beep           - beep
+- log            - print to log w/specified priority
+- print_info     - print info into specified buffer
+- config_file    - format full file name from relative to mcabber's config dir
+- status         - get current user status and message
+- roster         - get list of roster jids (rooms, buddies and agents)
+- current_buddy  - get jid of current buddy
+- buddy_info     - get table with info about jid and its resources
+- connection     - get lightuserdata for mcabber's lm connection
+- timer          - run function periodically
+- bgread         - run command and read it's output in background
+- add_feature    - add string to feature list (for disco#info)
+- del_feature    - delete string from feature list
+- add_completion - adds word to completions
+- del_completion - removes word from completions
+- command        - adds/removes mcabber command
+- option         - sets/gets mcabber option
+
+STRUCTURE
+
+To allow your chunk of configuration (eg foo.lua) to play friendly with other, do:
+
+- place it into a separate file, eg foo.lua
+- if you need hooks, do 
+  hook_d[hookname].foo = function ( args ) foo ( ) end
+- if you register commands, also do
+  commands_help['foo'] = "arg1 arg2\n\nFoo does bar."
+- append to the end of this file
+  dopath 'foo'
+- try to be friendly to module reloads, eg unregister handlers, where appropriate,
+  reset bindings
+- you can omit unregistering of commands and xmpp features - module will unregister
+  them automatically.
+- for now you can use parse_args and rebuild_args_string to deal with argument string,
+  but this is likely to be replaced with something more perfect.
 
 --]]
 
@@ -211,11 +228,11 @@
 main.command ( 'post',
 	function ( args )
 		main.run ( 'say_to -f ' .. args .. ' .' )
-	end )
+	end, 'filename' )
 main.command ( 's',
 	function ( args )
 		main.run ( ('status %s %s'):format ( args, mpd_getstatus () ) )
-	end, 0x10 )
+	end, 'status' )
 main.command ( 'cmd',
 	function ( args )
 		local to = main.current_buddy ()
@@ -229,7 +246,7 @@
 					return false
 				end
 			end )
-	end )
+	end, 'filename' )
 main.command ( 'exthelp',
 	function ( args )
 		if commands_help[args] then
@@ -243,7 +260,7 @@
 			print ( list:sub ( 1, -3 ) )
 			print ( "For built-in mcabber commands see /help" )
 		end
-	end, 0x1 )
+	end, 'cmd' )
 main.command ( 'reload',
 	function ( args )
 		dofile ( main.config_file ( 'mcabberrc.lua' ) )
--- a/examples/room_priv.lua	Wed Feb 25 22:58:34 2009 +0200
+++ b/examples/room_priv.lua	Thu Feb 26 01:05:02 2009 +0200
@@ -32,4 +32,10 @@
 main.run ( 'bind 338 = lua main.run ( "roster down" ); register_nicks ()' )
 main.run ( 'bind 339 = lua main.run ( "roster up" ); register_nicks ()' )
 
+hooks_d['hook-quit'].room_priv =
+	function ( args )
+		main.run ( 'bind 338 = roster down' )
+		main.run ( 'bind 339 = roster up' )
+	end
+
 -- vim: se ts=4: --
--- a/examples/transports.lua	Wed Feb 25 22:58:34 2009 +0200
+++ b/examples/transports.lua	Thu Feb 26 01:05:02 2009 +0200
@@ -1,5 +1,6 @@
 -- TRANSPORTED BUDDIES AVAILABILITY INDICATION
 
+-- XXX: to option?
 transport_jids = { 'icq.jabber.kiev.ua', 'mrim.unixzone.org.ua' }
 
 hooks_d['hook-status-change'].transports =
@@ -30,4 +31,15 @@
 		end
 	end
 
+hooks_d['hook-post-connect'].transports = hooks_d['hook-start'].transports
+
+hooks_d['hook-pre-disconnect'].transports =
+	function ( args )
+		for k, jid in pairs ( transport_jids ) do
+			-- when disconnected, all buddies are inaccessible, so, make them the same
+			main.run ( ("color roster * *@%s white"):format ( jid ) )
+			main.run ( ("color roster dn_? *@%s brightblack"):format ( jid ) )
+		end
+	end
+
 -- vim: se ts=4: --
--- a/examples/xep0030.lua	Wed Feb 25 22:58:34 2009 +0200
+++ b/examples/xep0030.lua	Thu Feb 26 01:05:02 2009 +0200
@@ -98,7 +98,7 @@
 					end
 				end, who )
 		end
-	end )
+	end, 'jid' )
 
 commands_help['disco'] = "[-t target_jid] [info | items] [node]\n\nService discovery request.\nInfo is sent if omitted.\nIf info reveals, that buddy can do items, items request also will be sent."
 
--- a/examples/xep0077.lua	Wed Feb 25 22:58:34 2009 +0200
+++ b/examples/xep0077.lua	Thu Feb 26 01:05:02 2009 +0200
@@ -151,7 +151,7 @@
 			who = full_current_jid ()
 		end
 		register_to ( who )
-	end, 0x2 )
+	end, 'jid' )
 
 main.command ( 'cancel',
 	function ( args )
@@ -162,7 +162,7 @@
 			who = full_current_jid ()
 		end
 		unregister_from ( who )
-	end, 0x2 )
+	end, 'jid' )
 
 commands_help['register'] = "[jid]\n\nSends registration request to jid (or current buddy). You, probably, then will need to fill and send some form."
 commands_help['cancel'] = "[jid]\n\nSends registration cancellation request to jid (or current buddy). May require a form filling."
--- a/examples/xep0146.lua	Wed Feb 25 22:58:34 2009 +0200
+++ b/examples/xep0146.lua	Thu Feb 26 01:05:02 2009 +0200
@@ -63,7 +63,7 @@
 		elseif args[1] then
 			remote_command ( who, args[1] )
 		end
-	end )
+	end, 'jid' )
 
 commands_help['remote'] = "[-t target_jid] [list | command]\n\nPrints list of available remote command or requests execution of specified command."
 
--- a/main.c	Wed Feb 25 22:58:34 2009 +0200
+++ b/main.c	Thu Feb 26 01:05:02 2009 +0200
@@ -322,6 +322,34 @@
 
 // MCABBER COMMANDS
 
+/// completion type
+/// Built-it completion types can be specified as string, instead of id.
+/// G:
+static const string2enum_t lua_completion_type[] = {
+	{ "cmd",       COMPL_CMD       },
+	{ "jid",       COMPL_JID       },
+	{ "urljid",    COMPL_URLJID    },
+	{ "name",      COMPL_NAME      },
+	{ "status",    COMPL_STATUS    },
+	{ "filename",  COMPL_FILENAME  },
+	{ "roster",    COMPL_ROSTER    },
+	{ "buffer",    COMPL_BUFFER    },
+	{ "group",     COMPL_GROUP     },
+	{ "groupname", COMPL_GROUPNAME },
+	{ "multiline", COMPL_MULTILINE },
+	{ "room",      COMPL_ROOM      },
+	{ "resource",  COMPL_RESOURCE  },
+	{ "auth",      COMPL_AUTH      },
+	{ "request",   COMPL_REQUEST   },
+	{ "events",    COMPL_EVENTS    },
+	{ "eventsid",  COMPL_EVENTSID  },
+	{ "pgp",       COMPL_PGP       },
+	{ "color",     COMPL_COLOR     },
+	{ "otr",       COMPL_OTR       },
+	{ "ortpolicy", COMPL_OTRPOLICY },
+	{ NULL,        0               },
+};
+
 typedef struct {
 	int        reference;
 	lua_State *L;
@@ -366,11 +394,11 @@
 /// Associates or breaks association between mcabber command name and lua function.
 /// To unregister command omit function argument.
 /// If you specify a third argument, table (even empty), function will return completion group id or nothing.
-/// You can also specify an integer instead of table, then no new id will be registered.
+/// You can also specify a string name (see completion type) instead of table, for non-builtin, you can just pass integer id.
 /// Note, that for now there are no way to unregister completion group, so, resources can be exausted easily.
 /// Also note, that it ignores keys in a completion table.
-/// A: string (command name), command function (optional), table (completions, optional)/integer (comletion group id, optional)
-/// R: integer (completion group id, optional) or nil
+/// A: string (command name), command function (optional), table (completions, optional)/completion type (or integer comletion group id, optional)
+/// R: completion type (integer completion group id or string for builtin types, optional)
 static int lua_main_command (lua_State *L)
 {
 	const char             *name = luaL_checkstring (L, 1);
@@ -390,7 +418,7 @@
 					}
 				}
 			} else
-				cid = luaL_checkinteger (L, 3);
+				cid = luaL_checkenum (L, 3, lua_completion_type);
 		}
 
 		cb = luaL_malloc (L, sizeof (lua_command_callback_t));
@@ -402,7 +430,7 @@
 		lua_added_commands = g_slist_prepend (lua_added_commands, g_strdup (name));
 
 		if (cid) {
-			lua_pushinteger (L, cid);
+			luaL_pushenum (L, cid, lua_completion_type);
 			return 1;
 		}
 	} else { // Unregister