examples/evil.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 28 Nov 2012 20:17:53 +0200
changeset 146 04d19c9c1196
parent 121 75a7d595817c
permissions -rw-r--r--
Fix module loading problem


local lm   = require 'lm'
local evil = require 'lm.evil'

evil.handler (
	function ( mess )
		local evillevel = tonumber(main.option ( 'lua_evil_sensibility' ))
		local mtype, smtype = mess:type ()
		if evillevel > 1 then
			main.print_info ( mess:attribute ( 'from' ), 'Evil stanza of type ' .. mtype .. '.' .. smtype .. ' detected!' )
		elseif evillevel > 0 then
			print ( 'Tainted by evil stanza of type ' .. mtype .. '.' .. smtype .. ' from ' .. ( mess:attribute ( 'from' ) or '... unknown in black' ) )
		end
		return main.yesno ( main.option ( 'lua_filter_evil' ) )
	end )

local stat2xmpp = {
	free     = 'chat',
	online   = '',
	away     = 'away',
	dnd      = 'dnd',
	notavail = 'xa',
	offline  = 'unavailable',
}

-- TODO improve interface, check if we sending right thing for offline
main.command ( 'evil',
	function ( args )
		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
				if i > 2 then
					text = text .. ' ' .. mesg
				end
			end
			local st = stat2xmpp[args[2]]
			if not st then
				st = ''
			end
			evil.presence ( conn, args.t, st, text:sub ( 2 ) )
		else
			local text = ''
			if args[1] == 'message' then
				for i, mesg in ipairs ( args ) do
					if i > 1 then
						text = text .. ' ' .. mesg
					end
				end
			else
				for i, mesg in ipairs ( args ) do
					text = text .. ' ' .. mesg
				end
			end
			local mtype = 'chat'
			if args.k then
				mtype = args.k
			end
			local who
			if args.t then
				who = args.t
			else
				who = main.current_buddy ()
			end
			evil.message ( conn, who, mtype, text:sub ( 2 ) )
		end
	end, true )

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."

local evil_handler = lm.message_handler.new ( evil.stanza_handler )
local evil_handler_registered = false

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
local evil_pd_handler = 
	function ( args )
		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-post-connect',   evil_pc_handler )
main.hook ( 'hook-pre-disconnect', evil_pd_handler )

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' )
		evil_pd_handler ()
	end )

local char2xmpp = {
	f = 'chat',
	o = '',
	a = 'away',
	d = 'dnd',
	n = 'xa',
	_ = 'unavailable',
}

-- hack, but working ;)
main.hook ( 'hook-my-status-change',
	function ( args )
		if main.yesno ( main.option ( 'lua_evil_mode' ) ) then
			local connection = main.connection ()
			if connection then
				evil.presence ( lm.connection.bless ( connection ), nil, char2xmpp[args.new_status], args.message )
			end
		end
	end )

-- vim: se ts=4 sw=4: --