component.lua

changeset 485
c9a144591649
parent 411
db462d4feb44
child 490
6b2f31da9610
equal deleted inserted replaced
484:5e2978489c95 485:c9a144591649
1 local verse = require "verse"; 1 local verse = require "verse";
2 local stream = verse.stream_mt; 2 local stream_mt = verse.stream_mt;
3 3
4 local jid_split = require "util.jid".split; 4 local jid_split = require "util.jid".split;
5 local lxp = require "lxp";
6 local st = require "util.stanza"; 5 local st = require "util.stanza";
7 local sha1 = require "util.hashes".sha1; 6 local sha1 = require "util.hashes".sha1;
8 7
9 -- Shortcuts to save having to load util.stanza 8 -- Shortcuts to save having to load util.stanza
10 verse.message, verse.presence, verse.iq, verse.stanza, verse.reply, verse.error_reply = 9 verse.message, verse.presence, verse.iq, verse.stanza, verse.reply, verse.error_reply =
40 end 39 end
41 40
42 return stream:event("stanza", stanza); 41 return stream:event("stanza", stanza);
43 end 42 end
44 43
45 function stream:reset() 44 function stream_mt:connect_component(jid, pass)
46 if self.stream then
47 self.stream:reset();
48 else
49 self.stream = new_xmpp_stream(self, stream_callbacks);
50 end
51 self.notopen = true;
52 return true;
53 end
54
55 function stream:connect_component(jid, pass)
56 self.jid, self.password = jid, pass; 45 self.jid, self.password = jid, pass;
57 self.username, self.host, self.resource = jid_split(jid); 46 self.username, self.host, self.resource = jid_split(jid);
47
48 -- Component stream methods
49 function self:reset()
50 if self.stream then
51 self.stream:reset();
52 else
53 self.stream = new_xmpp_stream(self, stream_callbacks);
54 end
55 self.notopen = true;
56 return true;
57 end
58
59 function self:reopen()
60 self:reset();
61 self:send(st.stanza("stream:stream", { to = self.jid, ["xmlns:stream"]='http://etherx.jabber.org/streams',
62 xmlns = xmlns_component, version = "1.0" }):top_tag());
63 end
64
65 function self:close(reason)
66 if not self.notopen then
67 self:send("</stream:stream>");
68 end
69 local on_disconnect = self.conn.disconnect();
70 self.conn:close();
71 on_disconnect(conn, reason);
72 end
73
74 function self:send_iq(iq, callback)
75 local id = self:new_id();
76 self.tracked_iqs[id] = callback;
77 iq.attr.id = id;
78 self:send(iq);
79 end
80
81 function self:new_id()
82 self.curr_id = self.curr_id + 1;
83 return tostring(self.curr_id);
84 end
58 85
59 function self.data(conn, data) 86 function self.data(conn, data)
60 local ok, err = self.stream:feed(data); 87 local ok, err = self.stream:feed(data);
61 if ok then return; end 88 if ok then return; end
62 stream:debug("Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " ")); 89 stream:debug("Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "));
119 -- Initialise connection 146 -- Initialise connection
120 self:connect(self.connect_host or self.host, self.connect_port or 5347); 147 self:connect(self.connect_host or self.host, self.connect_port or 5347);
121 self:reopen(); 148 self:reopen();
122 end 149 end
123 150
124 function stream:reopen()
125 self:reset();
126 self:send(st.stanza("stream:stream", { to = self.jid, ["xmlns:stream"]='http://etherx.jabber.org/streams',
127 xmlns = xmlns_component, version = "1.0" }):top_tag());
128 end
129
130 function stream:close(reason)
131 if not self.notopen then
132 self:send("</stream:stream>");
133 end
134 local on_disconnect = self.conn.disconnect();
135 self.conn:close();
136 on_disconnect(conn, reason);
137 end
138
139 function stream:send_iq(iq, callback)
140 local id = self:new_id();
141 self.tracked_iqs[id] = callback;
142 iq.attr.id = id;
143 self:send(iq);
144 end
145
146 function stream:new_id()
147 self.curr_id = self.curr_id + 1;
148 return tostring(self.curr_id);
149 end

mercurial