Software /
code /
prosody
Comparison
core/stanza_dispatch.lua @ 0:3e3171b59028
First commit, where do you want to go tomorrow?
author | matthew |
---|---|
date | Fri, 22 Aug 2008 21:09:04 +0000 |
child | 1:b8787e859fd2 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3e3171b59028 |
---|---|
1 | |
2 require "util.stanza" | |
3 | |
4 local st = stanza; | |
5 | |
6 local t_concat = table.concat; | |
7 local format = string.format; | |
8 | |
9 function init_stanza_dispatcher(session) | |
10 local iq_handlers = {}; | |
11 | |
12 local session_log = session.log; | |
13 local log = function (type, msg) session_log(type, "stanza_dispatcher", msg); end | |
14 local send = session.send; | |
15 | |
16 | |
17 | |
18 iq_handlers["jabber:iq:auth"] = | |
19 function (stanza) | |
20 local username = stanza[1]:child_with_name("username"); | |
21 local password = stanza[1]:child_with_name("password"); | |
22 local resource = stanza[1]:child_with_name("resource"); | |
23 if not (username and password and resource) then | |
24 local reply = st.reply(stanza); | |
25 send(reply:query("jabber:iq:auth") | |
26 :tag("username"):up() | |
27 :tag("password"):up() | |
28 :tag("resource"):up()); | |
29 return true; | |
30 else | |
31 username, password, resource = t_concat(username), t_concat(password), t_concat(resource); | |
32 print(username, password, resource) | |
33 local reply = st.reply(stanza); | |
34 require "core.usermanager" | |
35 if usermanager.validate_credentials(session.host, username, password) then | |
36 -- Authentication successful! | |
37 session.username = username; | |
38 session.resource = resource; | |
39 if not hosts[session.host].sessions[username] then | |
40 hosts[session.host].sessions[username] = { sessions = {} }; | |
41 end | |
42 hosts[session.host].sessions[username].sessions[resource] = session; | |
43 send(st.reply(stanza)); | |
44 return true; | |
45 else | |
46 local reply = st.reply(stanza); | |
47 reply.attr.type = "error"; | |
48 reply:tag("error", { code = "401", type = "auth" }) | |
49 :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" }); | |
50 send(reply); | |
51 return true; | |
52 end | |
53 end | |
54 | |
55 end | |
56 | |
57 iq_handlers["jabber:iq:roster"] = | |
58 function (stanza) | |
59 if stanza.attr.type == "get" then | |
60 session.roster = session.roster or rostermanager.getroster(session.username, session.host); | |
61 if session.roster == false then | |
62 send(st.reply(stanza) | |
63 :tag("error", { type = "wait" }) | |
64 :tag("internal-server-error", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"})); | |
65 return true; | |
66 else session.roster = session.roster or {}; | |
67 end | |
68 local roster = st.reply(stanza) | |
69 :query("jabber:iq:roster"); | |
70 for jid in pairs(session.roster) do | |
71 roster:tag("item", { jid = jid, subscription = "none" }):up(); | |
72 end | |
73 send(roster); | |
74 return true; | |
75 end | |
76 end | |
77 | |
78 | |
79 return function (stanza) | |
80 log("info", "--> "..tostring(stanza)); | |
81 if stanza.name == "iq" then | |
82 if not stanza[1] then log("warn", "<iq> without child is invalid"); return; end | |
83 if not stanza.attr.id then log("warn", "<iq> without id attribute is invalid"); end | |
84 local xmlns = stanza[1].attr.xmlns; | |
85 if not xmlns then log("warn", "Child of <iq> has no xmlns - invalid"); return; end | |
86 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 | |
87 if iq_handlers[xmlns] then | |
88 if iq_handlers[xmlns](stanza) then return; end; | |
89 end | |
90 log("warn", "Unhandled namespace: "..xmlns); | |
91 send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id)); | |
92 end | |
93 | |
94 end | |
95 -- Need to route stanza | |
96 if stanza.attr.to and ((not hosts[stanza.attr.to]) or hosts[stanza.attr.to].type ~= "local") then | |
97 stanza.attr.from = session.username.."@"..session.host; | |
98 session.send_to(stanza.attr.to, stanza); | |
99 end | |
100 end | |
101 | |
102 end | |
103 |