core/xmlhandlers.lua

changeset 150
d09b8a1ab046
parent 149
40e443eacbbd
parent 148
4c0dcd245d34
child 165
4adc53e03b4d
equal deleted inserted replaced
149:40e443eacbbd 150:d09b8a1ab046
13 local t_remove = table.remove; 13 local t_remove = table.remove;
14 local t_concat = table.concat; 14 local t_concat = table.concat;
15 local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end 15 local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end
16 local sm_destroy_session = import("core.sessionmanager", "destroy_session"); 16 local sm_destroy_session = import("core.sessionmanager", "destroy_session");
17 17
18 local default_log = require "util.logger".init("xmlhandlers");
19
18 local error = error; 20 local error = error;
19 21
20 module "xmlhandlers" 22 module "xmlhandlers"
21 23
22 local ns_prefixes = { 24 local ns_prefixes = {
27 local ns_stack = { "" }; 29 local ns_stack = { "" };
28 local curr_ns = ""; 30 local curr_ns = "";
29 local curr_tag; 31 local curr_tag;
30 local chardata = {}; 32 local chardata = {};
31 local xml_handlers = {}; 33 local xml_handlers = {};
32 local log = session.log; 34 local log = session.log or default_log;
33 local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end 35 --local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end
34 36
35 local send = session.send; 37 local send = session.send;
36 38
37 local stanza 39 local stanza
38 function xml_handlers:StartElement(name, attr) 40 function xml_handlers:StartElement(name, attr)
39 if stanza and #chardata > 0 then 41 if stanza and #chardata > 0 then
40 -- We have some character data in the buffer 42 -- We have some character data in the buffer
41 stanza:text(t_concat(chardata)); 43 stanza:text(t_concat(chardata));
42 chardata = {}; 44 chardata = {};
43 end 45 end
44 curr_ns,name = name:match("^(.+):(%w+)$"); 46 curr_ns,name = name:match("^(.+)|([%w%-]+)$");
45 if not stanza then 47 if curr_ns ~= "jabber:server" then
48 attr.xmlns = curr_ns;
49 end
50
51 -- FIXME !!!!!
52 for i, k in ipairs(attr) do
53 if type(k) == "string" then
54 local ns, nm = k:match("^([^|]+)|?([^|]-)$")
55 if ns and nm then
56 ns = ns_prefixes[ns];
57 if ns then
58 attr[ns..":"..nm] = attr[k];
59 attr[i] = ns..":"..nm;
60 attr[k] = nil;
61 end
62 end
63 end
64 end
65
66 if not stanza then --if we are not currently inside a stanza
46 if session.notopen then 67 if session.notopen then
47 if name == "stream" then 68 if name == "stream" then
48 streamopened(session, attr); 69 streamopened(session, attr);
49 return; 70 return;
50 end 71 end
51 error("Client failed to open stream successfully"); 72 error("Client failed to open stream successfully");
52 end 73 end
53 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then 74 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
54 error("Client sent invalid top-level stanza"); 75 error("Client sent invalid top-level stanza");
55 end 76 end
56 attr.xmlns = curr_ns; 77
57 stanza = st.stanza(name, attr); --{ to = attr.to, type = attr.type, id = attr.id, xmlns = curr_ns }); 78 stanza = st.stanza(name, attr); --{ to = attr.to, type = attr.type, id = attr.id, xmlns = curr_ns });
58 curr_tag = stanza; 79 curr_tag = stanza;
59 else 80 else -- we are inside a stanza, so add a tag
60 attr.xmlns = curr_ns; 81 attr.xmlns = nil;
82 if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then
83 attr.xmlns = curr_ns;
84 end
61 stanza:tag(name, attr); 85 stanza:tag(name, attr);
62 end 86 end
63 end 87 end
64 function xml_handlers:CharacterData(data) 88 function xml_handlers:CharacterData(data)
65 if stanza then 89 if stanza then
66 t_insert(chardata, data); 90 t_insert(chardata, data);
67 end 91 end
68 end 92 end
69 function xml_handlers:EndElement(name) 93 function xml_handlers:EndElement(name)
70 curr_ns,name = name:match("^(.+):(%w+)$"); 94 curr_ns,name = name:match("^(.+)|([%w%-]+)$");
71 if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 95 if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then
72 if name == "stream" then 96 if name == "stream" then
73 log("debug", "Stream closed"); 97 log("debug", "Stream closed");
74 sm_destroy_session(session); 98 sm_destroy_session(session);
75 return; 99 return;
100 elseif name == "error" then
101 error("Stream error: "..tostring(name)..": "..tostring(stanza));
76 else 102 else
77 error("XML parse error in client stream"); 103 error("XML parse error in client stream");
78 end 104 end
79 end 105 end
80 if stanza and #chardata > 0 then 106 if stanza and #chardata > 0 then

mercurial