aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authormatthew <matthew@silver>2008-08-24 18:01:20 +0100
committermatthew <matthew@silver>2008-08-24 18:01:20 +0100
commit5ea14b62950a6a9cbdc96c7354afddcf637d9d41 (patch)
tree1285f7cc14c92306dab94b104648dd1747812069 /core
downloadprosody-5ea14b62950a6a9cbdc96c7354afddcf637d9d41.tar.gz
prosody-5ea14b62950a6a9cbdc96c7354afddcf637d9d41.zip
Added all the files to please hg :/
Diffstat (limited to 'core')
-rw-r--r--core/offlinemessage.lua13
-rw-r--r--core/rostermanager.lua26
-rw-r--r--core/stanza_dispatch.lua150
-rw-r--r--core/usermanager.lua11
-rw-r--r--core/xmlhandlers.lua92
5 files changed, 292 insertions, 0 deletions
diff --git a/core/offlinemessage.lua b/core/offlinemessage.lua
new file mode 100644
index 00000000..dda9b7d8
--- /dev/null
+++ b/core/offlinemessage.lua
@@ -0,0 +1,13 @@
+
+require "util.datamanager"
+
+local datamanager = datamanager;
+local t_insert = table.insert;
+
+module "offlinemessage"
+
+function new(user, host, stanza)
+ local offlinedata = datamanager.load(user, host, "offlinemsg") or {};
+ t_insert(offlinedata, stanza);
+ return datamanager.store(user, host, "offlinemsg", offlinedata);
+end
diff --git a/core/rostermanager.lua b/core/rostermanager.lua
new file mode 100644
index 00000000..b31ca21b
--- /dev/null
+++ b/core/rostermanager.lua
@@ -0,0 +1,26 @@
+
+local mainlog = log;
+local function log(type, message)
+ mainlog(type, "rostermanager", message);
+end
+
+local setmetatable = setmetatable;
+local format = string.format;
+local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
+
+require "util.datamanager"
+
+local datamanager = datamanager;
+
+module "rostermanager"
+
+function getroster(username, host)
+ return {
+ ["mattj@localhost"] = true,
+ ["tobias@getjabber.ath.cx"] = true,
+ ["waqas@getjabber.ath.cx"] = true,
+ ["thorns@getjabber.ath.cx"] = true,
+ ["idw@getjabber.ath.cx"] = true,
+ }
+-- return datamanager.load(username, host, "roster") or {};
+end
diff --git a/core/stanza_dispatch.lua b/core/stanza_dispatch.lua
new file mode 100644
index 00000000..2c835517
--- /dev/null
+++ b/core/stanza_dispatch.lua
@@ -0,0 +1,150 @@
+
+require "util.stanza"
+
+local st = stanza;
+
+local t_concat = table.concat;
+local format = string.format;
+
+function init_stanza_dispatcher(session)
+ local iq_handlers = {};
+
+ local session_log = session.log;
+ local log = function (type, msg) session_log(type, "stanza_dispatcher", msg); end
+ local send = session.send;
+ local send_to;
+ do
+ local _send_to = session.send_to;
+ send_to = function (...) _send_to(session, ...); end
+ end
+
+ iq_handlers["jabber:iq:auth"] =
+ function (stanza)
+ local username = stanza.tags[1]:child_with_name("username");
+ local password = stanza.tags[1]:child_with_name("password");
+ local resource = stanza.tags[1]:child_with_name("resource");
+ if not (username and password and resource) then
+ local reply = st.reply(stanza);
+ send(reply:query("jabber:iq:auth")
+ :tag("username"):up()
+ :tag("password"):up()
+ :tag("resource"):up());
+ return true;
+ else
+ username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
+ print(username, password, resource)
+ local reply = st.reply(stanza);
+ require "core.usermanager"
+ if usermanager.validate_credentials(session.host, username, password) then
+ -- Authentication successful!
+ session.username = username;
+ session.resource = resource;
+ session.full_jid = username.."@"..session.host.."/"..session.resource;
+ if not hosts[session.host].sessions[username] then
+ hosts[session.host].sessions[username] = { sessions = {} };
+ end
+ hosts[session.host].sessions[username].sessions[resource] = session;
+ send(st.reply(stanza));
+ return true;
+ else
+ local reply = st.reply(stanza);
+ reply.attr.type = "error";
+ reply:tag("error", { code = "401", type = "auth" })
+ :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
+ send(reply);
+ return true;
+ end
+ end
+
+ end
+
+ iq_handlers["jabber:iq:roster"] =
+ function (stanza)
+ if stanza.attr.type == "get" then
+ session.roster = session.roster or rostermanager.getroster(session.username, session.host);
+ if session.roster == false then
+ send(st.reply(stanza)
+ :tag("error", { type = "wait" })
+ :tag("internal-server-error", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}));
+ return true;
+ else session.roster = session.roster or {};
+ end
+ local roster = st.reply(stanza)
+ :query("jabber:iq:roster");
+ for jid in pairs(session.roster) do
+ roster:tag("item", { jid = jid, subscription = "none" }):up();
+ end
+ send(roster);
+ return true;
+ end
+ end
+
+
+ return function (stanza)
+ log("info", "--> "..tostring(stanza));
+ if (not stanza.attr.to) or (hosts[stanza.attr.to] and hosts[stanza.attr.to].type == "local") then
+ if stanza.name == "iq" then
+ if not stanza.tags[1] then log("warn", "<iq> without child is invalid"); return; end
+ if not stanza.attr.id then log("warn", "<iq> without id attribute is invalid"); end
+ local xmlns = (stanza.tags[1].attr and stanza.tags[1].attr.xmlns) or nil;
+ if not xmlns then log("warn", "Child of <iq> has no xmlns - invalid"); return; end
+ 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
+ if iq_handlers[xmlns] then
+ if iq_handlers[xmlns](stanza) then return; end;
+ end
+ log("warn", "Unhandled namespace: "..xmlns);
+ send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id));
+ return;
+ end
+ elseif stanza.name == "presence" then
+ if session.roster then
+ local initial_presence = not session.last_presence;
+ session.last_presence = stanza;
+
+ -- Broadcast presence and probes
+ local broadcast = st.presence({ from = session.full_jid, type = stanza.attr.type });
+ --local probe = st.presence { from = broadcast.attr.from, type = "probe" };
+
+ for child in stanza:childtags() do
+ broadcast:add_child(child);
+ end
+ for contact_jid in pairs(session.roster) do
+ broadcast.attr.to = contact_jid;
+ send_to(contact_jid, broadcast);
+ if initial_presence then
+ print("Initital presence");
+ local node, host = jid.split(contact_jid);
+ if hosts[host] and hosts[host].type == "local" then
+ local contact = hosts[host].sessions[node]
+ if contact then
+ local pres = st.presence { to = session.full_jid };
+ for resource, contact_session in pairs(contact.sessions) do
+ if contact_session.last_presence then
+ pres.tags = contact_session.last_presence.tags;
+ pres.attr.from = contact_session.full_jid;
+ send(pres);
+ end
+ end
+ end
+ --FIXME: Do we send unavailable if they are offline?
+ else
+ probe.attr.to = contact;
+ send_to(contact, probe);
+ end
+ end
+ end
+
+ -- Probe for our contacts' presence
+ end
+ end
+ else
+ --end
+ --if stanza.attr.to and ((not hosts[stanza.attr.to]) or hosts[stanza.attr.to].type ~= "local") then
+ -- Need to route stanza
+ stanza.attr.from = session.username.."@"..session.host;
+ session:send_to(stanza.attr.to, stanza);
+ end
+ end
+
+end
+
diff --git a/core/usermanager.lua b/core/usermanager.lua
new file mode 100644
index 00000000..c98a1918
--- /dev/null
+++ b/core/usermanager.lua
@@ -0,0 +1,11 @@
+
+require "util.datamanager"
+local datamanager = datamanager;
+
+module "usermanager"
+
+function validate_credentials(host, username, password)
+ local credentials = datamanager.load(username, host, "accounts") or {};
+ if password == credentials.password then return true; end
+ return false;
+end
diff --git a/core/xmlhandlers.lua b/core/xmlhandlers.lua
new file mode 100644
index 00000000..4d536ce3
--- /dev/null
+++ b/core/xmlhandlers.lua
@@ -0,0 +1,92 @@
+
+require "util.stanza"
+
+local st = stanza;
+local tostring = tostring;
+local format = string.format;
+local m_random = math.random;
+local t_insert = table.insert;
+local t_remove = table.remove;
+local t_concat = table.concat;
+local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end
+
+local error = error;
+
+module "xmlhandlers"
+
+function init_xmlhandlers(session)
+ local ns_stack = { "" };
+ local curr_ns = "";
+ local curr_tag;
+ local chardata = {};
+ local xml_handlers = {};
+ local log = session.log;
+ local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end
+
+ local send = session.send;
+
+ local stanza
+ function xml_handlers:StartElement(name, attr)
+ if stanza and #chardata > 0 then
+ stanza:text(t_concat(chardata));
+ print("Char data:", t_concat(chardata));
+ chardata = {};
+ end
+ curr_ns,name = name:match("^(.+):(%w+)$");
+ print("Tag received:", name, tostring(curr_ns));
+ if not stanza then
+ if session.notopen then
+ if name == "stream" then
+ session.host = attr.to or error("Client failed to specify destination hostname");
+ session.version = attr.version or 0;
+ session.streamid = m_random(1000000, 99999999);
+ print(session, session.host, "Client opened stream");
+ send("<?xml version='1.0'?>");
+ send(format("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='%s' from='%s' >", session.streamid, session.host));
+ --send("<stream:features>");
+ --send("<mechanism>PLAIN</mechanism>");
+ --send [[<register xmlns="http://jabber.org/features/iq-register"/> ]]
+ --send("</stream:features>");
+ log("info", "core", "Stream opened successfully");
+ session.notopen = nil;
+ return;
+ end
+ error("Client failed to open stream successfully");
+ end
+ if name ~= "iq" and name ~= "presence" and name ~= "message" then
+ error("Client sent invalid top-level stanza");
+ end
+ stanza = st.stanza(name, { to = attr.to, type = attr.type, id = attr.id, xmlns = curr_ns });
+ curr_tag = stanza;
+ else
+ attr.xmlns = curr_ns;
+ stanza:tag(name, attr);
+ end
+ end
+ function xml_handlers:CharacterData(data)
+ if stanza then
+ t_insert(chardata, data);
+ end
+ end
+ function xml_handlers:EndElement(name)
+ curr_ns,name = name:match("^(.+):(%w+)$");
+ --print("<"..name.."/>", tostring(stanza), tostring(#stanza.last_add < 1), tostring(stanza.last_add[#stanza.last_add].name));
+ if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then error("XML parse error in client stream"); end
+ if stanza and #chardata > 0 then
+ stanza:text(t_concat(chardata));
+ print("Char data:", t_concat(chardata));
+ chardata = {};
+ end
+ -- Complete stanza
+ print(name, tostring(#stanza.last_add));
+ if #stanza.last_add == 0 then
+ session.stanza_dispatch(stanza);
+ stanza = nil;
+ else
+ stanza:up();
+ end
+ end
+ return xml_handlers;
+end
+
+return init_xmlhandlers;