Annotate

core/presencemanager.lua @ 1244:3df7417225fe

stanza_router: Changed to use the prosody.events object directly, rather than through eventmanager2
author Waqas Hussain <waqas20@gmail.com>
date Sun, 31 May 2009 00:43:24 +0500
parent 1074:7798735be42b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
896
2c0b9e3c11c3 0.3->0.4
Matthew Wild <mwild1@gmail.com>
parents: 873
diff changeset
1 -- Prosody IM v0.4
760
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 649
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 649
diff changeset
6 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
7 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 468
diff changeset
9
873
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
10
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
11 local log = require "util.logger".init("presencemanager")
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
12
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
13 local require = require;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
14 local pairs, ipairs = pairs, ipairs;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
15 local t_concat = table.concat;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
16 local s_find = string.find;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
17 local tonumber = tonumber;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
18
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
19 local st = require "util.stanza";
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
20 local jid_split = require "util.jid".split;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
21 local hosts = hosts;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
22
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
23 local rostermanager = require "core.rostermanager";
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
24 local sessionmanager = require "core.sessionmanager";
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
25 local offlinemanager = require "core.offlinemanager";
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
26
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
27 module "presencemanager"
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
28
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
29 function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound)
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
30 local type = stanza.attr.type;
966
d2692d427496 Fixed: Issue with presence error routing
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
31 if type and type ~= "unavailable" and type ~= "error" then
873
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
32 if inbound then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
33 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
34 else
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
35 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
36 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
37 elseif not inbound and not stanza.attr.to then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
38 handle_normal_presence(origin, stanza, core_route_stanza);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
39 else
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
40 core_route_stanza(origin, stanza);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
41 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
42 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
43
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
44 function handle_normal_presence(origin, stanza, core_route_stanza)
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
45 if origin.roster then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
46 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
47 local subscription = origin.roster[jid].subscription;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
48 if subscription == "both" or subscription == "from" then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
49 stanza.attr.to = jid;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
50 core_route_stanza(origin, stanza);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
51 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
52 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
53 local node, host = jid_split(stanza.attr.from);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
54 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
55 if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
56 stanza.attr.to = res.full_jid;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
57 core_route_stanza(origin, stanza);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
58 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
59 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
60 if stanza.attr.type == nil and not origin.presence then -- initial presence
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
61 local probe = st.presence({from = origin.full_jid, type = "probe"});
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
62 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
63 local subscription = origin.roster[jid].subscription;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
64 if subscription == "both" or subscription == "to" then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
65 probe.attr.to = jid;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
66 core_route_stanza(origin, probe);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
67 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
68 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
69 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
70 if res ~= origin and res.presence then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
71 res.presence.attr.to = origin.full_jid;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
72 core_route_stanza(res, res.presence);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
73 res.presence.attr.to = nil;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
74 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
75 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
76 if origin.roster.pending then -- resend incoming subscription requests
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
77 for jid in pairs(origin.roster.pending) do
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
78 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
79 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
80 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
81 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
82 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
83 if item.ask then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
84 request.attr.to = jid;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
85 core_route_stanza(origin, request);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
86 end
649
5f8382768530 Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents: 641
diff changeset
87 end
5f8382768530 Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents: 641
diff changeset
88 local offline = offlinemanager.load(node, host);
873
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
89 if offline then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
90 for _, msg in ipairs(offline) do
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
91 origin.send(msg); -- FIXME do we need to modify to/from in any way?
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
92 end
649
5f8382768530 Delete the offline message store only when offline messages are present
Waqas Hussain <waqas20@gmail.com>
parents: 641
diff changeset
93 offlinemanager.deleteAll(node, host);
873
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
94 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
95 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
96 origin.priority = 0;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
97 if stanza.attr.type == "unavailable" then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
98 origin.presence = nil;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
99 if origin.directed then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
100 local old_from = stanza.attr.from;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
101 stanza.attr.from = origin.full_jid;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
102 for jid in pairs(origin.directed) do
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
103 stanza.attr.to = jid;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
104 core_route_stanza(origin, stanza);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
105 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
106 stanza.attr.from = old_from;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
107 origin.directed = nil;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
108 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
109 else
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
110 origin.presence = stanza;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
111 local priority = stanza:child_with_name("priority");
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
112 if priority and #priority > 0 then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
113 priority = t_concat(priority);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
114 if s_find(priority, "^[+-]?[0-9]+$") then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
115 priority = tonumber(priority);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
116 if priority < -128 then priority = -128 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
117 if priority > 127 then priority = 127 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
118 origin.priority = priority;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
119 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
120 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
121 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
122 stanza.attr.to = nil; -- reset it
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
123 else
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
124 log("error", "presence recieved from client with no roster");
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
125 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
126 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
127
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
128 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
129 local h = hosts[host];
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
130 local count = 0;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
131 if h and h.type == "local" then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
132 local u = h.sessions[user];
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
133 if u then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
134 for k, session in pairs(u.sessions) do
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
135 local pres = session.presence;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
136 if pres then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
137 pres.attr.to = jid;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
138 pres.attr.from = session.full_jid;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
139 core_route_stanza(session, pres);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
140 pres.attr.to = nil;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
141 pres.attr.from = nil;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
142 count = count + 1;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
143 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
144 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
145 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
146 end
1074
7798735be42b presencemanager: Lower log level of a message
Matthew Wild <mwild1@gmail.com>
parents: 966
diff changeset
147 log("debug", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
873
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
148 return count;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
149 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
150
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
151 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
152 local node, host = jid_split(from_bare);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
153 local st_from, st_to = stanza.attr.from, stanza.attr.to;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
154 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
155 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
156 if stanza.attr.type == "subscribe" then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
157 -- 1. route stanza
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
158 -- 2. roster push (subscription = none, ask = subscribe)
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
159 if rostermanager.set_contact_pending_out(node, host, to_bare) then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
160 rostermanager.roster_push(node, host, to_bare);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
161 end -- else file error
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
162 core_route_stanza(origin, stanza);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
163 elseif stanza.attr.type == "unsubscribe" then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
164 -- 1. route stanza
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
165 -- 2. roster push (subscription = none or from)
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
166 if rostermanager.unsubscribe(node, host, to_bare) then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
167 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
168 end -- else file error
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
169 core_route_stanza(origin, stanza);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
170 elseif stanza.attr.type == "subscribed" then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
171 -- 1. route stanza
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
172 -- 2. roster_push ()
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
173 -- 3. send_presence_of_available_resources
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
174 if rostermanager.subscribed(node, host, to_bare) then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
175 rostermanager.roster_push(node, host, to_bare);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
176 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
177 core_route_stanza(origin, stanza);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
178 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
179 elseif stanza.attr.type == "unsubscribed" then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
180 -- 1. route stanza
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
181 -- 2. roster push (subscription = none or to)
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
182 if rostermanager.unsubscribed(node, host, to_bare) then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
183 rostermanager.roster_push(node, host, to_bare);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
184 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
185 core_route_stanza(origin, stanza);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
186 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
187 stanza.attr.from, stanza.attr.to = st_from, st_to;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
188 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
189
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
190 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
191 local node, host = jid_split(to_bare);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
192 local st_from, st_to = stanza.attr.from, stanza.attr.to;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
193 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
194 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
195 if stanza.attr.type == "probe" then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
196 if rostermanager.is_contact_subscribed(node, host, from_bare) then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
197 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
198 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
199 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
200 else
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
201 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
202 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
203 elseif stanza.attr.type == "subscribe" then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
204 if rostermanager.is_contact_subscribed(node, host, from_bare) then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
205 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
206 -- Sending presence is not clearly stated in the RFC, but it seems appropriate
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
207 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
208 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
209 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
210 else
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
211 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
212 if rostermanager.set_contact_pending_in(node, host, from_bare) then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
213 sessionmanager.send_to_available_resources(node, host, stanza);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
214 end -- TODO else return error, unable to save
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
215 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
216 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
217 elseif stanza.attr.type == "unsubscribe" then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
218 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
219 rostermanager.roster_push(node, host, from_bare);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
220 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
221 elseif stanza.attr.type == "subscribed" then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
222 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
223 rostermanager.roster_push(node, host, from_bare);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
224 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
225 elseif stanza.attr.type == "unsubscribed" then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
226 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
227 rostermanager.roster_push(node, host, from_bare);
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
228 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
229 end -- discard any other type
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
230 stanza.attr.from, stanza.attr.to = st_from, st_to;
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
231 end
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
232
e7ecde5d50e3 core.presencemanager: Convert Windows line endings to UNIX
Waqas Hussain <waqas20@gmail.com>
parents: 826
diff changeset
233 return _M;