# HG changeset patch # User Myhailo Danylenko # Date 1269987919 -10800 # Node ID ed4676536ed90df63411e83372e66188dbe66351 # Parent 59aeae623ac6726231a97cd2838d566e4ad90394 Update most useful scripts * new hooks interface * connection state checking diff -r 59aeae623ac6 -r ed4676536ed9 examples/attention.lua --- a/examples/attention.lua Wed Mar 31 00:28:04 2010 +0300 +++ b/examples/attention.lua Wed Mar 31 01:25:19 2010 +0300 @@ -3,8 +3,11 @@ local attention = require 'lm.attention' attention.handler ( - function ( mesg ) + function ( mesg, from ) local times = 0 + if from then + main.print_info ( from, "Buddy wants your attention!" ) + end main.timer ( 1, function () if times < 6 then @@ -18,13 +21,18 @@ main.command ( 'attention', function ( args ) + local connection = main.connection () + if not connection then + print "You are not online!" + return + end local who if args.t then who = args.t else who = main.full_jid () end - attention.send ( lm.connection.bless ( main.connection () ), who, args[1] ) + attention.send ( lm.connection.bless ( connection ), who, args[1] ) end, true, 'jid' ) commands_help['attention'] = "[-t to] [message]\n\nTries to get buddy's attention." @@ -32,19 +40,35 @@ local attention_handler = lm.message_handler.new ( attention.message_handler ) local attention_handler_registered = false -hooks_d['hook-post-connect'].attention = +local attention_pc_handler = + function ( args ) + local connection = main.connection () + if connection then + lm.connection.bless(connection):handler ( attention_handler, 'message', 'normal' ) + attention_handler_registered = true + end + end +main.hook ( 'hook-post-connect', attention_pc_handler ) +main.hook ( 'hook-pre-disconnect', function ( args ) - lm.connection.bless( main.connection () ):handler ( attention_handler, 'message', 'normal' ) - attention_handler_registered = true - hooks_d['hook-post-connect'].attention = nil - hooks_d['hook-quit'].attention = - function ( args ) - if attention_handler_registered then - lm.connection.bless( main.connection () ):handler ( attention_handler, 'message' ) - end + if attention_handler_registered then + local connection = main.connection () + if connection then + lm.connection.bless(connection):handler ( attention_handler, 'message' ) end - end + attention_handler_registered = false + end + end ) -main.add_feature ( 'urn:xmpp:attention:0' ) +-- register handler, if we are already connected +main.hook ( 'hook-lua-start', + function ( args ) + main.add_feature ( 'urn:xmpp:attention:0' ) + attention_pc_handler () + end ) +main.hook ('hook-lua-quit', + function ( args ) + main.del_feature ( 'urn:xmpp:attention:0' ) + end ) --- vim: se ts=4: -- +-- vim: se ts=4 sw=4: -- diff -r 59aeae623ac6 -r ed4676536ed9 examples/evil.lua --- a/examples/evil.lua Wed Mar 31 00:28:04 2010 +0300 +++ b/examples/evil.lua Wed Mar 31 01:25:19 2010 +0300 @@ -26,7 +26,12 @@ -- TODO improve interface, check if we sending right thing for offline main.command ( 'evil', function ( args ) - local conn = lm.connection.bless ( main.connection () ) + local connection = main.connection () + if not connection then + print "You are not online!" + return + end + local conn = lm.connection.bless ( connection ) if args[1] == 'status' then local text = '' for i, mesg in ipairs ( args ) do @@ -71,22 +76,39 @@ local evil_handler = lm.message_handler.new ( evil.stanza_handler ) local evil_handler_registered = false -hooks_d['hook-post-connect'].evil = +local evil_pc_handler = + function ( args ) + local connection = main.connection () + if connection then + lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq', 'first' ) + lm.connection.bless( main.connection () ):handler ( evil_handler, 'message', 'first' ) + lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence', 'first' ) + evil_handler_registered = true + end + end +main.hook ( 'hook-post-connect', evil_pc_handler ) +main.hook ( 'hook-pre-disconnect', function ( args ) - lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq', 'first' ) - lm.connection.bless( main.connection () ):handler ( evil_handler, 'message', 'first' ) - lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence', 'first' ) - evil_handler_registered = true - hooks_d['hook-post-connect'].evil = nil - hooks_d['hook-quit'].evil = - function ( args ) - if evil_handler_registered then - lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq' ) - lm.connection.bless( main.connection () ):handler ( evil_handler, 'message' ) - lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence' ) - end + if evil_handler_registered then + local connection = main.connection () + if connection then + lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq' ) + lm.connection.bless( main.connection () ):handler ( evil_handler, 'message' ) + lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence' ) end + evil_handler_registered = false + end + end ) + +main.hook ( 'hook-lua-start', + function ( args ) + main.add_feature ( 'http://jabber.org/protocol/evil' ) + evil_pc_handler () end +main.hook ( 'hook-lua-quit', + function ( args ) + main.del_feature ( 'http://jabber.org/protocol/evil' ) + end ) local char2xmpp = { f = 'chat', @@ -98,13 +120,14 @@ } -- hack, but working ;) -hooks_d['hook-my-status-change'].evil = +main.hook ( 'hook-my-status-change', function ( args ) if main.yesno ( main.option ( 'lua_evil_mode' ) ) then - evil.presence ( lm.connection.bless ( main.connection () ), nil, char2xmpp[args.new_status], args.message ) + local connection = main.connection () + if connection then + evil.presence ( lm.connection.bless ( connection ), nil, char2xmpp[args.new_status], args.message ) + end end - end + end ) -main.add_feature ( 'http://jabber.org/protocol/evil' ) - --- vim: se ts=4: -- +-- vim: se ts=4 sw=4: -- diff -r 59aeae623ac6 -r ed4676536ed9 examples/jobs.lua --- a/examples/jobs.lua Wed Mar 31 00:28:04 2010 +0300 +++ b/examples/jobs.lua Wed Mar 31 01:25:19 2010 +0300 @@ -63,15 +63,14 @@ commands_help['delay'] = "[-t target_jid] status_letter message\n\nDelays sending a message to target jid (or current buddy) until it switches to specified status." commands_help['job'] = "[del jid status_letter]\n\nLists available jobs or deletes specified one." -hooks_d['hook-status-change'].jobs = +main.hook ( 'hook-status-change', function ( args ) if delayed_jobs[args.jid] and delayed_jobs[args.jid][args.new_status] then main.run ( delayed_jobs[args.jid][args.new_status] ) delayed_jobs[args.jid][args.new_status] = nil end - end + end ) -hooks_d['hook-quit'].jobs = save_jobs - +main.hook ( 'hook-lua-quit', save_jobs ) --- vim: se ts=4: -- +-- vim: se ts=4 sw=4: -- diff -r 59aeae623ac6 -r ed4676536ed9 examples/lm/attention.lua --- a/examples/lm/attention.lua Wed Mar 31 00:28:04 2010 +0300 +++ b/examples/lm/attention.lua Wed Mar 31 01:25:19 2010 +0300 @@ -9,7 +9,7 @@ local O = { handler = - function ( mesg ) + function ( mesg, from ) end, } @@ -38,7 +38,7 @@ if body then body = body:value () end - O.handler ( body ) + O.handler ( body, mess:attribute ( "from" ) ) end return false end diff -r 59aeae623ac6 -r ed4676536ed9 examples/mcabberrc.lua --- a/examples/mcabberrc.lua Wed Mar 31 00:28:04 2010 +0300 +++ b/examples/mcabberrc.lua Wed Mar 31 01:25:19 2010 +0300 @@ -56,6 +56,7 @@ - del_completion - removes word from completions - command - adds/removes mcabber command - option - sets/gets mcabber option +- hook - creates hook handler object STRUCTURE @@ -181,90 +182,36 @@ end end ) --- commands to allow leading smileys -for k, arg in ipairs ( { ')', '/', '(', 'D', '-/', 'S', '1', ']', '[' } ) do - main.command ( arg, - function ( args ) - main.run ( 'say :' .. arg .. ' ' .. args ) - end ) -end - --- HOOKS - -hooks_d = { - ['hook-message-in'] = { }, - ['hook-message-out'] = { }, - ['hook-status-change'] = { }, - ['hook-my-status-change'] = { }, - ['hook-post-connect'] = { }, - ['hook-pre-disconnect'] = { }, - ['hook-start'] = { }, - ['hook-quit'] = { }, -} +-- SAVING URLS TO FILE +-- These are implemented in C in urlopen and openurl modules --- hook: --- - hook-message-in --- jid --- groupchat --- message --- - hook-message-out --- jid --- message --- - hook-status-change --- jid --- resource --- new_status --- old_status --- message --- - hook-my-status-change --- new_status --- message --- - hook-post-connect --- - hook-pre-disconnect --- - hook-start --- - hook-quit -function hook_handler ( args ) - if [hooks_d[args.hook] then - for mod, cb in pairs ( hooks_d[args.hook] ) do - if cb then - cb ( args ) - end - end - end -end - --- FIXME - -hooks_d['hook-post-connect'].fifoperms = - function ( args ) - -- TODO: posix - os.execute ( 'chmod 0666 ' .. main.option ( 'fifo_name' ) ) - hooks_d['hook-post-connect'].fifoperms = nil - end - --- SAVING URLS TO FILE - -require 'urls' +-- require 'urls' -- TRANSPORTED BUDDIES AVAILABILITY INDICATION +-- After all, I do not use this anymore :/ +-- But you can try. -require 'transports' +-- require 'transports' -- BEEPING ON ALL MESSAGES +-- Implemented in C by in-tree beep module -require 'beep' +-- require 'beep' -- MARKING +-- Implemented in C in marking module -require 'marking' +-- require 'marking' -- JOBS +-- Somewhat error-prone, requires jobs file existance, never seriously used by me :( -require 'jobs' +-- require 'jobs' -- ROOM NICK COMPLETION +-- Well, it's sorta hacky, but I do use it -require 'room_priv' +-- require 'room_priv' -- FORMS @@ -285,8 +232,9 @@ require 'muc' -- IN-BAND BYTESTREAMS (XEP-0047) +-- Implemented in C in streams module -require 'ibb' +-- require 'ibb' -- VCARD-TEMP (XEP-0054) @@ -301,42 +249,50 @@ require 'oob' -- MALICIOUS STANZAS (XEP-0076) +-- Well, that's joke XEP and joke module -require 'evil' +-- require 'evil' -- IN-BAND REGISTRATION (XEP-0077) require 'register' -- USER LOCATION (XEP-0080) +-- Implemented in C in geoloc module -require 'geoloc' +-- require 'geoloc' -- USER AVATAR (XEP-0084) +-- Implemented in C in avatar module -require 'avatar' +-- require 'avatar' -- USER MOOD (XEP-0107) +-- Implemented in C in mood module -require 'mood' +-- require 'mood' -- USER ACTIVITY (XEP-0108) +-- Implemented in C in activity module -require 'activity' +-- require 'activity' -- USER TUNE (XEP-0118) +-- Implemented in C in tune module -require 'tune' +-- require 'tune' -- REMOTE CONTROLLING CLIENTS (XEP-0146) require 'remote' -- PERSONAL EVENTING PROTOCOL (XEP-0163) +-- Implemented in C in pep module -- XMPP PING (XEP-0199) +-- Included into upstream as /request ping -require 'ping' +-- require 'ping' -- ATTENTION (XEP-0224) diff -r 59aeae623ac6 -r ed4676536ed9 examples/muc.lua --- a/examples/muc.lua Wed Mar 31 00:28:04 2010 +0300 +++ b/examples/muc.lua Wed Mar 31 01:25:19 2010 +0300 @@ -4,13 +4,18 @@ main.command ( 'room-config', function ( args ) + local connection = main.connection () + if not connection then + print "You are not online!" + return + end local who if args then who = args else who = main.current_buddy () end - local conn = lm.connection.bless ( main.connection () ) + local conn = lm.connection.bless ( connection ) muc.owner_config ( conn, who, function ( form, submit, reject ) insert_form ( form, @@ -40,4 +45,4 @@ commands_help['room-config'] = "[room_jid]\n\nRequests room configuration form from server. You must be owner of this room." --- vim: se ts=4: -- +-- vim: se ts=4 sw=4: -- diff -r 59aeae623ac6 -r ed4676536ed9 examples/oob.lua --- a/examples/oob.lua Wed Mar 31 00:28:04 2010 +0300 +++ b/examples/oob.lua Wed Mar 31 01:25:19 2010 +0300 @@ -14,6 +14,11 @@ main.command ( 'oob', function ( args ) + local connection = main.connection () + if not connection then + print "You are not online!" + return + end local who if args.t then who = args.t @@ -21,7 +26,7 @@ who = main.full_jid () end -- here we can run something external to put file on server and obtain link to it - oob.send ( lm.connection.bless ( main.connection () ), who, args[1], + oob.send ( lm.connection.bless ( connection ), who, args[1], function () main.print_info ( who, 'OOB link accepted' ) end, @@ -30,30 +35,47 @@ end, args[2] ) end, true ) -local oob_iq_handler = lm.message_handler.new ( oob.iq_handler ) +local oob_iq_handler = lm.message_handler.new ( oob.iq_handler ) local oob_message_handler = lm.message_handler.new ( oob.message_handler ) local oob_handler_registered = false -hooks_d['hook-post-connect'].oob = +local oob_pc_handler = function ( args ) - local conn = lm.connection.bless ( main.connection () ) - conn:handler ( oob_iq_handler, 'iq', 'normal' ) - conn:handler ( oob_message_handler, 'message', 'normal' ) - conn:handler ( oob_message_handler, 'presence', 'normal' ) - oob_handler_registered = true - hooks_d['hook-post-connect'].oob = nil - hooks_d['hook-quit'].oob = - function ( args ) - if oob_handler_registered then - local conn = lm.connection.bless ( main.connection () ) - conn:handler ( oob_iq_handler, 'iq' ) - conn:handler ( oob_message_handler, 'message' ) - conn:handler ( oob_message_handler, 'presence' ) - end + local connection = main.connection () + if connection then + local conn = lm.connection.bless ( connection ) + conn:handler ( oob_iq_handler, 'iq', 'normal' ) + conn:handler ( oob_message_handler, 'message', 'normal' ) + conn:handler ( oob_message_handler, 'presence', 'normal' ) + oob_handler_registered = true + end + end +main.hook ( 'hook-post-connect', oob_pc_handler ) +main.hook ( 'hook-pre-disconnect', + function ( args ) + if oob_handler_registered then + local connection = main.connection () + if connection then + local conn = lm.connection.bless ( connection ) + conn:handler ( oob_iq_handler, 'iq' ) + conn:handler ( oob_message_handler, 'message' ) + conn:handler ( oob_message_handler, 'presence' ) end + oob_handler_registered = false + end end -main.add_feature ( 'jabber:iq:oob' ) -main.add_feature ( 'jabber:x:oob' ) --- vim: se ts=4: -- +main.hook ( 'hook-lua-start', + function ( args ) + main.add_feature ( 'jabber:iq:oob' ) + main.add_feature ( 'jabber:x:oob' ) + oob_pc_handler () + end ) +main.hook ( 'hook-lua-quit', + function ( args ) + main.del_feature ( 'jabber:iq:oob' ) + main.del_feature ( 'jabber:x:oob' ) + end ) + +-- vim: se ts=4 sw=4: -- diff -r 59aeae623ac6 -r ed4676536ed9 examples/privacy.lua --- a/examples/privacy.lua Wed Mar 31 00:28:04 2010 +0300 +++ b/examples/privacy.lua Wed Mar 31 01:25:19 2010 +0300 @@ -9,8 +9,13 @@ main.command ( 'blocklist', function ( args ) + local connection = main.connection () + if not connection then + print "You are not online!" + return + end local action = args[1] - local conn = lm.connection.bless ( main.connection () ) + local conn = lm.connection.bless ( connection ) local who = main.current_buddy () privacy.list ( conn, 'mcabber', function ( list ) @@ -66,33 +71,31 @@ local privacy_handler = lm.message_handler.new ( privacy.iq_handler ) local privacy_handler_registered = false -hooks_d['hook-post-connect'].privacy = +privacy_pc_handler = function ( args ) - privacy.active ( lm.connection.bless ( main.connection () ), 'mcabber', - function () - print ( 'Server blocklist activated' ) - end, - function () - -- list may be absent, so, we will not pollute log with errors - end ) - lm.connection.bless( main.connection () ):handler ( privacy_handler, 'iq', 'normal' ) - privacy_handler_registered = true - hooks_d['hook-post-connect'].privacy = - function ( args ) - privacy.active ( lm.connection.bless ( main.connection () ), 'mcabber', - function () - print ( 'Server blocklist activated' ) - end, - function () - -- list may be absent, so, we will not pollute log with errors - end ) + local connection = main.connection () + if connection then + privacy.active ( lm.connection.bless ( connection ), 'mcabber', + function () + print ( 'Server blocklist activated' ) + end, + function () + -- list may be absent, so, we will not pollute log with errors + end ) + lm.connection.bless( connection ):handler ( privacy_handler, 'iq', 'normal' ) + privacy_handler_registered = true + end + end +main.hook ( 'hook-post-connect', privacy_pc_handler ) +main.hook ( 'hook-pre-disconnect', + function ( args ) + if privacy_handler_registered then + local connection = main.connection () + if connection then + lm.connection.bless( connection ):handler ( privacy_handler, 'iq' ) end - hooks_d['hook-quit'].privacy = - function ( args ) - if privacy_handler_registered then - lm.connection.bless( main.connection () ):handler ( privacy_handler, 'iq' ) - end - end + privacy_handler_registered = false + end end --- vim: se ts=4: -- +-- vim: se ts=4 sw=4: -- diff -r 59aeae623ac6 -r ed4676536ed9 examples/register.lua --- a/examples/register.lua Wed Mar 31 00:28:04 2010 +0300 +++ b/examples/register.lua Wed Mar 31 01:25:19 2010 +0300 @@ -4,13 +4,18 @@ main.command ( 'register', function ( args ) + local connection = main.connection () + if not connection then + print "You are not online!" + return + end local who if args and args ~= '' then who = args else who = main.full_jid () end - iq_register.register ( lm.connection.bless ( main.connection () ), who, + iq_register.register ( lm.connection.bless ( connection ), who, function ( form, submit, reject ) insert_form ( form, function ( form ) @@ -38,13 +43,18 @@ end, false, 'jid' ) main.command ( 'cancel', function ( args ) + local connection = main.connection () + if not connection then + print "You are not online!" + return + end local who if args and args ~= '' then who = args else who = main.full_jid () end - iq_register.unregister ( lm.connection.bless ( main.connection () ), who, + iq_register.unregister ( lm.connection.bless ( connection ), who, function ( form, submit, reject ) if not form then main.print_info ( who, 'Successfully unregistered' ) @@ -78,4 +88,4 @@ 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." --- vim: se ts=4: -- +-- vim: se ts=4 sw=4: -- diff -r 59aeae623ac6 -r ed4676536ed9 examples/remote.lua --- a/examples/remote.lua Wed Mar 31 00:28:04 2010 +0300 +++ b/examples/remote.lua Wed Mar 31 01:25:19 2010 +0300 @@ -4,6 +4,11 @@ main.command ( 'remote', function ( args ) + local connection = main.connection () + if not connection then + print "You are not online!" + return + end local who if args.t then who = args.t @@ -11,7 +16,7 @@ who = main.full_jid () end local action = args[1] - local conn = lm.connection.bless ( main.connection () ) + local conn = lm.connection.bless ( connection ) if action then remote.command ( conn, who, action, function ( form, submit, reject ) @@ -63,4 +68,4 @@ commands_help['remote'] = "[-t target_jid] [remote_command]\n\nPrints list of available remote command or requests execution of specified command." --- vim: se ts=4: -- +-- vim: se ts=4 sw=4: -- diff -r 59aeae623ac6 -r ed4676536ed9 examples/room_priv.lua --- a/examples/room_priv.lua Wed Mar 31 00:28:04 2010 +0300 +++ b/examples/room_priv.lua Wed Mar 31 01:25:19 2010 +0300 @@ -41,11 +41,11 @@ end end -hooks_d['hook-quit'].room_priv = +main.hook ( 'hook-lua-quit', function ( args ) for key, binding in pairs ( room_priv_restore ) do main.binding ( key, binding ) end - end + end ) -- vim: se ts=4: -- diff -r 59aeae623ac6 -r ed4676536ed9 examples/transports.lua --- a/examples/transports.lua Wed Mar 31 00:28:04 2010 +0300 +++ b/examples/transports.lua Wed Mar 31 01:25:19 2010 +0300 @@ -17,7 +17,7 @@ return false end -hooks_d['hook-status-change'].transports = +local transports_pc_handler = function ( args ) for k, jid in pairs ( transport_jids ) do if args.jid == jid then @@ -32,7 +32,7 @@ end end -hooks_d['hook-start'].transports = +main.hook ( 'hook-lua-start', function ( args ) for k, jid in pairs ( transport_jids ) do if not online ( jid ) then @@ -43,17 +43,17 @@ main.run ( ("color roster dn_? *@%s brightblack"):format ( jid ) ) end end - end + end ) +main.hook ( 'hook-status-change', transports_pc_handler ) +main.hook ( 'hook-post-connect', transports_pc_handler ) -hooks_d['hook-post-connect'].transports = hooks_d['hook-start'].transports - -hooks_d['hook-pre-disconnect'].transports = +main.hook ( 'hook-pre-disconnect', 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 + end ) --- vim: se ts=4: -- +-- vim: se ts=4 sw=4: -- diff -r 59aeae623ac6 -r ed4676536ed9 examples/vcard.lua --- a/examples/vcard.lua Wed Mar 31 00:28:04 2010 +0300 +++ b/examples/vcard.lua Wed Mar 31 01:25:19 2010 +0300 @@ -6,9 +6,14 @@ main.command ( 'vcard-temp', function ( args ) + local connection = main.connection () + if not connection then + print "You are not online!" + return + end local who local action = args[1] - local conn = lm.connection.bless ( main.connection () ) + local conn = lm.connection.bless ( connection ) if not action then who = conn:jid():gsub ( '/.*', '' ) elseif action == 'photo' then @@ -93,4 +98,4 @@ commands_help['vcard-temp'] = '[photo filename | . | jid]\n\nObtains vcard (as a form, but you may not submit it). Without arguments obtains your own vcard. "." means current buddy.\nYou can also publish your photo from file, that will automatically get your vcard, put a data into it and submit to server.' --- vim: se ts=4: -- +-- vim: se ts=4 sw=4: --