Annotate

core/stanza_router.lua @ 135:bf10d42dd41b

Merged from Matthew
author Waqas Hussain <waqas20@gmail.com>
date Thu, 23 Oct 2008 21:51:00 +0500
parent 130:907cff6b5359
parent 122:21f8d2175393
child 137:9a05dfb35dd5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
135
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
1
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
2 -- 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
3 -- 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
4 -- the rules from the RFCs (mainly 3921)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
5
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
6 require "core.servermanager"
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
7
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
8 local log = require "util.logger".init("stanzarouter")
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
9
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
10 local st = require "util.stanza";
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
11 local send = require "core.sessionmanager".send_to_session;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
12 -- local send_s2s = require "core.s2smanager".send_to_host;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
13 local user_exists = require "core.usermanager".user_exists;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
14
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
15 local jid_split = require "util.jid".split;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
16 local print, debug = print, debug;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
17
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
18 function core_process_stanza(origin, stanza)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
19 log("debug", "Received: "..tostring(stanza))
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
20 -- TODO verify validity of stanza (as well as JID validity)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
21 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
22 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
23 error("Invalid IQ");
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
24 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
25 error("Invalid IQ");
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
26 end
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
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
29 if origin.type == "c2s" and not origin.full_jid
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
30 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
31 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
32 error("Client MUST bind resource after auth");
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
33 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
34
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
35 local to = stanza.attr.to;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
36 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
37 -- 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
38
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
39 -- TODO presence subscriptions
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
40 if not to then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
41 core_handle_stanza(origin, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
42 elseif hosts[to] and hosts[to].type == "local" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
43 core_handle_stanza(origin, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
44 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
45 core_handle_stanza(origin, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
46 elseif origin.type == "c2s" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
47 core_route_stanza(origin, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
48 end
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
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
51 -- This function handles stanzas which are not routed any further,
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
52 -- that is, they are handled by this server
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
53 function core_handle_stanza(origin, stanza)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
54 -- Handlers
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
55 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
56 local session = origin;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
57
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
58 if stanza.name == "presence" and origin.roster then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
59 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
60 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
61 local subscription = origin.roster[jid].subscription;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
62 if subscription == "both" or subscription == "from" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
63 stanza.attr.to = jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
64 core_route_stanza(origin, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
65 end
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 local node, host = jid_split(stanza.attr.from);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
68 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
69 if res ~= origin then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
70 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
71 stanza.attr.to = res.full_jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
72 core_route_stanza(origin, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
73 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
74 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
75 res.presence.attr.to = origin.full_jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
76 core_route_stanza(res, res.presence);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
77 res.presence.attr.to = nil;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
78 end
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 if not origin.presence then -- presence probes on initial presence
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
82 local probe = st.presence({from = origin.full_jid, type = "probe"});
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
83 for jid in pairs(origin.roster) do
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
84 local subscription = origin.roster[jid].subscription;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
85 if subscription == "both" or subscription == "to" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
86 probe.attr.to = jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
87 core_route_stanza(origin, probe);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
88 end
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
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
91 origin.presence = stanza;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
92 stanza.attr.to = nil; -- reset it
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
93 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
94 -- TODO error, bad type
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
95 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
96 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
97 log("debug", "Routing stanza to local");
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
98 handle_stanza(session, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
99 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
100 end
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
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
103 function is_authorized_to_see_presence(origin, username, host)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
104 local roster = datamanager.load(username, host, "roster") or {};
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
105 local item = roster[origin.username.."@"..origin.host];
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
106 return item and (item.subscription == "both" or item.subscription == "from");
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
107 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
108
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
109 function core_route_stanza(origin, stanza)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
110 -- Hooks
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
111 --- ...later
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
112
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
113 -- Deliver
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
114 local to = stanza.attr.to;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
115 local node, host, resource = jid_split(to);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
116
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
117 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
118
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
119 local host_session = hosts[host]
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
120 if host_session and host_session.type == "local" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
121 -- Local host
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
122 local user = host_session.sessions[node];
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
123 if user then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
124 local res = user.sessions[resource];
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
125 if not res then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
126 -- 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
127 if stanza.name == "presence" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
128 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
129 if stanza.attr.type == "probe" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
130 if is_authorized_to_see_presence(origin, node, host) then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
131 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
132 if user.sessions[k].presence then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
133 local pres = user.sessions[k].presence;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
134 pres.attr.to = origin.full_jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
135 pres.attr.from = user.sessions[k].full_jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
136 send(origin, pres);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
137 pres.attr.to = nil;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
138 pres.attr.from = nil;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
139 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
140 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
141 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
142 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
143 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
144 elseif stanza.attr.type == "subscribe" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
145 -- TODO
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
146 elseif stanza.attr.type == "unsubscribe" 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 == "subscribed" 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 == "unsubscribed" 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 end -- discard any other type
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
153 else -- sender is available or unavailable
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
154 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
155 if user.sessions[k].full_jid then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
156 stanza.attr.to = user.sessions[k].full_jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
157 send(user.sessions[k], stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
158 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
159 end
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 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
162 for k in pairs(user.sessions) do
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
163 if user.sessions[k].full_jid then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
164 res = user.sessions[k];
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
165 break;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
166 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
167 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
168 -- TODO find resource with greatest priority
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
169 send(res, stanza);
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
170 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
171 -- TODO send IQ error
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
172 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
173 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
174 -- User + resource is online...
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
175 stanza.attr.to = res.full_jid;
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
176 send(res, stanza); -- Yay \o/
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
177 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
178 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
179 -- user not online
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
180 if user_exists(node, host) then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
181 if stanza.name == "presence" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
182 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
183 -- TODO send last recieved unavailable presence
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
184 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
185 -- TODO send unavailable presence
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
186 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
187 elseif stanza.name == "message" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
188 -- TODO send message error, or store offline messages
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
189 elseif stanza.name == "iq" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
190 -- TODO send IQ error
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
191 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
192 else -- user does not exist
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
193 -- 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
194 if stanza.name == "presence" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
195 if stanza.attr.type == "probe" then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
196 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
197 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
198 -- else ignore
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
199 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
200 send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
201 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
202 end
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 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
205 -- Remote host
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
206 if host_session then
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
207 -- Send to session
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
208 else
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
209 -- Need to establish the connection
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
210 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
211 end
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
212 stanza.attr.to = to; -- reset
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
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
215 function handle_stanza_toremote(stanza)
bf10d42dd41b Merged from Matthew
Waqas Hussain <waqas20@gmail.com>
parents: 130 122
diff changeset
216 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
217 end