core/stanza_dispatch.lua

Sun, 24 Aug 2008 01:51:02 +0000

author
matthew
date
Sun, 24 Aug 2008 01:51:02 +0000
changeset 1
b8787e859fd2
parent 0
3e3171b59028
child 2
9bb397205f26
permissions
-rw-r--r--

Switched to new connection framework, courtesy of the luadch project
Now supports SSL on 5223
Beginning support for presence (aka. the proper routing of stanzas)

0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
1
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
2 require "util.stanza"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
3
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
4 local st = stanza;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
5
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
6 local t_concat = table.concat;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
7 local format = string.format;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
8
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
9 function init_stanza_dispatcher(session)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
10 local iq_handlers = {};
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
11
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
12 local session_log = session.log;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
13 local log = function (type, msg) session_log(type, "stanza_dispatcher", msg); end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
14 local send = session.send;
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
15 local send_to;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
16 do
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
17 local _send_to = session.send_to;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
18 send_to = function (...) _send_to(session, ...); end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
19 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
20
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
21 iq_handlers["jabber:iq:auth"] =
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
22 function (stanza)
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
23 local username = stanza.tags[1]:child_with_name("username");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
24 local password = stanza.tags[1]:child_with_name("password");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
25 local resource = stanza.tags[1]:child_with_name("resource");
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
26 if not (username and password and resource) then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
27 local reply = st.reply(stanza);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
28 send(reply:query("jabber:iq:auth")
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
29 :tag("username"):up()
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
30 :tag("password"):up()
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
31 :tag("resource"):up());
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
32 return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
33 else
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
34 username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
35 print(username, password, resource)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
36 local reply = st.reply(stanza);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
37 require "core.usermanager"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
38 if usermanager.validate_credentials(session.host, username, password) then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
39 -- Authentication successful!
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
40 session.username = username;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
41 session.resource = resource;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
42 if not hosts[session.host].sessions[username] then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
43 hosts[session.host].sessions[username] = { sessions = {} };
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
44 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
45 hosts[session.host].sessions[username].sessions[resource] = session;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
46 send(st.reply(stanza));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
47 return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
48 else
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
49 local reply = st.reply(stanza);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
50 reply.attr.type = "error";
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
51 reply:tag("error", { code = "401", type = "auth" })
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
52 :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
53 send(reply);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
54 return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
55 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
56 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
57
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
58 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
59
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
60 iq_handlers["jabber:iq:roster"] =
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
61 function (stanza)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
62 if stanza.attr.type == "get" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
63 session.roster = session.roster or rostermanager.getroster(session.username, session.host);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
64 if session.roster == false then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
65 send(st.reply(stanza)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
66 :tag("error", { type = "wait" })
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
67 :tag("internal-server-error", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
68 return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
69 else session.roster = session.roster or {};
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
70 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
71 local roster = st.reply(stanza)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
72 :query("jabber:iq:roster");
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
73 for jid in pairs(session.roster) do
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
74 roster:tag("item", { jid = jid, subscription = "none" }):up();
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
75 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
76 send(roster);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
77 return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
78 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
79 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
80
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
81
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
82 return function (stanza)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
83 log("info", "--> "..tostring(stanza));
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
84 if (not stanza.attr.to) or (hosts[stanza.attr.to] and hosts[stanza.attr.to].type == "local") then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
85 if stanza.name == "iq" then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
86 if not stanza.tags[1] then log("warn", "<iq> without child is invalid"); return; end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
87 if not stanza.attr.id then log("warn", "<iq> without id attribute is invalid"); end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
88 local xmlns = (stanza.tags[1].attr and stanza.tags[1].attr.xmlns) or nil;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
89 if not xmlns then log("warn", "Child of <iq> has no xmlns - invalid"); return; end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
90 if (((not stanza.attr.to) or stanza.attr.to == session.host or stanza.attr.to:match("@[^/]+$")) and (stanza.attr.type == "get" or stanza.attr.type == "set")) then -- Stanza sent to us
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
91 if iq_handlers[xmlns] then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
92 if iq_handlers[xmlns](stanza) then return; end;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
93 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
94 log("warn", "Unhandled namespace: "..xmlns);
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
95 send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id));
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
96 return;
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
97 end
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
98 elseif stanza.name == "presence" then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
99 if session.roster then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
100 -- Broadcast presence and probes
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
101 local broadcast = st.presence({ from = session.username.."@"..session.host.."/"..session.resource });
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
102 local probe = st.presence { from = broadcast.attr.from, type = "probe" };
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
103
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
104 for child in stanza:children() do
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
105 broadcast:tag(child.name, child.attr);
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
106 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
107 for contact in pairs(session.roster) do
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
108 broadcast.attr.to = contact;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
109 send_to(contact, broadcast);
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
110 --local host = jid.host(contact);
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
111 --if hosts[host] and hosts[host].type == "local" then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
112 --local node, host = jid.split(contact);
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
113 --if host[host].sessions[node]
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
114 --local pres = st.presence { from = con
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
115 --else
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
116 -- probe.attr.to = contact;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
117 -- send_to(contact, probe);
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
118 --end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
119 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
120
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
121 -- Probe for our contacts' presence
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
122 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
123 end
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
124 else
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
125 --end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
126 --if stanza.attr.to and ((not hosts[stanza.attr.to]) or hosts[stanza.attr.to].type ~= "local") then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
127 -- Need to route stanza
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
128 stanza.attr.from = session.username.."@"..session.host;
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents: 0
diff changeset
129 session:send_to(stanza.attr.to, stanza);
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
130 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
131 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
132
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
133 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
134

mercurial