Annotate

core/presencemanager.lua @ 531:724fd647dc56

Quick fix for an issue that needs more looking into
author Matthew Wild <mwild1@gmail.com>
date Wed, 03 Dec 2008 18:01:56 +0000
parent 519:cccd610a0ef9
child 563:099d8a102deb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
1 -- Prosody IM v0.1
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
2 -- Copyright (C) 2008 Matthew Wild
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
3 -- Copyright (C) 2008 Waqas Hussain
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
4 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
5 -- This program is free software; you can redistribute it and/or
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
6 -- modify it under the terms of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
7 -- as published by the Free Software Foundation; either version 2
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
8 -- of the License, or (at your option) any later version.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
9 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
10 -- This program is distributed in the hope that it will be useful,
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
13 -- GNU General Public License for more details.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
14 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
15 -- You should have received a copy of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
16 -- along with this program; if not, write to the Free Software
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
18 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
19
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
20
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 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
23
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local require = require;
468
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
25 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
26 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
27 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
28 local tonumber = tonumber;
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 local st = require "util.stanza";
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 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
32 local hosts = hosts;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 local rostermanager = require "core.rostermanager";
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 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
36 local offlinemanager = require "core.offlinemanager";
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 module "presencemanager"
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39
468
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
40 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
41 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
42 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
43 if inbound then
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
44 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
45 else
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
46 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
47 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
48 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
49 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
50 else
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
51 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
52 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
53 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
54
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
55 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
56 if origin.roster then
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
57 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
58 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
59 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
60 stanza.attr.to = jid;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
61 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
62 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
63 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
64 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
65 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
66 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
67 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
68 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
69 end
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 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
72 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
73 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
74 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
75 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
76 probe.attr.to = jid;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
77 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
78 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
79 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
80 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
81 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
82 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
83 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
84 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
85 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
86 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
87 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
88 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
89 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
90 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
91 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
92 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
93 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
94 if item.ask then
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
95 request.attr.to = jid;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
96 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
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 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
100 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
101 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
102 offlinemanager.deleteAll(node, host);
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 origin.priority = 0;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
105 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
106 origin.presence = nil;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
107 else
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
108 origin.presence = stanza;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
109 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
110 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
111 priority = t_concat(priority);
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
112 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
113 priority = tonumber(priority);
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
114 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
115 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
116 origin.priority = priority;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
117 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
118 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
119 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
120 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
121 else
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
122 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
123 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
124 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
125
322
d370fea67264 Fix for broadcasting presence of available resources to newly approved contact
Waqas Hussain <waqas20@gmail.com>
parents: 321
diff changeset
126 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
127 local h = hosts[host];
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 local count = 0;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 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
130 local u = h.sessions[user];
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 if u then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 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
133 local pres = session.presence;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 if pres then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 pres.attr.to = jid;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 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
137 core_route_stanza(session, pres);
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 pres.attr.to = nil;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 pres.attr.from = nil;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 count = count + 1;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 end
325
5de1048b3fbf Adding logging for outbound presence broadcasts (changed it a bit)
Waqas Hussain <waqas20@gmail.com>
parents: 324
diff changeset
145 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
146 return count;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 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
150 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
151 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
152 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
153 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
154 if stanza.attr.type == "subscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 -- 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
157 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
158 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
159 end -- else file error
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 core_route_stanza(origin, stanza);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 elseif stanza.attr.type == "unsubscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 -- 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
164 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
165 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
166 end -- else file error
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 core_route_stanza(origin, stanza);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 elseif stanza.attr.type == "subscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 -- 2. roster_push ()
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 -- 3. send_presence_of_available_resources
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 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
173 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
174 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
175 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
176 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
177 elseif stanza.attr.type == "unsubscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 -- 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
180 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
181 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
182 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
183 core_route_stanza(origin, stanza);
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 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
186 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 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
189 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
190 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
191 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
192 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
193 if stanza.attr.type == "probe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 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
195 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
196 -- 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
197 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198 else
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 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
200 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
201 elseif stanza.attr.type == "subscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 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
203 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
204 -- 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
205 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
206 -- 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
207 end
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208 else
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
209 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
210 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
211 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
212 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
213 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
214 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215 elseif stanza.attr.type == "unsubscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 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
217 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
218 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 elseif stanza.attr.type == "subscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
220 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
221 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
222 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
223 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
224 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
225 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
226 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
227 end -- discard any other type
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228 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
229 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
231 return _M;