Mercurial > luasocket
comparison src/tp.lua @ 0:4b915342e2a8
LuaSocket 2.0.2 + CMake build description.
author | Eric Wing <ewing . public |-at-| gmail . com> |
---|---|
date | Tue, 26 Aug 2008 18:40:01 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4b915342e2a8 |
---|---|
1 ----------------------------------------------------------------------------- | |
2 -- Unified SMTP/FTP subsystem | |
3 -- LuaSocket toolkit. | |
4 -- Author: Diego Nehab | |
5 -- RCS ID: $Id: tp.lua,v 1.22 2006/03/14 09:04:15 diego Exp $ | |
6 ----------------------------------------------------------------------------- | |
7 | |
8 ----------------------------------------------------------------------------- | |
9 -- Declare module and import dependencies | |
10 ----------------------------------------------------------------------------- | |
11 local base = _G | |
12 local string = require("string") | |
13 local socket = require("socket") | |
14 local ltn12 = require("ltn12") | |
15 module("socket.tp") | |
16 | |
17 ----------------------------------------------------------------------------- | |
18 -- Program constants | |
19 ----------------------------------------------------------------------------- | |
20 TIMEOUT = 60 | |
21 | |
22 ----------------------------------------------------------------------------- | |
23 -- Implementation | |
24 ----------------------------------------------------------------------------- | |
25 -- gets server reply (works for SMTP and FTP) | |
26 local function get_reply(c) | |
27 local code, current, sep | |
28 local line, err = c:receive() | |
29 local reply = line | |
30 if err then return nil, err end | |
31 code, sep = socket.skip(2, string.find(line, "^(%d%d%d)(.?)")) | |
32 if not code then return nil, "invalid server reply" end | |
33 if sep == "-" then -- reply is multiline | |
34 repeat | |
35 line, err = c:receive() | |
36 if err then return nil, err end | |
37 current, sep = socket.skip(2, string.find(line, "^(%d%d%d)(.?)")) | |
38 reply = reply .. "\n" .. line | |
39 -- reply ends with same code | |
40 until code == current and sep == " " | |
41 end | |
42 return code, reply | |
43 end | |
44 | |
45 -- metatable for sock object | |
46 local metat = { __index = {} } | |
47 | |
48 function metat.__index:check(ok) | |
49 local code, reply = get_reply(self.c) | |
50 if not code then return nil, reply end | |
51 if base.type(ok) ~= "function" then | |
52 if base.type(ok) == "table" then | |
53 for i, v in base.ipairs(ok) do | |
54 if string.find(code, v) then | |
55 return base.tonumber(code), reply | |
56 end | |
57 end | |
58 return nil, reply | |
59 else | |
60 if string.find(code, ok) then return base.tonumber(code), reply | |
61 else return nil, reply end | |
62 end | |
63 else return ok(base.tonumber(code), reply) end | |
64 end | |
65 | |
66 function metat.__index:command(cmd, arg) | |
67 if arg then | |
68 return self.c:send(cmd .. " " .. arg.. "\r\n") | |
69 else | |
70 return self.c:send(cmd .. "\r\n") | |
71 end | |
72 end | |
73 | |
74 function metat.__index:sink(snk, pat) | |
75 local chunk, err = c:receive(pat) | |
76 return snk(chunk, err) | |
77 end | |
78 | |
79 function metat.__index:send(data) | |
80 return self.c:send(data) | |
81 end | |
82 | |
83 function metat.__index:receive(pat) | |
84 return self.c:receive(pat) | |
85 end | |
86 | |
87 function metat.__index:getfd() | |
88 return self.c:getfd() | |
89 end | |
90 | |
91 function metat.__index:dirty() | |
92 return self.c:dirty() | |
93 end | |
94 | |
95 function metat.__index:getcontrol() | |
96 return self.c | |
97 end | |
98 | |
99 function metat.__index:source(source, step) | |
100 local sink = socket.sink("keep-open", self.c) | |
101 local ret, err = ltn12.pump.all(source, sink, step or ltn12.pump.step) | |
102 return ret, err | |
103 end | |
104 | |
105 -- closes the underlying c | |
106 function metat.__index:close() | |
107 self.c:close() | |
108 return 1 | |
109 end | |
110 | |
111 -- connect with server and return c object | |
112 function connect(host, port, timeout, create) | |
113 local c, e = (create or socket.tcp)() | |
114 if not c then return nil, e end | |
115 c:settimeout(timeout or TIMEOUT) | |
116 local r, e = c:connect(host, port) | |
117 if not r then | |
118 c:close() | |
119 return nil, e | |
120 end | |
121 return base.setmetatable({c = c}, metat) | |
122 end | |
123 |