Fri, 05 Dec 2008 19:40:46 +0000
Don't say we need TLS after we've already started the handshake
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; |
545
60002993be04
Abstract xmlhandlers a bit more, also add error callbacks
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
55 | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
56 | local send = session.send; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
57 | |
331 | 58 | local cb_streamopened = stream_callbacks.streamopened; |
59 | local cb_streamclosed = stream_callbacks.streamclosed; | |
557
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
60 | local cb_error = stream_callbacks.error or function (session, e) error("XML stream error: "..tostring(e)); end; |
545
60002993be04
Abstract xmlhandlers a bit more, also add error callbacks
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
61 | local cb_handlestanza = stream_callbacks.handlestanza; |
331 | 62 | |
557
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
63 | local stream_ns = stream_callbacks.ns; |
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
64 | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
65 | local stanza |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
66 | function xml_handlers:StartElement(name, attr) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
67 | if stanza and #chardata > 0 then |
31
aaccbf07849b
Remove now useless debug output
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
68 | -- We have some character data in the buffer |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
69 | stanza:text(t_concat(chardata)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
70 | chardata = {}; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
71 | end |
148 | 72 | curr_ns,name = name:match("^(.+)|([%w%-]+)$"); |
73 | if curr_ns ~= "jabber:server" then | |
74 | attr.xmlns = curr_ns; | |
75 | end | |
76 | ||
77 | -- FIXME !!!!! | |
78 | for i, k in ipairs(attr) do | |
79 | if type(k) == "string" then | |
80 | local ns, nm = k:match("^([^|]+)|?([^|]-)$") | |
81 | if ns and nm then | |
82 | ns = ns_prefixes[ns]; | |
83 | if ns then | |
84 | attr[ns..":"..nm] = attr[k]; | |
85 | attr[i] = ns..":"..nm; | |
86 | attr[k] = nil; | |
87 | end | |
88 | end | |
89 | end | |
90 | end | |
145 | 91 | |
92 | 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
|
93 | if session.notopen then |
557
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
94 | if name == "stream" and curr_ns == stream_ns 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
|
95 | 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
|
96 | 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
|
97 | end |
545
60002993be04
Abstract xmlhandlers a bit more, also add error callbacks
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
98 | else |
60002993be04
Abstract xmlhandlers a bit more, also add error callbacks
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
99 | -- Garbage before stream? |
557
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
100 | cb_error(session, "no-stream"); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
101 | end |
545
60002993be04
Abstract xmlhandlers a bit more, also add error callbacks
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
102 | return; |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
103 | end |
38 | 104 | if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then |
557
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
105 | cb_error(session, "invalid-top-level-element"); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
106 | end |
145 | 107 | |
331 | 108 | stanza = st.stanza(name, attr); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
109 | curr_tag = stanza; |
145 | 110 | else -- we are inside a stanza, so add a tag |
148 | 111 | attr.xmlns = nil; |
112 | if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then | |
113 | attr.xmlns = curr_ns; | |
114 | end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
115 | stanza:tag(name, attr); |
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:CharacterData(data) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
119 | if stanza then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
120 | t_insert(chardata, data); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
121 | end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
122 | end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
123 | function xml_handlers:EndElement(name) |
148 | 124 | 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
|
125 | 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
|
126 | 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
|
127 | 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
|
128 | 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
|
129 | 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
|
130 | return; |
145 | 131 | elseif name == "error" then |
557
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
132 | cb_error(session, "stream-error", 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
|
133 | else |
557
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
134 | cb_error(session, "parse-error", "unexpected-element-close", 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
|
135 | 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
|
136 | end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
137 | if stanza and #chardata > 0 then |
31
aaccbf07849b
Remove now useless debug output
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
138 | -- We have some character data in the buffer |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
139 | stanza:text(t_concat(chardata)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
140 | chardata = {}; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
141 | end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
142 | -- Complete stanza |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
143 | if #stanza.last_add == 0 then |
545
60002993be04
Abstract xmlhandlers a bit more, also add error callbacks
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
144 | cb_handlestanza(session, stanza); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
145 | stanza = nil; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
146 | else |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
147 | stanza:up(); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
148 | end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
149 | end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
150 | return xml_handlers; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
151 | end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
152 | |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
153 | return init_xmlhandlers; |