4 -- |
4 -- |
5 -- This project is MIT/X11 licensed. Please see the |
5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. |
6 -- COPYING file in the source package for more information. |
7 -- |
7 -- |
8 |
8 |
|
9 local tblinsert = table.insert |
9 |
10 |
10 local lxp = require "lxp"; |
|
11 local lex = require "util.xmllex"; |
11 local lex = require "util.xmllex"; |
12 local st = require "util.stanza"; |
12 local index = lex.index |
13 local stanza_mt = st.stanza_mt; |
13 local tagindex_to_tree = lex.tagindex_to_tree |
14 |
14 local process_starttag = lex.process_starttag |
15 local tostring = tostring; |
|
16 local t_insert = table.insert; |
|
17 local t_concat = table.concat; |
|
18 local t_remove = table.remove; |
|
19 local setmetatable = setmetatable; |
|
20 |
15 |
21 local default_log = require "util.logger".init("xmppstream"); |
16 local default_log = require "util.logger".init("xmppstream"); |
22 |
17 |
23 local error = error; |
|
24 |
|
25 module "xmppstream" |
|
26 |
|
27 local xmlns_streams = "http://etherx.jabber.org/streams"; |
18 local xmlns_streams = "http://etherx.jabber.org/streams"; |
28 |
19 |
29 function new(session, stream_callbacks) |
20 local function new(session, stream_callbacks) |
30 local partial; |
21 local partial |
|
22 local openindexes = { } |
|
23 local lastindex = 0 |
31 |
24 |
32 local function feed(stream, data) |
|
33 index(data, |
|
34 end |
|
35 |
|
36 return { |
25 return { |
37 reset = function () |
26 reset = function ( ) |
38 parser = new_parser(handlers, ns_separator); |
27 partial = nil |
39 parse = parser.parse; |
28 openindexes = { } |
40 meta.reset(); |
29 end ; |
41 end, |
30 feed = function ( stream , data ) |
42 feed = function (self, data) |
31 while true do |
43 return parse(parser, data); |
32 partial = index ( data , partial ) |
44 end, |
33 for i = lastindex + 1 , #partial - 1 do |
45 set_session = meta.set_session; |
34 local v = partial [ i ] |
|
35 |
|
36 if v.finish then |
|
37 if v.type == "open" then |
|
38 tblinsert ( openindexes , i ) |
|
39 if #openindexes == 1 then |
|
40 local name , attr = process_starttag ( v ) |
|
41 stream_callbacks.streamopened ( session , attr ) |
|
42 end |
|
43 elseif v.type == "close" then |
|
44 if #openindexes == 2 then -- If closing level 2... |
|
45 local stanza = tagindex_to_tree ( partial , openindexes [ #openindexes ] , i ) |
|
46 stream_callbacks.handlestanza ( session , stanza ) |
|
47 elseif #openindexes == 1 then |
|
48 stream_callbacks.streamclosed ( session ) |
|
49 end |
|
50 --print ( "Closed level " .. #openindexes ) |
|
51 openindexes [ #openindexes ] = nil |
|
52 end |
|
53 end |
|
54 end |
|
55 lastindex = #partial - 1 |
|
56 break |
|
57 end |
|
58 end ; |
|
59 set_session = function ( newsession ) session = newsession end |
46 }; |
60 }; |
47 end |
61 end |
48 |
62 |
49 return _M; |
63 return { |
|
64 new = new ; |
|
65 } |