2 -- Copyright (C) 2008-2009 Matthew Wild
\r
3 -- Copyright (C) 2008-2009 Waqas Hussain
\r
5 -- This project is MIT/X11 licensed. Please see the
\r
6 -- COPYING file in the source package for more information.
\r
11 local log = require "util.logger".init("mod_presence")
\r
13 local require = require;
\r
14 local pairs, ipairs = pairs, ipairs;
\r
15 local t_concat = table.concat;
\r
16 local s_find = string.find;
\r
17 local tonumber = tonumber;
\r
19 local st = require "util.stanza";
\r
20 local jid_split = require "util.jid".split;
\r
21 local jid_bare = require "util.jid".bare;
\r
22 local hosts = hosts;
\r
24 local rostermanager = require "core.rostermanager";
\r
25 local sessionmanager = require "core.sessionmanager";
\r
26 local offlinemanager = require "core.offlinemanager";
\r
28 local _core_route_stanza = core_route_stanza;
\r
29 local core_route_stanza;
\r
30 function core_route_stanza(origin, stanza)
\r
31 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
\r
32 local node, host = jid_split(stanza.attr.to);
\r
34 if host and host.type == "local" then
\r
35 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
\r
39 _core_route_stanza(origin, stanza);
\r
42 function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound)
\r
43 local type = stanza.attr.type;
\r
44 if type and type ~= "unavailable" and type ~= "error" then
\r
46 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
\r
48 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
\r
50 elseif not inbound and not stanza.attr.to then
\r
51 handle_normal_presence(origin, stanza, core_route_stanza);
\r
53 core_route_stanza(origin, stanza);
\r
57 function handle_normal_presence(origin, stanza, core_route_stanza)
\r
58 if origin.roster then
\r
59 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
\r
60 local subscription = origin.roster[jid].subscription;
\r
61 if subscription == "both" or subscription == "from" then
\r
62 stanza.attr.to = jid;
\r
63 core_route_stanza(origin, stanza);
\r
66 local node, host = jid_split(stanza.attr.from);
\r
67 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
\r
68 if res ~= origin and res.presence then -- to resource
\r
69 stanza.attr.to = res.full_jid;
\r
70 core_route_stanza(origin, stanza);
\r
73 if stanza.attr.type == nil and not origin.presence then -- initial presence
\r
74 local probe = st.presence({from = origin.full_jid, type = "probe"});
\r
75 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
\r
76 local subscription = origin.roster[jid].subscription;
\r
77 if subscription == "both" or subscription == "to" then
\r
78 probe.attr.to = jid;
\r
79 core_route_stanza(origin, probe);
\r
82 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
\r
83 if res ~= origin and res.presence then
\r
84 res.presence.attr.to = origin.full_jid;
\r
85 core_route_stanza(res, res.presence);
\r
86 res.presence.attr.to = nil;
\r
89 if origin.roster.pending then -- resend incoming subscription requests
\r
90 for jid in pairs(origin.roster.pending) do
\r
91 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
\r
94 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
\r
95 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
\r
97 request.attr.to = jid;
\r
98 core_route_stanza(origin, request);
\r
101 local offline = offlinemanager.load(node, host);
\r
103 for _, msg in ipairs(offline) do
\r
104 origin.send(msg); -- FIXME do we need to modify to/from in any way?
\r
106 offlinemanager.deleteAll(node, host);
\r
109 origin.priority = 0;
\r
110 if stanza.attr.type == "unavailable" then
\r
111 origin.presence = nil;
\r
112 if origin.directed then
\r
113 local old_from = stanza.attr.from;
\r
114 stanza.attr.from = origin.full_jid;
\r
115 for jid in pairs(origin.directed) do
\r
116 stanza.attr.to = jid;
\r
117 core_route_stanza(origin, stanza);
\r
119 stanza.attr.from = old_from;
\r
120 origin.directed = nil;
\r
123 origin.presence = stanza;
\r
124 local priority = stanza:child_with_name("priority");
\r
125 if priority and #priority > 0 then
\r
126 priority = t_concat(priority);
\r
127 if s_find(priority, "^[+-]?[0-9]+$") then
\r
128 priority = tonumber(priority);
\r
129 if priority < -128 then priority = -128 end
\r
130 if priority > 127 then priority = 127 end
\r
131 origin.priority = priority;
\r
135 stanza.attr.to = nil; -- reset it
\r
137 log("error", "presence recieved from client with no roster");
\r
141 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)
\r
142 local h = hosts[host];
\r
144 if h and h.type == "local" then
\r
145 local u = h.sessions[user];
\r
147 for k, session in pairs(u.sessions) do
\r
148 local pres = session.presence;
\r
150 pres.attr.to = jid;
\r
151 pres.attr.from = session.full_jid;
\r
152 core_route_stanza(session, pres);
\r
153 pres.attr.to = nil;
\r
154 pres.attr.from = nil;
\r
160 log("info", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
\r
164 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
\r
165 local node, host = jid_split(from_bare);
\r
166 local st_from, st_to = stanza.attr.from, stanza.attr.to;
\r
167 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
\r
168 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
\r
169 if stanza.attr.type == "subscribe" then
\r
171 -- 2. roster push (subscription = none, ask = subscribe)
\r
172 if rostermanager.set_contact_pending_out(node, host, to_bare) then
\r
173 rostermanager.roster_push(node, host, to_bare);
\r
174 end -- else file error
\r
175 core_route_stanza(origin, stanza);
\r
176 elseif stanza.attr.type == "unsubscribe" then
\r
178 -- 2. roster push (subscription = none or from)
\r
179 if rostermanager.unsubscribe(node, host, to_bare) then
\r
180 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
\r
181 end -- else file error
\r
182 core_route_stanza(origin, stanza);
\r
183 elseif stanza.attr.type == "subscribed" then
\r
185 -- 2. roster_push ()
\r
186 -- 3. send_presence_of_available_resources
\r
187 if rostermanager.subscribed(node, host, to_bare) then
\r
188 rostermanager.roster_push(node, host, to_bare);
\r
190 core_route_stanza(origin, stanza);
\r
191 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
\r
192 elseif stanza.attr.type == "unsubscribed" then
\r
194 -- 2. roster push (subscription = none or to)
\r
195 if rostermanager.unsubscribed(node, host, to_bare) then
\r
196 rostermanager.roster_push(node, host, to_bare);
\r
198 core_route_stanza(origin, stanza);
\r
200 stanza.attr.from, stanza.attr.to = st_from, st_to;
\r
203 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
\r
204 local node, host = jid_split(to_bare);
\r
205 local st_from, st_to = stanza.attr.from, stanza.attr.to;
\r
206 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
\r
207 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
\r
208 if stanza.attr.type == "probe" then
\r
209 if rostermanager.is_contact_subscribed(node, host, from_bare) then
\r
210 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
\r
211 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
\r
214 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
\r
216 elseif stanza.attr.type == "subscribe" then
\r
217 if rostermanager.is_contact_subscribed(node, host, from_bare) then
\r
218 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
\r
219 -- Sending presence is not clearly stated in the RFC, but it seems appropriate
\r
220 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
\r
221 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
\r
224 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
\r
225 if rostermanager.set_contact_pending_in(node, host, from_bare) then
\r
226 sessionmanager.send_to_available_resources(node, host, stanza);
\r
227 end -- TODO else return error, unable to save
\r
230 elseif stanza.attr.type == "unsubscribe" then
\r
231 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
\r
232 rostermanager.roster_push(node, host, from_bare);
\r
234 elseif stanza.attr.type == "subscribed" then
\r
235 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
\r
236 rostermanager.roster_push(node, host, from_bare);
\r
238 elseif stanza.attr.type == "unsubscribed" then
\r
239 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
\r
240 rostermanager.roster_push(node, host, from_bare);
\r
242 end -- discard any other type
\r
243 stanza.attr.from, stanza.attr.to = st_from, st_to;
\r
246 local function presence_handler(data)
\r
247 local origin, stanza = data.origin, data.stanza;
\r
248 local to = stanza.attr.to;
\r
249 local node, host = jid_split(to);
\r
250 local to_bare = jid_bare(to);
\r
251 local from_bare = jid_bare(stanza.attr.from);
\r
252 if origin.type == "c2s" then
\r
253 if to ~= nil and not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence
\r
254 origin.directed = origin.directed or {};
\r
255 origin.directed[to] = true;
\r
257 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
\r
258 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
\r
260 handle_normal_presence(origin, stanza, core_route_stanza);
\r
262 core_route_stanza(origin, stanza);
\r
264 elseif (origin.type == "s2sin" or origin.type == "component") and hosts[host] then
\r
265 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
\r
266 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
\r
268 core_route_stanza(origin, stanza);
\r
273 local add_handler = require "core.eventmanager2".add_handler;
\r
274 local remove_handler = require "core.eventmanager2".remove_handler;
\r
276 add_handler(module:get_host().."/presence", presence_handler);
\r
277 module.unload = function()
\r
278 remove_handler(module:get_host().."/presence", presence_handler);
\r