Merge with 0.7.
[prosody.git] / plugins / mod_iq.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 local user_exists = require "core.usermanager".user_exists;
13
14 local full_sessions = full_sessions;
15 local bare_sessions = bare_sessions;
16
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
33 module:hook("iq/bare", function(data)
34         -- IQ to bare JID recieved
35         local origin, stanza = data.origin, data.stanza;
36
37         local to = stanza.attr.to;
38         if to and not bare_sessions[to] then -- quick check for account existance
39                 local node, host = jid_split(to);
40                 if not user_exists(node, host) then -- full check for account existance
41                         if stanza.attr.type == "get" or stanza.attr.type == "set" then
42                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
43                         end
44                         return true;
45                 end
46         end
47         -- TODO fire post processing events
48         if stanza.attr.type == "get" or stanza.attr.type == "set" then
49                 return module:fire_event("iq/bare/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name, data);
50         else
51                 module:fire_event("iq/bare/"..stanza.attr.id, data);
52                 return true;
53         end
54 end);
55
56 module:hook("iq/self", function(data)
57         -- IQ to bare JID recieved
58         local origin, stanza = data.origin, data.stanza;
59
60         if stanza.attr.type == "get" or stanza.attr.type == "set" then
61                 return module:fire_event("iq/self/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name, data);
62         else
63                 module:fire_event("iq/self/"..stanza.attr.id, data);
64                 return true;
65         end
66 end);
67
68 module:hook("iq/host", function(data)
69         -- IQ to a local host recieved
70         local origin, stanza = data.origin, data.stanza;
71
72         if stanza.attr.type == "get" or stanza.attr.type == "set" then
73                 return module:fire_event("iq/host/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name, data);
74         else
75                 module:fire_event("iq/host/"..stanza.attr.id, data);
76                 return true;
77         end
78 end);