plugins/mod_iq.lua

1
-- Prosody IM
2
-- Copyright (C) 2008-2010 Matthew Wild
3
-- Copyright (C) 2008-2010 Waqas Hussain
4
-- 
5
-- This project is MIT/X11 licensed. Please see the
6
-- COPYING file in the source package for more information.
7
--
8
 
9
 
10
local st = require "util.stanza";
11
local jid_split = require "util.jid".split;
12
 
13
local full_sessions = full_sessions;
14
local bare_sessions = bare_sessions;
15
 
16
if module:get_host_type() == "local" then
17
	module:hook("iq/full", function(data)
18
		-- IQ to full JID recieved
19
		local origin, stanza = data.origin, data.stanza;
20
 
21
		local session = full_sessions[stanza.attr.to];
22
		if session then
23
			-- TODO fire post processing event
24
			session.send(stanza);
25
		else -- resource not online
26
			if stanza.attr.type == "get" or stanza.attr.type == "set" then
27
				origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
28
			end
29
		end
30
		return true;
31
	end);
32
end
33
 
34
module:hook("iq/bare", function(data)
35
	-- IQ to bare JID recieved
36
	local origin, stanza = data.origin, data.stanza;
37
	local type = stanza.attr.type;
38
 
39
	-- TODO fire post processing events
40
	if type == "get" or type == "set" then
41
		local child = stanza.tags[1];
42
		local ret = module:fire_event("iq/bare/"..child.attr.xmlns..":"..child.name, data);
43
		if ret ~= nil then return ret; end
44
		return module:fire_event("iq-"..type.."/bare/"..child.attr.xmlns..":"..child.name, data);
45
	else
46
		return module:fire_event("iq-"..type.."/bare/"..stanza.attr.id, data);
47
	end
48
end);
49
 
50
module:hook("iq/self", function(data)
51
	-- IQ to self JID recieved
52
	local origin, stanza = data.origin, data.stanza;
53
	local type = stanza.attr.type;
54
 
55
	if type == "get" or type == "set" then
56
		local child = stanza.tags[1];
57
		local ret = module:fire_event("iq/self/"..child.attr.xmlns..":"..child.name, data);
58
		if ret ~= nil then return ret; end
59
		return module:fire_event("iq-"..type.."/self/"..child.attr.xmlns..":"..child.name, data);
60
	else
61
		return module:fire_event("iq-"..type.."/self/"..stanza.attr.id, data);
62
	end
63
end);
64
 
65
module:hook("iq/host", function(data)
66
	-- IQ to a local host recieved
67
	local origin, stanza = data.origin, data.stanza;
68
	local type = stanza.attr.type;
69
 
70
	if type == "get" or type == "set" then
71
		local child = stanza.tags[1];
72
		local ret = module:fire_event("iq/host/"..child.attr.xmlns..":"..child.name, data);
73
		if ret ~= nil then return ret; end
74
		return module:fire_event("iq-"..type.."/host/"..child.attr.xmlns..":"..child.name, data);
75
	else
76
		return module:fire_event("iq-"..type.."/host/"..stanza.attr.id, data);
77
	end
78
end);