- Remove some debugging code
authorMatthew Wild <mwild1@gmail.com>
Sun, 24 Aug 2008 21:48:27 +0000 (22:48 +0100)
committerMatthew Wild <mwild1@gmail.com>
Sun, 24 Aug 2008 21:48:27 +0000 (22:48 +0100)
- Don't allow stanzas to be sent before successful auth (needs further review)
- Make init_xmlhandlers local
- Fix indentation in main.lua
- Fix disconnect logic (it broke if client disconnected before auth, etc.)

DEPENDS [new file with mode: 0644]
core/stanza_dispatch.lua
core/xmlhandlers.lua
main.lua

diff --git a/DEPENDS b/DEPENDS
new file mode 100644 (file)
index 0000000..b284cc2
--- /dev/null
+++ b/DEPENDS
@@ -0,0 +1,8 @@
+The easiest way to install dependencies is using the luarocks tool.
+
+Rocks:
+luaexpat
+luasocket
+
+Non-rocks:
+LuaSec for SSL connections
index 2c83551736966cbba2040b5256188d23ff40cdc7..e76d6dddcc390fb9091c145747b9c26ebb1eb0a5 100644 (file)
@@ -32,7 +32,6 @@ function init_stanza_dispatcher(session)
                                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
@@ -96,7 +95,9 @@ function init_stanza_dispatcher(session)
                                                send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id));
                                                return;
                                        end
-                               elseif stanza.name == "presence" then
+                               end
+                               if not session.username then log("warn", "Attempt to use an unauthed stream!"); return; end
+                               if stanza.name == "presence" then
                                        if session.roster then
                                                local initial_presence = not session.last_presence;
                                                session.last_presence = stanza;
@@ -112,7 +113,6 @@ function init_stanza_dispatcher(session)
                                                        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]
@@ -137,7 +137,7 @@ function init_stanza_dispatcher(session)
                                                -- Probe for our contacts' presence
                                        end
                                end
-                       else
+                       elseif session.username then
                        --end                           
                        --if stanza.attr.to and ((not hosts[stanza.attr.to]) or hosts[stanza.attr.to].type ~= "local") then
                                -- Need to route stanza
@@ -147,4 +147,3 @@ function init_stanza_dispatcher(session)
                end
 
 end
-
index 4d536ce3634f0c6cbf0f317a39342cc8c7687706..b9c557fae935d8757d03d6436898aea3c9574564 100644 (file)
@@ -42,7 +42,7 @@ function init_xmlhandlers(session)
                                                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(format("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='%s' from='%s' version='1.0'>", session.streamid, session.host));
                                                --send("<stream:features>");
                                                --send("<mechanism>PLAIN</mechanism>");
                                                --send [[<register xmlns="http://jabber.org/features/iq-register"/> ]]
index 7a423f47a8e46df6309e728541d1e8c2c5c2534d..3221418cab0c357be9811f712e6a5428fbe615ac 100644 (file)
--- a/main.lua
+++ b/main.lua
@@ -10,7 +10,7 @@ function log(type, area, message)
 end
 
 require "core.stanza_dispatch"
-init_xmlhandlers = require "core.xmlhandlers"
+local init_xmlhandlers = require "core.xmlhandlers"
 require "core.rostermanager"
 require "core.offlinemessage"
 require "core.usermanager"
@@ -103,7 +103,7 @@ end
 
 function handler(conn, data, err)
        local session = sessions[conn];
-       
+
        if not session then
                sessions[conn] = { conn = conn, notopen = true, priority = 0 };
                session = sessions[conn];
@@ -114,6 +114,7 @@ function handler(conn, data, err)
                do
                        local conn_name = tostring(conn):match("%w+$");
                        log = function (type, area, message) mainlog(type, conn_name, message); end
+                       --log = function () end
                end
                local print = function (...) log("info", "core", t_concatall({...}, "\t")); end
                session.log = log;
@@ -125,24 +126,27 @@ function handler(conn, data, err)
                local send = function (data) print("Sending...", tostring(data)); conn.write(tostring(data)); end;
                session.send, session.send_to = send, send_to;
 
-                       print("Client connected");
+               print("Client connected");
                
-                       session.stanza_dispatch = init_stanza_dispatcher(session);
-                       session.xml_handlers = init_xmlhandlers(session);
-                       session.parser = lxp.new(session.xml_handlers, ":");
+               session.stanza_dispatch = init_stanza_dispatcher(session);
+               session.xml_handlers = init_xmlhandlers(session);
+               session.parser = lxp.new(session.xml_handlers, ":");
                        
-                       function session.disconnect(err)
-                               if session.last_presence.attr.type ~= "unavailable" then
-                                       local pres = st.presence{ type = "unavailable" };
-                                       if err == "closed" then err = "connection closed"; end
-                                       pres:tag("status"):text("Disconnected: "..err);
-                                       session.stanza_dispatch(pres);
-                               end
+               function session.disconnect(err)
+                       if session.last_presence and session.last_presence.attr.type ~= "unavailable" then
+                               local pres = st.presence{ type = "unavailable" };
+                               if err == "closed" then err = "connection closed"; end
+                               pres:tag("status"):text("Disconnected: "..err);
+                               session.stanza_dispatch(pres);
+                       end
+                       if session.username then
                                hosts[session.host].sessions[session.username] = nil;
-                               session = nil;
-                               print("Disconnected: "..err);
                        end
+                       session = nil;
+                       print("Disconnected: "..err);
+                       collectgarbage("collect");
                end
+       end
        if data then
                session.parser:parse(data);
        end