author | Matthew Wild <mwild1@gmail.com> |
Fri, 24 Oct 2008 07:27:36 +0100 | |
branch | s2s |
changeset 148 | 4c0dcd245d34 |
parent 146 | 3826ca244eb6 |
child 150 | d09b8a1ab046 |
permissions | -rw-r--r-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
1 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
2 |
require "util.stanza" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
3 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
4 |
local st = stanza; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
5 |
local tostring = tostring; |
148 | 6 |
local pairs = pairs; |
7 |
local ipairs = ipairs; |
|
8 |
local type = type; |
|
9 |
local print = print; |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
10 |
local format = string.format; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
11 |
local m_random = math.random; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
12 |
local t_insert = table.insert; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
13 |
local t_remove = table.remove; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
14 |
local t_concat = table.concat; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
15 |
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
|
16 |
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
|
17 |
|
145 | 18 |
local default_log = require "util.logger".init("xmlhandlers"); |
19 |
||
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
20 |
local error = error; |
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 |
module "xmlhandlers" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
23 |
|
148 | 24 |
local ns_prefixes = { |
25 |
["http://www.w3.org/XML/1998/namespace"] = "xml"; |
|
26 |
} |
|
27 |
||
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
28 |
function init_xmlhandlers(session, streamopened) |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
29 |
local ns_stack = { "" }; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
30 |
local curr_ns = ""; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
31 |
local curr_tag; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
32 |
local chardata = {}; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
33 |
local xml_handlers = {}; |
145 | 34 |
local log = session.log or default_log; |
148 | 35 |
--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
|
36 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
37 |
local send = session.send; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
38 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
39 |
local stanza |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
40 |
function xml_handlers:StartElement(name, attr) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
41 |
if stanza and #chardata > 0 then |
31
aaccbf07849b
Remove now useless debug output
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
42 |
-- We have some character data in the buffer |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
43 |
stanza:text(t_concat(chardata)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
44 |
chardata = {}; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
45 |
end |
148 | 46 |
curr_ns,name = name:match("^(.+)|([%w%-]+)$"); |
47 |
if curr_ns ~= "jabber:server" then |
|
48 |
attr.xmlns = curr_ns; |
|
49 |
end |
|
50 |
||
51 |
-- FIXME !!!!! |
|
52 |
for i, k in ipairs(attr) do |
|
53 |
if type(k) == "string" then |
|
54 |
local ns, nm = k:match("^([^|]+)|?([^|]-)$") |
|
55 |
if ns and nm then |
|
56 |
ns = ns_prefixes[ns]; |
|
57 |
if ns then |
|
58 |
attr[ns..":"..nm] = attr[k]; |
|
59 |
attr[i] = ns..":"..nm; |
|
60 |
attr[k] = nil; |
|
61 |
end |
|
62 |
end |
|
63 |
end |
|
64 |
end |
|
145 | 65 |
|
66 |
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
|
67 |
if session.notopen then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
68 |
if name == "stream" then |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
69 |
streamopened(session, attr); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
70 |
return; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
71 |
end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
72 |
error("Client failed to open stream successfully"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
73 |
end |
38 | 74 |
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
|
75 |
error("Client sent invalid top-level stanza"); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
76 |
end |
145 | 77 |
|
38 | 78 |
stanza = st.stanza(name, attr); --{ to = attr.to, type = attr.type, id = attr.id, xmlns = curr_ns }); |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
79 |
curr_tag = stanza; |
145 | 80 |
else -- we are inside a stanza, so add a tag |
148 | 81 |
attr.xmlns = nil; |
82 |
if curr_ns ~= "jabber:server" and curr_ns ~= "jabber:client" then |
|
83 |
attr.xmlns = curr_ns; |
|
84 |
end |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
85 |
stanza:tag(name, attr); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
86 |
end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
87 |
end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
88 |
function xml_handlers:CharacterData(data) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
89 |
if stanza then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
90 |
t_insert(chardata, data); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
91 |
end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
92 |
end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
93 |
function xml_handlers:EndElement(name) |
148 | 94 |
curr_ns,name = name:match("^(.+)|([%w%-]+)$"); |
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
|
95 |
if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then |
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
|
96 |
if name == "stream" then |
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
|
97 |
log("debug", "Stream closed"); |
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
|
98 |
sm_destroy_session(session); |
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
|
99 |
return; |
145 | 100 |
elseif name == "error" then |
101 |
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
|
102 |
else |
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
|
103 |
error("XML parse error in client stream"); |
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
|
104 |
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
|
105 |
end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
106 |
if stanza and #chardata > 0 then |
31
aaccbf07849b
Remove now useless debug output
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
107 |
-- We have some character data in the buffer |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
108 |
stanza:text(t_concat(chardata)); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
109 |
chardata = {}; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
110 |
end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
111 |
-- Complete stanza |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
112 |
if #stanza.last_add == 0 then |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
113 |
session.stanza_dispatch(stanza); |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
114 |
stanza = nil; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
115 |
else |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
116 |
stanza:up(); |
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 |
end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
119 |
return xml_handlers; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
120 |
end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
121 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
122 |
return init_xmlhandlers; |