core/xmlhandlers.lua

Fri, 15 May 2009 07:24:16 +0500

author
Waqas Hussain <waqas20@gmail.com>
date
Fri, 15 May 2009 07:24:16 +0500
changeset 1153
1184cb19b6f2
parent 1051
0327c569eb1a
child 1154
570c0427fcb8
permissions
-rw-r--r--

core.xmlhandlers: Removed unused variables

894
2c0b9e3c11c3 0.3->0.4
Matthew Wild <mwild1@gmail.com>
parents: 844
diff changeset
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
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 625
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 625
diff changeset
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
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
15 local pairs = pairs;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
16 local ipairs = ipairs;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
17 local type = type;
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
18 local t_insert = table.insert;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
19 local t_concat = table.concat;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
20
145
fbb3a4ff9cf1 dialback keys now verified
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
21 local default_log = require "util.logger".init("xmlhandlers");
fbb3a4ff9cf1 dialback keys now verified
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
22
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
23 local error = error;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
24
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
25 module "xmlhandlers"
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
26
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
27 local ns_prefixes = {
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
28 ["http://www.w3.org/XML/1998/namespace"] = "xml";
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
29 }
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
30
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 166
diff changeset
31 function init_xmlhandlers(session, stream_callbacks)
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
32 local ns_stack = { "" };
625
cad4dcfbf295 Change xmlhandlers to match stream opening tag with ns+tag
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
33 local curr_ns, name = "";
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
34 local curr_tag;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
35 local chardata = {};
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
36 local xml_handlers = {};
145
fbb3a4ff9cf1 dialback keys now verified
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
37 local log = session.log or default_log;
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
38
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 166
diff changeset
39 local cb_streamopened = stream_callbacks.streamopened;
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 166
diff changeset
40 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
41 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
42 local cb_handlestanza = stream_callbacks.handlestanza;
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 166
diff changeset
43
625
cad4dcfbf295 Change xmlhandlers to match stream opening tag with ns+tag
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
44 local stream_tag = stream_callbacks.stream_tag;
899
b95368f199a1 core.xmlhandlers: Filter out default stream namespace from stanzas
Matthew Wild <mwild1@gmail.com>
parents: 894
diff changeset
45 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
46
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
47 local stanza
625
cad4dcfbf295 Change xmlhandlers to match stream opening tag with ns+tag
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
48 function xml_handlers:StartElement(tagname, attr)
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
49 if stanza and #chardata > 0 then
31
aaccbf07849b Remove now useless debug output
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
50 -- We have some character data in the buffer
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
51 stanza:text(t_concat(chardata));
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
52 chardata = {};
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
53 end
1051
0327c569eb1a xmlhandlers: Fix tag pattern again for the default namespace
Matthew Wild <mwild1@gmail.com>
parents: 1008
diff changeset
54 local curr_ns,name = tagname:match("^(.-)|?([^%|]-)$");
0327c569eb1a xmlhandlers: Fix tag pattern again for the default namespace
Matthew Wild <mwild1@gmail.com>
parents: 1008
diff changeset
55 if not name then
0327c569eb1a xmlhandlers: Fix tag pattern again for the default namespace
Matthew Wild <mwild1@gmail.com>
parents: 1008
diff changeset
56 curr_ns, name = "", curr_ns;
0327c569eb1a xmlhandlers: Fix tag pattern again for the default namespace
Matthew Wild <mwild1@gmail.com>
parents: 1008
diff changeset
57 end
0327c569eb1a xmlhandlers: Fix tag pattern again for the default namespace
Matthew Wild <mwild1@gmail.com>
parents: 1008
diff changeset
58
899
b95368f199a1 core.xmlhandlers: Filter out default stream namespace from stanzas
Matthew Wild <mwild1@gmail.com>
parents: 894
diff changeset
59 if curr_ns ~= stream_default_ns then
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
60 attr.xmlns = curr_ns;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
61 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
62
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
63 -- FIXME !!!!!
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
64 for i, k in ipairs(attr) do
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
65 if type(k) == "string" then
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
66 local ns, nm = k:match("^([^|]+)|?([^|]-)$")
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
67 if ns and nm then
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
68 ns = ns_prefixes[ns];
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
69 if ns then
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
70 attr[ns..":"..nm] = attr[k];
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
71 attr[i] = ns..":"..nm;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
72 attr[k] = nil;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
73 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
74 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
75 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
76 end
145
fbb3a4ff9cf1 dialback keys now verified
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
77
fbb3a4ff9cf1 dialback keys now verified
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
78 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
79 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
80 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
81 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
82 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
83 end
545
60002993be04 Abstract xmlhandlers a bit more, also add error callbacks
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
84 else
60002993be04 Abstract xmlhandlers a bit more, also add error callbacks
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
85 -- 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
86 cb_error(session, "no-stream");
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
87 end
545
60002993be04 Abstract xmlhandlers a bit more, also add error callbacks
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
88 return;
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
89 end
38
Matthew Wild <mwild1@gmail.com>
parents: 31
diff changeset
90 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
91 cb_error(session, "invalid-top-level-element");
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
92 end
145
fbb3a4ff9cf1 dialback keys now verified
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
93
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 166
diff changeset
94 stanza = st.stanza(name, attr);
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
95 curr_tag = stanza;
145
fbb3a4ff9cf1 dialback keys now verified
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
96 else -- we are inside a stanza, so add a tag
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
97 attr.xmlns = nil;
899
b95368f199a1 core.xmlhandlers: Filter out default stream namespace from stanzas
Matthew Wild <mwild1@gmail.com>
parents: 894
diff changeset
98 if curr_ns ~= stream_default_ns then
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
99 attr.xmlns = curr_ns;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents: 146
diff changeset
100 end
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
101 stanza:tag(name, attr);
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
102 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
103 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
104 function xml_handlers:CharacterData(data)
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
105 if stanza then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
106 t_insert(chardata, data);
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
107 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
108 end
625
cad4dcfbf295 Change xmlhandlers to match stream opening tag with ns+tag
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
109 function xml_handlers:EndElement(tagname)
1051
0327c569eb1a xmlhandlers: Fix tag pattern again for the default namespace
Matthew Wild <mwild1@gmail.com>
parents: 1008
diff changeset
110 curr_ns,name = tagname:match("^(.-)|?([^%|]-)$");
0327c569eb1a xmlhandlers: Fix tag pattern again for the default namespace
Matthew Wild <mwild1@gmail.com>
parents: 1008
diff changeset
111 if not name then
0327c569eb1a xmlhandlers: Fix tag pattern again for the default namespace
Matthew Wild <mwild1@gmail.com>
parents: 1008
diff changeset
112 curr_ns, name = "", curr_ns;
0327c569eb1a xmlhandlers: Fix tag pattern again for the default namespace
Matthew Wild <mwild1@gmail.com>
parents: 1008
diff changeset
113 end
334
bffd80e8c7a3 *ahem* Yes, move along please... though really, quite a classic. :)
Matthew Wild <mwild1@gmail.com>
parents: 331
diff changeset
114 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
115 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
116 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
117 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
118 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
119 return;
145
fbb3a4ff9cf1 dialback keys now verified
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
120 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
121 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
122 else
557
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
123 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
124 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
125 end
838
1035846d6273 core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
126 if stanza then
844
503ca8da1000 core.xmlhandlers: Remove redundant check in condition
Matthew Wild <mwild1@gmail.com>
parents: 838
diff changeset
127 if #chardata > 0 then
838
1035846d6273 core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
128 -- We have some character data in the buffer
1035846d6273 core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
129 stanza:text(t_concat(chardata));
1035846d6273 core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
130 chardata = {};
1035846d6273 core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
131 end
1035846d6273 core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
132 -- Complete stanza
1035846d6273 core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
133 if #stanza.last_add == 0 then
1035846d6273 core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
134 cb_handlestanza(session, stanza);
1035846d6273 core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
135 stanza = nil;
1035846d6273 core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
136 else
1035846d6273 core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
137 stanza:up();
1035846d6273 core.xmlhandlers: Optimise completed stanza logic
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
138 end
1
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 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
141 return xml_handlers;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
142 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
143
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
144 return init_xmlhandlers;

mercurial