examples/evil.lua
changeset 68 742878c74b8e
parent 66 542f61e113cb
child 99 ed4676536ed9
equal deleted inserted replaced
67:d33ca5572e91 68:742878c74b8e
     1 
     1 
     2 -- MALICIOUS STANZAS (XEP-0076)
     2 local lm   = require 'lm'
       
     3 local evil = require 'lm.evil'
     3 
     4 
     4 -- library
     5 evil.handler (
       
     6 	function ( mess )
       
     7 		local evillevel = tonumber(main.option ( 'lua_evil_sensibility' ))
       
     8 		local mtype, smtype = mess:type ()
       
     9 		if evillevel > 1 then
       
    10 			main.print_info ( mess:attribute ( 'from' ), 'Evil stanza of type ' .. mtype .. '.' .. smtype .. ' detected!' )
       
    11 		elseif evillevel > 0 then
       
    12 			print ( 'Tainted by evil stanza of type ' .. mtype .. '.' .. smtype .. ' from ' .. ( mess:attribute ( 'from' ) or '... unknown in black' ) )
       
    13 		end
       
    14 		return main.yesno ( main.option ( 'lua_filter_evil' ) )
       
    15 	end )
     5 
    16 
     6 local lm = require 'lm'
    17 local stat2xmpp = {
     7 local iq = require 'iq'
    18 	free     = 'chat',
     8 
    19 	online   = '',
     9 -- public
    20 	away     = 'away',
    10 
    21 	dnd      = 'dnd',
    11 local O = {
    22 	notavail = 'xa',
    12 	handler =
    23 	offline  = 'unavailable',
    13 		function ( mess )
       
    14 			return false
       
    15 		end,
       
    16 }
    24 }
    17 
    25 
    18 local F = { }
    26 -- TODO improve interface, check if we sending right thing for offline
       
    27 main.command ( 'evil',
       
    28 	function ( args )
       
    29 		local conn = lm.connection.bless ( main.connection () )
       
    30 		if args[1] == 'status' then
       
    31 			local text = ''
       
    32 			for i, mesg in ipairs ( args ) do
       
    33 				if i > 2 then
       
    34 					text = text .. ' ' .. mesg
       
    35 				end
       
    36 			end
       
    37 			local st = stat2xmpp[args[2]]
       
    38 			if not st then
       
    39 				st = ''
       
    40 			end
       
    41 			evil.presence ( conn, args.t, st, text:sub ( 2 ) )
       
    42 		else
       
    43 			local text = ''
       
    44 			if args[1] == 'message' then
       
    45 				for i, mesg in ipairs ( args ) do
       
    46 					if i > 1 then
       
    47 						text = text .. ' ' .. mesg
       
    48 					end
       
    49 				end
       
    50 			else
       
    51 				for i, mesg in ipairs ( args ) do
       
    52 					text = text .. ' ' .. mesg
       
    53 				end
       
    54 			end
       
    55 			local mtype = 'chat'
       
    56 			if args.k then
       
    57 				mtype = args.k
       
    58 			end
       
    59 			local who
       
    60 			if args.t then
       
    61 				who = args.t
       
    62 			else
       
    63 				who = main.current_buddy ()
       
    64 			end
       
    65 			evil.message ( conn, who, mtype, text:sub ( 2 ) )
       
    66 		end
       
    67 	end, true )
    19 
    68 
    20 function F.handler ( handler )
    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."
    21 	O.handler = handler
       
    22 end
       
    23 
    70 
    24 function F.message ( conn, to, mtype, message )
    71 local evil_handler = lm.message_handler.new ( evil.stanza_handler )
    25 	conn:send ( lm.message.create { mtype = 'message-' .. mtype, to = to,
    72 local evil_handler_registered = false
    26 			body = { message },
       
    27 			evil = { xmlns = 'http://jabber.org/protocol/evil' },
       
    28 		} )
       
    29 end
       
    30 
    73 
    31 function F.presence ( conn, to, status, message )
    74 hooks_d['hook-post-connect'].evil =
    32 	local mtype = 'presence-available'
    75 	function ( args )
    33 	if status == 'unavailable' then
    76 		lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq',       'first' )
    34 		mtype = 'presence-unavailable'
    77 		lm.connection.bless( main.connection () ):handler ( evil_handler, 'message',  'first' )
    35 		status = ''
    78 		lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence', 'first' )
       
    79 		evil_handler_registered = true
       
    80 		hooks_d['hook-post-connect'].evil = nil
       
    81 		hooks_d['hook-quit'].evil =
       
    82 			function ( args )
       
    83 				if evil_handler_registered then
       
    84 					lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq'       )
       
    85 					lm.connection.bless( main.connection () ):handler ( evil_handler, 'message'  )
       
    86 					lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence' )
       
    87 				end
       
    88 			end
    36 	end
    89 	end
    37 	conn:send ( lm.message.create { mtype = mtype, from = conn:jid (), to = to,
       
    38 			show   = { status },
       
    39 			status = { message },
       
    40 			evil   = { xmlns = 'http://jabber.org/protocol/evil' },
       
    41 		} )
       
    42 end
       
    43 
    90 
    44 function F.iq ( conn, to, action, contents, success, fail )
    91 local char2xmpp = {
    45 	contents.evil = { xmlns = 'http://jabber.org/protocol/evil' }
    92 	f = 'chat',
    46 	iq.send ( conn, to, action, contents, success, fail )
    93 	o = '',
    47 end
    94 	a = 'away',
       
    95 	d = 'dnd',
       
    96 	n = 'xa',
       
    97 	_ = 'unavailable',
       
    98 }
    48 
    99 
    49 function F.stanza_handler ( conn, mess )
   100 -- hack, but working ;)
    50 	local e = mess:child ( 'evil' )
   101 hooks_d['hook-my-status-change'].evil =
    51 	if e and e:attribute ( 'xmlns' ) == 'http://jabber.org/protocol/evil' then
   102 	function ( args )
    52 		return O.handler ( mess )
   103 		if main.yesno ( main.option ( 'lua_evil_mode' ) ) then
       
   104 			evil.presence ( lm.connection.bless ( main.connection () ), nil, char2xmpp[args.new_status], args.message )
       
   105 		end
    53 	end
   106 	end
    54 end
       
    55 
   107 
    56 return F
   108 main.add_feature ( 'http://jabber.org/protocol/evil' )
    57 
   109 
    58 -- vim: se ts=4: --
   110 -- vim: se ts=4: --