xmppstream.lua

Fri, 17 Sep 2010 14:47:45 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 17 Sep 2010 14:47:45 +0100
changeset 2
c04d4fd34685
parent 0
319733864f05
child 3
ab02540afcf3
permissions
-rw-r--r--

demo_string.lua: Example opening a feed from a string

0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local lxp = require "lxp";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local st = require "stanza";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local tostring = tostring;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local t_insert = table.insert;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local t_concat = table.concat;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local error = error;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 module "xmppstream"
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local new_parser = lxp.new;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local ns_prefixes = {
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 ["http://www.w3.org/XML/1998/namespace"] = "xml";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 };
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local xmlns_streams = "http://etherx.jabber.org/streams";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local ns_separator = "\1";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 function new_sax_handlers(session, stream_callbacks)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local xml_handlers = {};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local log = session.log;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local cb_streamopened = stream_callbacks.streamopened;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local cb_streamclosed = stream_callbacks.streamclosed;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local cb_error = stream_callbacks.error or function(session, e) error("XML stream error: "..tostring(e)); end;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local cb_handlestanza = stream_callbacks.handlestanza;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local stream_ns = stream_callbacks.stream_ns or xmlns_streams;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local stream_tag = stream_ns..ns_separator..(stream_callbacks.stream_tag or "stream");
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local stream_error_tag = stream_ns..ns_separator..(stream_callbacks.error_tag or "error");
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local stream_default_ns = stream_callbacks.default_ns;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local chardata, stanza = {};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 function xml_handlers:StartElement(tagname, attr)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 if stanza and #chardata > 0 then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 -- We have some character data in the buffer
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 stanza:text(t_concat(chardata));
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 chardata = {};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local curr_ns,name = tagname:match(ns_pattern);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 if name == "" then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 curr_ns, name = "", curr_ns;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 if curr_ns ~= stream_default_ns then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 attr.xmlns = curr_ns;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 -- FIXME !!!!!
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 for i=1,#attr do
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 local k = attr[i];
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 attr[i] = nil;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 local ns, nm = k:match(ns_pattern);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 if nm ~= "" then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 ns = ns_prefixes[ns];
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 if ns then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 attr[ns..":"..nm] = attr[k];
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 attr[k] = nil;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 if not stanza then --if we are not currently inside a stanza
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 if session.notopen then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 if tagname == stream_tag then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 if cb_streamopened then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 cb_streamopened(session, attr);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 else
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 -- Garbage before stream?
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 cb_error(session, "no-stream");
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 return;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 cb_error(session, "invalid-top-level-element");
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 stanza = st.stanza(name, attr);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 else -- we are inside a stanza, so add a tag
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 attr.xmlns = nil;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 if curr_ns ~= stream_default_ns then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 attr.xmlns = curr_ns;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 stanza:tag(name, attr);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 function xml_handlers:CharacterData(data)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 if stanza then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 t_insert(chardata, data);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 function xml_handlers:EndElement(tagname)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 if stanza then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 if #chardata > 0 then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 -- We have some character data in the buffer
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 stanza:text(t_concat(chardata));
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 chardata = {};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 -- Complete stanza
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 if #stanza.last_add == 0 then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 if tagname ~= stream_error_tag then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 cb_handlestanza(session, stanza);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 else
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 cb_error(session, "stream-error", stanza);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 stanza = nil;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 else
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 stanza:up();
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 else
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 if tagname == stream_tag then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 if cb_streamclosed then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 cb_streamclosed(session);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 else
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 local curr_ns,name = tagname:match(ns_pattern);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 if name == "" then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 curr_ns, name = "", curr_ns;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 cb_error(session, "parse-error", "unexpected-element-close", name);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 stanza, chardata = nil, {};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 local function reset()
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 stanza, chardata = nil, {};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 return xml_handlers, { reset = reset };
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 function new(session, stream_callbacks)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 local handlers, meta = new_sax_handlers(session, stream_callbacks);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 local parser = new_parser(handlers, ns_separator);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 local parse = parser.parse;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 return {
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 reset = function ()
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 parser = new_parser(handlers, ns_separator);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 parse = parser.parse;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 meta.reset();
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 end,
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 feed = function (self, data)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 return parse(parser, data);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 };
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 return _M;

mercurial