core/xmlhandlers.lua

changeset 625
cad4dcfbf295
parent 615
4ae3e81513f3
child 758
b1885732e979
equal deleted inserted replaced
624:04fe1a00aa16 625:cad4dcfbf295
45 ["http://www.w3.org/XML/1998/namespace"] = "xml"; 45 ["http://www.w3.org/XML/1998/namespace"] = "xml";
46 } 46 }
47 47
48 function init_xmlhandlers(session, stream_callbacks) 48 function init_xmlhandlers(session, stream_callbacks)
49 local ns_stack = { "" }; 49 local ns_stack = { "" };
50 local curr_ns = ""; 50 local curr_ns, name = "";
51 local curr_tag; 51 local curr_tag;
52 local chardata = {}; 52 local chardata = {};
53 local xml_handlers = {}; 53 local xml_handlers = {};
54 local log = session.log or default_log; 54 local log = session.log or default_log;
55
56 local send = session.send;
57 55
58 local cb_streamopened = stream_callbacks.streamopened; 56 local cb_streamopened = stream_callbacks.streamopened;
59 local cb_streamclosed = stream_callbacks.streamclosed; 57 local cb_streamclosed = stream_callbacks.streamclosed;
60 local cb_error = stream_callbacks.error or function (session, e) error("XML stream error: "..tostring(e)); end; 58 local cb_error = stream_callbacks.error or function (session, e) error("XML stream error: "..tostring(e)); end;
61 local cb_handlestanza = stream_callbacks.handlestanza; 59 local cb_handlestanza = stream_callbacks.handlestanza;
62 60
63 local stream_ns = stream_callbacks.ns; 61 local stream_tag = stream_callbacks.stream_tag;
64 62
65 local stanza 63 local stanza
66 function xml_handlers:StartElement(name, attr) 64 function xml_handlers:StartElement(tagname, attr)
67 if stanza and #chardata > 0 then 65 if stanza and #chardata > 0 then
68 -- We have some character data in the buffer 66 -- We have some character data in the buffer
69 stanza:text(t_concat(chardata)); 67 stanza:text(t_concat(chardata));
70 chardata = {}; 68 chardata = {};
71 end 69 end
72 curr_ns,name = name:match("^(.+)|([%w%-]+)$"); 70 local curr_ns,name = tagname:match("^(.+)|([%w%-]+)$");
73 if curr_ns ~= "jabber:server" then 71 if curr_ns ~= "jabber:server" then
74 attr.xmlns = curr_ns; 72 attr.xmlns = curr_ns;
75 end 73 end
76 74
77 -- FIXME !!!!! 75 -- FIXME !!!!!
89 end 87 end
90 end 88 end
91 89
92 if not stanza then --if we are not currently inside a stanza 90 if not stanza then --if we are not currently inside a stanza
93 if session.notopen then 91 if session.notopen then
94 if name == "stream" and curr_ns == stream_ns then 92 if tagname == stream_tag then
95 if cb_streamopened then 93 if cb_streamopened then
96 cb_streamopened(session, attr); 94 cb_streamopened(session, attr);
97 end 95 end
98 else 96 else
99 -- Garbage before stream? 97 -- Garbage before stream?
118 function xml_handlers:CharacterData(data) 116 function xml_handlers:CharacterData(data)
119 if stanza then 117 if stanza then
120 t_insert(chardata, data); 118 t_insert(chardata, data);
121 end 119 end
122 end 120 end
123 function xml_handlers:EndElement(name) 121 function xml_handlers:EndElement(tagname)
124 curr_ns,name = name:match("^(.+)|([%w%-]+)$"); 122 curr_ns,name = tagname:match("^(.+)|([%w%-]+)$");
125 if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 123 if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then
126 if name == "stream" then 124 if tagname == stream_tag then
127 if cb_streamclosed then 125 if cb_streamclosed then
128 cb_streamclosed(session); 126 cb_streamclosed(session);
129 end 127 end
130 return; 128 return;
131 elseif name == "error" then 129 elseif name == "error" then

mercurial