mod_ping: Convert from Windows line endings
[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         local to = stanza.attr.to;
30         if to and not bare_sessions[to] then -- quick check for account existance
31                 local node, host = jid_split(to);
32                 if not user_exists(node, host) then -- full check for account existance
33                         if stanza.attr.type == "get" or stanza.attr.type == "set" then
34                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
35                         end
36                         return true;
37                 end
38         end
39         -- TODO fire post processing events
40         if stanza.attr.type == "get" or stanza.attr.type == "set" then
41                 return module:fire_event("iq/bare/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name, data);
42         else
43                 module:fire_event("iq/bare/"..stanza.attr.id, data);
44                 return true;
45         end
46 end);
47
48 module:hook("iq/host", function(data)
49         -- IQ to a local host recieved
50         local origin, stanza = data.origin, data.stanza;
51
52         if stanza.attr.type == "get" or stanza.attr.type == "set" then
53                 return module:fire_event("iq/host/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name, data);
54         else
55                 module:fire_event("iq/host/"..stanza.attr.id, data);
56                 return true;
57         end
58 end);