examples/attention.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Sun, 22 Mar 2009 04:54:29 +0200
changeset 50 12d8dd774fcc
child 66 542f61e113cb
permissions -rw-r--r--
Attention
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     1
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     2
-- ATTENTION (XEP-0224)
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     3
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     4
-- library
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     5
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     6
require 'lm'
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     7
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     8
-- public
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     9
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    10
attention = {
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    11
	handler =
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    12
		function ( mesg )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    13
		end,
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    14
}
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    15
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    16
function attention.send ( conn, to, message )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    17
	local body = nil
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    18
	if message then
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    19
		body = { message }
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    20
	end
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    21
	conn:send (
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
		lm.message.create { mtype = 'message-headline', to = to,
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    23
			attention = { xmlns = 'urn:xmpp:attention:0' },
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    24
			body = body,
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    25
		} )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    26
end
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    27
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
-- private
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    29
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    30
local attention_incoming_message_handler = lm.message_handler.new (
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    31
	function ( conn, mess )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    32
		local a = mess:child ( 'attention' )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    33
		if a and a:attribute ( 'xmlns' ) == 'urn:xmpp:attention:0' then
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    34
			local body = mess:child ( 'body' )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    35
			if body then
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    36
				body = body:value ()
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    37
			end
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    38
			attention.handler ( body )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    39
		end
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
		return false
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    41
	end )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    42
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    43
-- mcabber
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    44
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    45
attention.handler =
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    46
	function ( mesg )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    47
		local times = 0
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    48
		main.timer ( 1,
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    49
			function ()
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    50
				if times < 6 then
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    51
					main.beep ()
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    52
					times = times + 1
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    53
					return true
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    54
				end
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    55
				return false
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    56
			end )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    57
	end
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    58
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    59
main.command ( 'attention',
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    60
	function ( args )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    61
		local who
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
		if args.t then
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
			who = args.t
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
		else
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    65
			who = main.full_jid ()
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
		end
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    67
		attention.send ( lm.connection.bless ( main.connection () ), who, args[1] )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    68
	end, true, 'jid' )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    69
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    70
commands_help['attention'] = "[-t to] [message]\n\nTries to get buddy's attention."
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    71
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    72
local attention_handler_registered = false
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    73
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
hooks_d['hook-post-connect'].attention =
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
	function ( args )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
		lm.connection.bless( main.connection () ):handler ( attention_incoming_message_handler, 'message', 'normal' )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
		attention_handler_registered = true
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
		hooks_d['hook-post-connect'].attention = nil
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    79
		hooks_d['hook-quit'].attention =
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
			function ( args )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    81
				if attention_handler_registered then
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    82
					lm.connection.bless( main.connection () ):handler ( attention_incoming_message_handler, 'message' )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    83
				end
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    84
			end
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    85
	end
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    86
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    87
main.add_feature ( 'urn:xmpp:attention:0' )
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    88
12d8dd774fcc Attention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    89
-- vim: se ts=4: --