mod_console: Don't allow bang bang as the first command in a session, or when the...
[prosody.git] / util / xmppstream.lua
index a0be4a8e957cff05ce4386407f89ccd361ed09f6..2f232fd81127fe71626ab5bbb73941618af1c539 100644 (file)
@@ -1,6 +1,6 @@
 -- Prosody IM
--- Copyright (C) 2008-2009 Matthew Wild
--- Copyright (C) 2008-2009 Waqas Hussain
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
 -- 
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
@@ -32,8 +32,8 @@ local ns_separator = "\1";
 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
 
 function new_sax_handlers(session, stream_callbacks)
-       local chardata = {};
        local xml_handlers = {};
+       
        local log = session.log or default_log;
        
        local cb_streamopened = stream_callbacks.streamopened;
@@ -47,7 +47,7 @@ function new_sax_handlers(session, stream_callbacks)
        
        local stream_default_ns = stream_callbacks.default_ns;
        
-       local stanza;
+       local chardata, stanza = {};
        function xml_handlers:StartElement(tagname, attr)
                if stanza and #chardata > 0 then
                        -- We have some character data in the buffer
@@ -69,8 +69,8 @@ function new_sax_handlers(session, stream_callbacks)
                        attr[i] = nil;
                        local ns, nm = k:match(ns_pattern);
                        if nm ~= "" then
-                               ns = ns_prefixes[ns]; 
-                               if ns then 
+                               ns = ns_prefixes[ns];
+                               if ns then
                                        attr[ns..":"..nm] = attr[k];
                                        attr[k] = nil;
                                end
@@ -95,10 +95,6 @@ function new_sax_handlers(session, stream_callbacks)
                        
                        stanza = st.stanza(name, attr);
                else -- we are inside a stanza, so add a tag
-                       attr.xmlns = nil;
-                       if curr_ns ~= stream_default_ns then
-                               attr.xmlns = curr_ns;
-                       end
                        stanza:tag(name, attr);
                end
        end
@@ -140,11 +136,35 @@ function new_sax_handlers(session, stream_callbacks)
                        stanza, chardata = nil, {};
                end
        end
-       return xml_handlers;
+       
+       local function reset()
+               stanza, chardata = nil, {};
+       end
+       
+       local function set_session(stream, new_session)
+               session = new_session;
+               log = new_session.log or default_log;
+       end
+       
+       return xml_handlers, { reset = reset, set_session = set_session };
 end
 
 function new(session, stream_callbacks)
-       return new_parser(new_sax_handlers(session, stream_callbacks), ns_separator);
+       local handlers, meta = new_sax_handlers(session, stream_callbacks);
+       local parser = new_parser(handlers, ns_separator);
+       local parse = parser.parse;
+
+       return {
+               reset = function ()
+                       parser = new_parser(handlers, ns_separator);
+                       parse = parser.parse;
+                       meta.reset();
+               end,
+               feed = function (self, data)
+                       return parse(parser, data);
+               end,
+               set_session = meta.set_session;
+       };
 end
 
 return _M;