Commit a01797e34d872fe82b81217e9bc0fbfec86c1f68
1 parent
06b9a814
plop
git-svn-id: http://svn.net-core.org/repos/t-engine4@2150 51575b47-30f0-44d4-a5cc-537603b46e54
Showing
3 changed files
with
96 additions
and
3 deletions
game/xmpp/Client.lua
0 → 100644
1 | +-- TE4 - T-Engine 4 | |
2 | +-- Copyright (C) 2009, 2010 Nicolas Casalini | |
3 | +-- | |
4 | +-- This program is free software: you can redistribute it and/or modify | |
5 | +-- it under the terms of the GNU General Public License as published by | |
6 | +-- the Free Software Foundation, either version 3 of the License, or | |
7 | +-- (at your option) any later version. | |
8 | +-- | |
9 | +-- This program is distributed in the hope that it will be useful, | |
10 | +-- but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | +-- GNU General Public License for more details. | |
13 | +-- | |
14 | +-- You should have received a copy of the GNU General Public License | |
15 | +-- along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | +-- | |
17 | +-- Nicolas Casalini "DarkGod" | |
18 | +-- darkgod@te4.org | |
19 | + | |
20 | +require "engine.class" | |
21 | +local socket = require "socket" | |
22 | + | |
23 | +module(..., package.seeall, class.make) | |
24 | + | |
25 | +function _M:init() | |
26 | +end | |
27 | + | |
28 | +function _M:connect() | |
29 | + if not self.login or not self.pass then return end | |
30 | + if self.sock then return true end | |
31 | + self.sock = socket.connect("te4.org", 5122) | |
32 | + if not self.sock then return end | |
33 | + return self:login() | |
34 | +end | |
35 | + | |
36 | +function _M:write(str, ...) | |
37 | + self.sock:send(str:format(...)) | |
38 | +end | |
39 | + | |
40 | +function _M:read(ncode) | |
41 | + local l = self.sock:receive("*l") | |
42 | + if not l then error("no data") end | |
43 | + if ncode and l:sub(1, 3) ~= ncode then | |
44 | + error("bad return code: "..ncode.." != "..l:sub(1, 3)) | |
45 | + end | |
46 | + return l | |
47 | +end | |
48 | + | |
49 | +function _M:login() | |
50 | + if self.login and self.pass then | |
51 | + self:write("%s\n%s\n", self.login, self.pass) | |
52 | + if self:read("200") then | |
53 | + return true | |
54 | + end | |
55 | + end | |
56 | +end | |
57 | + | |
58 | +function _M:command(c, ...) | |
59 | + self.sock:send(("%s %s\n"):format(c, table.concat({...}, " "))) | |
60 | +end | |
61 | + | |
62 | +function _M:run() | |
63 | + while true do | |
64 | + if self:connect() then | |
65 | + local l = self:read() | |
66 | + print("GOT: ", l) | |
67 | + else | |
68 | + core.game.sleep(5000) -- Wait 5 seconds before retry | |
69 | + end | |
70 | + end | |
71 | +end | ... | ... |
1 | +-- TE4 - T-Engine 4 | |
2 | +-- Copyright (C) 2009, 2010 Nicolas Casalini | |
3 | +-- | |
4 | +-- This program is free software: you can redistribute it and/or modify | |
5 | +-- it under the terms of the GNU General Public License as published by | |
6 | +-- the Free Software Foundation, either version 3 of the License, or | |
7 | +-- (at your option) any later version. | |
8 | +-- | |
9 | +-- This program is distributed in the hope that it will be useful, | |
10 | +-- but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | +-- GNU General Public License for more details. | |
13 | +-- | |
14 | +-- You should have received a copy of the GNU General Public License | |
15 | +-- along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | +-- | |
17 | +-- Nicolas Casalini "DarkGod" | |
18 | +-- darkgod@te4.org | |
19 | + | |
1 | 20 | print("TE4Online starting...") |
2 | 21 | |
3 | -require "socket" | |
22 | +local Client = require "xmpp.Client" | |
4 | 23 | |
5 | -local sock = socket.connect("te4.org", 5122) | |
6 | -if not sock then return end | |
24 | +while true do | |
25 | + local c = Client.new() | |
26 | + local ok, err = pcall(c.run, c) | |
27 | + if not ok and err then print("TE4Online error: ", err) end | |
28 | +end | ... | ... |
No preview for this file type
-
Please register or login to post a comment