examples/evil.lua
changeset 63 423555c07763
parent 62 fb81aeb45e00
child 64 bf7521ed96eb
equal deleted inserted replaced
62:fb81aeb45e00 63:423555c07763
    50 
    50 
    51 -- mcabber
    51 -- mcabber
    52 
    52 
    53 evil.handler =
    53 evil.handler =
    54 	function ( mess )
    54 	function ( mess )
       
    55 		local evillevel = tonumber(main.option ( 'lua_evil_sensibility' ))
    55 		local mtype, smtype = mess:type ()
    56 		local mtype, smtype = mess:type ()
    56 		main.print_info ( mess:attribute ( 'from' ), 'Evil stanza of type ' .. mtype .. ', ' .. ( smtype or '' ) .. ' detected!' )
    57 		if evillevel > 1 then
       
    58 			main.print_info ( mess:attribute ( 'from' ), 'Evil stanza of type "' .. mtype .. ' ' .. smtype .. '" detected!' )
       
    59 		elseif evillevel > 0 then
       
    60 			print ( 'Tainted by evil stanza of type "' .. mtype .. ' ' .. smtype .. '" from ' .. ( mess:attribute ( 'from' ) or '... unknown in black' ) )
       
    61 		end
    57 		return main.yesno ( main.option ( 'lua_filter_evil' ) )
    62 		return main.yesno ( main.option ( 'lua_filter_evil' ) )
    58 	end
    63 	end
    59 
    64 
    60 local stat2xmpp = {
    65 local stat2xmpp = {
    61 	free     = 'chat',
    66 	free     = 'chat',
    63 	away     = 'away',
    68 	away     = 'away',
    64 	dnd      = 'dnd',
    69 	dnd      = 'dnd',
    65 	notavail = 'xa',
    70 	notavail = 'xa',
    66 	offline  = 'unavailable',
    71 	offline  = 'unavailable',
    67 }
    72 }
    68 
       
    69 
    73 
    70 -- TODO improve interface, check if we sending right thing for offline
    74 -- TODO improve interface, check if we sending right thing for offline
    71 main.command ( 'evil',
    75 main.command ( 'evil',
    72 	function ( args )
    76 	function ( args )
    73 		local conn = lm.connection.bless ( main.connection () )
    77 		local conn = lm.connection.bless ( main.connection () )
   114 
   118 
   115 local evil_handler_registered = false
   119 local evil_handler_registered = false
   116 
   120 
   117 hooks_d['hook-post-connect'].evil =
   121 hooks_d['hook-post-connect'].evil =
   118 	function ( args )
   122 	function ( args )
   119 		lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'iq', 'normal' )
   123 		lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'iq',       'first' )
   120 		lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'message', 'normal' )
   124 		lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'message',  'first' )
   121 		lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'presence', 'normal' )
   125 		lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'presence', 'first' )
   122 		evil_handler_registered = true
   126 		evil_handler_registered = true
   123 		hooks_d['hook-post-connect'].evil = nil
   127 		hooks_d['hook-post-connect'].evil = nil
   124 		hooks_d['hook-quit'].evil =
   128 		hooks_d['hook-quit'].evil =
   125 			function ( args )
   129 			function ( args )
   126 				if evil_handler_registered then
   130 				if evil_handler_registered then
   127 					lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'iq' )
   131 					lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'iq'       )
   128 					lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'message' )
   132 					lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'message'  )
   129 					lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'presence' )
   133 					lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'presence' )
   130 				end
   134 				end
   131 			end
   135 			end
   132 	end
   136 	end
   133 
   137 
       
   138 local char2xmpp = {
       
   139 	f = 'chat',
       
   140 	o = '',
       
   141 	a = 'away',
       
   142 	d = 'dnd',
       
   143 	n = 'xa',
       
   144 	_ = 'unavailable',
       
   145 }
       
   146 
       
   147 -- hack, but working ;)
       
   148 hooks_d['hook-my-status-change'].evil =
       
   149 	function ( args )
       
   150 		if main.yesno ( main.option ( 'lua_evil_mode' ) ) then
       
   151 			evil.presence ( lm.connection.bless ( main.connection () ), nil, char2xmpp[args.new_status], args.message )
       
   152 		end
       
   153 	end
       
   154 
   134 main.add_feature ( 'http://jabber.org/protocol/evil' )
   155 main.add_feature ( 'http://jabber.org/protocol/evil' )
   135 
   156 
   136 -- vim: se ts=4: --
   157 -- vim: se ts=4: --