xmlhandlers: Removed an unnecessary check
[prosody.git] / core / xmlhandlers.lua
1 -- Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 pairs = pairs;
16 local ipairs = ipairs;
17 local t_insert = table.insert;
18 local t_concat = table.concat;
19
20 local default_log = require "util.logger".init("xmlhandlers");
21
22 local error = error;
23
24 module "xmlhandlers"
25
26 local ns_prefixes = {
27                                                 ["http://www.w3.org/XML/1998/namespace"] = "xml";
28                                 }
29
30 function init_xmlhandlers(session, stream_callbacks)
31                 local ns_stack = { "" };
32                 local curr_ns, name = "";
33                 local curr_tag;
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_tag = stream_callbacks.stream_tag;
44                 local stream_default_ns = stream_callbacks.default_ns;
45                 
46                 local stanza
47                 function xml_handlers:StartElement(tagname, attr)
48                         if stanza and #chardata > 0 then
49                                 -- We have some character data in the buffer
50                                 stanza:text(t_concat(chardata));
51                                 chardata = {};
52                         end
53                         local curr_ns,name = tagname:match("^(.-)|?([^%|]-)$");
54                         if not name then
55                                 curr_ns, name = "", curr_ns;
56                         end
57
58                         if curr_ns ~= stream_default_ns then
59                                 attr.xmlns = curr_ns;
60                         end
61                         
62                         -- FIXME !!!!!
63                         for i, k in ipairs(attr) do
64                                 local ns, nm = k:match("^([^|]+)|?([^|]-)$")
65                                 if ns and nm then
66                                         ns = ns_prefixes[ns]; 
67                                         if ns then 
68                                                 attr[ns..":"..nm] = attr[k];
69                                                 attr[i] = ns..":"..nm;
70                                                 attr[k] = nil;
71                                         end
72                                 end
73                         end
74                         
75                         if not stanza then --if we are not currently inside a stanza
76                                 if session.notopen then
77                                         if tagname == stream_tag then
78                                                 if cb_streamopened then
79                                                         cb_streamopened(session, attr);
80                                                 end
81                                         else
82                                                 -- Garbage before stream?
83                                                 cb_error(session, "no-stream");
84                                         end
85                                         return;
86                                 end
87                                 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
88                                         cb_error(session, "invalid-top-level-element");
89                                 end
90                                 
91                                 stanza = st.stanza(name, attr);
92                                 curr_tag = stanza;
93                         else -- we are inside a stanza, so add a tag
94                                 attr.xmlns = nil;
95                                 if curr_ns ~= stream_default_ns then
96                                         attr.xmlns = curr_ns;
97                                 end
98                                 stanza:tag(name, attr);
99                         end
100                 end
101                 function xml_handlers:CharacterData(data)
102                         if stanza then
103                                 t_insert(chardata, data);
104                         end
105                 end
106                 function xml_handlers:EndElement(tagname)
107                         curr_ns,name = tagname:match("^(.-)|?([^%|]-)$");
108                         if not name then
109                                 curr_ns, name = "", curr_ns;
110                         end
111                         if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 
112                                 if tagname == stream_tag then
113                                         if cb_streamclosed then
114                                                 cb_streamclosed(session);
115                                         end
116                                         return;
117                                 elseif name == "error" then
118                                         cb_error(session, "stream-error", stanza);
119                                 else
120                                         cb_error(session, "parse-error", "unexpected-element-close", name);
121                                 end
122                         end
123                         if stanza then
124                                 if #chardata > 0 then
125                                         -- We have some character data in the buffer
126                                         stanza:text(t_concat(chardata));
127                                         chardata = {};
128                                 end
129                                 -- Complete stanza
130                                 if #stanza.last_add == 0 then
131                                         cb_handlestanza(session, stanza);
132                                         stanza = nil;
133                                 else
134                                         stanza:up();
135                                 end
136                         end
137                 end
138         return xml_handlers;
139 end
140
141 return init_xmlhandlers;