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