examples/evil.lua
changeset 99 ed4676536ed9
parent 68 742878c74b8e
child 111 5bcdb71ef2f2
equal deleted inserted replaced
98:59aeae623ac6 99:ed4676536ed9
    24 }
    24 }
    25 
    25 
    26 -- TODO improve interface, check if we sending right thing for offline
    26 -- TODO improve interface, check if we sending right thing for offline
    27 main.command ( 'evil',
    27 main.command ( 'evil',
    28 	function ( args )
    28 	function ( args )
    29 		local conn = lm.connection.bless ( main.connection () )
    29 		local connection = main.connection ()
       
    30 		if not connection then
       
    31 			print "You are not online!"
       
    32 			return
       
    33 		end
       
    34 		local conn = lm.connection.bless ( connection )
    30 		if args[1] == 'status' then
    35 		if args[1] == 'status' then
    31 			local text = ''
    36 			local text = ''
    32 			for i, mesg in ipairs ( args ) do
    37 			for i, mesg in ipairs ( args ) do
    33 				if i > 2 then
    38 				if i > 2 then
    34 					text = text .. ' ' .. mesg
    39 					text = text .. ' ' .. mesg
    69 commands_help['evil'] = "[-t jid] [status stat [message] | [-k message_type] [message] message]\n\nSends evil message or presence.\nmessage_type may be chat, normal, headline.\nNote, that for now it will not change mcabber's status."
    74 commands_help['evil'] = "[-t jid] [status stat [message] | [-k message_type] [message] message]\n\nSends evil message or presence.\nmessage_type may be chat, normal, headline.\nNote, that for now it will not change mcabber's status."
    70 
    75 
    71 local evil_handler = lm.message_handler.new ( evil.stanza_handler )
    76 local evil_handler = lm.message_handler.new ( evil.stanza_handler )
    72 local evil_handler_registered = false
    77 local evil_handler_registered = false
    73 
    78 
    74 hooks_d['hook-post-connect'].evil =
    79 local evil_pc_handler =
    75 	function ( args )
    80 	function ( args )
    76 		lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq',       'first' )
    81 		local connection = main.connection ()
    77 		lm.connection.bless( main.connection () ):handler ( evil_handler, 'message',  'first' )
    82 		if connection then
    78 		lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence', 'first' )
    83 			lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq',       'first' )
    79 		evil_handler_registered = true
    84 			lm.connection.bless( main.connection () ):handler ( evil_handler, 'message',  'first' )
    80 		hooks_d['hook-post-connect'].evil = nil
    85 			lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence', 'first' )
    81 		hooks_d['hook-quit'].evil =
    86 			evil_handler_registered = true
    82 			function ( args )
    87 		end
    83 				if evil_handler_registered then
    88 	end
    84 					lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq'       )
    89 main.hook ( 'hook-post-connect', evil_pc_handler )
    85 					lm.connection.bless( main.connection () ):handler ( evil_handler, 'message'  )
    90 main.hook ( 'hook-pre-disconnect',
    86 					lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence' )
    91 	function ( args )
    87 				end
    92 		if evil_handler_registered then
       
    93 			local connection = main.connection ()
       
    94 			if connection then
       
    95 				lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq'       )
       
    96 				lm.connection.bless( main.connection () ):handler ( evil_handler, 'message'  )
       
    97 				lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence' )
    88 			end
    98 			end
       
    99 			evil_handler_registered = false
       
   100 		end
       
   101 	end )
       
   102 
       
   103 main.hook ( 'hook-lua-start',
       
   104 	function ( args )
       
   105 		main.add_feature ( 'http://jabber.org/protocol/evil' )
       
   106 		evil_pc_handler ()
    89 	end
   107 	end
       
   108 main.hook ( 'hook-lua-quit',
       
   109 	function ( args )
       
   110 		main.del_feature ( 'http://jabber.org/protocol/evil' )
       
   111 	end )
    90 
   112 
    91 local char2xmpp = {
   113 local char2xmpp = {
    92 	f = 'chat',
   114 	f = 'chat',
    93 	o = '',
   115 	o = '',
    94 	a = 'away',
   116 	a = 'away',
    96 	n = 'xa',
   118 	n = 'xa',
    97 	_ = 'unavailable',
   119 	_ = 'unavailable',
    98 }
   120 }
    99 
   121 
   100 -- hack, but working ;)
   122 -- hack, but working ;)
   101 hooks_d['hook-my-status-change'].evil =
   123 main.hook ( 'hook-my-status-change',
   102 	function ( args )
   124 	function ( args )
   103 		if main.yesno ( main.option ( 'lua_evil_mode' ) ) then
   125 		if main.yesno ( main.option ( 'lua_evil_mode' ) ) then
   104 			evil.presence ( lm.connection.bless ( main.connection () ), nil, char2xmpp[args.new_status], args.message )
   126 			local connection = main.connection ()
       
   127 			if connection then
       
   128 				evil.presence ( lm.connection.bless ( connection ), nil, char2xmpp[args.new_status], args.message )
       
   129 			end
   105 		end
   130 		end
   106 	end
   131 	end )
   107 
   132 
   108 main.add_feature ( 'http://jabber.org/protocol/evil' )
   133 -- vim: se ts=4 sw=4: --
   109 
       
   110 -- vim: se ts=4: --