Annotate

core/stanza_router.lua @ 138:cb0dd442ca63

Making the best attempt out of a bad merge from waqas
author Matthew Wild <mwild1@gmail.com>
date Thu, 23 Oct 2008 18:24:55 +0100
parent 137:9a05dfb35dd5
parent 127:93f3c6b94c75
child 139:8f17ba74823c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
138
cb0dd442ca63 Making the best attempt out of a bad merge from waqas
Matthew Wild <mwild1@gmail.com>
parents: 137 127
diff changeset
1 <<<<<<< local
135
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
2
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
3 -- The code in this file should be self-explanatory, though the logic is horrible
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
4 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
5 -- the rules from the RFCs (mainly 3921)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
6
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
7 require "core.servermanager"
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
8
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
9 local log = require "util.logger".init("stanzarouter")
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
10
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
11 local st = require "util.stanza";
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
12 local send = require "core.sessionmanager".send_to_session;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
13 -- local send_s2s = require "core.s2smanager".send_to_host;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
14 local user_exists = require "core.usermanager".user_exists;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
15
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
16 local jid_split = require "util.jid".split;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
17 local print, debug = print, debug;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
18
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
19 function core_process_stanza(origin, stanza)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
20 log("debug", "Received: "..tostring(stanza))
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
21 -- TODO verify validity of stanza (as well as JID validity)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
22 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
23 if stanza.attr.type == "set" or stanza.attr.type == "get" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
24 error("Invalid IQ");
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
25 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
26 error("Invalid IQ");
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
27 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
28 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
29
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
30 if origin.type == "c2s" and not origin.full_jid
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
31 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
32 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
33 error("Client MUST bind resource after auth");
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
34 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
35
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
36 local to = stanza.attr.to;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
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)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
38 -- TODO also, stazas should be returned to their original state before the function ends
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
39
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
40 -- TODO presence subscriptions
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
41 if not to then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
42 core_handle_stanza(origin, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
43 elseif hosts[to] and hosts[to].type == "local" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
44 core_handle_stanza(origin, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
45 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
46 core_handle_stanza(origin, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
47 elseif origin.type == "c2s" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
48 core_route_stanza(origin, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
49 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
50 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
51
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
52 -- This function handles stanzas which are not routed any further,
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
53 -- that is, they are handled by this server
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
54 function core_handle_stanza(origin, stanza)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
55 -- Handlers
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
56 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
57 local session = origin;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
58
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
59 if stanza.name == "presence" and origin.roster then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
60 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
61 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
62 local subscription = origin.roster[jid].subscription;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
63 if subscription == "both" or subscription == "from" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
64 stanza.attr.to = jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
65 core_route_stanza(origin, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
66 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
67 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
68 local node, host = jid_split(stanza.attr.from);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
69 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources and from resources
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
70 if res ~= origin then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
71 if res.full_jid then -- to resource. FIXME is this check correct? Maybe it should be res.presence
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
72 stanza.attr.to = res.full_jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
73 core_route_stanza(origin, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
74 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
75 if res.presence then -- from all resources for which we have presence
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
76 res.presence.attr.to = origin.full_jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
77 core_route_stanza(res, res.presence);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
78 res.presence.attr.to = nil;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
79 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
80 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
81 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
82 if not origin.presence then -- presence probes on initial presence
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
83 local probe = st.presence({from = origin.full_jid, type = "probe"});
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
84 for jid in pairs(origin.roster) do
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
85 local subscription = origin.roster[jid].subscription;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
86 if subscription == "both" or subscription == "to" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
87 probe.attr.to = jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
88 core_route_stanza(origin, probe);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
89 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
90 end
137
9a05dfb35dd5 Added TODO
Waqas Hussain <waqas20@gmail.com>
parents: 135
diff changeset
91 -- TODO resend subscription requests
135
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
92 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
93 origin.presence = stanza;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
94 stanza.attr.to = nil; -- reset it
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
95 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
96 -- TODO error, bad type
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
97 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
98 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
99 log("debug", "Routing stanza to local");
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
100 handle_stanza(session, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
101 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
102 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
103 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
104
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
105 function is_authorized_to_see_presence(origin, username, host)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
106 local roster = datamanager.load(username, host, "roster") or {};
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
107 local item = roster[origin.username.."@"..origin.host];
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
108 return item and (item.subscription == "both" or item.subscription == "from");
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
109 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
110
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
111 function core_route_stanza(origin, stanza)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
112 -- Hooks
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
113 --- ...later
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
114
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
115 -- Deliver
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
116 local to = stanza.attr.to;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
117 local node, host, resource = jid_split(to);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
118
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
119 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
120
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
121 local host_session = hosts[host]
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
122 if host_session and host_session.type == "local" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
123 -- Local host
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
124 local user = host_session.sessions[node];
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
125 if user then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
126 local res = user.sessions[resource];
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
127 if not res then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
128 -- if we get here, resource was not specified or was unavailable
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
129 if stanza.name == "presence" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
130 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
131 if stanza.attr.type == "probe" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
132 if is_authorized_to_see_presence(origin, node, host) then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
133 for k in pairs(user.sessions) do -- return presence for all resources
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
134 if user.sessions[k].presence then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
135 local pres = user.sessions[k].presence;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
136 pres.attr.to = origin.full_jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
137 pres.attr.from = user.sessions[k].full_jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
138 send(origin, pres);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
139 pres.attr.to = nil;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
140 pres.attr.from = nil;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
141 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
142 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
143 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
144 send(origin, st.presence({from=user.."@"..host, to=origin.username.."@"..origin.host, type="unsubscribed"}));
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
145 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
146 elseif stanza.attr.type == "subscribe" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
147 -- TODO
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
148 elseif stanza.attr.type == "unsubscribe" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
149 -- TODO
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
150 elseif stanza.attr.type == "subscribed" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
151 -- TODO
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
152 elseif stanza.attr.type == "unsubscribed" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
153 -- TODO
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
154 end -- discard any other type
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
155 else -- sender is available or unavailable
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
156 for k in pairs(user.sessions) do -- presence broadcast to all user resources
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
157 if user.sessions[k].full_jid then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
158 stanza.attr.to = user.sessions[k].full_jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
159 send(user.sessions[k], stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
160 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
161 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
162 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
163 elseif stanza.name == "message" then -- select a resource to recieve message
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
164 for k in pairs(user.sessions) do
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
165 if user.sessions[k].full_jid then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
166 res = user.sessions[k];
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
167 break;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
168 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
169 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
170 -- TODO find resource with greatest priority
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
171 send(res, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
172 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
173 -- TODO send IQ error
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
174 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
175 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
176 -- User + resource is online...
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
177 stanza.attr.to = res.full_jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
178 send(res, stanza); -- Yay \o/
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
179 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
180 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
181 -- user not online
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
182 if user_exists(node, host) then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
183 if stanza.name == "presence" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
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?
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
185 -- TODO send last recieved unavailable presence
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
186 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
187 -- TODO send unavailable presence
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
188 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
189 elseif stanza.name == "message" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
190 -- TODO send message error, or store offline messages
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
191 elseif stanza.name == "iq" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
192 -- TODO send IQ error
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
193 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
194 else -- user does not exist
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
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?
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
196 if stanza.name == "presence" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
197 if stanza.attr.type == "probe" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
198 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
199 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
200 -- else ignore
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
201 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
202 send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
203 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
204 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
205 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
206 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
207 -- Remote host
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
208 if host_session then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
209 -- Send to session
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
210 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
211 -- Need to establish the connection
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
212 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
213 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
214 stanza.attr.to = to; -- reset
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
215 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
216
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
217 function handle_stanza_toremote(stanza)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
218 log("error", "Stanza bound for remote host, but s2s is not implemented");
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
219 end
138
cb0dd442ca63 Making the best attempt out of a bad merge from waqas
Matthew Wild <mwild1@gmail.com>
parents: 137 127
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
127
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 122
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
127
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 122
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
138
cb0dd442ca63 Making the best attempt out of a bad merge from waqas
Matthew Wild <mwild1@gmail.com>
parents: 137 127
diff changeset
418 >>>>>>> other