Software /
code /
prosody
Annotate
core/xmlhandlers.lua @ 982:dbbeb73952e6
componentmanager: Improve default component stanza handler
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 11 Apr 2009 22:23:49 +0100 (2009-04-11) |
parent | 899:b95368f199a1 |
child | 1003:afae75e37ceb |
rev | line source |
---|---|
896 | 1 -- Prosody IM v0.4 |
760
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
4 -- |
758 | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
7 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
8 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
355
diff
changeset
|
9 |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
10 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
11 require "util.stanza" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
12 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
13 local st = stanza; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
14 local tostring = tostring; |
148 | 15 local pairs = pairs; |
16 local ipairs = ipairs; | |
17 local type = type; | |
18 local print = print; | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
19 local format = string.format; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
20 local m_random = math.random; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
21 local t_insert = table.insert; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
22 local t_remove = table.remove; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
23 local t_concat = table.concat; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
24 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
|
25 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
|
26 |
145 | 27 local default_log = require "util.logger".init("xmlhandlers"); |
28 | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
29 local error = error; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
30 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
31 module "xmlhandlers" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
32 |
148 | 33 local ns_prefixes = { |
34 ["http://www.w3.org/XML/1998/namespace"] = "xml"; | |
35 } | |
36 | |
331 | 37 function init_xmlhandlers(session, stream_callbacks) |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
38 local ns_stack = { "" }; |
625
cad4dcfbf295
Change xmlhandlers to match stream opening tag with ns+tag
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
39 local curr_ns, name = ""; |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
40 local curr_tag; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
41 local chardata = {}; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
42 local xml_handlers = {}; |
145 | 43 local log = session.log or default_log; |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
44 |
331 | 45 local cb_streamopened = stream_callbacks.streamopened; |
46 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
|
47 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
|
48 local cb_handlestanza = stream_callbacks.handlestanza; |
331 | 49 |
625
cad4dcfbf295
Change xmlhandlers to match stream opening tag with ns+tag
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
50 local stream_tag = stream_callbacks.stream_tag; |
899
b95368f199a1
core.xmlhandlers: Filter out default stream namespace from stanzas
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
51 local stream_default_ns = stream_callbacks.default_ns; |
557
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
52 |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
53 local stanza |
625
cad4dcfbf295
Change xmlhandlers to match stream opening tag with ns+tag
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
54 function xml_handlers:StartElement(tagname, attr) |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
55 if stanza and #chardata > 0 then |
31
aaccbf07849b
Remove now useless debug output
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
56 -- We have some character data in the buffer |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
57 stanza:text(t_concat(chardata)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
58 chardata = {}; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
59 end |
625
cad4dcfbf295
Change xmlhandlers to match stream opening tag with ns+tag
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
60 local curr_ns,name = tagname:match("^(.+)|([%w%-]+)$"); |
899
b95368f199a1
core.xmlhandlers: Filter out default stream namespace from stanzas
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
61 if curr_ns ~= stream_default_ns then |
148 | 62 attr.xmlns = curr_ns; |
63 end | |
64 | |
65 -- FIXME !!!!! | |
66 for i, k in ipairs(attr) do | |
67 if type(k) == "string" then | |
68 local ns, nm = k:match("^([^|]+)|?([^|]-)$") | |
69 if ns and nm then | |
70 ns = ns_prefixes[ns]; | |
71 if ns then | |
72 attr[ns..":"..nm] = attr[k]; | |
73 attr[i] = ns..":"..nm; | |
74 attr[k] = nil; | |
75 end | |
76 end | |
77 end | |
78 end | |
145 | 79 |
80 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
|
81 if session.notopen then |
625
cad4dcfbf295
Change xmlhandlers to match stream opening tag with ns+tag
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
82 if tagname == stream_tag 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
|
83 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
|
84 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
|
85 end |
545
60002993be04
Abstract xmlhandlers a bit more, also add error callbacks
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
86 else |
60002993be04
Abstract xmlhandlers a bit more, also add error callbacks
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
87 -- 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
|
88 cb_error(session, "no-stream"); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
89 end |
545
60002993be04
Abstract xmlhandlers a bit more, also add error callbacks
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
90 return; |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
91 end |
38 | 92 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
|
93 cb_error(session, "invalid-top-level-element"); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
94 end |
145 | 95 |
331 | 96 stanza = st.stanza(name, attr); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
97 curr_tag = stanza; |
145 | 98 else -- we are inside a stanza, so add a tag |
148 | 99 attr.xmlns = nil; |
899
b95368f199a1
core.xmlhandlers: Filter out default stream namespace from stanzas
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
100 if curr_ns ~= stream_default_ns then |
148 | 101 attr.xmlns = curr_ns; |
102 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
103 stanza:tag(name, attr); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
104 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
105 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
106 function xml_handlers:CharacterData(data) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
107 if stanza then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
108 t_insert(chardata, data); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
109 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
110 end |
625
cad4dcfbf295
Change xmlhandlers to match stream opening tag with ns+tag
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
111 function xml_handlers:EndElement(tagname) |
cad4dcfbf295
Change xmlhandlers to match stream opening tag with ns+tag
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
112 curr_ns,name = tagname:match("^(.+)|([%w%-]+)$"); |
334
bffd80e8c7a3
*ahem* Yes, move along please... though really, quite a classic. :)
Matthew Wild <mwild1@gmail.com>
parents:
331
diff
changeset
|
113 if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then |
625
cad4dcfbf295
Change xmlhandlers to match stream opening tag with ns+tag
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
114 if tagname == stream_tag 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
|
115 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
|
116 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
|
117 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
|
118 return; |
145 | 119 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
|
120 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
|
121 else |
557
c9b3ffb08fe3
Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents:
545
diff
changeset
|
122 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
|
123 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
|
124 end |
838
1035846d6273
core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
125 if stanza then |
844
503ca8da1000
core.xmlhandlers: Remove redundant check in condition
Matthew Wild <mwild1@gmail.com>
parents:
838
diff
changeset
|
126 if #chardata > 0 then |
838
1035846d6273
core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
127 -- We have some character data in the buffer |
1035846d6273
core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
128 stanza:text(t_concat(chardata)); |
1035846d6273
core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
129 chardata = {}; |
1035846d6273
core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
130 end |
1035846d6273
core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
131 -- Complete stanza |
1035846d6273
core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
132 if #stanza.last_add == 0 then |
1035846d6273
core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
133 cb_handlestanza(session, stanza); |
1035846d6273
core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
134 stanza = nil; |
1035846d6273
core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
135 else |
1035846d6273
core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
136 stanza:up(); |
1035846d6273
core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
137 end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
138 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
139 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
140 return xml_handlers; |
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 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
143 return init_xmlhandlers; |