046bebd6cf0bc339fbcd8ab84338ab860023fd08
[prosody.git] / plugins / mod_iq.lua
1
2 local st = require "util.stanza";
3 local jid_split = require "util.jid".split;
4 local user_exists = require "core.usermanager".user_exists;
5
6 local full_sessions = full_sessions;
7 local bare_sessions = bare_sessions;
8
9 module:hook("iq/full", function(data)
10         -- IQ to full JID recieved
11         local origin, stanza = data.origin, data.stanza;
12
13         local session = full_sessions[stanza.attr.to];
14         if session then
15                 -- TODO fire post processing event
16                 session.send(stanza);
17         else -- resource not online
18                 if stanza.attr.type == "get" or stanza.attr.type == "set" then
19                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
20                 end
21         end
22         return true;
23 end);
24
25 module:hook("iq/bare", function(data)
26         -- IQ to bare JID recieved
27         local origin, stanza = data.origin, data.stanza;
28
29         if not bare_sessions[stanza.attr.to] then -- quick check for account existance
30                 local node, host = jid_split(stanza.attr.to);
31                 if not user_exists(node, host) then -- full check for account existance
32                         if stanza.attr.type == "get" or stanza.attr.type == "set" then
33                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
34                         end
35                         return true;
36                 end
37         end
38         -- TODO fire post processing events
39         if #stanza.tags == 1 then
40                 return module:fire_event("iq/bare/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name, data);
41         else
42                 return true; -- TODO do something with results and errors
43         end
44 end);
45
46 module:hook("iq/host", function(data)
47         -- IQ to a local host recieved
48         local origin, stanza = data.origin, data.stanza;
49
50         if #stanza.tags == 1 then
51                 return module:fire_event("iq/host/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name, data);
52         else
53                 return true; -- TODO do something with results and errors
54         end
55 end);