Software / code / verse
Comparison
client.lua @ 1:7c8d0a2fc004
Break client-specific code into verse.client module
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Tue, 04 Aug 2009 18:25:56 +0100 |
| child | 10:3a422606a040 |
comparison
equal
deleted
inserted
replaced
| 0:caf260adc453 | 1:7c8d0a2fc004 |
|---|---|
| 1 local verse = require "verse2"; | |
| 2 local stream = verse.stream_mt; | |
| 3 | |
| 4 local jid_split = require "jid".split; | |
| 5 local lxp = require "lxp"; | |
| 6 local st = require "util.stanza"; | |
| 7 local init_xmlhandlers = require "xmlhandlers"; | |
| 8 | |
| 9 | |
| 10 local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", | |
| 11 default_ns = "jabber:client" }; | |
| 12 | |
| 13 function stream_callbacks.streamopened(stream, attr) | |
| 14 if not stream:event("opened") then | |
| 15 stream.notopen = nil; | |
| 16 end | |
| 17 return true; | |
| 18 end | |
| 19 | |
| 20 function stream_callbacks.streamclosed(stream) | |
| 21 return stream:event("closed"); | |
| 22 end | |
| 23 | |
| 24 function stream_callbacks.handlestanza(stream, stanza) | |
| 25 return stream:event("stanza", stanza); | |
| 26 end | |
| 27 | |
| 28 local function reset_stream(stream) | |
| 29 -- Reset stream | |
| 30 local parser = lxp.new(init_xmlhandlers(stream, stream_callbacks), "|"); | |
| 31 stream.parser = parser; | |
| 32 | |
| 33 stream.notopen = true; | |
| 34 | |
| 35 function stream.data(conn, data) | |
| 36 local ok, err = parser:parse(data); | |
| 37 if ok then return; end | |
| 38 stream:debug("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " ")); | |
| 39 stream:close("xml-not-well-formed"); | |
| 40 end | |
| 41 | |
| 42 return true; | |
| 43 end | |
| 44 | |
| 45 function stream:connect_client(jid, pass) | |
| 46 self.jid, self.password = jid, pass; | |
| 47 self.username, self.host, self.resource = jid_split(jid); | |
| 48 | |
| 49 reset_stream(self); | |
| 50 self:hook("incoming-raw", function (data) return self.data(self.conn, data); end); | |
| 51 | |
| 52 -- Initialise connection | |
| 53 self:connect(self.connect_host or self.host, self.connect_port or 5222); | |
| 54 self:send(st.stanza("stream:stream", { to = self.host, ["xmlns:stream"]='http://etherx.jabber.org/streams' }):top_tag()); | |
| 55 end | |
| 56 |