Annotate

core/presencemanager.lua @ 563:099d8a102deb

Add TLS socket to readlist before handshake starts, fixes major slow-down on TLS connections
author Matthew Wild <mwild1@gmail.com>
date Fri, 05 Dec 2008 19:24:01 +0000
parent 519:cccd610a0ef9
child 565:3a49d85cafbc
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
563
099d8a102deb Add TLS socket to readlist before handshake starts, fixes major slow-down on TLS connections
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
24 local tostring = tostring;
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local require = require;
468
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
26 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
27 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
28 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
29 local tonumber = tonumber;
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 local st = require "util.stanza";
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 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
33 local hosts = hosts;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 local rostermanager = require "core.rostermanager";
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 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
37 local offlinemanager = require "core.offlinemanager";
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 module "presencemanager"
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40
468
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
41 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
42 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
43 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
44 if inbound then
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
45 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
46 else
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
47 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
48 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
49 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
50 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
51 else
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
52 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
53 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
54 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
55
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
56 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
57 if origin.roster then
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
58 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
59 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
60 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
61 stanza.attr.to = jid;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
62 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
63 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
64 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
65 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
66 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
67 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
68 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
69 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
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 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
73 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
74 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
75 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
76 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
77 probe.attr.to = jid;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
78 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
79 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
80 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
81 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
82 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
83 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
84 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
85 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
86 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
87 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
88 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
89 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
90 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
91 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
92 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
93 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
94 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
95 if item.ask then
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
96 request.attr.to = jid;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
97 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
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 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
101 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
102 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
103 offlinemanager.deleteAll(node, host);
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 origin.priority = 0;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
106 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
107 origin.presence = nil;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
108 else
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
109 origin.presence = stanza;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
110 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
111 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
112 priority = t_concat(priority);
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
113 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
114 priority = tonumber(priority);
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
115 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
116 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
117 origin.priority = priority;
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 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
121 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
122 else
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
123 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
124 end
563
099d8a102deb Add TLS socket to readlist before handshake starts, fixes major slow-down on TLS connections
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
125
099d8a102deb Add TLS socket to readlist before handshake starts, fixes major slow-down on TLS connections
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
126 if origin.conntimetotal then
099d8a102deb Add TLS socket to readlist before handshake starts, fixes major slow-down on TLS connections
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
127 local session = origin;
099d8a102deb Add TLS socket to readlist before handshake starts, fixes major slow-down on TLS connections
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
128 origin.log("BLAH", "***********\n\n\n\n\n\n****************");
099d8a102deb Add TLS socket to readlist before handshake starts, fixes major slow-down on TLS connections
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
129 origin.send(st.stanza("message", { from = session.host, to=session.full_jid, type = "normal" }):body("Your login took "..tostring(session.conntimetotal).." seconds"));
099d8a102deb Add TLS socket to readlist before handshake starts, fixes major slow-down on TLS connections
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
130 origin.conntimetotal = nil;
099d8a102deb Add TLS socket to readlist before handshake starts, fixes major slow-down on TLS connections
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
131 end
099d8a102deb Add TLS socket to readlist before handshake starts, fixes major slow-down on TLS connections
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
132
468
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
133 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
134
322
d370fea67264 Fix for broadcasting presence of available resources to newly approved contact
Waqas Hussain <waqas20@gmail.com>
parents: 321
diff changeset
135 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
136 local h = hosts[host];
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 local count = 0;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 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
139 local u = h.sessions[user];
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 if u then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 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
142 local pres = session.presence;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 if pres then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 pres.attr.to = jid;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 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
146 core_route_stanza(session, pres);
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 pres.attr.to = nil;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 pres.attr.from = nil;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 count = count + 1;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 end
325
5de1048b3fbf Adding logging for outbound presence broadcasts (changed it a bit)
Waqas Hussain <waqas20@gmail.com>
parents: 324
diff changeset
154 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
155 return count;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 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
159 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
160 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
161 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
162 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
163 if stanza.attr.type == "subscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 -- 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
166 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
167 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
168 end -- else file error
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 core_route_stanza(origin, stanza);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 elseif stanza.attr.type == "unsubscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 -- 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
173 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
174 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
175 end -- else file error
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 core_route_stanza(origin, stanza);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 elseif stanza.attr.type == "subscribed" 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 ()
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 -- 3. send_presence_of_available_resources
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 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
182 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
183 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
184 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
185 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
186 elseif stanza.attr.type == "unsubscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 -- 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
189 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
190 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
191 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
192 core_route_stanza(origin, stanza);
321
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 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
195 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
196
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
197 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
198 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
199 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
200 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
201 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
202 if stanza.attr.type == "probe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
203 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
204 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
205 -- 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
206 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207 else
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208 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
209 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
210 elseif stanza.attr.type == "subscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
211 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
212 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
213 -- 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
214 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
215 -- 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
216 end
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
217 else
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
218 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
219 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
220 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
221 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
222 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
223 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
224 elseif stanza.attr.type == "unsubscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
225 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
226 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
227 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228 elseif stanza.attr.type == "subscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229 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
230 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
231 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
232 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
233 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
234 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
235 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
236 end -- discard any other type
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
237 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
238 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
239
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
240 return _M;