|
1 -- Prosody IM v0.4 |
|
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 |
|
11 local log = require "util.logger".init("mod_presence") |
|
12 |
|
13 local require = require; |
|
14 local pairs, ipairs = pairs, ipairs; |
|
15 local t_concat = table.concat; |
|
16 local s_find = string.find; |
|
17 local tonumber = tonumber; |
|
18 |
|
19 local st = require "util.stanza"; |
|
20 local jid_split = require "util.jid".split; |
|
21 local jid_bare = require "util.jid".bare; |
|
22 local hosts = hosts; |
|
23 |
|
24 local rostermanager = require "core.rostermanager"; |
|
25 local sessionmanager = require "core.sessionmanager"; |
|
26 local offlinemanager = require "core.offlinemanager"; |
|
27 |
|
28 function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound) |
|
29 local type = stanza.attr.type; |
|
30 if type and type ~= "unavailable" and type ~= "error" then |
|
31 if inbound then |
|
32 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); |
|
33 else |
|
34 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); |
|
35 end |
|
36 elseif not inbound and not stanza.attr.to then |
|
37 handle_normal_presence(origin, stanza, core_route_stanza); |
|
38 else |
|
39 core_route_stanza(origin, stanza); |
|
40 end |
|
41 end |
|
42 |
|
43 function handle_normal_presence(origin, stanza, core_route_stanza) |
|
44 if origin.roster then |
|
45 for jid in pairs(origin.roster) do -- broadcast to all interested contacts |
|
46 local subscription = origin.roster[jid].subscription; |
|
47 if subscription == "both" or subscription == "from" then |
|
48 stanza.attr.to = jid; |
|
49 core_route_stanza(origin, stanza); |
|
50 end |
|
51 end |
|
52 local node, host = jid_split(stanza.attr.from); |
|
53 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources |
|
54 if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence |
|
55 stanza.attr.to = res.full_jid; |
|
56 core_route_stanza(origin, stanza); |
|
57 end |
|
58 end |
|
59 if stanza.attr.type == nil and not origin.presence then -- initial presence |
|
60 local probe = st.presence({from = origin.full_jid, type = "probe"}); |
|
61 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to |
|
62 local subscription = origin.roster[jid].subscription; |
|
63 if subscription == "both" or subscription == "to" then |
|
64 probe.attr.to = jid; |
|
65 core_route_stanza(origin, probe); |
|
66 end |
|
67 end |
|
68 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources |
|
69 if res ~= origin and res.presence then |
|
70 res.presence.attr.to = origin.full_jid; |
|
71 core_route_stanza(res, res.presence); |
|
72 res.presence.attr.to = nil; |
|
73 end |
|
74 end |
|
75 if origin.roster.pending then -- resend incoming subscription requests |
|
76 for jid in pairs(origin.roster.pending) do |
|
77 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original? |
|
78 end |
|
79 end |
|
80 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host}); |
|
81 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests |
|
82 if item.ask then |
|
83 request.attr.to = jid; |
|
84 core_route_stanza(origin, request); |
|
85 end |
|
86 end |
|
87 local offline = offlinemanager.load(node, host); |
|
88 if offline then |
|
89 for _, msg in ipairs(offline) do |
|
90 origin.send(msg); -- FIXME do we need to modify to/from in any way? |
|
91 end |
|
92 offlinemanager.deleteAll(node, host); |
|
93 end |
|
94 end |
|
95 origin.priority = 0; |
|
96 if stanza.attr.type == "unavailable" then |
|
97 origin.presence = nil; |
|
98 if origin.directed then |
|
99 local old_from = stanza.attr.from; |
|
100 stanza.attr.from = origin.full_jid; |
|
101 for jid in pairs(origin.directed) do |
|
102 stanza.attr.to = jid; |
|
103 core_route_stanza(origin, stanza); |
|
104 end |
|
105 stanza.attr.from = old_from; |
|
106 origin.directed = nil; |
|
107 end |
|
108 else |
|
109 origin.presence = stanza; |
|
110 local priority = stanza:child_with_name("priority"); |
|
111 if priority and #priority > 0 then |
|
112 priority = t_concat(priority); |
|
113 if s_find(priority, "^[+-]?[0-9]+$") then |
|
114 priority = tonumber(priority); |
|
115 if priority < -128 then priority = -128 end |
|
116 if priority > 127 then priority = 127 end |
|
117 origin.priority = priority; |
|
118 end |
|
119 end |
|
120 end |
|
121 stanza.attr.to = nil; -- reset it |
|
122 else |
|
123 log("error", "presence recieved from client with no roster"); |
|
124 end |
|
125 end |
|
126 |
|
127 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza) |
|
128 local h = hosts[host]; |
|
129 local count = 0; |
|
130 if h and h.type == "local" then |
|
131 local u = h.sessions[user]; |
|
132 if u then |
|
133 for k, session in pairs(u.sessions) do |
|
134 local pres = session.presence; |
|
135 if pres then |
|
136 pres.attr.to = jid; |
|
137 pres.attr.from = session.full_jid; |
|
138 core_route_stanza(session, pres); |
|
139 pres.attr.to = nil; |
|
140 pres.attr.from = nil; |
|
141 count = count + 1; |
|
142 end |
|
143 end |
|
144 end |
|
145 end |
|
146 log("info", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid); |
|
147 return count; |
|
148 end |
|
149 |
|
150 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza) |
|
151 local node, host = jid_split(from_bare); |
|
152 local st_from, st_to = stanza.attr.from, stanza.attr.to; |
|
153 stanza.attr.from, stanza.attr.to = from_bare, to_bare; |
|
154 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare); |
|
155 if stanza.attr.type == "subscribe" then |
|
156 -- 1. route stanza |
|
157 -- 2. roster push (subscription = none, ask = subscribe) |
|
158 if rostermanager.set_contact_pending_out(node, host, to_bare) then |
|
159 rostermanager.roster_push(node, host, to_bare); |
|
160 end -- else file error |
|
161 core_route_stanza(origin, stanza); |
|
162 elseif stanza.attr.type == "unsubscribe" then |
|
163 -- 1. route stanza |
|
164 -- 2. roster push (subscription = none or from) |
|
165 if rostermanager.unsubscribe(node, host, to_bare) then |
|
166 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed? |
|
167 end -- else file error |
|
168 core_route_stanza(origin, stanza); |
|
169 elseif stanza.attr.type == "subscribed" then |
|
170 -- 1. route stanza |
|
171 -- 2. roster_push () |
|
172 -- 3. send_presence_of_available_resources |
|
173 if rostermanager.subscribed(node, host, to_bare) then |
|
174 rostermanager.roster_push(node, host, to_bare); |
|
175 end |
|
176 core_route_stanza(origin, stanza); |
|
177 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza); |
|
178 elseif stanza.attr.type == "unsubscribed" then |
|
179 -- 1. route stanza |
|
180 -- 2. roster push (subscription = none or to) |
|
181 if rostermanager.unsubscribed(node, host, to_bare) then |
|
182 rostermanager.roster_push(node, host, to_bare); |
|
183 end |
|
184 core_route_stanza(origin, stanza); |
|
185 end |
|
186 stanza.attr.from, stanza.attr.to = st_from, st_to; |
|
187 end |
|
188 |
|
189 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza) |
|
190 local node, host = jid_split(to_bare); |
|
191 local st_from, st_to = stanza.attr.from, stanza.attr.to; |
|
192 stanza.attr.from, stanza.attr.to = from_bare, to_bare; |
|
193 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare); |
|
194 if stanza.attr.type == "probe" then |
|
195 if rostermanager.is_contact_subscribed(node, host, from_bare) then |
|
196 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then |
|
197 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) |
|
198 end |
|
199 else |
|
200 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"})); |
|
201 end |
|
202 elseif stanza.attr.type == "subscribe" then |
|
203 if rostermanager.is_contact_subscribed(node, host, from_bare) then |
|
204 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed |
|
205 -- Sending presence is not clearly stated in the RFC, but it seems appropriate |
|
206 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then |
|
207 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) |
|
208 end |
|
209 else |
|
210 if not rostermanager.is_contact_pending_in(node, host, from_bare) then |
|
211 if rostermanager.set_contact_pending_in(node, host, from_bare) then |
|
212 sessionmanager.send_to_available_resources(node, host, stanza); |
|
213 end -- TODO else return error, unable to save |
|
214 end |
|
215 end |
|
216 elseif stanza.attr.type == "unsubscribe" then |
|
217 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then |
|
218 rostermanager.roster_push(node, host, from_bare); |
|
219 end |
|
220 elseif stanza.attr.type == "subscribed" then |
|
221 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then |
|
222 rostermanager.roster_push(node, host, from_bare); |
|
223 end |
|
224 elseif stanza.attr.type == "unsubscribed" then |
|
225 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then |
|
226 rostermanager.roster_push(node, host, from_bare); |
|
227 end |
|
228 end -- discard any other type |
|
229 stanza.attr.from, stanza.attr.to = st_from, st_to; |
|
230 end |
|
231 |
|
232 local function presence_handler(data) |
|
233 local origin, stanza = data.origin, data.stanza; |
|
234 local to = stanza.attr.to; |
|
235 local node, host = jid_split(to); |
|
236 local to_bare = jid_bare(to); |
|
237 local from_bare = jid_bare(stanza.attr.from); |
|
238 if origin.type == "c2s" then |
|
239 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 |
|
240 origin.directed = origin.directed or {}; |
|
241 origin.directed[to] = true; |
|
242 end |
|
243 if to == nil and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then |
|
244 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); |
|
245 elseif not to then |
|
246 handle_normal_presence(origin, stanza); |
|
247 else |
|
248 core_route_stanza(origin, stanza); |
|
249 end |
|
250 elseif (origin.type == "s2sin" or origin.type == "component") and hosts[host] then |
|
251 if to == nil and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then |
|
252 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); |
|
253 else |
|
254 core_route_stanza(origin, stanza); |
|
255 end |
|
256 end |
|
257 end |
|
258 |
|
259 local add_handler = require "core.eventmanager2".add_handler; |
|
260 local remove_handler = require "core.eventmanager2".remove_handler; |
|
261 |
|
262 add_handler(module:get_host().."/presence", presence_handler); |
|
263 module.unload = function() |
|
264 remove_handler(module:get_host().."/presence", presence_handler); |
|
265 end |
|
266 |
|
267 return _M; |