Software /
code /
prosody
Annotate
plugins/mod_presence.lua @ 1477:e0411d6c9bae
mod_presence: Broadcast presence to resources before contacts
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sun, 05 Jul 2009 12:43:25 +0500 |
parent | 1476:5d6199a9b4f6 |
child | 1479:eef17ebe3ca1 |
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; | |
1470
af4b918e3432
mod_presence: Declare t_insert, fixes a potential traceback
Matthew Wild <mwild1@gmail.com>
parents:
1463
diff
changeset
|
13 local t_concat, t_insert = table.concat, table.insert; |
1009 | 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 |
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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 -- 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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 |
1009 | 63 function handle_normal_presence(origin, stanza, core_route_stanza) |
1475
16c8b1a8f6a7
mod_presence: Changed handle_normal_presence to use a local roster rather than origin's roster
Waqas Hussain <waqas20@gmail.com>
parents:
1474
diff
changeset
|
64 local roster = origin.roster; |
1476
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
65 local node, host = origin.username, origin.host; |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
66 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
67 if res ~= origin and res.presence then -- to resource |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
68 stanza.attr.to = res.full_jid; |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
69 core_route_stanza(origin, stanza); |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
70 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
71 end |
1477
e0411d6c9bae
mod_presence: Broadcast presence to resources before contacts
Waqas Hussain <waqas20@gmail.com>
parents:
1476
diff
changeset
|
72 for jid, item in pairs(roster) do -- broadcast to all interested contacts |
e0411d6c9bae
mod_presence: Broadcast presence to resources before contacts
Waqas Hussain <waqas20@gmail.com>
parents:
1476
diff
changeset
|
73 if item.subscription == "both" or item.subscription == "from" then |
e0411d6c9bae
mod_presence: Broadcast presence to resources before contacts
Waqas Hussain <waqas20@gmail.com>
parents:
1476
diff
changeset
|
74 stanza.attr.to = jid; |
e0411d6c9bae
mod_presence: Broadcast presence to resources before contacts
Waqas Hussain <waqas20@gmail.com>
parents:
1476
diff
changeset
|
75 core_route_stanza(origin, stanza); |
e0411d6c9bae
mod_presence: Broadcast presence to resources before contacts
Waqas Hussain <waqas20@gmail.com>
parents:
1476
diff
changeset
|
76 end |
e0411d6c9bae
mod_presence: Broadcast presence to resources before contacts
Waqas Hussain <waqas20@gmail.com>
parents:
1476
diff
changeset
|
77 end |
1476
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
78 if stanza.attr.type == nil and not origin.presence then -- initial presence |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
79 local probe = st.presence({from = origin.full_jid, type = "probe"}); |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
80 for jid, item in pairs(roster) do -- probe all contacts we are subscribed to |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
81 if item.subscription == "both" or item.subscription == "to" then |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
82 probe.attr.to = jid; |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
83 core_route_stanza(origin, probe); |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
84 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
85 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
86 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
87 if res ~= origin and res.presence then |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
88 res.presence.attr.to = origin.full_jid; |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
89 core_route_stanza(res, res.presence); |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
90 res.presence.attr.to = nil; |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
91 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
92 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
93 if roster.pending then -- resend incoming subscription requests |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
94 for jid in pairs(roster.pending) do |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
95 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original? |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
96 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
97 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
98 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host}); |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
99 for jid, item in pairs(roster) do -- resend outgoing subscription requests |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
100 if item.ask then |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
101 request.attr.to = jid; |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
102 core_route_stanza(origin, request); |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
103 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
104 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
105 local offline = offlinemanager.load(node, host); |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
106 if offline then |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
107 for _, msg in ipairs(offline) do |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
108 origin.send(msg); -- FIXME do we need to modify to/from in any way? |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
109 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
110 offlinemanager.deleteAll(node, host); |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
111 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
112 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
113 if stanza.attr.type == "unavailable" then |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
114 origin.presence = nil; |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
115 if origin.priority then |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
116 origin.priority = nil; |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
117 recalc_resource_map(origin); |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
118 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
119 if origin.directed then |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
120 for jid in pairs(origin.directed) do |
1009 | 121 stanza.attr.to = jid; |
122 core_route_stanza(origin, stanza); | |
123 end | |
1476
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
124 origin.directed = nil; |
1009 | 125 end |
1476
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
126 else |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
127 origin.presence = stanza; |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
128 local priority = stanza:child_with_name("priority"); |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
129 if priority and #priority > 0 then |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
130 priority = t_concat(priority); |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
131 if s_find(priority, "^[+-]?[0-9]+$") then |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
132 priority = tonumber(priority); |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
133 if priority < -128 then priority = -128 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
134 if priority > 127 then priority = 127 end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
135 else priority = 0; end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
136 else priority = 0; end |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
137 if origin.priority ~= priority then |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
138 origin.priority = priority; |
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
139 recalc_resource_map(origin); |
1009 | 140 end |
141 end | |
1476
5d6199a9b4f6
mod_presence: Prevented normal presence from non-interested resources from being dropped
Waqas Hussain <waqas20@gmail.com>
parents:
1475
diff
changeset
|
142 stanza.attr.to = nil; -- reset it |
1009 | 143 end |
144 | |
145 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza) | |
146 local h = hosts[host]; | |
147 local count = 0; | |
148 if h and h.type == "local" then | |
149 local u = h.sessions[user]; | |
150 if u then | |
151 for k, session in pairs(u.sessions) do | |
152 local pres = session.presence; | |
153 if pres then | |
154 pres.attr.to = jid; | |
155 core_route_stanza(session, pres); | |
156 pres.attr.to = nil; | |
157 count = count + 1; | |
158 end | |
159 end | |
160 end | |
161 end | |
1211
d60e68855176
mod_presence: Lower some log levels to their correct values
Matthew Wild <mwild1@gmail.com>
parents:
1210
diff
changeset
|
162 log("debug", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid); |
1009 | 163 return count; |
164 end | |
165 | |
166 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza) | |
167 local node, host = jid_split(from_bare); | |
168 local st_from, st_to = stanza.attr.from, stanza.attr.to; | |
169 stanza.attr.from, stanza.attr.to = from_bare, to_bare; | |
170 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare); | |
171 if stanza.attr.type == "subscribe" then | |
172 -- 1. route stanza | |
173 -- 2. roster push (subscription = none, ask = subscribe) | |
174 if rostermanager.set_contact_pending_out(node, host, to_bare) then | |
175 rostermanager.roster_push(node, host, to_bare); | |
176 end -- else file error | |
177 core_route_stanza(origin, stanza); | |
178 elseif stanza.attr.type == "unsubscribe" then | |
179 -- 1. route stanza | |
180 -- 2. roster push (subscription = none or from) | |
181 if rostermanager.unsubscribe(node, host, to_bare) then | |
182 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed? | |
183 end -- else file error | |
184 core_route_stanza(origin, stanza); | |
185 elseif stanza.attr.type == "subscribed" then | |
186 -- 1. route stanza | |
187 -- 2. roster_push () | |
188 -- 3. send_presence_of_available_resources | |
189 if rostermanager.subscribed(node, host, to_bare) then | |
190 rostermanager.roster_push(node, host, to_bare); | |
191 end | |
192 core_route_stanza(origin, stanza); | |
193 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza); | |
194 elseif stanza.attr.type == "unsubscribed" then | |
195 -- 1. route stanza | |
196 -- 2. roster push (subscription = none or to) | |
197 if rostermanager.unsubscribed(node, host, to_bare) then | |
198 rostermanager.roster_push(node, host, to_bare); | |
199 end | |
200 core_route_stanza(origin, stanza); | |
201 end | |
202 stanza.attr.from, stanza.attr.to = st_from, st_to; | |
203 end | |
204 | |
205 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza) | |
206 local node, host = jid_split(to_bare); | |
207 local st_from, st_to = stanza.attr.from, stanza.attr.to; | |
208 stanza.attr.from, stanza.attr.to = from_bare, to_bare; | |
209 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare); | |
210 if stanza.attr.type == "probe" then | |
211 if rostermanager.is_contact_subscribed(node, host, from_bare) then | |
212 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then | |
213 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) | |
214 end | |
215 else | |
216 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"})); | |
217 end | |
218 elseif stanza.attr.type == "subscribe" then | |
219 if rostermanager.is_contact_subscribed(node, host, from_bare) then | |
220 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed | |
221 -- Sending presence is not clearly stated in the RFC, but it seems appropriate | |
222 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then | |
223 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) | |
224 end | |
225 else | |
226 if not rostermanager.is_contact_pending_in(node, host, from_bare) then | |
227 if rostermanager.set_contact_pending_in(node, host, from_bare) then | |
228 sessionmanager.send_to_available_resources(node, host, stanza); | |
229 end -- TODO else return error, unable to save | |
230 end | |
231 end | |
232 elseif stanza.attr.type == "unsubscribe" then | |
233 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then | |
234 rostermanager.roster_push(node, host, from_bare); | |
235 end | |
236 elseif stanza.attr.type == "subscribed" then | |
237 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then | |
238 rostermanager.roster_push(node, host, from_bare); | |
239 end | |
240 elseif stanza.attr.type == "unsubscribed" then | |
241 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then | |
242 rostermanager.roster_push(node, host, from_bare); | |
243 end | |
244 end -- discard any other type | |
245 stanza.attr.from, stanza.attr.to = st_from, st_to; | |
246 end | |
247 | |
1279
fa00d56a9fd3
mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1278
diff
changeset
|
248 local outbound_presence_handler = function(data) |
1282
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
249 -- outbound presence recieved |
1276
d0e80c1578e1
mod_presence: Handle outbound presence to full JIDs
Waqas Hussain <waqas20@gmail.com>
parents:
1245
diff
changeset
|
250 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
|
251 |
1286
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
252 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
|
253 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
|
254 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
|
255 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
|
256 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
|
257 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
|
258 end |
1278
2abf85791f29
mod_presence: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1277
diff
changeset
|
259 |
1286
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
260 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
|
261 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
|
262 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
|
263 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
|
264 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
|
265 else |
a9b1675ad16e
mod_presence: Check for nil 'to' attribute in all cases for outgoing stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1285
diff
changeset
|
266 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
|
267 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
|
268 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
|
269 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
|
270 end |
fa00d56a9fd3
mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1278
diff
changeset
|
271 |
fa00d56a9fd3
mod_presence: Handle all outbound presence stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1278
diff
changeset
|
272 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
|
273 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
|
274 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
|
275 |
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
276 module:hook("presence/bare", function(data) |
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
277 -- inbound presence to bare JID recieved |
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
278 local origin, stanza = data.origin, data.stanza; |
1282
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
279 |
1283
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
280 local to = stanza.attr.to; |
1287
ac82c7b9c76b
mod_presence: Fix a global access
Waqas Hussain <waqas20@gmail.com>
parents:
1286
diff
changeset
|
281 local t = stanza.attr.type; |
1283
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
282 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
|
283 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
|
284 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
|
285 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
|
286 end |
0a6e2d6ae459
mod_presence: Check for nil 'to' attribute in all cases for incoming stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
1284
diff
changeset
|
287 |
1283
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
288 local user = bare_sessions[to]; |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
289 if user then |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
290 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
|
291 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
|
292 session.send(stanza); |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
293 end |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
294 end |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
295 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
|
296 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
|
297 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
|
298 end |
1284
c0fb8379696e
mod_presence: return true from incoming presence handlers to prevent further processing
Waqas Hussain <waqas20@gmail.com>
parents:
1283
diff
changeset
|
299 return true; |
1281
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
300 end); |
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
301 module:hook("presence/full", function(data) |
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
302 -- inbound presence to full JID recieved |
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
303 local origin, stanza = data.origin, data.stanza; |
1282
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
304 |
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
305 local t = stanza.attr.type; |
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
306 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
|
307 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
|
308 return true; |
ff58ef687a3f
mod_presence: Handle subscriptions and probes
Waqas Hussain <waqas20@gmail.com>
parents:
1281
diff
changeset
|
309 end |
1283
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
310 |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
311 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
|
312 if session then |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
313 -- TODO fire post processing event |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
314 session.send(stanza); |
2e57f2176612
mod_presence: Handle non-subscription presence and routing
Waqas Hussain <waqas20@gmail.com>
parents:
1282
diff
changeset
|
315 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
|
316 return true; |
1281
bc65d57c76ef
mod_presence: Add hooks for inbound presence
Waqas Hussain <waqas20@gmail.com>
parents:
1280
diff
changeset
|
317 end); |