Software /
code /
prosody
Annotate
plugins/mod_presence.lua @ 1448:700ea94e1769
Automated merge with http://waqas.ath.cx:8000/
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 29 Jun 2009 14:13:49 +0100 |
parent | 1418:d14de6cb8b5b |
child | 1463:4ad920999cfa |
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 | |
1210
342f401f354c
mod_presence: Use logger supplied by modulemanager
Matthew Wild <mwild1@gmail.com>
parents:
1209
diff
changeset
|
9 local log = module._log; |
1009 | 10 |
11 local require = require; | |
12 local pairs, ipairs = pairs, ipairs; | |
13 local t_concat = table.concat; | |
14 local s_find = string.find; | |
15 local tonumber = tonumber; | |
16 | |
17 local st = require "util.stanza"; | |
18 local jid_split = require "util.jid".split; | |
19 local jid_bare = require "util.jid".bare; | |
20 local hosts = hosts; | |
21 | |
22 local rostermanager = require "core.rostermanager"; | |
23 local sessionmanager = require "core.sessionmanager"; | |
24 local offlinemanager = require "core.offlinemanager"; | |
25 | |
1044
41a0c76127f4
mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1023
diff
changeset
|
26 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
|
27 local core_route_stanza; |
41a0c76127f4
mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1023
diff
changeset
|
28 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
|
29 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
|
30 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
|
31 host = hosts[host]; |
41a0c76127f4
mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1023
diff
changeset
|
32 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
|
33 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
|
34 return; |
41a0c76127f4
mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1023
diff
changeset
|
35 end |
41a0c76127f4
mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1023
diff
changeset
|
36 end |
41a0c76127f4
mod_presence: Fix for local presence subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1023
diff
changeset
|
37 _core_route_stanza(origin, stanza); |
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 |
1009 | 40 function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound) |
41 local type = stanza.attr.type; | |
42 if type and type ~= "unavailable" and type ~= "error" then | |
43 if inbound then | |
44 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); | |
45 else | |
46 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); | |
47 end | |
48 elseif not inbound and not stanza.attr.to then | |
49 handle_normal_presence(origin, stanza, core_route_stanza); | |
50 else | |
51 core_route_stanza(origin, stanza); | |
52 end | |
53 end | |
54 | |
1418
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
55 local function select_top_resources(user) |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
56 local priority = 0; |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
57 local recipients = {}; |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
58 for _, session in pairs(user.sessions) do -- find resource with greatest priority |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
59 if session.presence then |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
60 -- TODO check active privacy list for session |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
61 local p = session.priority; |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
62 if p > priority then |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
63 priority = p; |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
64 recipients = {session}; |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
65 elseif p == priority then |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
66 t_insert(recipients, session); |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
67 end |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
68 end |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
69 end |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
70 return recipients; |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
71 end |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
72 local function recalc_resource_map(origin) |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
73 local user = hosts[origin.host].sessions[origin.username]; |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
74 user.top_resources = select_top_resources(user); |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
75 if #user.top_resources == 0 then user.top_resources = nil; end |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
76 end |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
77 |
1009 | 78 function handle_normal_presence(origin, stanza, core_route_stanza) |
79 if origin.roster then | |
80 for jid in pairs(origin.roster) do -- broadcast to all interested contacts | |
81 local subscription = origin.roster[jid].subscription; | |
82 if subscription == "both" or subscription == "from" then | |
83 stanza.attr.to = jid; | |
84 core_route_stanza(origin, stanza); | |
85 end | |
86 end | |
87 local node, host = jid_split(stanza.attr.from); | |
88 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
|
89 if res ~= origin and res.presence then -- to resource |
1009 | 90 stanza.attr.to = res.full_jid; |
91 core_route_stanza(origin, stanza); | |
92 end | |
93 end | |
94 if stanza.attr.type == nil and not origin.presence then -- initial presence | |
95 local probe = st.presence({from = origin.full_jid, type = "probe"}); | |
96 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to | |
97 local subscription = origin.roster[jid].subscription; | |
98 if subscription == "both" or subscription == "to" then | |
99 probe.attr.to = jid; | |
100 core_route_stanza(origin, probe); | |
101 end | |
102 end | |
103 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources | |
104 if res ~= origin and res.presence then | |
105 res.presence.attr.to = origin.full_jid; | |
106 core_route_stanza(res, res.presence); | |
107 res.presence.attr.to = nil; | |
108 end | |
109 end | |
110 if origin.roster.pending then -- resend incoming subscription requests | |
111 for jid in pairs(origin.roster.pending) do | |
112 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original? | |
113 end | |
114 end | |
115 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host}); | |
116 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests | |
117 if item.ask then | |
118 request.attr.to = jid; | |
119 core_route_stanza(origin, request); | |
120 end | |
121 end | |
122 local offline = offlinemanager.load(node, host); | |
123 if offline then | |
124 for _, msg in ipairs(offline) do | |
125 origin.send(msg); -- FIXME do we need to modify to/from in any way? | |
126 end | |
127 offlinemanager.deleteAll(node, host); | |
128 end | |
129 end | |
130 if stanza.attr.type == "unavailable" then | |
131 origin.presence = nil; | |
1418
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
132 if origin.priority then |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
133 origin.priority = nil; |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
134 recalc_resource_map(origin); |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
135 end |
1009 | 136 if origin.directed then |
137 local old_from = stanza.attr.from; | |
138 stanza.attr.from = origin.full_jid; | |
139 for jid in pairs(origin.directed) do | |
140 stanza.attr.to = jid; | |
141 core_route_stanza(origin, stanza); | |
142 end | |
143 stanza.attr.from = old_from; | |
144 origin.directed = nil; | |
145 end | |
146 else | |
147 origin.presence = stanza; | |
148 local priority = stanza:child_with_name("priority"); | |
149 if priority and #priority > 0 then | |
150 priority = t_concat(priority); | |
151 if s_find(priority, "^[+-]?[0-9]+$") then | |
152 priority = tonumber(priority); | |
153 if priority < -128 then priority = -128 end | |
154 if priority > 127 then priority = 127 end | |
1418
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
155 else priority = 0; end |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
156 else priority = 0; end |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
157 if origin.priority ~= priority then |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
158 origin.priority = priority; |
d14de6cb8b5b
mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
Waqas Hussain <waqas20@gmail.com>
parents:
1287
diff
changeset
|
159 recalc_resource_map(origin); |
1009 | 160 end |
161 end | |
162 stanza.attr.to = nil; -- reset it | |
163 else | |
1211
d60e68855176
mod_presence: Lower some log levels to their correct values
Matthew Wild <mwild1@gmail.com>
parents:
1210
diff
changeset
|
164 log("warn", "presence recieved from client with no roster"); |
1009 | 165 end |
166 end | |
167 | |
168 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza) | |
169 local h = hosts[host]; | |
170 local count = 0; | |
171 if h and h.type == "local" then | |
172 local u = h.sessions[user]; | |
173 if u then | |
174 for k, session in pairs(u.sessions) do | |
175 local pres = session.presence; | |
176 if pres then | |
177 pres.attr.to = jid; | |
178 core_route_stanza(session, pres); | |
179 pres.attr.to = nil; | |
180 count = count + 1; | |
181 end | |
182 end | |
183 end | |
184 end | |
1211
d60e68855176
mod_presence: Lower some log levels to their correct values
Matthew Wild <mwild1@gmail.com>
parents:
1210
diff
changeset
|
185 log("debug", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid); |
1009 | 186 return count; |
187 end | |
188 | |
189 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza) | |
190 local node, host = jid_split(from_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", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare); | |
194 if stanza.attr.type == "subscribe" then | |
195 -- 1. route stanza | |
196 -- 2. roster push (subscription = none, ask = subscribe) | |
197 if rostermanager.set_contact_pending_out(node, host, to_bare) then | |
198 rostermanager.roster_push(node, host, to_bare); | |
199 end -- else file error | |
200 core_route_stanza(origin, stanza); | |
201 elseif stanza.attr.type == "unsubscribe" then | |
202 -- 1. route stanza | |
203 -- 2. roster push (subscription = none or from) | |
204 if rostermanager.unsubscribe(node, host, to_bare) then | |
205 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed? | |
206 end -- else file error | |
207 core_route_stanza(origin, stanza); | |
208 elseif stanza.attr.type == "subscribed" then | |
209 -- 1. route stanza | |
210 -- 2. roster_push () | |
211 -- 3. send_presence_of_available_resources | |
212 if rostermanager.subscribed(node, host, to_bare) then | |
213 rostermanager.roster_push(node, host, to_bare); | |
214 end | |
215 core_route_stanza(origin, stanza); | |
216 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza); | |
217 elseif stanza.attr.type == "unsubscribed" then | |
218 -- 1. route stanza | |
219 -- 2. roster push (subscription = none or to) | |
220 if rostermanager.unsubscribed(node, host, to_bare) then | |
221 rostermanager.roster_push(node, host, to_bare); | |
222 end | |
223 core_route_stanza(origin, stanza); | |
224 end | |
225 stanza.attr.from, stanza.attr.to = st_from, st_to; | |
226 end | |
227 | |
228 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza) | |
229 local node, host = jid_split(to_bare); | |
230 local st_from, st_to = stanza.attr.from, stanza.attr.to; | |
231 stanza.attr.from, stanza.attr.to = from_bare, to_bare; | |
232 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare); | |
233 if stanza.attr.type == "probe" then | |
234 if rostermanager.is_contact_subscribed(node, host, from_bare) then | |
235 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then | |
236 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) | |
237 end | |
238 else | |
239 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"})); | |
240 end | |
241 elseif stanza.attr.type == "subscribe" then | |
242 if rostermanager.is_contact_subscribed(node, host, from_bare) then | |
243 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed | |
244 -- Sending presence is not clearly stated in the RFC, but it seems appropriate | |
245 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then | |
246 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) | |
247 end | |
248 else | |
249 if not rostermanager.is_contact_pending_in(node, host, from_bare) then | |
250 if rostermanager.set_contact_pending_in(node, host, from_bare) then | |
251 sessionmanager.send_to_available_resources(node, host, stanza); | |
252 end -- TODO else return error, unable to save | |
253 end | |
254 end | |
255 elseif stanza.attr.type == "unsubscribe" then | |
256 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then | |
257 rostermanager.roster_push(node, host, from_bare); | |
258 end | |
259 elseif stanza.attr.type == "subscribed" then | |
260 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then | |
261 rostermanager.roster_push(node, host, from_bare); | |
262 end | |
263 elseif stanza.attr.type == "unsubscribed" then | |
264 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then | |
265 rostermanager.roster_push(node, host, from_bare); | |
266 end | |
267 end -- discard any other type | |
268 stanza.attr.from, stanza.attr.to = st_from, st_to; | |
269 end | |
270 | |
271 local function presence_handler(data) | |
272 local origin, stanza = data.origin, data.stanza; | |
273 local to = stanza.attr.to; | |
274 local node, host = jid_split(to); | |
275 local to_bare = jid_bare(to); | |
276 local from_bare = jid_bare(stanza.attr.from); | |
277 if origin.type == "c2s" then | |
278 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 | |
279 origin.directed = origin.directed or {}; | |
1150
d71a8f28f18b
mod_presence: Added a FIXME comment about directed presence
Waqas Hussain <waqas20@gmail.com>
parents:
1147
diff
changeset
|
280 origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to? |
1009 | 281 end |
1019
8d750336e517
mod_presence: Fix incorrect internal routing for probes and subscriptions
Waqas Hussain <waqas20@gmail.com>
parents:
1011
diff
changeset
|
282 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then |
1009 | 283 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); |
284 elseif not to then | |
1011
beb039827c9f
Stopped using presencemanager in stanza_router
Waqas Hussain <waqas20@gmail.com>
parents:
1009
diff
changeset
|
285 handle_normal_presence(origin, stanza, core_route_stanza); |
1009 | 286 else |
287 core_route_stanza(origin, stanza); | |
288 end | |
289 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
|
290 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then |
1009 | 291 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); |
292 else | |
293 core_route_stanza(origin, stanza); | |
294 end | |
295 end | |
1147
513c1d071045
mod_presence: return true from the presence handler
Waqas Hussain <waqas20@gmail.com>
parents:
1057
diff
changeset
|
296 return true; |
1009 | 297 end |
298 | |
1245
be5fe60bd866
mod_presence: Changed to use the prosody.events object directly, rather than through eventmanager2
Waqas Hussain <waqas20@gmail.com>
parents:
1211
diff
changeset
|
299 prosody.events.add_handler(module:get_host().."/presence", presence_handler); |
1009 | 300 module.unload = function() |
1245
be5fe60bd866
mod_presence: Changed to use the prosody.events object directly, rather than through eventmanager2
Waqas Hussain <waqas20@gmail.com>
parents:
1211
diff
changeset
|
301 prosody.events.remove_handler(module:get_host().."/presence", presence_handler); |
1009 | 302 end |
1276
d0e80c1578e1
mod_presence: Handle outbound presence to full JIDs
Waqas Hussain <waqas20@gmail.com>
parents:
1245
diff
changeset
|
303 |
1279
fa00d56a9fd3
mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1278
diff
changeset
|
304 local outbound_presence_handler = function(data) |
1282
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
305 -- outbound presence recieved |
1276
d0e80c1578e1
mod_presence: Handle outbound presence to full JIDs
Waqas Hussain <waqas20@gmail.com>
parents:
1245
diff
changeset
|
306 local origin, stanza = data.origin, data.stanza; |
d0e80c1578e1
mod_presence: Handle outbound presence to full JIDs
Waqas Hussain <waqas20@gmail.com>
parents:
1245
diff
changeset
|
307 |
1286
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
308 local to = stanza.attr.to; |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
309 if to then |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
310 local t = stanza.attr.type; |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
311 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
312 handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza); |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
313 return true; |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
314 end |
1278
2abf85791f29
mod_presence: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1277
diff
changeset
|
315 |
1286
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
316 local to_bare = jid_bare(to); |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
317 if not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
318 origin.directed = origin.directed or {}; |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
319 if t then -- removing from directed presence list on sending an error or unavailable |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
320 origin.directed[to] = nil; -- FIXME does it make more sense to add to_bare rather than to? |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
321 else |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
322 origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to? |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
323 end |
1277
f2b50efe8d44
mod_presence: Remove JIDs from directed presence list on sending error or unavailable presence
Waqas Hussain <waqas20@gmail.com>
parents:
1276
diff
changeset
|
324 end |
1286
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
325 end -- TODO maybe handle normal presence here, instead of letting it pass to incoming handlers? |
1279
fa00d56a9fd3
mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1278
diff
changeset
|
326 end |
fa00d56a9fd3
mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1278
diff
changeset
|
327 |
fa00d56a9fd3
mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1278
diff
changeset
|
328 module:hook("pre-presence/full", outbound_presence_handler); |
fa00d56a9fd3
mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1278
diff
changeset
|
329 module:hook("pre-presence/bare", outbound_presence_handler); |
fa00d56a9fd3
mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1278
diff
changeset
|
330 module:hook("pre-presence/host", outbound_presence_handler); |
1281
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
331 |
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
332 module:hook("presence/bare", function(data) |
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
333 -- inbound presence to bare JID recieved |
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
334 local origin, stanza = data.origin, data.stanza; |
1282
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
335 |
1283
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
336 local to = stanza.attr.to; |
1287
ac82c7b9c76b
mod_presence: Fix a global access
Waqas Hussain <waqas20@gmail.com>
parents:
1286
diff
changeset
|
337 local t = stanza.attr.type; |
1283
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
338 if to then |
1285
0a6e2d6ae459
mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1284
diff
changeset
|
339 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to bare JID |
0a6e2d6ae459
mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1284
diff
changeset
|
340 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza); |
0a6e2d6ae459
mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1284
diff
changeset
|
341 return true; |
0a6e2d6ae459
mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1284
diff
changeset
|
342 end |
0a6e2d6ae459
mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1284
diff
changeset
|
343 |
1283
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
344 local user = bare_sessions[to]; |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
345 if user then |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
346 for _, session in pairs(user.sessions) do |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
347 if session.presence then -- only send to available resources |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
348 session.send(stanza); |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
349 end |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
350 end |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
351 end -- no resources not online, discard |
1285
0a6e2d6ae459
mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1284
diff
changeset
|
352 elseif not t or t == "unavailable" then |
1283
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
353 handle_normal_presence(origin, stanza, core_route_stanza); |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
354 end |
1284
c0fb8379696e
mod_presence: return true from incoming presence handlers to prevent further processing
Waqas Hussain <waqas20@gmail.com>
parents:
1283
diff
changeset
|
355 return true; |
1281
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
356 end); |
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
357 module:hook("presence/full", function(data) |
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
358 -- inbound presence to full JID recieved |
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
359 local origin, stanza = data.origin, data.stanza; |
1282
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
360 |
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
361 local t = stanza.attr.type; |
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
362 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to full JID |
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
363 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza); |
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
364 return true; |
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
365 end |
1283
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
366 |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
367 local session = full_sessions[stanza.attr.to]; |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
368 if session then |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
369 -- TODO fire post processing event |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
370 session.send(stanza); |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
371 end -- resource not online, discard |
1284
c0fb8379696e
mod_presence: return true from incoming presence handlers to prevent further processing
Waqas Hussain <waqas20@gmail.com>
parents:
1283
diff
changeset
|
372 return true; |
1281
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
373 end); |