annotate src/tp.lua @ 4:f0a27ccb1193 tip

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