1 |
1 |
2 require "util.stanza" |
2 require "util.stanza" |
3 |
3 |
4 local st = stanza; |
4 local st = stanza; |
5 local tostring = tostring; |
5 local tostring = tostring; |
|
6 local pairs = pairs; |
|
7 local ipairs = ipairs; |
|
8 local type = type; |
|
9 local print = print; |
6 local format = string.format; |
10 local format = string.format; |
7 local m_random = math.random; |
11 local m_random = math.random; |
8 local t_insert = table.insert; |
12 local t_insert = table.insert; |
9 local t_remove = table.remove; |
13 local t_remove = table.remove; |
10 local t_concat = table.concat; |
14 local t_concat = table.concat; |
15 |
19 |
16 local error = error; |
20 local error = error; |
17 |
21 |
18 module "xmlhandlers" |
22 module "xmlhandlers" |
19 |
23 |
|
24 local ns_prefixes = { |
|
25 ["http://www.w3.org/XML/1998/namespace"] = "xml"; |
|
26 } |
|
27 |
20 function init_xmlhandlers(session, streamopened) |
28 function init_xmlhandlers(session, streamopened) |
21 local ns_stack = { "" }; |
29 local ns_stack = { "" }; |
22 local curr_ns = ""; |
30 local curr_ns = ""; |
23 local curr_tag; |
31 local curr_tag; |
24 local chardata = {}; |
32 local chardata = {}; |
25 local xml_handlers = {}; |
33 local xml_handlers = {}; |
26 local log = session.log or default_log; |
34 local log = session.log or default_log; |
27 local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end |
35 --local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end |
28 |
36 |
29 local send = session.send; |
37 local send = session.send; |
30 |
38 |
31 local stanza |
39 local stanza |
32 function xml_handlers:StartElement(name, attr) |
40 function xml_handlers:StartElement(name, attr) |
33 if stanza and #chardata > 0 then |
41 if stanza and #chardata > 0 then |
34 -- We have some character data in the buffer |
42 -- We have some character data in the buffer |
35 stanza:text(t_concat(chardata)); |
43 stanza:text(t_concat(chardata)); |
36 chardata = {}; |
44 chardata = {}; |
37 end |
45 end |
38 curr_ns,name = name:match("^(.+):([%w%-]+)$"); |
46 curr_ns,name = name:match("^(.+)|([%w%-]+)$"); |
39 attr.xmlns = curr_ns; |
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 |
40 |
65 |
41 if not stanza then --if we are not currently inside a stanza |
66 if not stanza then --if we are not currently inside a stanza |
42 if session.notopen then |
67 if session.notopen then |
43 if name == "stream" then |
68 if name == "stream" then |
44 streamopened(session, attr); |
69 streamopened(session, attr); |
51 end |
76 end |
52 |
77 |
53 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 }); |
54 curr_tag = stanza; |
79 curr_tag = stanza; |
55 else -- we are inside a stanza, so add a tag |
80 else -- we are inside a stanza, so add a tag |
56 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 |
57 stanza:tag(name, attr); |
85 stanza:tag(name, attr); |
58 end |
86 end |
59 end |
87 end |
60 function xml_handlers:CharacterData(data) |
88 function xml_handlers:CharacterData(data) |
61 if stanza then |
89 if stanza then |
62 t_insert(chardata, data); |
90 t_insert(chardata, data); |
63 end |
91 end |
64 end |
92 end |
65 function xml_handlers:EndElement(name) |
93 function xml_handlers:EndElement(name) |
66 curr_ns,name = name:match("^(.+):([%w%-]+)$"); |
94 curr_ns,name = name:match("^(.+)|([%w%-]+)$"); |
67 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 |
68 if name == "stream" then |
96 if name == "stream" then |
69 log("debug", "Stream closed"); |
97 log("debug", "Stream closed"); |
70 sm_destroy_session(session); |
98 sm_destroy_session(session); |
71 return; |
99 return; |