GPL->MIT!
[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 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                 
52                 local stanza
53                 function xml_handlers:StartElement(tagname, attr)
54                         if stanza and #chardata > 0 then
55                                 -- We have some character data in the buffer
56                                 stanza:text(t_concat(chardata));
57                                 chardata = {};
58                         end
59                         local curr_ns,name = tagname:match("^(.+)|([%w%-]+)$");
60                         if curr_ns ~= "jabber:server" then
61                                 attr.xmlns = curr_ns;
62                         end
63                         
64                         -- FIXME !!!!!
65                         for i, k in ipairs(attr) do
66                                 if type(k) == "string" then
67                                         local ns, nm = k:match("^([^|]+)|?([^|]-)$")
68                                         if ns and nm then
69                                                 ns = ns_prefixes[ns]; 
70                                                 if ns then 
71                                                         attr[ns..":"..nm] = attr[k];
72                                                         attr[i] = ns..":"..nm;
73                                                         attr[k] = nil;
74                                                 end
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                                 curr_tag = stanza;
97                         else -- we are inside a stanza, so add a tag
98                                 attr.xmlns = nil;
99                                 if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then
100                                         attr.xmlns = curr_ns;
101                                 end
102                                 stanza:tag(name, attr);
103                         end
104                 end
105                 function xml_handlers:CharacterData(data)
106                         if stanza then
107                                 t_insert(chardata, data);
108                         end
109                 end
110                 function xml_handlers:EndElement(tagname)
111                         curr_ns,name = tagname:match("^(.+)|([%w%-]+)$");
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 stanza and #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;