Annotate

core/xmlhandlers.lua @ 1:b8787e859fd2

Switched to new connection framework, courtesy of the luadch project Now supports SSL on 5223 Beginning support for presence (aka. the proper routing of stanzas)
author matthew
date Sun, 24 Aug 2008 01:51:02 +0000
child 7:dcc5ac721c20
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
6 local format = string.format;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
7 local m_random = math.random;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
8 local t_insert = table.insert;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
9 local t_remove = table.remove;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
10 local t_concat = table.concat;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
11 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
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 error = error;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
14
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
15 module "xmlhandlers"
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
16
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
17 function init_xmlhandlers(session)
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
18 local ns_stack = { "" };
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
19 local curr_ns = "";
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
20 local curr_tag;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
21 local chardata = {};
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
22 local xml_handlers = {};
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
23 local log = session.log;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
24 local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
25
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
26 local send = session.send;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
27
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
28 local stanza
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
29 function xml_handlers:StartElement(name, attr)
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
30 if stanza and #chardata > 0 then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
31 stanza:text(t_concat(chardata));
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
32 print("Char data:", t_concat(chardata));
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
33 chardata = {};
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
34 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
35 curr_ns,name = name:match("^(.+):(%w+)$");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
36 print("Tag received:", name, tostring(curr_ns));
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
37 if not stanza then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
38 if session.notopen then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
39 if name == "stream" then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
40 session.host = attr.to or error("Client failed to specify destination hostname");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
41 session.version = attr.version or 0;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
42 session.streamid = m_random(1000000, 99999999);
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
43 print(session, session.host, "Client opened stream");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
44 send("<?xml version='1.0'?>");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
45 send(format("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='%s' from='%s' >", session.streamid, session.host));
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
46 --send("<stream:features>");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
47 --send("<mechanism>PLAIN</mechanism>");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
48 --send [[<register xmlns="http://jabber.org/features/iq-register"/> ]]
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
49 --send("</stream:features>");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
50 log("info", "core", "Stream opened successfully");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
51 session.notopen = nil;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
52 return;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
53 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
54 error("Client failed to open stream successfully");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
55 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
56 if name ~= "iq" and name ~= "presence" and name ~= "message" then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
57 error("Client sent invalid top-level stanza");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
58 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
59 stanza = st.stanza(name, { to = attr.to, type = attr.type, id = attr.id, xmlns = curr_ns });
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
60 curr_tag = stanza;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
61 else
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
62 attr.xmlns = curr_ns;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
63 stanza:tag(name, attr);
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
64 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
65 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
66 function xml_handlers:CharacterData(data)
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
67 if stanza then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
68 t_insert(chardata, data);
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
69 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
70 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
71 function xml_handlers:EndElement(name)
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
72 curr_ns,name = name:match("^(.+):(%w+)$");
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
73 --print("<"..name.."/>", tostring(stanza), tostring(#stanza.last_add < 1), tostring(stanza.last_add[#stanza.last_add].name));
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
74 if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then error("XML parse error in client stream"); end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
75 if stanza and #chardata > 0 then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
76 stanza:text(t_concat(chardata));
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
77 print("Char data:", t_concat(chardata));
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
78 chardata = {};
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
79 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
80 -- Complete stanza
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
81 print(name, tostring(#stanza.last_add));
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
82 if #stanza.last_add == 0 then
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
83 session.stanza_dispatch(stanza);
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
84 stanza = nil;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
85 else
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
86 stanza:up();
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 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
89 return xml_handlers;
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
90 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
91
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
92 return init_xmlhandlers;