Software / code / verse
Comparison
init.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 |
| parent | 0:caf260adc453 |
| child | 2:9e9f3be09131 |
comparison
equal
deleted
inserted
replaced
| 0:caf260adc453 | 1:7c8d0a2fc004 |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 local server = require "server"; | 3 local server = require "server"; |
| 4 local events = require "events"; | |
| 4 local xmlhandlers = require "xmlhandlers"; | 5 local xmlhandlers = require "xmlhandlers"; |
| 5 local jid = require "jid"; | 6 local st = require "util.stanza"; |
| 6 local jid_split = jid.split; | |
| 7 | 7 |
| 8 module("verse", package.seeall); | 8 module("verse", package.seeall); |
| 9 local verse = _M; | 9 local verse = _M; |
| 10 | 10 |
| 11 local stream = {}; | 11 local stream = {}; |
| 12 stream.__index = stream; | 12 stream.__index = stream; |
| 13 stream_mt = stream; | |
| 13 | 14 |
| 14 function verse.new() | 15 function verse.new() |
| 15 return setmetatable({}, stream); | 16 local t = {}; |
| 17 t.id = tostring(t):match("%x*$"); | |
| 18 t.logger = logger.init(t.id); | |
| 19 t.events = events.new(); | |
| 20 return setmetatable(t, stream); | |
| 16 end | 21 end |
| 17 | 22 |
| 18 function verse.loop() | 23 function verse.loop() |
| 19 return server.loop(); | 24 return server.loop(); |
| 20 end | 25 end |
| 21 | 26 |
| 22 function stream:connect(jid, pass) | 27 function stream:connect(connect_host, connect_port) |
| 23 self.jid, self.password = jid, pass; | 28 connect_host = connect_host or "localhost"; |
| 24 self.username, self.host, self.resource = jid_split(jid); | 29 connect_port = tonumber(connect_port) or 5222; |
| 25 local conn, err = server.addclient(self.connect_host or self.host, tonumber(self.connect_port) or 5222, new_listener(self), "*a"); | |
| 26 | 30 |
| 31 -- Create and initiate connection | |
| 32 local conn = socket.tcp() | |
| 33 conn:settimeout(0); | |
| 34 local success, err = conn:connect(connect_host, connect_port); | |
| 35 | |
| 36 if not success and err ~= "timeout" then | |
| 37 self:warn("connect() to %s:%d failed: %s", connect_host, connect_port, err); | |
| 38 return false, err; | |
| 39 end | |
| 40 | |
| 41 --local conn, err = server.addclient(self.connect_host or self.host, tonumber(self.connect_port) or 5222, new_listener(self), "*a"); | |
| 42 local conn = server.wrapclient(conn, connect_host, connect_port, new_listener(self), "*a"); --, hosts[from_host].ssl_ctx, false ); | |
| 27 if not conn then | 43 if not conn then |
| 28 return nil, err; | 44 return nil, err; |
| 29 end | 45 end |
| 30 | 46 |
| 31 self.conn = conn; | 47 self.conn = conn; |
| 48 local w, t = conn.write, tostring; | |
| 49 self.send = function (_, data) return w(t(data)); end | |
| 32 end | 50 end |
| 33 | 51 |
| 52 -- Logging functions | |
| 53 function stream:debug(...) | |
| 54 return self.logger("debug", ...); | |
| 55 end | |
| 56 | |
| 57 function stream:warn(...) | |
| 58 return self.logger("warn", ...); | |
| 59 end | |
| 60 | |
| 61 function stream:error(...) | |
| 62 return self.logger("error", ...); | |
| 63 end | |
| 64 | |
| 65 -- Event handling | |
| 66 function stream:event(name, ...) | |
| 67 return self.events.fire_event(name, ...); | |
| 68 end | |
| 69 | |
| 70 function stream:hook(name, callback) | |
| 71 return self.events.add_handler(name, callback); | |
| 72 end | |
| 73 | |
| 74 -- Listener factory | |
| 34 function new_listener(stream) | 75 function new_listener(stream) |
| 35 local conn_listener = {}; | 76 local conn_listener = {}; |
| 36 | 77 |
| 37 function conn_listener.incoming(conn, data) | 78 function conn_listener.incoming(conn, data) |
| 79 stream:debug("Data"); | |
| 38 if not stream.connected then | 80 if not stream.connected then |
| 39 stream.connected = true; | 81 stream.connected = true; |
| 40 stream.send = function (stream, data) stream:debug("Sending data: "..tostring(data)); return conn.write(tostring(data)); end; | 82 stream.send = function (stream, data) stream:debug("Sending data: "..tostring(data)); return conn.write(tostring(data)); end; |
| 41 stream:event("connected"); | 83 stream:event("connected"); |
| 42 end | 84 end |