examples/oob.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 31 Mar 2010 01:25:19 +0300
changeset 99 ed4676536ed9
parent 68 742878c74b8e
child 100 521c27baa387
permissions -rw-r--r--
Update most useful scripts * new hooks interface * connection state checking


local lm  = require 'lm'
local oob = require 'lm.oob'

oob.handler (
	function ( from, url, desc, success, fail )
		if desc then
			main.print_info ( from, 'Buddy wants you to download link: ' .. url .. ' (' .. desc .. ')' )
		else
			main.print_info ( from, 'Buddy wants you to download link: ' .. url )
		end
		success ()
	end )

main.command ( 'oob',
	function ( args )
		local connection = main.connection ()
		if not connection then
			print "You are not online!"
			return
		end
		local who
		if args.t then
			who = args.t
		else
			who = main.full_jid ()
		end
		-- here we can run something external to put file on server and obtain link to it
		oob.send ( lm.connection.bless ( connection ), who, args[1],
			function ()
				main.print_info ( who, 'OOB link accepted' )
			end,
			function ( mesg )
				main.print_info ( who, 'OOB link refused: ' .. mesg )
			end, args[2] )
	end, true )

local oob_iq_handler      = lm.message_handler.new ( oob.iq_handler )
local oob_message_handler = lm.message_handler.new ( oob.message_handler )
local oob_handler_registered = false

local oob_pc_handler =
	function ( args )
		local connection = main.connection ()
		if connection then
			local conn = lm.connection.bless ( connection )
			conn:handler ( oob_iq_handler, 'iq', 'normal' )
			conn:handler ( oob_message_handler, 'message', 'normal' )
			conn:handler ( oob_message_handler, 'presence', 'normal' )
			oob_handler_registered = true
		end
	end
main.hook ( 'hook-post-connect', oob_pc_handler )
main.hook ( 'hook-pre-disconnect',
	function ( args )
		if oob_handler_registered then
			local connection = main.connection ()
			if connection then
				local conn = lm.connection.bless ( connection )
				conn:handler ( oob_iq_handler, 'iq' )
				conn:handler ( oob_message_handler, 'message' )
				conn:handler ( oob_message_handler, 'presence' )
			end
			oob_handler_registered = false
		end
	end


main.hook ( 'hook-lua-start',
	function ( args )
		main.add_feature ( 'jabber:iq:oob' )
		main.add_feature ( 'jabber:x:oob' )
		oob_pc_handler ()
	end )
main.hook ( 'hook-lua-quit',
	function ( args )
		main.del_feature ( 'jabber:iq:oob' )
		main.del_feature ( 'jabber:x:oob' )
	end )

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