96e3f3ac2fab6ca7444514577a8fedd53fac38af
[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
13 local error = error;
14
15 module "xmlhandlers"
16
17 function init_xmlhandlers(session)
18                 local ns_stack = { "" };
19                 local curr_ns = "";
20                 local curr_tag;
21                 local chardata = {};
22                 local xml_handlers = {};
23                 local log = session.log;
24                 local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end
25                 
26                 local send = session.send;
27                 
28                 local stanza
29                 function xml_handlers:StartElement(name, attr)
30                         if stanza and #chardata > 0 then
31                                 -- We have some character data in the buffer
32                                 stanza:text(t_concat(chardata));
33                                 chardata = {};
34                         end
35                         curr_ns,name = name:match("^(.+):(%w+)$");
36                         if not stanza then
37                                 if session.notopen then
38                                         if name == "stream" then
39                                                 session.host = attr.to or error("Client failed to specify destination hostname");
40                                                 session.version = attr.version or 0;
41                                                 session.streamid = m_random(1000000, 99999999);
42                                                 print(session, session.host, "Client opened stream");
43                                                 send("<?xml version='1.0'?>");
44                                                 send(format("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='%s' from='%s'>", session.streamid, session.host));
45                                                 --send("<stream:features>");
46                                                 --send("<mechanism>PLAIN</mechanism>");
47                                                 --send [[<register xmlns="http://jabber.org/features/iq-register"/> ]]
48                                                 --send("</stream:features>");
49                                                 log("info", "core", "Stream opened successfully");
50                                                 session.notopen = nil;
51                                                 return;
52                                         end
53                                         error("Client failed to open stream successfully");
54                                 end
55                                 if name ~= "iq" and name ~= "presence" and name ~= "message" then
56                                         error("Client sent invalid top-level stanza");
57                                 end
58                                 stanza = st.stanza(name, { to = attr.to, type = attr.type, id = attr.id, xmlns = curr_ns });
59                                 curr_tag = stanza;
60                         else
61                                 attr.xmlns = curr_ns;
62                                 stanza:tag(name, attr);
63                         end
64                 end
65                 function xml_handlers:CharacterData(data)
66                         if stanza then
67                                 t_insert(chardata, data);
68                         end
69                 end
70                 function xml_handlers:EndElement(name)
71                         curr_ns,name = name:match("^(.+):(%w+)$");
72                         if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then error("XML parse error in client stream"); end
73                         if stanza and #chardata > 0 then
74                                 -- We have some character data in the buffer
75                                 stanza:text(t_concat(chardata));
76                                 chardata = {};
77                         end
78                         -- Complete stanza
79                         if #stanza.last_add == 0 then
80                                 session.stanza_dispatch(stanza);
81                                 stanza = nil;
82                         else
83                                 stanza:up();
84                         end
85                 end
86         return xml_handlers;
87 end
88
89 return init_xmlhandlers;