tools/ejabberd2prosody: Fixed private storage export
[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 type = type;
18 local print = print;
19 local format = string.format;
20 local m_random = math.random;
21 local t_insert = table.insert;
22 local t_remove = table.remove;
23 local t_concat = table.concat;
24 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
25 local sm_destroy_session = import("core.sessionmanager", "destroy_session");
26
27 local default_log = require "util.logger".init("xmlhandlers");
28
29 local error = error;
30
31 module "xmlhandlers"
32
33 local ns_prefixes = {
34                                                 ["http://www.w3.org/XML/1998/namespace"] = "xml";
35                                 }
36
37 function init_xmlhandlers(session, stream_callbacks)
38                 local ns_stack = { "" };
39                 local curr_ns, name = "";
40                 local curr_tag;
41                 local chardata = {};
42                 local xml_handlers = {};
43                 local log = session.log or default_log;
44                 
45                 local cb_streamopened = stream_callbacks.streamopened;
46                 local cb_streamclosed = stream_callbacks.streamclosed;
47                 local cb_error = stream_callbacks.error or function (session, e) error("XML stream error: "..tostring(e)); end;
48                 local cb_handlestanza = stream_callbacks.handlestanza;
49                 
50                 local stream_tag = stream_callbacks.stream_tag;
51                 local stream_default_ns = stream_callbacks.default_ns;
52                 
53                 local stanza
54                 function xml_handlers:StartElement(tagname, attr)
55                         if stanza and #chardata > 0 then
56                                 -- We have some character data in the buffer
57                                 stanza:text(t_concat(chardata));
58                                 chardata = {};
59                         end
60                         local curr_ns,name = tagname:match("^(.-)|?([^%|]-)$");
61                         if not name then
62                                 curr_ns, name = "", curr_ns;
63                         end
64
65                         if curr_ns ~= stream_default_ns then
66                                 attr.xmlns = curr_ns;
67                         end
68                         
69                         -- FIXME !!!!!
70                         for i, k in ipairs(attr) do
71                                 if type(k) == "string" then
72                                         local ns, nm = k:match("^([^|]+)|?([^|]-)$")
73                                         if ns and nm then
74                                                 ns = ns_prefixes[ns]; 
75                                                 if ns then 
76                                                         attr[ns..":"..nm] = attr[k];
77                                                         attr[i] = ns..":"..nm;
78                                                         attr[k] = nil;
79                                                 end
80                                         end
81                                 end
82                         end
83                         
84                         if not stanza then --if we are not currently inside a stanza
85                                 if session.notopen then
86                                         if tagname == stream_tag then
87                                                 if cb_streamopened then
88                                                         cb_streamopened(session, attr);
89                                                 end
90                                         else
91                                                 -- Garbage before stream?
92                                                 cb_error(session, "no-stream");
93                                         end
94                                         return;
95                                 end
96                                 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
97                                         cb_error(session, "invalid-top-level-element");
98                                 end
99                                 
100                                 stanza = st.stanza(name, attr);
101                                 curr_tag = stanza;
102                         else -- we are inside a stanza, so add a tag
103                                 attr.xmlns = nil;
104                                 if curr_ns ~= stream_default_ns then
105                                         attr.xmlns = curr_ns;
106                                 end
107                                 stanza:tag(name, attr);
108                         end
109                 end
110                 function xml_handlers:CharacterData(data)
111                         if stanza then
112                                 t_insert(chardata, data);
113                         end
114                 end
115                 function xml_handlers:EndElement(tagname)
116                         curr_ns,name = tagname:match("^(.-)|?([^%|]-)$");
117                         if not name then
118                                 curr_ns, name = "", curr_ns;
119                         end
120                         if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 
121                                 if tagname == stream_tag then
122                                         if cb_streamclosed then
123                                                 cb_streamclosed(session);
124                                         end
125                                         return;
126                                 elseif name == "error" then
127                                         cb_error(session, "stream-error", stanza);
128                                 else
129                                         cb_error(session, "parse-error", "unexpected-element-close", name);
130                                 end
131                         end
132                         if stanza then
133                                 if #chardata > 0 then
134                                         -- We have some character data in the buffer
135                                         stanza:text(t_concat(chardata));
136                                         chardata = {};
137                                 end
138                                 -- Complete stanza
139                                 if #stanza.last_add == 0 then
140                                         cb_handlestanza(session, stanza);
141                                         stanza = nil;
142                                 else
143                                         stanza:up();
144                                 end
145                         end
146                 end
147         return xml_handlers;
148 end
149
150 return init_xmlhandlers;