# HG changeset patch # User matthew # Date 1219589522 0 # Node ID 09c3845ed442f8fdd157f1810fdd93ce54e946de # Parent f674eb704134e572ff3e9e33a235060aa511c02e Presence unavailable on disconnect diff -r f674eb704134 -r 09c3845ed442 TODO --- a/TODO Sun Aug 24 13:29:01 2008 +0000 +++ b/TODO Sun Aug 24 14:52:02 2008 +0000 @@ -4,3 +4,5 @@ Further down the line: - Clustering +- Pubsub/PEP +- Plugins! diff -r f674eb704134 -r 09c3845ed442 core/stanza_dispatch.lua --- a/core/stanza_dispatch.lua Sun Aug 24 13:29:01 2008 +0000 +++ b/core/stanza_dispatch.lua Sun Aug 24 14:52:02 2008 +0000 @@ -106,7 +106,7 @@ --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; diff -r f674eb704134 -r 09c3845ed442 main.lua --- a/main.lua Sun Aug 24 13:29:01 2008 +0000 +++ b/main.lua Sun Aug 24 14:52:02 2008 +0000 @@ -132,6 +132,12 @@ 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 @@ 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(); diff -r f674eb704134 -r 09c3845ed442 util/stanza.lua --- a/util/stanza.lua Sun Aug 24 13:29:01 2008 +0000 +++ b/util/stanza.lua Sun Aug 24 14:52:02 2008 +0000 @@ -6,7 +6,7 @@ local pairs = pairs; local ipairs = ipairs; local type = type; - +local s_gsub = string.gsub; module "stanza" stanza_mt = {}; @@ -78,10 +78,21 @@ end +do + local xml_entities = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }; + 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 = "";