Tue, 04 Aug 2009 00:10:45 +0100
Beginning of new verse
0 | 1 | |
2 | ||
3 | local server = require "server"; | |
4 | local xmlhandlers = require "xmlhandlers"; | |
5 | local jid = require "jid"; | |
6 | local jid_split = jid.split; | |
7 | ||
8 | module("verse", package.seeall); | |
9 | local verse = _M; | |
10 | ||
11 | local stream = {}; | |
12 | stream.__index = stream; | |
13 | ||
14 | function verse.new() | |
15 | return setmetatable({}, stream); | |
16 | end | |
17 | ||
18 | function verse.loop() | |
19 | return server.loop(); | |
20 | end | |
21 | ||
22 | function stream:connect(jid, pass) | |
23 | self.jid, self.password = jid, pass; | |
24 | self.username, self.host, self.resource = jid_split(jid); | |
25 | local conn, err = server.addclient(self.connect_host or self.host, tonumber(self.connect_port) or 5222, new_listener(self), "*a"); | |
26 | ||
27 | if not conn then | |
28 | return nil, err; | |
29 | end | |
30 | ||
31 | self.conn = conn; | |
32 | end | |
33 | ||
34 | function new_listener(stream) | |
35 | local conn_listener = {}; | |
36 | ||
37 | function conn_listener.incoming(conn, data) | |
38 | if not stream.connected then | |
39 | stream.connected = true; | |
40 | stream.send = function (stream, data) stream:debug("Sending data: "..tostring(data)); return conn.write(tostring(data)); end; | |
41 | stream:event("connected"); | |
42 | end | |
43 | if data then | |
44 | stream:event("incoming-raw", data); | |
45 | end | |
46 | end | |
47 | ||
48 | function conn_listener.disconnect(conn, err) | |
49 | stream.connected = false; | |
50 | stream:event("disconnected", { reason = err }); | |
51 | end | |
52 | ||
53 | return conn_listener; | |
54 | end | |
55 | ||
56 | ||
57 | local log = require "util.logger".init("verse"); | |
58 | ||
59 | return verse; |