Software / code / prosody
Annotate
core/stanza_dispatch.lua @ 181:d952eae776dc
Don't set cursor inside added child when using add_child()
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sun, 26 Oct 2008 14:39:52 +0000 |
| parent | 7:dcc5ac721c20 |
| rev | line source |
|---|---|
| 0 | 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; | |
|
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 | 20 |
| 21 iq_handlers["jabber:iq:auth"] = | |
| 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 | 26 if not (username and password and resource) then |
| 27 local reply = st.reply(stanza); | |
| 28 send(reply:query("jabber:iq:auth") | |
| 29 :tag("username"):up() | |
| 30 :tag("password"):up() | |
| 31 :tag("resource"):up()); | |
| 32 return true; | |
| 33 else | |
| 34 username, password, resource = t_concat(username), t_concat(password), t_concat(resource); | |
| 35 local reply = st.reply(stanza); | |
| 36 require "core.usermanager" | |
| 37 if usermanager.validate_credentials(session.host, username, password) then | |
| 38 -- Authentication successful! | |
| 39 session.username = username; | |
| 40 session.resource = resource; | |
| 2 | 41 session.full_jid = username.."@"..session.host.."/"..session.resource; |
| 0 | 42 if not hosts[session.host].sessions[username] then |
| 43 hosts[session.host].sessions[username] = { sessions = {} }; | |
| 44 end | |
| 45 hosts[session.host].sessions[username].sessions[resource] = session; | |
| 46 send(st.reply(stanza)); | |
| 47 return true; | |
| 48 else | |
| 49 local reply = st.reply(stanza); | |
| 50 reply.attr.type = "error"; | |
| 51 reply:tag("error", { code = "401", type = "auth" }) | |
| 52 :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" }); | |
| 53 send(reply); | |
| 54 return true; | |
| 55 end | |
| 56 end | |
| 57 | |
| 58 end | |
| 59 | |
| 60 iq_handlers["jabber:iq:roster"] = | |
| 61 function (stanza) | |
| 62 if stanza.attr.type == "get" then | |
| 63 session.roster = session.roster or rostermanager.getroster(session.username, session.host); | |
| 64 if session.roster == false then | |
| 65 send(st.reply(stanza) | |
| 66 :tag("error", { type = "wait" }) | |
| 67 :tag("internal-server-error", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"})); | |
| 68 return true; | |
| 69 else session.roster = session.roster or {}; | |
| 70 end | |
| 71 local roster = st.reply(stanza) | |
| 72 :query("jabber:iq:roster"); | |
| 73 for jid in pairs(session.roster) do | |
| 74 roster:tag("item", { jid = jid, subscription = "none" }):up(); | |
| 75 end | |
| 76 send(roster); | |
| 77 return true; | |
| 78 end | |
| 79 end | |
| 80 | |
| 81 | |
| 82 return function (stanza) | |
| 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 | 97 end |
| 7 | 98 end |
| 99 if not session.username then log("warn", "Attempt to use an unauthed stream!"); return; end | |
| 100 if stanza.name == "presence" then | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
101 if session.roster then |
| 2 | 102 local initial_presence = not session.last_presence; |
| 103 session.last_presence = stanza; | |
| 104 | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
105 -- Broadcast presence and probes |
| 2 | 106 local broadcast = st.presence({ from = session.full_jid, type = stanza.attr.type }); |
| 107 --local probe = st.presence { from = broadcast.attr.from, type = "probe" }; | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
108 |
| 2 | 109 for child in stanza:childtags() do |
| 4 | 110 broadcast:add_child(child); |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
111 end |
| 2 | 112 for contact_jid in pairs(session.roster) do |
| 113 broadcast.attr.to = contact_jid; | |
| 114 send_to(contact_jid, broadcast); | |
| 115 if initial_presence then | |
| 116 local node, host = jid.split(contact_jid); | |
| 117 if hosts[host] and hosts[host].type == "local" then | |
| 118 local contact = hosts[host].sessions[node] | |
| 119 if contact then | |
| 120 local pres = st.presence { to = session.full_jid }; | |
| 121 for resource, contact_session in pairs(contact.sessions) do | |
| 122 if contact_session.last_presence then | |
| 123 pres.tags = contact_session.last_presence.tags; | |
| 124 pres.attr.from = contact_session.full_jid; | |
| 125 send(pres); | |
| 126 end | |
| 127 end | |
| 128 end | |
| 129 --FIXME: Do we send unavailable if they are offline? | |
| 130 else | |
| 131 probe.attr.to = contact; | |
| 132 send_to(contact, probe); | |
| 133 end | |
| 134 end | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
135 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
136 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
137 -- Probe for our contacts' presence |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
138 end |
| 0 | 139 end |
| 7 | 140 elseif session.username then |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
141 --end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
142 --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
|
143 -- Need to route stanza |
| 0 | 144 stanza.attr.from = session.username.."@"..session.host; |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
0
diff
changeset
|
145 session:send_to(stanza.attr.to, stanza); |
| 0 | 146 end |
| 147 end | |
| 148 | |
| 149 end |