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