core/xmlhandlers.lua

changeset 3834
1bb987224406
parent 3833
f1833a3e0b4e
child 3835
f6f0c460e1f2
equal deleted inserted replaced
3833:f1833a3e0b4e 3834:1bb987224406
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10
11 require "util.stanza"
12
13 local st = stanza;
14 local tostring = tostring;
15 local t_insert = table.insert;
16 local t_concat = table.concat;
17
18 local default_log = require "util.logger".init("xmlhandlers");
19
20 local error = error;
21
22 module "xmlhandlers"
23
24 local ns_prefixes = {
25 ["http://www.w3.org/XML/1998/namespace"] = "xml";
26 };
27
28 local xmlns_streams = "http://etherx.jabber.org/streams";
29
30 local ns_separator = "\1";
31 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
32
33 function init_xmlhandlers(session, stream_callbacks)
34 local chardata = {};
35 local xml_handlers = {};
36 local log = session.log or default_log;
37
38 local cb_streamopened = stream_callbacks.streamopened;
39 local cb_streamclosed = stream_callbacks.streamclosed;
40 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
41 local cb_handlestanza = stream_callbacks.handlestanza;
42
43 local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
44 local stream_tag = stream_ns..ns_separator..(stream_callbacks.stream_tag or "stream");
45 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
46
47 local stream_default_ns = stream_callbacks.default_ns;
48
49 local stanza;
50 function xml_handlers:StartElement(tagname, attr)
51 if stanza and #chardata > 0 then
52 -- We have some character data in the buffer
53 stanza:text(t_concat(chardata));
54 chardata = {};
55 end
56 local curr_ns,name = tagname:match(ns_pattern);
57 if name == "" then
58 curr_ns, name = "", curr_ns;
59 end
60
61 if curr_ns ~= stream_default_ns then
62 attr.xmlns = curr_ns;
63 end
64
65 -- FIXME !!!!!
66 for i=1,#attr do
67 local k = attr[i];
68 attr[i] = nil;
69 local ns, nm = k:match(ns_pattern);
70 if nm ~= "" then
71 ns = ns_prefixes[ns];
72 if ns then
73 attr[ns..":"..nm] = attr[k];
74 attr[k] = nil;
75 end
76 end
77 end
78
79 if not stanza then --if we are not currently inside a stanza
80 if session.notopen then
81 if tagname == stream_tag then
82 if cb_streamopened then
83 cb_streamopened(session, attr);
84 end
85 else
86 -- Garbage before stream?
87 cb_error(session, "no-stream");
88 end
89 return;
90 end
91 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
92 cb_error(session, "invalid-top-level-element");
93 end
94
95 stanza = st.stanza(name, attr);
96 else -- we are inside a stanza, so add a tag
97 attr.xmlns = nil;
98 if curr_ns ~= stream_default_ns then
99 attr.xmlns = curr_ns;
100 end
101 stanza:tag(name, attr);
102 end
103 end
104 function xml_handlers:CharacterData(data)
105 if stanza then
106 t_insert(chardata, data);
107 end
108 end
109 function xml_handlers:EndElement(tagname)
110 if stanza then
111 if #chardata > 0 then
112 -- We have some character data in the buffer
113 stanza:text(t_concat(chardata));
114 chardata = {};
115 end
116 -- Complete stanza
117 local last_add = stanza.last_add;
118 if not last_add or #last_add == 0 then
119 if tagname ~= stream_error_tag then
120 cb_handlestanza(session, stanza);
121 else
122 cb_error(session, "stream-error", stanza);
123 end
124 stanza = nil;
125 else
126 stanza:up();
127 end
128 else
129 if tagname == stream_tag then
130 if cb_streamclosed then
131 cb_streamclosed(session);
132 end
133 else
134 local curr_ns,name = tagname:match(ns_pattern);
135 if name == "" then
136 curr_ns, name = "", curr_ns;
137 end
138 cb_error(session, "parse-error", "unexpected-element-close", name);
139 end
140 stanza, chardata = nil, {};
141 end
142 end
143 return xml_handlers;
144 end
145
146 return init_xmlhandlers;

mercurial