xmlhandlers: More refactoring, split up stream_ns and stream_tag, add stream_error_ta...
[prosody.git] / core / xmlhandlers.lua
index fc070115473be34fa59915319c5009fa5e8cc0a9..6e693d8669187314f6d704204d7e0eceddde4006 100644 (file)
@@ -1,4 +1,4 @@
--- Prosody IM v0.4
+-- Prosody IM
 -- Copyright (C) 2008-2009 Matthew Wild
 -- Copyright (C) 2008-2009 Waqas Hussain
 -- 
@@ -27,9 +27,13 @@ local ns_prefixes = {
                                                ["http://www.w3.org/XML/1998/namespace"] = "xml";
                                }
 
+local xmlns_streams = "http://etherx.jabber.org/streams";
+
+local ns_separator = "\1";
+local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
+
 function init_xmlhandlers(session, stream_callbacks)
                local ns_stack = { "" };
-               local curr_ns, name = "";
                local curr_tag;
                local chardata = {};
                local xml_handlers = {};
@@ -40,7 +44,10 @@ function init_xmlhandlers(session, stream_callbacks)
                local cb_error = stream_callbacks.error or function (session, e) error("XML stream error: "..tostring(e)); end;
                local cb_handlestanza = stream_callbacks.handlestanza;
                
-               local stream_tag = stream_callbacks.stream_tag;
+               local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
+               local stream_tag = stream_ns..ns_separator..(stream_callbacks.stream_tag or "stream");
+               local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
+               
                local stream_default_ns = stream_callbacks.default_ns;
                
                local stanza
@@ -50,8 +57,8 @@ function init_xmlhandlers(session, stream_callbacks)
                                stanza:text(t_concat(chardata));
                                chardata = {};
                        end
-                       local curr_ns,name = tagname:match("^(.-)|?([^%|]-)$");
-                       if not name then
+                       local curr_ns,name = tagname:match(ns_pattern);
+                       if name == "" then
                                curr_ns, name = "", curr_ns;
                        end
 
@@ -60,13 +67,14 @@ function init_xmlhandlers(session, stream_callbacks)
                        end
                        
                        -- FIXME !!!!!
-                       for i, k in ipairs(attr) do
-                               local ns, nm = k:match("^([^|]+)|?([^|]-)$")
-                               if ns and nm then
+                       for i=1,#attr do
+                               local k = attr[i];
+                               attr[i] = nil;
+                               local ns, nm = k:match(ns_pattern);
+                               if nm ~= "" then
                                        ns = ns_prefixes[ns]; 
                                        if ns then 
                                                attr[ns..":"..nm] = attr[k];
-                                               attr[i] = ns..":"..nm;
                                                attr[k] = nil;
                                        end
                                end
@@ -104,8 +112,8 @@ function init_xmlhandlers(session, stream_callbacks)
                        end
                end
                function xml_handlers:EndElement(tagname)
-                       curr_ns,name = tagname:match("^(.-)|?([^%|]-)$");
-                       if not name then
+                       local curr_ns,name = tagname:match(ns_pattern);
+                       if name == "" then
                                curr_ns, name = "", curr_ns;
                        end
                        if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 
@@ -113,26 +121,25 @@ function init_xmlhandlers(session, stream_callbacks)
                                        if cb_streamclosed then
                                                cb_streamclosed(session);
                                        end
-                                       return;
                                elseif name == "error" then
                                        cb_error(session, "stream-error", stanza);
                                else
                                        cb_error(session, "parse-error", "unexpected-element-close", name);
                                end
+                               stanza, chardata = nil, {};
+                               return;
                        end
-                       if stanza then
-                               if #chardata > 0 then
-                                       -- We have some character data in the buffer
-                                       stanza:text(t_concat(chardata));
-                                       chardata = {};
-                               end
-                               -- Complete stanza
-                               if #stanza.last_add == 0 then
-                                       cb_handlestanza(session, stanza);
-                                       stanza = nil;
-                               else
-                                       stanza:up();
-                               end
+                       if #chardata > 0 then
+                               -- We have some character data in the buffer
+                               stanza:text(t_concat(chardata));
+                               chardata = {};
+                       end
+                       -- Complete stanza
+                       if #stanza.last_add == 0 then
+                               cb_handlestanza(session, stanza);
+                               stanza = nil;
+                       else
+                               stanza:up();
                        end
                end
        return xml_handlers;