Moved directory auto-creation to datamanager
[prosody.git] / core / xmlhandlers.lua
1 -- Prosody IM v0.2
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21
22 require "util.stanza"
23
24 local st = stanza;
25 local tostring = tostring;
26 local pairs = pairs;
27 local ipairs = ipairs;
28 local type = type;
29 local print = print;
30 local format = string.format;
31 local m_random = math.random;
32 local t_insert = table.insert;
33 local t_remove = table.remove;
34 local t_concat = table.concat;
35 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
36 local sm_destroy_session = import("core.sessionmanager", "destroy_session");
37
38 local default_log = require "util.logger".init("xmlhandlers");
39
40 local error = error;
41
42 module "xmlhandlers"
43
44 local ns_prefixes = {
45                                                 ["http://www.w3.org/XML/1998/namespace"] = "xml";
46                                 }
47
48 function init_xmlhandlers(session, stream_callbacks)
49                 local ns_stack = { "" };
50                 local curr_ns = "";
51                 local curr_tag;
52                 local chardata = {};
53                 local xml_handlers = {};
54                 local log = session.log or default_log;
55
56                 local send = session.send;
57                 
58                 local cb_streamopened = stream_callbacks.streamopened;
59                 local cb_streamclosed = stream_callbacks.streamclosed;
60                 local cb_error = stream_callbacks.error or function (session, e) error("XML stream error: "..tostring(e)); end;
61                 local cb_handlestanza = stream_callbacks.handlestanza;
62                 
63                 local stream_ns = stream_callbacks.ns;
64                 
65                 local stanza
66                 function xml_handlers:StartElement(name, attr)
67                         if stanza and #chardata > 0 then
68                                 -- We have some character data in the buffer
69                                 stanza:text(t_concat(chardata));
70                                 chardata = {};
71                         end
72                         curr_ns,name = name:match("^(.+)|([%w%-]+)$");
73                         if curr_ns ~= "jabber:server" then
74                                 attr.xmlns = curr_ns;
75                         end
76                         
77                         -- FIXME !!!!!
78                         for i, k in ipairs(attr) do
79                                 if type(k) == "string" then
80                                         local ns, nm = k:match("^([^|]+)|?([^|]-)$")
81                                         if ns and nm then
82                                                 ns = ns_prefixes[ns]; 
83                                                 if ns then 
84                                                         attr[ns..":"..nm] = attr[k];
85                                                         attr[i] = ns..":"..nm;
86                                                         attr[k] = nil;
87                                                 end
88                                         end
89                                 end
90                         end
91                         
92                         if not stanza then --if we are not currently inside a stanza
93                                 if session.notopen then
94                                         if name == "stream" and curr_ns == stream_ns then
95                                                 if cb_streamopened then
96                                                         cb_streamopened(session, attr);
97                                                 end
98                                         else
99                                                 -- Garbage before stream?
100                                                 cb_error(session, "no-stream");
101                                         end
102                                         return;
103                                 end
104                                 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
105                                         cb_error(session, "invalid-top-level-element");
106                                 end
107                                 
108                                 stanza = st.stanza(name, attr);
109                                 curr_tag = stanza;
110                         else -- we are inside a stanza, so add a tag
111                                 attr.xmlns = nil;
112                                 if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then
113                                         attr.xmlns = curr_ns;
114                                 end
115                                 stanza:tag(name, attr);
116                         end
117                 end
118                 function xml_handlers:CharacterData(data)
119                         if stanza then
120                                 t_insert(chardata, data);
121                         end
122                 end
123                 function xml_handlers:EndElement(name)
124                         curr_ns,name = name:match("^(.+)|([%w%-]+)$");
125                         if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 
126                                 if name == "stream" then
127                                         if cb_streamclosed then
128                                                 cb_streamclosed(session);
129                                         end
130                                         return;
131                                 elseif name == "error" then
132                                         cb_error(session, "stream-error", stanza);
133                                 else
134                                         cb_error(session, "parse-error", "unexpected-element-close", name);
135                                 end
136                         end
137                         if stanza and #chardata > 0 then
138                                 -- We have some character data in the buffer
139                                 stanza:text(t_concat(chardata));
140                                 chardata = {};
141                         end
142                         -- Complete stanza
143                         if #stanza.last_add == 0 then
144                                 cb_handlestanza(session, stanza);
145                                 stanza = nil;
146                         else
147                                 stanza:up();
148                         end
149                 end
150         return xml_handlers;
151 end
152
153 return init_xmlhandlers;