Software / code / prosody
Annotate
core/xmlhandlers.lua @ 524:3f9f67f1a106
Merge from waqas
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 03 Dec 2008 15:09:58 +0000 |
| parent | 519:cccd610a0ef9 |
| child | 545:60002993be04 |
| rev | line source |
|---|---|
|
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
1 -- Prosody IM v0.1 |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
2 -- Copyright (C) 2008 Matthew Wild |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
3 -- Copyright (C) 2008 Waqas Hussain |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
4 -- |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
5 -- This program is free software; you can redistribute it and/or |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
6 -- modify it under the terms of the GNU General Public License |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
7 -- as published by the Free Software Foundation; either version 2 |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
8 -- of the License, or (at your option) any later version. |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
9 -- |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
10 -- This program is distributed in the hope that it will be useful, |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
13 -- GNU General Public License for more details. |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
14 -- |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
15 -- You should have received a copy of the GNU General Public License |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
16 -- along with this program; if not, write to the Free Software |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
18 -- |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
19 |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
20 |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
21 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
22 require "util.stanza" |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
23 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
24 local st = stanza; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
25 local tostring = tostring; |
| 148 | 26 local pairs = pairs; |
| 27 local ipairs = ipairs; | |
| 28 local type = type; | |
| 29 local print = print; | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
30 local format = string.format; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
31 local m_random = math.random; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
32 local t_insert = table.insert; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
33 local t_remove = table.remove; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
34 local t_concat = table.concat; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
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 |
|
53
14ea0fe6ca86
Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful.
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
36 local sm_destroy_session = import("core.sessionmanager", "destroy_session"); |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
37 |
| 145 | 38 local default_log = require "util.logger".init("xmlhandlers"); |
| 39 | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
40 local error = error; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
41 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
42 module "xmlhandlers" |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
43 |
| 148 | 44 local ns_prefixes = { |
| 45 ["http://www.w3.org/XML/1998/namespace"] = "xml"; | |
| 46 } | |
| 47 | |
| 331 | 48 function init_xmlhandlers(session, stream_callbacks) |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
49 local ns_stack = { "" }; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
50 local curr_ns = ""; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
51 local curr_tag; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
52 local chardata = {}; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
53 local xml_handlers = {}; |
| 145 | 54 local log = session.log or default_log; |
| 148 | 55 --local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
56 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
57 local send = session.send; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
58 |
| 331 | 59 local cb_streamopened = stream_callbacks.streamopened; |
| 60 local cb_streamclosed = stream_callbacks.streamclosed; | |
| 61 | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
62 local stanza |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
63 function xml_handlers:StartElement(name, attr) |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
64 if stanza and #chardata > 0 then |
|
31
aaccbf07849b
Remove now useless debug output
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
65 -- We have some character data in the buffer |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
66 stanza:text(t_concat(chardata)); |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
67 chardata = {}; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
68 end |
| 148 | 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 | |
| 145 | 88 |
| 89 if not stanza then --if we are not currently inside a stanza | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
90 if session.notopen then |
|
341
a9e02b5c58d2
Don't error if streamopened/streamclosed callback is not specified for a session
Matthew Wild <mwild1@gmail.com>
parents:
334
diff
changeset
|
91 if name == "stream" then |
|
a9e02b5c58d2
Don't error if streamopened/streamclosed callback is not specified for a session
Matthew Wild <mwild1@gmail.com>
parents:
334
diff
changeset
|
92 if cb_streamopened then |
|
a9e02b5c58d2
Don't error if streamopened/streamclosed callback is not specified for a session
Matthew Wild <mwild1@gmail.com>
parents:
334
diff
changeset
|
93 cb_streamopened(session, attr); |
|
a9e02b5c58d2
Don't error if streamopened/streamclosed callback is not specified for a session
Matthew Wild <mwild1@gmail.com>
parents:
334
diff
changeset
|
94 end |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
95 return; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
96 end |
|
166
d4ee015fcee4
Backed out changeset 4adc53e03b4d (garbage collection)
Matthew Wild <mwild1@gmail.com>
parents:
165
diff
changeset
|
97 error("Client failed to open stream successfully"); |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
98 end |
| 38 | 99 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
100 error("Client sent invalid top-level stanza"); |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
101 end |
| 145 | 102 |
| 331 | 103 stanza = st.stanza(name, attr); |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
104 curr_tag = stanza; |
| 145 | 105 else -- we are inside a stanza, so add a tag |
| 148 | 106 attr.xmlns = nil; |
| 107 if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then | |
| 108 attr.xmlns = curr_ns; | |
| 109 end | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
110 stanza:tag(name, attr); |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
111 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
112 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
113 function xml_handlers:CharacterData(data) |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
114 if stanza then |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
115 t_insert(chardata, data); |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
116 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
117 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
118 function xml_handlers:EndElement(name) |
| 148 | 119 curr_ns,name = name:match("^(.+)|([%w%-]+)$"); |
|
334
bffd80e8c7a3
*ahem* Yes, move along please... though really, quite a classic. :)
Matthew Wild <mwild1@gmail.com>
parents:
331
diff
changeset
|
120 if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then |
|
341
a9e02b5c58d2
Don't error if streamopened/streamclosed callback is not specified for a session
Matthew Wild <mwild1@gmail.com>
parents:
334
diff
changeset
|
121 if name == "stream" then |
|
53
14ea0fe6ca86
Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful.
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
122 log("debug", "Stream closed"); |
|
341
a9e02b5c58d2
Don't error if streamopened/streamclosed callback is not specified for a session
Matthew Wild <mwild1@gmail.com>
parents:
334
diff
changeset
|
123 if cb_streamclosed then |
|
a9e02b5c58d2
Don't error if streamopened/streamclosed callback is not specified for a session
Matthew Wild <mwild1@gmail.com>
parents:
334
diff
changeset
|
124 cb_streamclosed(session); |
|
a9e02b5c58d2
Don't error if streamopened/streamclosed callback is not specified for a session
Matthew Wild <mwild1@gmail.com>
parents:
334
diff
changeset
|
125 end |
|
53
14ea0fe6ca86
Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful.
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
126 return; |
| 145 | 127 elseif name == "error" then |
| 128 error("Stream error: "..tostring(name)..": "..tostring(stanza)); | |
|
53
14ea0fe6ca86
Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful.
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
129 else |
|
341
a9e02b5c58d2
Don't error if streamopened/streamclosed callback is not specified for a session
Matthew Wild <mwild1@gmail.com>
parents:
334
diff
changeset
|
130 error("XML parse error in client stream with element: "..name); |
|
53
14ea0fe6ca86
Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful.
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
131 end |
|
14ea0fe6ca86
Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful.
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
132 end |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
133 if stanza and #chardata > 0 then |
|
31
aaccbf07849b
Remove now useless debug output
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
134 -- We have some character data in the buffer |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
135 stanza:text(t_concat(chardata)); |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
136 chardata = {}; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
137 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
138 -- Complete stanza |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
139 if #stanza.last_add == 0 then |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
140 session.stanza_dispatch(stanza); |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
141 stanza = nil; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
142 else |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
143 stanza:up(); |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
144 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
145 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
146 return xml_handlers; |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
147 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
148 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
149 return init_xmlhandlers; |