Annotate

init.lua @ 0:caf260adc453

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