Annotate

plugins/mod_presence.lua @ 1019:8d750336e517

mod_presence: Fix incorrect internal routing for probes and subscriptions
author Waqas Hussain <waqas20@gmail.com>
date Tue, 21 Apr 2009 20:52:11 +0500
parent 1011:beb039827c9f
child 1023:c0a3ef0a1f34
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 -- Prosody IM v0.4
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 --
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 --
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local log = require "util.logger".init("mod_presence")
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local require = require;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local pairs, ipairs = pairs, ipairs;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local t_concat = table.concat;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 local s_find = string.find;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local tonumber = tonumber;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 local st = require "util.stanza";
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 local jid_split = require "util.jid".split;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 local jid_bare = require "util.jid".bare;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local hosts = hosts;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local rostermanager = require "core.rostermanager";
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local sessionmanager = require "core.sessionmanager";
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 local offlinemanager = require "core.offlinemanager";
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 local type = stanza.attr.type;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 if type and type ~= "unavailable" and type ~= "error" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 if inbound then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 elseif not inbound and not stanza.attr.to then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 handle_normal_presence(origin, stanza, core_route_stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 function handle_normal_presence(origin, stanza, core_route_stanza)
1019
8d750336e517 mod_presence: Fix incorrect internal routing for probes and subscriptions
Waqas Hussain <waqas20@gmail.com>
parents: 1011
diff changeset
44 error("hello world")
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 if origin.roster then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 local subscription = origin.roster[jid].subscription;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 if subscription == "both" or subscription == "from" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 stanza.attr.to = jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 local node, host = jid_split(stanza.attr.from);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
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
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 stanza.attr.to = res.full_jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 if stanza.attr.type == nil and not origin.presence then -- initial presence
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 local probe = st.presence({from = origin.full_jid, type = "probe"});
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 local subscription = origin.roster[jid].subscription;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 if subscription == "both" or subscription == "to" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 probe.attr.to = jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 core_route_stanza(origin, probe);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 if res ~= origin and res.presence then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 res.presence.attr.to = origin.full_jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 core_route_stanza(res, res.presence);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 res.presence.attr.to = nil;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 if origin.roster.pending then -- resend incoming subscription requests
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 for jid in pairs(origin.roster.pending) do
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 if item.ask then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 request.attr.to = jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 core_route_stanza(origin, request);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 local offline = offlinemanager.load(node, host);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 if offline then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 for _, msg in ipairs(offline) do
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 origin.send(msg); -- FIXME do we need to modify to/from in any way?
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 offlinemanager.deleteAll(node, host);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 origin.priority = 0;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 if stanza.attr.type == "unavailable" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 origin.presence = nil;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 if origin.directed then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 local old_from = stanza.attr.from;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 stanza.attr.from = origin.full_jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 for jid in pairs(origin.directed) do
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 stanza.attr.to = jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 stanza.attr.from = old_from;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 origin.directed = nil;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 origin.presence = stanza;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 local priority = stanza:child_with_name("priority");
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 if priority and #priority > 0 then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 priority = t_concat(priority);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 if s_find(priority, "^[+-]?[0-9]+$") then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 priority = tonumber(priority);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 if priority < -128 then priority = -128 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 if priority > 127 then priority = 127 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 origin.priority = priority;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 stanza.attr.to = nil; -- reset it
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 log("error", "presence recieved from client with no roster");
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 local h = hosts[host];
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 local count = 0;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 if h and h.type == "local" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 local u = h.sessions[user];
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 if u then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 for k, session in pairs(u.sessions) do
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 local pres = session.presence;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 if pres then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 pres.attr.to = jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 pres.attr.from = session.full_jid;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 core_route_stanza(session, pres);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 pres.attr.to = nil;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 pres.attr.from = nil;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 count = count + 1;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 log("info", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 return count;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 local node, host = jid_split(from_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 local st_from, st_to = stanza.attr.from, stanza.attr.to;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 if stanza.attr.type == "subscribe" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 -- 1. route stanza
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 -- 2. roster push (subscription = none, ask = subscribe)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 if rostermanager.set_contact_pending_out(node, host, to_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 rostermanager.roster_push(node, host, to_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 end -- else file error
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 elseif stanza.attr.type == "unsubscribe" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 -- 1. route stanza
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 -- 2. roster push (subscription = none or from)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 if rostermanager.unsubscribe(node, host, to_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 end -- else file error
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 elseif stanza.attr.type == "subscribed" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 -- 1. route stanza
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 -- 2. roster_push ()
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 -- 3. send_presence_of_available_resources
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
174 if rostermanager.subscribed(node, host, to_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 rostermanager.roster_push(node, host, to_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 elseif stanza.attr.type == "unsubscribed" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 -- 1. route stanza
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 -- 2. roster push (subscription = none or to)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182 if rostermanager.unsubscribed(node, host, to_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 rostermanager.roster_push(node, host, to_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187 stanza.attr.from, stanza.attr.to = st_from, st_to;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
189
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
190 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
191 local node, host = jid_split(to_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
192 local st_from, st_to = stanza.attr.from, stanza.attr.to;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 if stanza.attr.type == "probe" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
196 if rostermanager.is_contact_subscribed(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
197 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
200 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
201 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
203 elseif stanza.attr.type == "subscribe" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
204 if rostermanager.is_contact_subscribed(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
205 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
206 -- Sending presence is not clearly stated in the RFC, but it seems appropriate
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
209 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
210 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
211 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
212 if rostermanager.set_contact_pending_in(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
213 sessionmanager.send_to_available_resources(node, host, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
214 end -- TODO else return error, unable to save
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
217 elseif stanza.attr.type == "unsubscribe" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
218 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 rostermanager.roster_push(node, host, from_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
220 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
221 elseif stanza.attr.type == "subscribed" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
222 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
223 rostermanager.roster_push(node, host, from_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
224 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
225 elseif stanza.attr.type == "unsubscribed" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
226 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
227 rostermanager.roster_push(node, host, from_bare);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229 end -- discard any other type
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230 stanza.attr.from, stanza.attr.to = st_from, st_to;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
231 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
232
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
233 local function presence_handler(data)
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
234 local origin, stanza = data.origin, data.stanza;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
235 local to = stanza.attr.to;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
236 local node, host = jid_split(to);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
237 local to_bare = jid_bare(to);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
238 local from_bare = jid_bare(stanza.attr.from);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
239 if origin.type == "c2s" then
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
240 if to ~= nil and not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
241 origin.directed = origin.directed or {};
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
242 origin.directed[to] = true;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
243 end
1019
8d750336e517 mod_presence: Fix incorrect internal routing for probes and subscriptions
Waqas Hussain <waqas20@gmail.com>
parents: 1011
diff changeset
244 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
245 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
246 elseif not to then
1011
beb039827c9f Stopped using presencemanager in stanza_router
Waqas Hussain <waqas20@gmail.com>
parents: 1009
diff changeset
247 handle_normal_presence(origin, stanza, core_route_stanza);
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
248 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
249 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
250 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
251 elseif (origin.type == "s2sin" or origin.type == "component") and hosts[host] then
1019
8d750336e517 mod_presence: Fix incorrect internal routing for probes and subscriptions
Waqas Hussain <waqas20@gmail.com>
parents: 1011
diff changeset
252 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
1009
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
253 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
254 else
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
255 core_route_stanza(origin, stanza);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
256 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
257 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
258 end
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
259
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
260 local add_handler = require "core.eventmanager2".add_handler;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
261 local remove_handler = require "core.eventmanager2".remove_handler;
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
262
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
263 add_handler(module:get_host().."/presence", presence_handler);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
264 module.unload = function()
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
265 remove_handler(module:get_host().."/presence", presence_handler);
a2dd83bc3afa mod_presence: initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
266 end