examples/oob.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Tue, 31 Mar 2009 18:35:34 +0300
changeset 68 742878c74b8e
parent 66 542f61e113cb
child 99 ed4676536ed9
permissions -rw-r--r--
Lm separation, privacy * Library parts moved to lm.* * mc_* renamed to plain names * Privacy lists suppart (not tested)


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 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 ( main.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

hooks_d['hook-post-connect'].oob =
	function ( args )
		local conn = lm.connection.bless ( main.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
		hooks_d['hook-post-connect'].oob = nil
		hooks_d['hook-quit'].oob =
			function ( args )
				if oob_handler_registered then
					local conn = lm.connection.bless ( main.connection () )
					conn:handler ( oob_iq_handler, 'iq' )
					conn:handler ( oob_message_handler, 'message' )
					conn:handler ( oob_message_handler, 'presence' )
				end
			end
	end

main.add_feature ( 'jabber:iq:oob' )
main.add_feature ( 'jabber:x:oob' )

-- vim: se ts=4: --