examples/evil.lua
changeset 62 fb81aeb45e00
child 63 423555c07763
equal deleted inserted replaced
61:5182f466da7d 62:fb81aeb45e00
       
     1 
       
     2 -- MALICIOUS STANZAS (XEP-0076)
       
     3 
       
     4 -- FIXME for now we only can send evil messages
       
     5 --       also we cannot detect stream-level evil :(
       
     6 
       
     7 -- library
       
     8 
       
     9 require 'lm'
       
    10 require 'iq'
       
    11 
       
    12 -- public
       
    13 
       
    14 evil = {
       
    15 	handler =
       
    16 		function ( mess )
       
    17 			return false
       
    18 		end,
       
    19 }
       
    20 
       
    21 function evil.message ( conn, to, mtype, message )
       
    22 	conn:send ( lm.message.create { mtype = 'message-' .. mtype, to = to,
       
    23 			body = { message },
       
    24 			evil = { xmlns = 'http://jabber.org/protocol/evil' },
       
    25 		} )
       
    26 end
       
    27 
       
    28 function evil.presence ( conn, to, status, message )
       
    29 	local mtype = 'presence-available'
       
    30 	if status == 'unavailable' then
       
    31 		mtype = 'presence-unavailable'
       
    32 		status = ''
       
    33 	end
       
    34 	conn:send ( lm.message.create { mtype = mtype, from = conn:jid (), to = to,
       
    35 			show   = { status },
       
    36 			status = { message },
       
    37 			evil   = { xmlns = 'http://jabber.org/protocol/evil' },
       
    38 		} )
       
    39 end
       
    40 
       
    41 -- private
       
    42 
       
    43 local evil_incoming_stanza_handler = lm.message_handler.new (
       
    44 	function ( conn, mess )
       
    45 		local e = mess:child ( 'evil' )
       
    46 		if e and e:attribute ( 'xmlns' ) == 'http://jabber.org/protocol/evil' then
       
    47 			return evil.handler ( mess )
       
    48 		end
       
    49 	end )
       
    50 
       
    51 -- mcabber
       
    52 
       
    53 evil.handler =
       
    54 	function ( mess )
       
    55 		local mtype, smtype = mess:type ()
       
    56 		main.print_info ( mess:attribute ( 'from' ), 'Evil stanza of type ' .. mtype .. ', ' .. ( smtype or '' ) .. ' detected!' )
       
    57 		return main.yesno ( main.option ( 'lua_filter_evil' ) )
       
    58 	end
       
    59 
       
    60 local stat2xmpp = {
       
    61 	free     = 'chat',
       
    62 	online   = '',
       
    63 	away     = 'away',
       
    64 	dnd      = 'dnd',
       
    65 	notavail = 'xa',
       
    66 	offline  = 'unavailable',
       
    67 }
       
    68 
       
    69 
       
    70 -- TODO improve interface, check if we sending right thing for offline
       
    71 main.command ( 'evil',
       
    72 	function ( args )
       
    73 		local conn = lm.connection.bless ( main.connection () )
       
    74 		if args[1] == 'status' then
       
    75 			local text = ''
       
    76 			for i, mesg in ipairs ( args ) do
       
    77 				if i > 2 then
       
    78 					text = text .. ' ' .. mesg
       
    79 				end
       
    80 			end
       
    81 			local st = stat2xmpp[args[2]]
       
    82 			if not st then
       
    83 				st = ''
       
    84 			end
       
    85 			evil.presence ( conn, args.t, st, text:sub ( 2 ) )
       
    86 		else
       
    87 			local text = ''
       
    88 			if args[1] == 'message' then
       
    89 				for i, mesg in ipairs ( args ) do
       
    90 					if i > 1 then
       
    91 						text = text .. ' ' .. mesg
       
    92 					end
       
    93 				end
       
    94 			else
       
    95 				for i, mesg in ipairs ( args ) do
       
    96 					text = text .. ' ' .. mesg
       
    97 				end
       
    98 			end
       
    99 			local mtype = 'chat'
       
   100 			if args.k then
       
   101 				mtype = args.k
       
   102 			end
       
   103 			local who
       
   104 			if args.t then
       
   105 				who = args.t
       
   106 			else
       
   107 				who = main.current_buddy ()
       
   108 			end
       
   109 			evil.message ( conn, who, mtype, text:sub ( 2 ) )
       
   110 		end
       
   111 	end, true )
       
   112 
       
   113 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."
       
   114 
       
   115 local evil_handler_registered = false
       
   116 
       
   117 hooks_d['hook-post-connect'].evil =
       
   118 	function ( args )
       
   119 		lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'iq', 'normal' )
       
   120 		lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'message', 'normal' )
       
   121 		lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'presence', 'normal' )
       
   122 		evil_handler_registered = true
       
   123 		hooks_d['hook-post-connect'].evil = nil
       
   124 		hooks_d['hook-quit'].evil =
       
   125 			function ( args )
       
   126 				if evil_handler_registered then
       
   127 					lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'iq' )
       
   128 					lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'message' )
       
   129 					lm.connection.bless( main.connection () ):handler ( evil_incoming_stanza_handler, 'presence' )
       
   130 				end
       
   131 			end
       
   132 	end
       
   133 
       
   134 main.add_feature ( 'http://jabber.org/protocol/evil' )
       
   135 
       
   136 -- vim: se ts=4: --