Insert copyright/license headers
[prosody.git] / core / xmlhandlers.lua
1 -- Prosody IM v0.1
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21
22 require "util.stanza"
23
24 local st = stanza;
25 local tostring = tostring;
26 local pairs = pairs;
27 local ipairs = ipairs;
28 local type = type;
29 local print = print;
30 local format = string.format;
31 local m_random = math.random;
32 local t_insert = table.insert;
33 local t_remove = table.remove;
34 local t_concat = table.concat;
35 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
36 local sm_destroy_session = import("core.sessionmanager", "destroy_session");
37
38 local default_log = require "util.logger".init("xmlhandlers");
39
40 local error = error;
41
42 module "xmlhandlers"
43
44 local ns_prefixes = {
45                                                 ["http://www.w3.org/XML/1998/namespace"] = "xml";
46                                 }
47
48 function init_xmlhandlers(session, stream_callbacks)
49                 local ns_stack = { "" };
50                 local curr_ns = "";
51                 local curr_tag;
52                 local chardata = {};
53                 local xml_handlers = {};
54                 local log = session.log or default_log;
55                 --local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end
56                 
57                 local send = session.send;
58                 
59                 local cb_streamopened = stream_callbacks.streamopened;
60                 local cb_streamclosed = stream_callbacks.streamclosed;
61                 
62                 local stanza
63                 function xml_handlers:StartElement(name, attr)
64                         if stanza and #chardata > 0 then
65                                 -- We have some character data in the buffer
66                                 stanza:text(t_concat(chardata));
67                                 chardata = {};
68                         end
69                         curr_ns,name = name:match("^(.+)|([%w%-]+)$");
70                         if curr_ns ~= "jabber:server" then
71                                 attr.xmlns = curr_ns;
72                         end
73                         
74                         -- FIXME !!!!!
75                         for i, k in ipairs(attr) do
76                                 if type(k) == "string" then
77                                         local ns, nm = k:match("^([^|]+)|?([^|]-)$")
78                                         if ns and nm then
79                                                 ns = ns_prefixes[ns]; 
80                                                 if ns then 
81                                                         attr[ns..":"..nm] = attr[k];
82                                                         attr[i] = ns..":"..nm;
83                                                         attr[k] = nil;
84                                                 end
85                                         end
86                                 end
87                         end
88                         
89                         if not stanza then --if we are not currently inside a stanza
90                                 if session.notopen then
91                                         if name == "stream" then
92                                                 if cb_streamopened then
93                                                         cb_streamopened(session, attr);
94                                                 end
95                                                 return;
96                                         end
97                                         error("Client failed to open stream successfully");
98                                 end
99                                 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
100                                         error("Client sent invalid top-level stanza");
101                                 end
102                                 
103                                 stanza = st.stanza(name, attr);
104                                 curr_tag = stanza;
105                         else -- we are inside a stanza, so add a tag
106                                 attr.xmlns = nil;
107                                 if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then
108                                         attr.xmlns = curr_ns;
109                                 end
110                                 stanza:tag(name, attr);
111                         end
112                 end
113                 function xml_handlers:CharacterData(data)
114                         if stanza then
115                                 t_insert(chardata, data);
116                         end
117                 end
118                 function xml_handlers:EndElement(name)
119                         curr_ns,name = name:match("^(.+)|([%w%-]+)$");
120                         if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 
121                                 if name == "stream" then
122                                         log("debug", "Stream closed");
123                                         if cb_streamclosed then
124                                                 cb_streamclosed(session);
125                                         end
126                                         return;
127                                 elseif name == "error" then
128                                         error("Stream error: "..tostring(name)..": "..tostring(stanza));
129                                 else
130                                         error("XML parse error in client stream with element: "..name);
131                                 end
132                         end
133                         if stanza and #chardata > 0 then
134                                 -- We have some character data in the buffer
135                                 stanza:text(t_concat(chardata));
136                                 chardata = {};
137                         end
138                         -- Complete stanza
139                         if #stanza.last_add == 0 then
140                                 session.stanza_dispatch(stanza);
141                                 stanza = nil;
142                         else
143                                 stanza:up();
144                         end
145                 end
146         return xml_handlers;
147 end
148
149 return init_xmlhandlers;