mod_posix: Updated to use module:get_option instead of configmanager
[prosody.git] / core / xmlhandlers.lua
1 -- Prosody IM
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=1,#attr do
64                                 local k = attr[i];
65                                 attr[i] = nil;
66                                 local ns, nm = k:match("^([^|]+)|?([^|]-)$")
67                                 if ns and nm then
68                                         ns = ns_prefixes[ns]; 
69                                         if ns then 
70                                                 attr[ns..":"..nm] = attr[k];
71                                                 attr[k] = nil;
72                                         end
73                                 end
74                         end
75                         
76                         if not stanza then --if we are not currently inside a stanza
77                                 if session.notopen then
78                                         if tagname == stream_tag then
79                                                 if cb_streamopened then
80                                                         cb_streamopened(session, attr);
81                                                 end
82                                         else
83                                                 -- Garbage before stream?
84                                                 cb_error(session, "no-stream");
85                                         end
86                                         return;
87                                 end
88                                 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
89                                         cb_error(session, "invalid-top-level-element");
90                                 end
91                                 
92                                 stanza = st.stanza(name, attr);
93                                 curr_tag = stanza;
94                         else -- we are inside a stanza, so add a tag
95                                 attr.xmlns = nil;
96                                 if curr_ns ~= stream_default_ns then
97                                         attr.xmlns = curr_ns;
98                                 end
99                                 stanza:tag(name, attr);
100                         end
101                 end
102                 function xml_handlers:CharacterData(data)
103                         if stanza then
104                                 t_insert(chardata, data);
105                         end
106                 end
107                 function xml_handlers:EndElement(tagname)
108                         curr_ns,name = tagname:match("^(.-)|?([^%|]-)$");
109                         if not name then
110                                 curr_ns, name = "", curr_ns;
111                         end
112                         if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 
113                                 if tagname == stream_tag then
114                                         if cb_streamclosed then
115                                                 cb_streamclosed(session);
116                                         end
117                                         return;
118                                 elseif name == "error" then
119                                         cb_error(session, "stream-error", stanza);
120                                 else
121                                         cb_error(session, "parse-error", "unexpected-element-close", name);
122                                 end
123                         end
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         return xml_handlers;
138 end
139
140 return init_xmlhandlers;