5f61f5fbaefdbad0265276e4ab25e47691c62fe4
[prosody.git] / core / stanza_dispatch.lua
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         local send_to;
16         do
17                 local _send_to = session.send_to;
18                 send_to = function (...) _send_to(session, ...); end
19         end
20
21         iq_handlers["jabber:iq:auth"] = 
22                 function (stanza)
23                         local username = stanza.tags[1]:child_with_name("username");
24                         local password = stanza.tags[1]:child_with_name("password");
25                         local resource = stanza.tags[1]:child_with_name("resource");
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                                 print(username, password, resource)
36                                 local reply = st.reply(stanza);
37                                 require "core.usermanager"
38                                 if usermanager.validate_credentials(session.host, username, password) then
39                                         -- Authentication successful!
40                                         session.username = username;
41                                         session.resource = resource;
42                                         session.full_jid = username.."@"..session.host.."/"..session.resource;
43                                         if not hosts[session.host].sessions[username] then
44                                                 hosts[session.host].sessions[username] = { sessions = {} };
45                                         end
46                                         hosts[session.host].sessions[username].sessions[resource] = session;
47                                         send(st.reply(stanza));
48                                         return true;
49                                 else
50                                         local reply = st.reply(stanza);
51                                         reply.attr.type = "error";
52                                         reply:tag("error", { code = "401", type = "auth" })
53                                                 :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
54                                         send(reply);
55                                         return true;
56                                 end
57                         end
58                         
59                 end
60                 
61         iq_handlers["jabber:iq:roster"] =
62                 function (stanza)
63                         if stanza.attr.type == "get" then
64                                 session.roster = session.roster or rostermanager.getroster(session.username, session.host);
65                                 if session.roster == false then
66                                         send(st.reply(stanza)
67                                                 :tag("error", { type = "wait" })
68                                                 :tag("internal-server-error", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}));
69                                         return true;
70                                 else session.roster = session.roster or {};
71                                 end
72                                 local roster = st.reply(stanza)
73                                                         :query("jabber:iq:roster");
74                                 for jid in pairs(session.roster) do
75                                         roster:tag("item", { jid = jid, subscription = "none" }):up();
76                                 end
77                                 send(roster);
78                                 return true;
79                         end
80                 end
81
82
83         return  function (stanza)
84                         log("info", "--> "..tostring(stanza));
85                         if (not stanza.attr.to) or (hosts[stanza.attr.to] and hosts[stanza.attr.to].type == "local") then
86                                 if stanza.name == "iq" then
87                                         if not stanza.tags[1] then log("warn", "<iq> without child is invalid"); return; end
88                                         if not stanza.attr.id then log("warn", "<iq> without id attribute is invalid"); end
89                                         local xmlns = (stanza.tags[1].attr and stanza.tags[1].attr.xmlns) or nil;
90                                         if not xmlns then log("warn", "Child of <iq> has no xmlns - invalid"); return; end
91                                         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
92                                                 if iq_handlers[xmlns] then
93                                                         if iq_handlers[xmlns](stanza) then return; end;
94                                                 end
95                                                 log("warn", "Unhandled namespace: "..xmlns);
96                                                 send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id));
97                                                 return;
98                                         end
99                                 elseif stanza.name == "presence" then
100                                         if session.roster then
101                                                 local initial_presence = not session.last_presence;
102                                                 session.last_presence = stanza;
103                                                 
104                                                 -- Broadcast presence and probes
105                                                 local broadcast = st.presence({ from = session.full_jid, type = stanza.attr.type });
106                                                 --local probe = st.presence { from = broadcast.attr.from, type = "probe" };
107
108                                                 for child in stanza:childtags() do
109                                                         broadcast:text(tostring(child));
110                                                 end
111                                                 for contact_jid in pairs(session.roster) do
112                                                         broadcast.attr.to = contact_jid;
113                                                         send_to(contact_jid, broadcast);
114                                                         if initial_presence then
115                                                                 print("Initital presence");
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
135                                                 end
136                                                 
137                                                 -- Probe for our contacts' presence
138                                         end
139                                 end
140                         else
141                         --end                           
142                         --if stanza.attr.to and ((not hosts[stanza.attr.to]) or hosts[stanza.attr.to].type ~= "local") then
143                                 -- Need to route stanza
144                                 stanza.attr.from = session.username.."@"..session.host;
145                                 session:send_to(stanza.attr.to, stanza);
146                         end
147                 end
148
149 end
150