3037a848517051f9d4bcc90252bd70f34ff5c816
[prosody.git] / core / xmlhandlers.lua
1
2 require "util.stanza"
3
4 local st = stanza;
5 local tostring = tostring;
6 local pairs = pairs;
7 local ipairs = ipairs;
8 local type = type;
9 local print = print;
10 local format = string.format;
11 local m_random = math.random;
12 local t_insert = table.insert;
13 local t_remove = table.remove;
14 local t_concat = table.concat;
15 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
16 local sm_destroy_session = import("core.sessionmanager", "destroy_session");
17
18 local default_log = require "util.logger".init("xmlhandlers");
19
20 local error = error;
21
22 module "xmlhandlers"
23
24 local ns_prefixes = {
25                                                 ["http://www.w3.org/XML/1998/namespace"] = "xml";
26                                 }
27
28 function init_xmlhandlers(session, streamopened)
29                 local ns_stack = { "" };
30                 local curr_ns = "";
31                 local curr_tag;
32                 local chardata = {};
33                 local xml_handlers = {};
34                 local log = session.log or default_log;
35                 --local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end
36                 
37                 local send = session.send;
38                 
39                 local stanza
40                 function xml_handlers:StartElement(name, attr)
41                         if stanza and #chardata > 0 then
42                                 -- We have some character data in the buffer
43                                 stanza:text(t_concat(chardata));
44                                 chardata = {};
45                         end
46                         curr_ns,name = name:match("^(.+)|([%w%-]+)$");
47                         if curr_ns ~= "jabber:server" then
48                                 attr.xmlns = curr_ns;
49                         end
50                         
51                         -- FIXME !!!!!
52                         for i, k in ipairs(attr) do
53                                 if type(k) == "string" then
54                                         local ns, nm = k:match("^([^|]+)|?([^|]-)$")
55                                         if ns and nm then
56                                                 ns = ns_prefixes[ns]; 
57                                                 if ns then 
58                                                         attr[ns..":"..nm] = attr[k];
59                                                         attr[i] = ns..":"..nm;
60                                                         attr[k] = nil;
61                                                 end
62                                         end
63                                 end
64                         end
65                         
66                         if not stanza then --if we are not currently inside a stanza
67                                 if session.notopen then
68                                         if name == "stream" then
69                                                 streamopened(session, attr);
70                                                 return;
71                                         end
72                                         error("Client failed to open stream successfully");
73                                 end
74                                 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
75                                         error("Client sent invalid top-level stanza");
76                                 end
77                                 
78                                 stanza = st.stanza(name, attr); --{ to = attr.to, type = attr.type, id = attr.id, xmlns = curr_ns });
79                                 curr_tag = stanza;
80                         else -- we are inside a stanza, so add a tag
81                                 attr.xmlns = nil;
82                                 if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then
83                                         attr.xmlns = curr_ns;
84                                 end
85                                 stanza:tag(name, attr);
86                         end
87                 end
88                 function xml_handlers:CharacterData(data)
89                         if stanza then
90                                 t_insert(chardata, data);
91                         end
92                 end
93                 function xml_handlers:EndElement(name)
94                         curr_ns,name = name:match("^(.+)|([%w%-]+)$");
95                         if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 
96                                 if name == "stream" then
97                                         log("debug", "Stream closed");
98                                         sm_destroy_session(session);
99                                         return;
100                                 elseif name == "error" then
101                                         error("Stream error: "..tostring(name)..": "..tostring(stanza));
102                                 else
103                                         error("XML parse error in client stream");
104                                 end
105                         end
106                         if stanza and #chardata > 0 then
107                                 -- We have some character data in the buffer
108                                 stanza:text(t_concat(chardata));
109                                 chardata = {};
110                         end
111                         -- Complete stanza
112                         if #stanza.last_add == 0 then
113                                 session.stanza_dispatch(stanza);
114                                 stanza = nil;
115                         else
116                                 stanza:up();
117                         end
118                 end
119         return xml_handlers;
120 end
121
122 return init_xmlhandlers;