Comparison

core/xmlhandlers.lua @ 403:da92afa267cf

Merging with main branch.
author Tobias Markmann <tm@ayena.de>
date Sun, 23 Nov 2008 20:44:48 +0100
parent 355:e25c8d91b37f
child 519:cccd610a0ef9
comparison
equal deleted inserted replaced
402:50f1c09541cd 403:da92afa267cf
23 23
24 local ns_prefixes = { 24 local ns_prefixes = {
25 ["http://www.w3.org/XML/1998/namespace"] = "xml"; 25 ["http://www.w3.org/XML/1998/namespace"] = "xml";
26 } 26 }
27 27
28 function init_xmlhandlers(session, streamopened) 28 function init_xmlhandlers(session, stream_callbacks)
29 local ns_stack = { "" }; 29 local ns_stack = { "" };
30 local curr_ns = ""; 30 local curr_ns = "";
31 local curr_tag; 31 local curr_tag;
32 local chardata = {}; 32 local chardata = {};
33 local xml_handlers = {}; 33 local xml_handlers = {};
34 local log = session.log or default_log; 34 local log = session.log or default_log;
35 --local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end 35 --local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end
36 36
37 local send = session.send; 37 local send = session.send;
38
39 local cb_streamopened = stream_callbacks.streamopened;
40 local cb_streamclosed = stream_callbacks.streamclosed;
38 41
39 local stanza 42 local stanza
40 function xml_handlers:StartElement(name, attr) 43 function xml_handlers:StartElement(name, attr)
41 if stanza and #chardata > 0 then 44 if stanza and #chardata > 0 then
42 -- We have some character data in the buffer 45 -- We have some character data in the buffer
64 end 67 end
65 68
66 if not stanza then --if we are not currently inside a stanza 69 if not stanza then --if we are not currently inside a stanza
67 if session.notopen then 70 if session.notopen then
68 if name == "stream" then 71 if name == "stream" then
69 streamopened(session, attr); 72 if cb_streamopened then
73 cb_streamopened(session, attr);
74 end
70 return; 75 return;
71 end 76 end
72 error("Client failed to open stream successfully"); 77 error("Client failed to open stream successfully");
73 end 78 end
74 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then 79 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
75 error("Client sent invalid top-level stanza"); 80 error("Client sent invalid top-level stanza");
76 end 81 end
77 82
78 stanza = st.stanza(name, attr); --{ to = attr.to, type = attr.type, id = attr.id, xmlns = curr_ns }); 83 stanza = st.stanza(name, attr);
79 curr_tag = stanza; 84 curr_tag = stanza;
80 else -- we are inside a stanza, so add a tag 85 else -- we are inside a stanza, so add a tag
81 attr.xmlns = nil; 86 attr.xmlns = nil;
82 if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then 87 if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then
83 attr.xmlns = curr_ns; 88 attr.xmlns = curr_ns;
90 t_insert(chardata, data); 95 t_insert(chardata, data);
91 end 96 end
92 end 97 end
93 function xml_handlers:EndElement(name) 98 function xml_handlers:EndElement(name)
94 curr_ns,name = name:match("^(.+)|([%w%-]+)$"); 99 curr_ns,name = name:match("^(.+)|([%w%-]+)$");
95 if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 100 if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then
96 if name == "stream" then 101 if name == "stream" then
97 log("debug", "Stream closed"); 102 log("debug", "Stream closed");
98 sm_destroy_session(session); 103 if cb_streamclosed then
104 cb_streamclosed(session);
105 end
99 return; 106 return;
100 elseif name == "error" then 107 elseif name == "error" then
101 error("Stream error: "..tostring(name)..": "..tostring(stanza)); 108 error("Stream error: "..tostring(name)..": "..tostring(stanza));
102 else 109 else
103 error("XML parse error in client stream"); 110 error("XML parse error in client stream with element: "..name);
104 end 111 end
105 end 112 end
106 if stanza and #chardata > 0 then 113 if stanza and #chardata > 0 then
107 -- We have some character data in the buffer 114 -- We have some character data in the buffer
108 stanza:text(t_concat(chardata)); 115 stanza:text(t_concat(chardata));