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


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
local oob_pd_handler = 
	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-post-connect',   oob_pc_handler )
main.hook ( 'hook-pre-disconnect', oob_pd_handler )

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

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