Merge 0.10->trunk
[prosody.git] / 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
12 local full_sessions = prosody.full_sessions;
13
14 if module:get_host_type() == "local" then
15         module:hook("iq/full", function(data)
16                 -- IQ to full JID recieved
17                 local origin, stanza = data.origin, data.stanza;
18
19                 local session = full_sessions[stanza.attr.to];
20                 if not (session and session.send(stanza)) then
21                         if stanza.attr.type == "get" or stanza.attr.type == "set" then
22                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
23                         end
24                 end
25                 return true;
26         end);
27 end
28
29 module:hook("iq/bare", function(data)
30         -- IQ to bare JID recieved
31         local stanza = data.stanza;
32         local type = stanza.attr.type;
33
34         -- TODO fire post processing events
35         if type == "get" or type == "set" then
36                 local child = stanza.tags[1];
37                 local xmlns = child.attr.xmlns or "jabber:client";
38                 local ret = module:fire_event("iq/bare/"..xmlns..":"..child.name, data);
39                 if ret ~= nil then return ret; end
40                 return module:fire_event("iq-"..type.."/bare/"..xmlns..":"..child.name, data);
41         else
42                 return module:fire_event("iq-"..type.."/bare/"..stanza.attr.id, data);
43         end
44 end);
45
46 module:hook("iq/self", function(data)
47         -- IQ to self JID recieved
48         local stanza = data.stanza;
49         local type = stanza.attr.type;
50
51         if type == "get" or type == "set" then
52                 local child = stanza.tags[1];
53                 local xmlns = child.attr.xmlns or "jabber:client";
54                 local ret = module:fire_event("iq/self/"..xmlns..":"..child.name, data);
55                 if ret ~= nil then return ret; end
56                 return module:fire_event("iq-"..type.."/self/"..xmlns..":"..child.name, data);
57         else
58                 return module:fire_event("iq-"..type.."/self/"..stanza.attr.id, data);
59         end
60 end);
61
62 module:hook("iq/host", function(data)
63         -- IQ to a local host recieved
64         local stanza = data.stanza;
65         local type = stanza.attr.type;
66
67         if type == "get" or type == "set" then
68                 local child = stanza.tags[1];
69                 local xmlns = child.attr.xmlns or "jabber:client";
70                 local ret = module:fire_event("iq/host/"..xmlns..":"..child.name, data);
71                 if ret ~= nil then return ret; end
72                 return module:fire_event("iq-"..type.."/host/"..xmlns..":"..child.name, data);
73         else
74                 return module:fire_event("iq-"..type.."/host/"..stanza.attr.id, data);
75         end
76 end);