Annotate

core/stanza_router.lua @ 140:a0fc73d5f48c

Backed out changeset 8f17ba74823c
author Matthew Wild <mwild1@gmail.com>
date Thu, 23 Oct 2008 18:39:26 +0100
parent 139:8f17ba74823c
child 141:37b3e9ed8918
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
140
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
1 <<<<<<< local
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
2
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
3 -- The code in this file should be self-explanatory, though the logic is horrible
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
4 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
5 -- the rules from the RFCs (mainly 3921)
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
6
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
7 require "core.servermanager"
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
8
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
9 local log = require "util.logger".init("stanzarouter")
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
10
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
11 local st = require "util.stanza";
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
12 local send = require "core.sessionmanager".send_to_session;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
13 -- local send_s2s = require "core.s2smanager".send_to_host;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
14 local user_exists = require "core.usermanager".user_exists;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
15
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
16 local jid_split = require "util.jid".split;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
17 local print, debug = print, debug;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
18
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
19 function core_process_stanza(origin, stanza)
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
20 log("debug", "Received: "..tostring(stanza))
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
21 -- TODO verify validity of stanza (as well as JID validity)
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
22 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
23 if stanza.attr.type == "set" or stanza.attr.type == "get" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
24 error("Invalid IQ");
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
25 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
26 error("Invalid IQ");
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
27 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
28 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
29
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
30 if origin.type == "c2s" and not origin.full_jid
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
31 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
32 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
33 error("Client MUST bind resource after auth");
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
34 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
35
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
36 local to = stanza.attr.to;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
37 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s)
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
38 -- TODO also, stazas should be returned to their original state before the function ends
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
39
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
40 -- TODO presence subscriptions
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
41 if not to then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
42 core_handle_stanza(origin, stanza);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
43 elseif hosts[to] and hosts[to].type == "local" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
44 core_handle_stanza(origin, stanza);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
45 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
46 core_handle_stanza(origin, stanza);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
47 elseif origin.type == "c2s" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
48 core_route_stanza(origin, stanza);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
49 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
50 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
51
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
52 -- This function handles stanzas which are not routed any further,
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
53 -- that is, they are handled by this server
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
54 function core_handle_stanza(origin, stanza)
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
55 -- Handlers
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
56 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
57 local session = origin;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
58
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
59 if stanza.name == "presence" and origin.roster then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
60 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
61 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
62 local subscription = origin.roster[jid].subscription;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
63 if subscription == "both" or subscription == "from" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
64 stanza.attr.to = jid;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
65 core_route_stanza(origin, stanza);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
66 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
67 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
68 local node, host = jid_split(stanza.attr.from);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
69 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources and from resources
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
70 if res ~= origin then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
71 if res.full_jid then -- to resource. FIXME is this check correct? Maybe it should be res.presence
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
72 stanza.attr.to = res.full_jid;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
73 core_route_stanza(origin, stanza);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
74 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
75 if res.presence then -- from all resources for which we have presence
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
76 res.presence.attr.to = origin.full_jid;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
77 core_route_stanza(res, res.presence);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
78 res.presence.attr.to = nil;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
79 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
80 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
81 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
82 if not origin.presence then -- presence probes on initial presence
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
83 local probe = st.presence({from = origin.full_jid, type = "probe"});
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
84 for jid in pairs(origin.roster) do
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
85 local subscription = origin.roster[jid].subscription;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
86 if subscription == "both" or subscription == "to" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
87 probe.attr.to = jid;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
88 core_route_stanza(origin, probe);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
89 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
90 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
91 -- TODO resend subscription requests
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
92 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
93 origin.presence = stanza;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
94 stanza.attr.to = nil; -- reset it
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
95 else
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
96 -- TODO error, bad type
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
97 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
98 else
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
99 log("debug", "Routing stanza to local");
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
100 handle_stanza(session, stanza);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
101 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
102 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
103 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
104
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
105 function is_authorized_to_see_presence(origin, username, host)
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
106 local roster = datamanager.load(username, host, "roster") or {};
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
107 local item = roster[origin.username.."@"..origin.host];
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
108 return item and (item.subscription == "both" or item.subscription == "from");
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
109 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
110
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
111 function core_route_stanza(origin, stanza)
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
112 -- Hooks
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
113 --- ...later
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
114
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
115 -- Deliver
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
116 local to = stanza.attr.to;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
117 local node, host, resource = jid_split(to);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
118
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
119 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
120
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
121 local host_session = hosts[host]
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
122 if host_session and host_session.type == "local" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
123 -- Local host
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
124 local user = host_session.sessions[node];
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
125 if user then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
126 local res = user.sessions[resource];
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
127 if not res then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
128 -- if we get here, resource was not specified or was unavailable
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
129 if stanza.name == "presence" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
130 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
131 if stanza.attr.type == "probe" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
132 if is_authorized_to_see_presence(origin, node, host) then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
133 for k in pairs(user.sessions) do -- return presence for all resources
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
134 if user.sessions[k].presence then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
135 local pres = user.sessions[k].presence;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
136 pres.attr.to = origin.full_jid;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
137 pres.attr.from = user.sessions[k].full_jid;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
138 send(origin, pres);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
139 pres.attr.to = nil;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
140 pres.attr.from = nil;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
141 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
142 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
143 else
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
144 send(origin, st.presence({from=user.."@"..host, to=origin.username.."@"..origin.host, type="unsubscribed"}));
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
145 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
146 elseif stanza.attr.type == "subscribe" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
147 -- TODO
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
148 elseif stanza.attr.type == "unsubscribe" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
149 -- TODO
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
150 elseif stanza.attr.type == "subscribed" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
151 -- TODO
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
152 elseif stanza.attr.type == "unsubscribed" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
153 -- TODO
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
154 end -- discard any other type
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
155 else -- sender is available or unavailable
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
156 for k in pairs(user.sessions) do -- presence broadcast to all user resources
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
157 if user.sessions[k].full_jid then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
158 stanza.attr.to = user.sessions[k].full_jid;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
159 send(user.sessions[k], stanza);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
160 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
161 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
162 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
163 elseif stanza.name == "message" then -- select a resource to recieve message
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
164 for k in pairs(user.sessions) do
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
165 if user.sessions[k].full_jid then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
166 res = user.sessions[k];
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
167 break;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
168 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
169 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
170 -- TODO find resource with greatest priority
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
171 send(res, stanza);
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
172 else
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
173 -- TODO send IQ error
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
174 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
175 else
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
176 -- User + resource is online...
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
177 stanza.attr.to = res.full_jid;
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
178 send(res, stanza); -- Yay \o/
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
179 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
180 else
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
181 -- user not online
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
182 if user_exists(node, host) then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
183 if stanza.name == "presence" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
184 if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s?
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
185 -- TODO send last recieved unavailable presence
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
186 else
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
187 -- TODO send unavailable presence
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
188 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
189 elseif stanza.name == "message" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
190 -- TODO send message error, or store offline messages
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
191 elseif stanza.name == "iq" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
192 -- TODO send IQ error
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
193 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
194 else -- user does not exist
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
195 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
196 if stanza.name == "presence" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
197 if stanza.attr.type == "probe" then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
198 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
199 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
200 -- else ignore
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
201 else
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
202 send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
203 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
204 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
205 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
206 else
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
207 -- Remote host
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
208 if host_session then
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
209 -- Send to session
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
210 else
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
211 -- Need to establish the connection
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
212 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
213 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
214 stanza.attr.to = to; -- reset
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
215 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
216
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
217 function handle_stanza_toremote(stanza)
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
218 log("error", "Stanza bound for remote host, but s2s is not implemented");
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
219 end
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
220 =======
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 -- The code in this file should be self-explanatory, though the logic is horrible
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 -- the rules from the RFCs (mainly 3921)
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 require "core.servermanager"
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 local log = require "util.logger".init("stanzarouter")
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229
71
fbb4ef1da82e Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents: 68
diff changeset
230 local st = require "util.stanza";
fbb4ef1da82e Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents: 68
diff changeset
231 local send = require "core.sessionmanager".send_to_session;
122
21f8d2175393 No s2s yet :)
Matthew Wild <mwild1@gmail.com>
parents: 121
diff changeset
232 -- local send_s2s = require "core.s2smanager".send_to_host;
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
233 local user_exists = require "core.usermanager".user_exists;
71
fbb4ef1da82e Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents: 68
diff changeset
234
105
b099f0f80775 Removed an unnecessary line
Waqas Hussain <waqas20@gmail.com>
parents: 83
diff changeset
235 local jid_split = require "util.jid".split;
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
236 local print = print;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 function core_process_stanza(origin, stanza)
66
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 31
diff changeset
239 log("debug", "Received: "..tostring(stanza))
73
937448005121 Added to a comment
Waqas Hussain <waqas20@gmail.com>
parents: 72
diff changeset
240 -- TODO verify validity of stanza (as well as JID validity)
83
79608fc8f98d Fixed routing for IQs to bare JIDs, and added a simple IQ validity check
Waqas Hussain <waqas20@gmail.com>
parents: 78
diff changeset
241 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
242 if stanza.attr.type == "set" or stanza.attr.type == "get" then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
243 error("Invalid IQ");
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
244 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
245 error("Invalid IQ");
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
246 end
83
79608fc8f98d Fixed routing for IQs to bare JIDs, and added a simple IQ validity check
Waqas Hussain <waqas20@gmail.com>
parents: 78
diff changeset
247 end
78
972e31cc91e8 Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents: 73
diff changeset
248
972e31cc91e8 Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents: 73
diff changeset
249 if origin.type == "c2s" and not origin.full_jid
83
79608fc8f98d Fixed routing for IQs to bare JIDs, and added a simple IQ validity check
Waqas Hussain <waqas20@gmail.com>
parents: 78
diff changeset
250 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
78
972e31cc91e8 Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents: 73
diff changeset
251 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
972e31cc91e8 Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents: 73
diff changeset
252 error("Client MUST bind resource after auth");
972e31cc91e8 Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents: 73
diff changeset
253 end
972e31cc91e8 Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents: 73
diff changeset
254
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 local to = stanza.attr.to;
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
256 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s)
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
257 -- TODO also, stazas should be returned to their original state before the function ends
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
259 -- TODO presence subscriptions
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
260 if not to then
119
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
261 core_handle_stanza(origin, stanza);
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
262 elseif hosts[to] and hosts[to].type == "local" then
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
263 core_handle_stanza(origin, stanza);
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
264 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
265 core_handle_stanza(origin, stanza);
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
266 elseif origin.type == "c2s" then
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
267 core_route_stanza(origin, stanza);
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
268 end
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
269 end
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
270
121
74e5919e4737 Added a comment, removed all the old code
Matthew Wild <mwild1@gmail.com>
parents: 119
diff changeset
271 -- This function handles stanzas which are not routed any further,
74e5919e4737 Added a comment, removed all the old code
Matthew Wild <mwild1@gmail.com>
parents: 119
diff changeset
272 -- that is, they are handled by this server
119
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
273 function core_handle_stanza(origin, stanza)
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
274 -- Handlers
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
275 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
276 local session = origin;
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
277
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
278 if stanza.name == "presence" and origin.roster then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
279 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
280 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
281 local subscription = origin.roster[jid].subscription;
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
282 if subscription == "both" or subscription == "from" then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
283 stanza.attr.to = jid;
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
284 core_route_stanza(origin, stanza);
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
285 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
286 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
287 --[[local node, host = jid_split(stanza.attr.from);
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
288 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
289 if res.full_jid then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
290 res = user.sessions[k];
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
291 break;
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
292 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
293 end]]
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
294 if not origin.presence then -- presence probes on initial presence
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
295 local probe = st.presence({from = origin.full_jid, type = "probe"});
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
296 for jid in pairs(origin.roster) do
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
297 local subscription = origin.roster[jid].subscription;
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
298 if subscription == "both" or subscription == "to" then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
299 probe.attr.to = jid;
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
300 core_route_stanza(origin, probe);
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
301 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
302 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
303 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
304 origin.presence = stanza;
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
305 stanza.attr.to = nil; -- reset it
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
306 else
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
307 -- TODO error, bad type
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
308 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
309 else
119
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
310 log("debug", "Routing stanza to local");
b48a573608e8 Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents: 113
diff changeset
311 handle_stanza(session, stanza);
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
312 end
71
fbb4ef1da82e Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents: 68
diff changeset
313 end
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315
140
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
316 -- TODO: Does this function belong here?
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
317 function is_authorized_to_see_presence(origin, username, host)
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
318 local roster = datamanager.load(username, host, "roster") or {};
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
319 local item = roster[origin.username.."@"..origin.host];
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
320 return item and (item.subscription == "both" or item.subscription == "from");
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
321 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
322
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323 function core_route_stanza(origin, stanza)
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 -- Hooks
68
ceb7a55676a4 Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
325 --- ...later
ceb7a55676a4 Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
326
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 -- Deliver
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
328 local to = stanza.attr.to;
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
329 local node, host, resource = jid_split(to);
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
330
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
331 if stanza.name == "presence" and stanza.attr.type == "probe" then resource = nil; end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
332
68
ceb7a55676a4 Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
333 local host_session = hosts[host]
ceb7a55676a4 Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
334 if host_session and host_session.type == "local" then
ceb7a55676a4 Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
335 -- Local host
71
fbb4ef1da82e Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents: 68
diff changeset
336 local user = host_session.sessions[node];
fbb4ef1da82e Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents: 68
diff changeset
337 if user then
72
e78db62beebc Code cleanup
Waqas Hussain <waqas20@gmail.com>
parents: 71
diff changeset
338 local res = user.sessions[resource];
71
fbb4ef1da82e Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents: 68
diff changeset
339 if not res then
fbb4ef1da82e Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents: 68
diff changeset
340 -- if we get here, resource was not specified or was unavailable
106
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
341 if stanza.name == "presence" then
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
342 if stanza.attr.type == "probe" then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
343 if is_authorized_to_see_presence(origin, node, host) then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
344 for k in pairs(user.sessions) do -- return presence for all resources
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
345 if user.sessions[k].presence then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
346 local pres = user.sessions[k].presence;
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
347 pres.attr.to = origin.full_jid;
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
348 pres.attr.from = user.sessions[k].full_jid;
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
349 send(origin, pres);
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
350 pres.attr.to = nil;
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
351 pres.attr.from = nil;
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
352 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
353 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
354 else
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
355 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
356 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
357 else
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
358 for k in pairs(user.sessions) do -- presence broadcast to all user resources
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
359 if user.sessions[k].full_jid then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
360 stanza.attr.to = user.sessions[k].full_jid;
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
361 send(user.sessions[k], stanza);
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
362 end
106
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
363 end
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
364 end
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
365 elseif stanza.name == "message" then -- select a resource to recieve message
106
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
366 for k in pairs(user.sessions) do
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
367 if user.sessions[k].full_jid then
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
368 res = user.sessions[k];
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
369 break;
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
370 end
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
371 end
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
372 -- TODO find resource with greatest priority
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
373 send(res, stanza);
106
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
374 else
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
375 -- TODO send IQ error
71
fbb4ef1da82e Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents: 68
diff changeset
376 end
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
377 else
121
74e5919e4737 Added a comment, removed all the old code
Matthew Wild <mwild1@gmail.com>
parents: 119
diff changeset
378 -- User + resource is online...
106
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
379 stanza.attr.to = res.full_jid;
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
380 send(res, stanza); -- Yay \o/
f2a3d204a76a Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents: 105
diff changeset
381 end
71
fbb4ef1da82e Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents: 68
diff changeset
382 else
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
383 -- user not online
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
384 if user_exists(node, host) then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
385 if stanza.name == "presence" then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
386 if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s?
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
387 -- TODO send last recieved unavailable presence
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
388 else
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
389 -- TODO send unavailable presence
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
390 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
391 elseif stanza.name == "message" then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
392 -- TODO send message error, or store offline messages
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
393 elseif stanza.name == "iq" then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
394 -- TODO send IQ error
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
395 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
396 else -- user does not exist
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
397 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
398 if stanza.name == "presence" then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
399 if stanza.attr.type == "probe" then
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
400 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
401 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
402 -- else ignore
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
403 else
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
404 send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
405 end
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
406 end
71
fbb4ef1da82e Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents: 68
diff changeset
407 end
68
ceb7a55676a4 Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
408 else
ceb7a55676a4 Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
409 -- Remote host
140
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
410 send_s2s(origin.host, host, stanza);
68
ceb7a55676a4 Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
411 end
113
9026fdad1531 Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents: 106
diff changeset
412 stanza.attr.to = to; -- reset
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
413 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
414
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
415 function handle_stanza_toremote(stanza)
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
416 log("error", "Stanza bound for remote host, but s2s is not implemented");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
417 end
140
a0fc73d5f48c Backed out changeset 8f17ba74823c
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
418 >>>>>>> other