Annotate

core/presencemanager.lua @ 649:5f8382768530

Delete the offline message store only when offline messages are present
author Waqas Hussain <waqas20@gmail.com>
date Fri, 26 Dec 2008 21:48:08 +0500
parent 641:658a2de74afc
child 758:b1885732e979
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
615
4ae3e81513f3 0.1 -> 0.2
Matthew Wild <mwild1@gmail.com>
parents: 565
diff changeset
1 -- Prosody IM v0.2
519
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
649
5f8382768530 Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents: 641
diff changeset
98 end
5f8382768530 Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents: 641
diff changeset
99 local offline = offlinemanager.load(node, host);
5f8382768530 Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents: 641
diff changeset
100 if offline then
5f8382768530 Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents: 641
diff changeset
101 for _, msg in ipairs(offline) do
5f8382768530 Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents: 641
diff changeset
102 origin.send(msg); -- FIXME do we need to modify to/from in any way?
5f8382768530 Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents: 641
diff changeset
103 end
5f8382768530 Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents: 641
diff changeset
104 offlinemanager.deleteAll(node, host);
468
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
105 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
106 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
107 origin.priority = 0;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
108 if stanza.attr.type == "unavailable" then
641
658a2de74afc Directed presence
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
109 origin.presence = nil;
658a2de74afc Directed presence
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
110 if origin.directed then
658a2de74afc Directed presence
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
111 for _, jid in ipairs(origin.directed) do
658a2de74afc Directed presence
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
112 stanza.attr.to = jid;
658a2de74afc Directed presence
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
113 core_route_stanza(origin, stanza);
658a2de74afc Directed presence
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
114 end
658a2de74afc Directed presence
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
115 origin.directed = nil;
658a2de74afc Directed presence
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
116 end
468
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
117 else
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
118 origin.presence = stanza;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
119 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
120 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
121 priority = t_concat(priority);
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
122 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
123 priority = tonumber(priority);
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
124 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
125 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
126 origin.priority = priority;
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
127 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
128 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
129 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
130 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
131 else
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
132 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
133 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
134 end
ab49cb6d0e92 Moved incoming c2s presence handling from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents: 326
diff changeset
135
322
d370fea67264 Fix for broadcasting presence of available resources to newly approved contact
Waqas Hussain <waqas20@gmail.com>
parents: 321
diff changeset
136 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
137 local h = hosts[host];
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 local count = 0;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 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
140 local u = h.sessions[user];
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 if u then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 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
143 local pres = session.presence;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 if pres then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 pres.attr.to = jid;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 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
147 core_route_stanza(session, pres);
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 pres.attr.to = nil;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 pres.attr.from = nil;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 count = count + 1;
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
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 end
325
5de1048b3fbf Adding logging for outbound presence broadcasts (changed it a bit)
Waqas Hussain <waqas20@gmail.com>
parents: 324
diff changeset
155 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
156 return count;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 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
160 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
161 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
162 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
163 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
164 if stanza.attr.type == "subscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 -- 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
167 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
168 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
169 end -- else file error
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 core_route_stanza(origin, stanza);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 elseif stanza.attr.type == "unsubscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 -- 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
174 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
175 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
176 end -- else file error
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 core_route_stanza(origin, stanza);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 elseif stanza.attr.type == "subscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 -- 2. roster_push ()
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 -- 3. send_presence_of_available_resources
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182 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
183 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
184 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
185 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
186 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
187 elseif stanza.attr.type == "unsubscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
189 -- 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
190 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
191 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
192 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
193 core_route_stanza(origin, stanza);
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 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
196 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
197
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198 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
199 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
200 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
201 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
202 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
203 if stanza.attr.type == "probe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
204 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
205 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
206 -- 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
207 end
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 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
210 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
211 elseif stanza.attr.type == "subscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
212 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
213 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
214 -- 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
215 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
216 -- 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
217 end
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
218 else
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 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
220 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
221 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
222 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
223 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
224 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
225 elseif stanza.attr.type == "unsubscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
226 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
227 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
228 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229 elseif stanza.attr.type == "subscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230 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
231 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
232 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
233 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
234 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
235 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
236 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
237 end -- discard any other type
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
238 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
239 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
240
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
241 return _M;