Presence unavailable on disconnect
authormatthew <devnull@localhost>
Sun, 24 Aug 2008 14:52:02 +0000 (14:52 +0000)
committermatthew <devnull@localhost>
Sun, 24 Aug 2008 14:52:02 +0000 (14:52 +0000)
TODO
core/stanza_dispatch.lua
main.lua
util/stanza.lua

diff --git a/TODO b/TODO
index 82d39c0f0b55da17315c76b2974b821ef93b5e8d..ed349dadfa48f58f20288c69dab5382cae37129c 100644 (file)
--- a/TODO
+++ b/TODO
@@ -4,3 +4,5 @@
 
 Further down the line:
 - Clustering
+- Pubsub/PEP
+- Plugins!
index 5f61f5fbaefdbad0265276e4ab25e47691c62fe4..2c83551736966cbba2040b5256188d23ff40cdc7 100644 (file)
@@ -106,7 +106,7 @@ function init_stanza_dispatcher(session)
                                                --local probe = st.presence { from = broadcast.attr.from, type = "probe" };
 
                                                for child in stanza:childtags() do
-                                                       broadcast:text(tostring(child));
+                                                       broadcast:add_child(child);
                                                end
                                                for contact_jid in pairs(session.roster) do
                                                        broadcast.attr.to = contact_jid;
index 0df6bbd18a28e240fc9e17d282ce44f6b9c867cd..7a423f47a8e46df6309e728541d1e8c2c5c2534d 100644 (file)
--- a/main.lua
+++ b/main.lua
@@ -132,6 +132,12 @@ function handler(conn, data, err)
                        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
                                hosts[session.host].sessions[session.username] = nil;
                                session = nil;
                                print("Disconnected: "..err);
@@ -154,8 +160,9 @@ setmetatable(_G, { __index = function (t, k) print("WARNING: ATTEMPT TO READ A N
 
 
 local protected_handler = function (...) local success, ret = pcall(handler, ...); if not success then print("ERROR on "..tostring((select(1, ...)))..": "..ret); end end;
+local protected_disconnect = function (...) local success, ret = pcall(disconnect, ...); if not success then print("ERROR on "..tostring((select(1, ...))).." disconnect: "..ret); end end;
 
-print( server.add( { listener = protected_handler, disconnect = disconnect }, 5222, "*", 1, nil ) )    -- server.add will send a status message
-print( server.add( { listener = protected_handler, disconnect = disconnect }, 5223, "*", 1, ssl_ctx ) )    -- server.add will send a status message
+print( server.add( { listener = protected_handler, disconnect = protected_disconnect }, 5222, "*", 1, nil ) )    -- server.add will send a status message
+print( server.add( { listener = protected_handler, disconnect = protected_disconnect }, 5223, "*", 1, ssl_ctx ) )    -- server.add will send a status message
 
 server.loop();
index b4deda39c2e105a86dfd7125c581b54f08965983..35277e9cdeeb5504acc2b4f9a9d960c4388a91a7 100644 (file)
@@ -6,7 +6,7 @@ local setmetatable  =  setmetatable;
 local pairs         =         pairs;
 local ipairs        =        ipairs;
 local type          =          type;
-
+local s_gsub        =   string.gsub;
 module "stanza"
 
 stanza_mt = {};
@@ -78,10 +78,21 @@ function stanza_mt:childtags()
                                            
 end
 
+do
+       local xml_entities = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
+       function xml_escape(s) return s_gsub(s, "['&<>\"]", xml_entities); end
+end
+
+local xml_escape = xml_escape;
+
 function stanza_mt.__tostring(t)
        local children_text = "";
        for n, child in ipairs(t) do
-               children_text = children_text .. tostring(child);
+               if type(child) == "string" then 
+                       children_text = children_text .. xml_escape(child);
+               else
+                       children_text = children_text .. tostring(child);
+               end
        end
 
        local attr_string = "";