Software /
code /
prosody
Annotate
core/presencemanager.lua @ 485:f8456f0da769
Make ejabberd2prosody.lua a little more cross-platform :)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 29 Nov 2008 19:52:20 +0000 |
parent | 468:ab49cb6d0e92 |
child | 519:cccd610a0ef9 |
rev | line source |
---|---|
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 local log = require "util.logger".init("presencemanager") |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 local require = require; |
468
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
5 local pairs, ipairs = pairs, ipairs; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
6 local t_concat = table.concat; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
7 local s_find = string.find; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
8 local tonumber = tonumber; |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 local st = require "util.stanza"; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 local jid_split = require "util.jid".split; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 local hosts = hosts; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 local rostermanager = require "core.rostermanager"; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 local sessionmanager = require "core.sessionmanager"; |
468
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
16 local offlinemanager = require "core.offlinemanager"; |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 module "presencemanager" |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 |
468
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
20 function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound) |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
21 local type = stanza.attr.type; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
22 if type and type ~= "unavailable" then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
23 if inbound then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
24 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
25 else |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
26 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
27 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
28 elseif not inbound and not stanza.attr.to then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
29 handle_normal_presence(origin, stanza, core_route_stanza); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
30 else |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
31 core_route_stanza(origin, stanza); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
32 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
33 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
34 |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
35 function handle_normal_presence(origin, stanza, core_route_stanza) |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
36 if origin.roster then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
37 for jid in pairs(origin.roster) do -- broadcast to all interested contacts |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
38 local subscription = origin.roster[jid].subscription; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
39 if subscription == "both" or subscription == "from" then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
40 stanza.attr.to = jid; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
41 core_route_stanza(origin, stanza); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
42 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
43 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
44 local node, host = jid_split(stanza.attr.from); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
45 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
46 if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
47 stanza.attr.to = res.full_jid; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
48 core_route_stanza(origin, stanza); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
49 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
50 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
51 if stanza.attr.type == nil and not origin.presence then -- initial presence |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
52 local probe = st.presence({from = origin.full_jid, type = "probe"}); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
53 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
54 local subscription = origin.roster[jid].subscription; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
55 if subscription == "both" or subscription == "to" then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
56 probe.attr.to = jid; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
57 core_route_stanza(origin, probe); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
58 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
59 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
60 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
61 if res ~= origin and res.presence then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
62 res.presence.attr.to = origin.full_jid; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
63 core_route_stanza(res, res.presence); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
64 res.presence.attr.to = nil; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
65 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
66 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
67 if origin.roster.pending then -- resend incoming subscription requests |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
68 for jid in pairs(origin.roster.pending) do |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
69 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original? |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
70 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
71 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
72 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host}); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
73 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
74 if item.ask then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
75 request.attr.to = jid; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
76 core_route_stanza(origin, request); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
77 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
78 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
79 for _, msg in ipairs(offlinemanager.load(node, host) or {}) do |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
80 origin.send(msg); -- FIXME do we need to modify to/from in any way? |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
81 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
82 offlinemanager.deleteAll(node, host); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
83 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
84 origin.priority = 0; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
85 if stanza.attr.type == "unavailable" then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
86 origin.presence = nil; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
87 else |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
88 origin.presence = stanza; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
89 local priority = stanza:child_with_name("priority"); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
90 if priority and #priority > 0 then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
91 priority = t_concat(priority); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
92 if s_find(priority, "^[+-]?[0-9]+$") then |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
93 priority = tonumber(priority); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
94 if priority < -128 then priority = -128 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
95 if priority > 127 then priority = 127 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
96 origin.priority = priority; |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
97 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
98 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
99 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
100 stanza.attr.to = nil; -- reset it |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
101 else |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
102 log("error", "presence recieved from client with no roster"); |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
103 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
104 end |
ab49cb6d0e92
Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
326
diff
changeset
|
105 |
322
d370fea67264
Fix for broadcasting presence of available resources to newly approved contact
Waqas Hussain <waqas20@gmail.com>
parents:
321
diff
changeset
|
106 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza) |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 local h = hosts[host]; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 local count = 0; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 if h and h.type == "local" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 local u = h.sessions[user]; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 if u then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 for k, session in pairs(u.sessions) do |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 local pres = session.presence; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 if pres then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 pres.attr.to = jid; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 pres.attr.from = session.full_jid; |
323
7c9d2cabd245
Second fix for broadcasting presence of available resources to newly approved contact
Waqas Hussain <waqas20@gmail.com>
parents:
322
diff
changeset
|
117 core_route_stanza(session, pres); |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 pres.attr.to = nil; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 pres.attr.from = nil; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 count = count + 1; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 end |
325
5de1048b3fbf
Adding logging for outbound presence broadcasts (changed it a bit)
Waqas Hussain <waqas20@gmail.com>
parents:
324
diff
changeset
|
125 log("info", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid); |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 return count; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza) |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 local node, host = jid_split(from_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 local st_from, st_to = stanza.attr.from, stanza.attr.to; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 stanza.attr.from, stanza.attr.to = from_bare, to_bare; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 if stanza.attr.type == "subscribe" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 -- 1. route stanza |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 -- 2. roster push (subscription = none, ask = subscribe) |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 if rostermanager.set_contact_pending_out(node, host, to_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 rostermanager.roster_push(node, host, to_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 end -- else file error |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 core_route_stanza(origin, stanza); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 elseif stanza.attr.type == "unsubscribe" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 -- 1. route stanza |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 -- 2. roster push (subscription = none or from) |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 if rostermanager.unsubscribe(node, host, to_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed? |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 end -- else file error |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 core_route_stanza(origin, stanza); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 elseif stanza.attr.type == "subscribed" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 -- 1. route stanza |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 -- 2. roster_push () |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 -- 3. send_presence_of_available_resources |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 if rostermanager.subscribed(node, host, to_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 rostermanager.roster_push(node, host, to_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 end |
326
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
155 core_route_stanza(origin, stanza); |
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
156 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza); |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 elseif stanza.attr.type == "unsubscribed" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 -- 1. route stanza |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 -- 2. roster push (subscription = none or to) |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
160 if rostermanager.unsubscribed(node, host, to_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 rostermanager.roster_push(node, host, to_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
162 end |
326
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
163 core_route_stanza(origin, stanza); |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
164 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 stanza.attr.from, stanza.attr.to = st_from, st_to; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
166 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza) |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
169 local node, host = jid_split(to_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 local st_from, st_to = stanza.attr.from, stanza.attr.to; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
171 stanza.attr.from, stanza.attr.to = from_bare, to_bare; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
172 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
173 if stanza.attr.type == "probe" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
174 if rostermanager.is_contact_subscribed(node, host, from_bare) then |
322
d370fea67264
Fix for broadcasting presence of available resources to newly approved contact
Waqas Hussain <waqas20@gmail.com>
parents:
321
diff
changeset
|
175 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
176 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
177 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
178 else |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
179 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"})); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
181 elseif stanza.attr.type == "subscribe" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
182 if rostermanager.is_contact_subscribed(node, host, from_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
183 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed |
326
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
184 -- Sending presence is not clearly stated in the RFC, but it seems appropriate |
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
185 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then |
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
186 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) |
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
187 end |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
188 else |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 if not rostermanager.is_contact_pending_in(node, host, from_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 if rostermanager.set_contact_pending_in(node, host, from_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 sessionmanager.send_to_available_resources(node, host, stanza); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 end -- TODO else return error, unable to save |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
193 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
194 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
195 elseif stanza.attr.type == "unsubscribe" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
196 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 rostermanager.roster_push(node, host, from_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
198 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
199 elseif stanza.attr.type == "subscribed" then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
200 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
201 rostermanager.roster_push(node, host, from_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
202 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
203 elseif stanza.attr.type == "unsubscribed" then |
326
99a8317d1235
Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
325
diff
changeset
|
204 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then |
321
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
205 rostermanager.roster_push(node, host, from_bare); |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
206 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
207 end -- discard any other type |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
208 stanza.attr.from, stanza.attr.to = st_from, st_to; |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
209 end |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
210 |
31fe15ce6fac
Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
211 return _M; |