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