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