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


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' )

commands_help['attention'] = "[-t to] [message]\n\nTries to get buddy's attention."

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
main.hook ( 'hook-post-connect', attention_pc_handler )
main.hook ( 'hook-pre-disconnect',
	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 )

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

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