examples/attention.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 attention = require 'lm.attention'

attention.handler (
	function ( mesg, from )
		local times = 0
		if from then
			main.print_info ( from, "Buddy wants your attention!" )
		end
		main.timer ( 1,
			function ()
				if times < 6 then
					main.beep ()
					times = times + 1
					return true
				end
				return false
			end )
	end )

main.command ( 'attention',
	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
		attention.send ( lm.connection.bless ( connection ), who, args[1] )
	end, true, 'jid' )

local attention_handler = lm.message_handler.new ( attention.message_handler )
local attention_handler_registered = false

local attention_pc_handler =
	function ( args )
		local connection = main.connection ()
		if connection then
			lm.connection.bless(connection):handler ( attention_handler, 'message', 'normal' )
			attention_handler_registered = true
		end
	end
local attention_pd_handler = 
	function ( args )
		if attention_handler_registered then
			local connection = main.connection ()
			if connection then
				lm.connection.bless(connection):handler ( attention_handler, 'message' )
			end
			attention_handler_registered = false
		end
	end
main.hook ( 'hook-post-connect',   attention_pc_handler )
main.hook ( 'hook-pre-disconnect', attention_pd_handler )

-- register handler, if we are already connected
main.hook ( 'hook-lua-start',
	function ( args )
		main.add_feature ( 'urn:xmpp:attention:0' )
		attention_pc_handler ()
	end )
main.hook ('hook-lua-quit',
	function ( args )
		main.del_feature ( 'urn:xmpp:attention:0' )
		attention_pd_handler ()
	end )

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