Comparison

core/presencemanager.lua @ 468:ab49cb6d0e92

Moved incoming c2s presence handling from stanza_router to presencemanager
author Waqas Hussain <waqas20@gmail.com>
date Sat, 29 Nov 2008 08:25:34 +0500
parent 326:99a8317d1235
child 519:cccd610a0ef9
comparison
equal deleted inserted replaced
465:9ab51c483cf3 468:ab49cb6d0e92
1 1
2 local log = require "util.logger".init("presencemanager") 2 local log = require "util.logger".init("presencemanager")
3 3
4 local require = require; 4 local require = require;
5 local pairs = pairs; 5 local pairs, ipairs = pairs, ipairs;
6 local t_concat = table.concat;
7 local s_find = string.find;
8 local tonumber = tonumber;
6 9
7 local st = require "util.stanza"; 10 local st = require "util.stanza";
8 local jid_split = require "util.jid".split; 11 local jid_split = require "util.jid".split;
9 local hosts = hosts; 12 local hosts = hosts;
10 13
11 local rostermanager = require "core.rostermanager"; 14 local rostermanager = require "core.rostermanager";
12 local sessionmanager = require "core.sessionmanager"; 15 local sessionmanager = require "core.sessionmanager";
16 local offlinemanager = require "core.offlinemanager";
13 17
14 module "presencemanager" 18 module "presencemanager"
19
20 function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound)
21 local type = stanza.attr.type;
22 if type and type ~= "unavailable" then
23 if inbound then
24 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
25 else
26 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
27 end
28 elseif not inbound and not stanza.attr.to then
29 handle_normal_presence(origin, stanza, core_route_stanza);
30 else
31 core_route_stanza(origin, stanza);
32 end
33 end
34
35 function handle_normal_presence(origin, stanza, core_route_stanza)
36 if origin.roster then
37 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
38 local subscription = origin.roster[jid].subscription;
39 if subscription == "both" or subscription == "from" then
40 stanza.attr.to = jid;
41 core_route_stanza(origin, stanza);
42 end
43 end
44 local node, host = jid_split(stanza.attr.from);
45 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
46 if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence
47 stanza.attr.to = res.full_jid;
48 core_route_stanza(origin, stanza);
49 end
50 end
51 if stanza.attr.type == nil and not origin.presence then -- initial presence
52 local probe = st.presence({from = origin.full_jid, type = "probe"});
53 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
54 local subscription = origin.roster[jid].subscription;
55 if subscription == "both" or subscription == "to" then
56 probe.attr.to = jid;
57 core_route_stanza(origin, probe);
58 end
59 end
60 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
61 if res ~= origin and res.presence then
62 res.presence.attr.to = origin.full_jid;
63 core_route_stanza(res, res.presence);
64 res.presence.attr.to = nil;
65 end
66 end
67 if origin.roster.pending then -- resend incoming subscription requests
68 for jid in pairs(origin.roster.pending) do
69 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
70 end
71 end
72 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
73 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
74 if item.ask then
75 request.attr.to = jid;
76 core_route_stanza(origin, request);
77 end
78 end
79 for _, msg in ipairs(offlinemanager.load(node, host) or {}) do
80 origin.send(msg); -- FIXME do we need to modify to/from in any way?
81 end
82 offlinemanager.deleteAll(node, host);
83 end
84 origin.priority = 0;
85 if stanza.attr.type == "unavailable" then
86 origin.presence = nil;
87 else
88 origin.presence = stanza;
89 local priority = stanza:child_with_name("priority");
90 if priority and #priority > 0 then
91 priority = t_concat(priority);
92 if s_find(priority, "^[+-]?[0-9]+$") then
93 priority = tonumber(priority);
94 if priority < -128 then priority = -128 end
95 if priority > 127 then priority = 127 end
96 origin.priority = priority;
97 end
98 end
99 end
100 stanza.attr.to = nil; -- reset it
101 else
102 log("error", "presence recieved from client with no roster");
103 end
104 end
15 105
16 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza) 106 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)
17 local h = hosts[host]; 107 local h = hosts[host];
18 local count = 0; 108 local count = 0;
19 if h and h.type == "local" then 109 if h and h.type == "local" then