10 local iq_handlers = {}; |
10 local iq_handlers = {}; |
11 |
11 |
12 local session_log = session.log; |
12 local session_log = session.log; |
13 local log = function (type, msg) session_log(type, "stanza_dispatcher", msg); end |
13 local log = function (type, msg) session_log(type, "stanza_dispatcher", msg); end |
14 local send = session.send; |
14 local send = session.send; |
15 |
15 local send_to; |
16 |
16 do |
|
17 local _send_to = session.send_to; |
|
18 send_to = function (...) _send_to(session, ...); end |
|
19 end |
17 |
20 |
18 iq_handlers["jabber:iq:auth"] = |
21 iq_handlers["jabber:iq:auth"] = |
19 function (stanza) |
22 function (stanza) |
20 local username = stanza[1]:child_with_name("username"); |
23 local username = stanza.tags[1]:child_with_name("username"); |
21 local password = stanza[1]:child_with_name("password"); |
24 local password = stanza.tags[1]:child_with_name("password"); |
22 local resource = stanza[1]:child_with_name("resource"); |
25 local resource = stanza.tags[1]:child_with_name("resource"); |
23 if not (username and password and resource) then |
26 if not (username and password and resource) then |
24 local reply = st.reply(stanza); |
27 local reply = st.reply(stanza); |
25 send(reply:query("jabber:iq:auth") |
28 send(reply:query("jabber:iq:auth") |
26 :tag("username"):up() |
29 :tag("username"):up() |
27 :tag("password"):up() |
30 :tag("password"):up() |
76 end |
79 end |
77 |
80 |
78 |
81 |
79 return function (stanza) |
82 return function (stanza) |
80 log("info", "--> "..tostring(stanza)); |
83 log("info", "--> "..tostring(stanza)); |
81 if stanza.name == "iq" then |
84 if (not stanza.attr.to) or (hosts[stanza.attr.to] and hosts[stanza.attr.to].type == "local") then |
82 if not stanza[1] then log("warn", "<iq> without child is invalid"); return; end |
85 if stanza.name == "iq" then |
83 if not stanza.attr.id then log("warn", "<iq> without id attribute is invalid"); end |
86 if not stanza.tags[1] then log("warn", "<iq> without child is invalid"); return; end |
84 local xmlns = stanza[1].attr.xmlns; |
87 if not stanza.attr.id then log("warn", "<iq> without id attribute is invalid"); end |
85 if not xmlns then log("warn", "Child of <iq> has no xmlns - invalid"); return; end |
88 local xmlns = (stanza.tags[1].attr and stanza.tags[1].attr.xmlns) or nil; |
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 |
89 if not xmlns then log("warn", "Child of <iq> has no xmlns - invalid"); return; end |
87 if iq_handlers[xmlns] then |
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 |
88 if iq_handlers[xmlns](stanza) then return; end; |
91 if iq_handlers[xmlns] then |
|
92 if iq_handlers[xmlns](stanza) then return; end; |
|
93 end |
|
94 log("warn", "Unhandled namespace: "..xmlns); |
|
95 send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id)); |
|
96 return; |
89 end |
97 end |
90 log("warn", "Unhandled namespace: "..xmlns); |
98 elseif stanza.name == "presence" then |
91 send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id)); |
99 if session.roster then |
|
100 -- Broadcast presence and probes |
|
101 local broadcast = st.presence({ from = session.username.."@"..session.host.."/"..session.resource }); |
|
102 local probe = st.presence { from = broadcast.attr.from, type = "probe" }; |
|
103 |
|
104 for child in stanza:children() do |
|
105 broadcast:tag(child.name, child.attr); |
|
106 end |
|
107 for contact in pairs(session.roster) do |
|
108 broadcast.attr.to = contact; |
|
109 send_to(contact, broadcast); |
|
110 --local host = jid.host(contact); |
|
111 --if hosts[host] and hosts[host].type == "local" then |
|
112 --local node, host = jid.split(contact); |
|
113 --if host[host].sessions[node] |
|
114 --local pres = st.presence { from = con |
|
115 --else |
|
116 -- probe.attr.to = contact; |
|
117 -- send_to(contact, probe); |
|
118 --end |
|
119 end |
|
120 |
|
121 -- Probe for our contacts' presence |
|
122 end |
92 end |
123 end |
93 |
124 else |
94 end |
125 --end |
95 -- Need to route stanza |
126 --if stanza.attr.to and ((not hosts[stanza.attr.to]) or hosts[stanza.attr.to].type ~= "local") then |
96 if stanza.attr.to and ((not hosts[stanza.attr.to]) or hosts[stanza.attr.to].type ~= "local") then |
127 -- Need to route stanza |
97 stanza.attr.from = session.username.."@"..session.host; |
128 stanza.attr.from = session.username.."@"..session.host; |
98 session.send_to(stanza.attr.to, stanza); |
129 session:send_to(stanza.attr.to, stanza); |
99 end |
130 end |
100 end |
131 end |
101 |
132 |
102 end |
133 end |
103 |
134 |