61a9e387bf5b2c6e82006f1ba530dcde13051924
[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, stream_callbacks)
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 cb_streamopened = stream_callbacks.streamopened;
40                 local cb_streamclosed = stream_callbacks.streamclosed;
41                 
42                 local stanza
43                 function xml_handlers:StartElement(name, attr)
44                         if stanza and #chardata > 0 then
45                                 -- We have some character data in the buffer
46                                 stanza:text(t_concat(chardata));
47                                 chardata = {};
48                         end
49                         curr_ns,name = name:match("^(.+)|([%w%-]+)$");
50                         if curr_ns ~= "jabber:server" then
51                                 attr.xmlns = curr_ns;
52                         end
53                         
54                         -- FIXME !!!!!
55                         for i, k in ipairs(attr) do
56                                 if type(k) == "string" then
57                                         local ns, nm = k:match("^([^|]+)|?([^|]-)$")
58                                         if ns and nm then
59                                                 ns = ns_prefixes[ns]; 
60                                                 if ns then 
61                                                         attr[ns..":"..nm] = attr[k];
62                                                         attr[i] = ns..":"..nm;
63                                                         attr[k] = nil;
64                                                 end
65                                         end
66                                 end
67                         end
68                         
69                         if not stanza then --if we are not currently inside a stanza
70                                 if session.notopen then
71                                         print("client opening with "..tostring(name));
72                                         if name == "stream" and cb_streamopened then
73                                                 cb_streamopened(session, attr);
74                                                 return;
75                                         end
76                                         error("Client failed to open stream successfully");
77                                 end
78                                 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
79                                         error("Client sent invalid top-level stanza");
80                                 end
81                                 
82                                 stanza = st.stanza(name, attr);
83                                 curr_tag = stanza;
84                         else -- we are inside a stanza, so add a tag
85                                 attr.xmlns = nil;
86                                 if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then
87                                         attr.xmlns = curr_ns;
88                                 end
89                                 stanza:tag(name, attr);
90                         end
91                 end
92                 function xml_handlers:CharacterData(data)
93                         if stanza then
94                                 t_insert(chardata, data);
95                         end
96                 end
97                 function xml_handlers:EndElement(name)
98                         curr_ns,name = name:match("^(.+)|([%w%-]+)$");
99                         if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 
100                                 if name == "stream" and cb_streamclosed then
101                                         log("debug", "Stream closed");
102                                         cb_streamclosed(session);
103                                         return;
104                                 elseif name == "error" then
105                                         error("Stream error: "..tostring(name)..": "..tostring(stanza));
106                                 else
107                                         error("XML parse error in client stream");
108                                 end
109                         end
110                         if stanza and #chardata > 0 then
111                                 -- We have some character data in the buffer
112                                 stanza:text(t_concat(chardata));
113                                 chardata = {};
114                         end
115                         -- Complete stanza
116                         if #stanza.last_add == 0 then
117                                 session.stanza_dispatch(stanza);
118                                 stanza = nil;
119                         else
120                                 stanza:up();
121                         end
122                 end
123         return xml_handlers;
124 end
125
126 return init_xmlhandlers;