aboutsummaryrefslogtreecommitdiff
path: root/src/ftp.lua
diff options
context:
space:
mode:
authormoteus <mimir@newmail.ru>2013-05-27 12:45:09 +0400
committermoteus <mimir@newmail.ru>2013-05-27 12:45:09 +0400
commit920bc9762954454468d056a61391635cbd849406 (patch)
tree048c112f95fa578d239ae3e35b2aec0a482f0c23 /src/ftp.lua
parentbd51d8c1a5bb30e6a358dee6e85963f777edfff4 (diff)
downloadluasocket-920bc9762954454468d056a61391635cbd849406.tar.gz
luasocket-920bc9762954454468d056a61391635cbd849406.tar.bz2
luasocket-920bc9762954454468d056a61391635cbd849406.zip
Build with Lua 5.2 without LUA_COMPAT_MODULE flag.
LUASOCKET_USE_GLOBAL flag enable create global variables when load socket/mime modules.
Diffstat (limited to 'src/ftp.lua')
-rw-r--r--src/ftp.lua39
1 files changed, 20 insertions, 19 deletions
diff --git a/src/ftp.lua b/src/ftp.lua
index 5aa646b..714b388 100644
--- a/src/ftp.lua
+++ b/src/ftp.lua
@@ -15,27 +15,27 @@ local socket = require("socket")
15local url = require("socket.url") 15local url = require("socket.url")
16local tp = require("socket.tp") 16local tp = require("socket.tp")
17local ltn12 = require("ltn12") 17local ltn12 = require("ltn12")
18module("socket.ftp") 18socket.ftp = {}
19 19local _M = socket.ftp
20----------------------------------------------------------------------------- 20-----------------------------------------------------------------------------
21-- Program constants 21-- Program constants
22----------------------------------------------------------------------------- 22-----------------------------------------------------------------------------
23-- timeout in seconds before the program gives up on a connection 23-- timeout in seconds before the program gives up on a connection
24TIMEOUT = 60 24_M.TIMEOUT = 60
25-- default port for ftp service 25-- default port for ftp service
26PORT = 21 26_M.PORT = 21
27-- this is the default anonymous password. used when no password is 27-- this is the default anonymous password. used when no password is
28-- provided in url. should be changed to your e-mail. 28-- provided in url. should be changed to your e-mail.
29USER = "ftp" 29_M.USER = "ftp"
30PASSWORD = "anonymous@anonymous.org" 30_M.PASSWORD = "anonymous@anonymous.org"
31 31
32----------------------------------------------------------------------------- 32-----------------------------------------------------------------------------
33-- Low level FTP API 33-- Low level FTP API
34----------------------------------------------------------------------------- 34-----------------------------------------------------------------------------
35local metat = { __index = {} } 35local metat = { __index = {} }
36 36
37function open(server, port, create) 37function _M.open(server, port, create)
38 local tp = socket.try(tp.connect(server, port or PORT, TIMEOUT, create)) 38 local tp = socket.try(tp.connect(server, port or _M.PORT, _M.TIMEOUT, create))
39 local f = base.setmetatable({ tp = tp }, metat) 39 local f = base.setmetatable({ tp = tp }, metat)
40 -- make sure everything gets closed in an exception 40 -- make sure everything gets closed in an exception
41 f.try = socket.newtry(function() f:close() end) 41 f.try = socket.newtry(function() f:close() end)
@@ -43,22 +43,22 @@ function open(server, port, create)
43end 43end
44 44
45function metat.__index:portconnect() 45function metat.__index:portconnect()
46 self.try(self.server:settimeout(TIMEOUT)) 46 self.try(self.server:settimeout(_M.TIMEOUT))
47 self.data = self.try(self.server:accept()) 47 self.data = self.try(self.server:accept())
48 self.try(self.data:settimeout(TIMEOUT)) 48 self.try(self.data:settimeout(_M.TIMEOUT))
49end 49end
50 50
51function metat.__index:pasvconnect() 51function metat.__index:pasvconnect()
52 self.data = self.try(socket.tcp()) 52 self.data = self.try(socket.tcp())
53 self.try(self.data:settimeout(TIMEOUT)) 53 self.try(self.data:settimeout(_M.TIMEOUT))
54 self.try(self.data:connect(self.pasvt.ip, self.pasvt.port)) 54 self.try(self.data:connect(self.pasvt.ip, self.pasvt.port))
55end 55end
56 56
57function metat.__index:login(user, password) 57function metat.__index:login(user, password)
58 self.try(self.tp:command("user", user or USER)) 58 self.try(self.tp:command("user", user or _M.USER))
59 local code, reply = self.try(self.tp:check{"2..", 331}) 59 local code, reply = self.try(self.tp:check{"2..", 331})
60 if code == 331 then 60 if code == 331 then
61 self.try(self.tp:command("pass", password or PASSWORD)) 61 self.try(self.tp:command("pass", password or _M.PASSWORD))
62 self.try(self.tp:check("2..")) 62 self.try(self.tp:check("2.."))
63 end 63 end
64 return 1 64 return 1
@@ -87,7 +87,7 @@ function metat.__index:port(ip, port)
87 ip, port = self.try(self.tp:getcontrol():getsockname()) 87 ip, port = self.try(self.tp:getcontrol():getsockname())
88 self.server = self.try(socket.bind(ip, 0)) 88 self.server = self.try(socket.bind(ip, 0))
89 ip, port = self.try(self.server:getsockname()) 89 ip, port = self.try(self.server:getsockname())
90 self.try(self.server:settimeout(TIMEOUT)) 90 self.try(self.server:settimeout(_M.TIMEOUT))
91 end 91 end
92 local pl = math.mod(port, 256) 92 local pl = math.mod(port, 256)
93 local ph = (port - pl)/256 93 local ph = (port - pl)/256
@@ -199,7 +199,7 @@ end
199local function tput(putt) 199local function tput(putt)
200 putt = override(putt) 200 putt = override(putt)
201 socket.try(putt.host, "missing hostname") 201 socket.try(putt.host, "missing hostname")
202 local f = open(putt.host, putt.port, putt.create) 202 local f = _M.open(putt.host, putt.port, putt.create)
203 f:greet() 203 f:greet()
204 f:login(putt.user, putt.password) 204 f:login(putt.user, putt.password)
205 if putt.type then f:type(putt.type) end 205 if putt.type then f:type(putt.type) end
@@ -234,7 +234,7 @@ local function sput(u, body)
234 return tput(putt) 234 return tput(putt)
235end 235end
236 236
237put = socket.protect(function(putt, body) 237_M.put = socket.protect(function(putt, body)
238 if base.type(putt) == "string" then return sput(putt, body) 238 if base.type(putt) == "string" then return sput(putt, body)
239 else return tput(putt) end 239 else return tput(putt) end
240end) 240end)
@@ -242,7 +242,7 @@ end)
242local function tget(gett) 242local function tget(gett)
243 gett = override(gett) 243 gett = override(gett)
244 socket.try(gett.host, "missing hostname") 244 socket.try(gett.host, "missing hostname")
245 local f = open(gett.host, gett.port, gett.create) 245 local f = _M.open(gett.host, gett.port, gett.create)
246 f:greet() 246 f:greet()
247 f:login(gett.user, gett.password) 247 f:login(gett.user, gett.password)
248 if gett.type then f:type(gett.type) end 248 if gett.type then f:type(gett.type) end
@@ -260,7 +260,7 @@ local function sget(u)
260 return table.concat(t) 260 return table.concat(t)
261end 261end
262 262
263command = socket.protect(function(cmdt) 263_M.command = socket.protect(function(cmdt)
264 cmdt = override(cmdt) 264 cmdt = override(cmdt)
265 socket.try(cmdt.host, "missing hostname") 265 socket.try(cmdt.host, "missing hostname")
266 socket.try(cmdt.command, "missing command") 266 socket.try(cmdt.command, "missing command")
@@ -273,8 +273,9 @@ command = socket.protect(function(cmdt)
273 return f:close() 273 return f:close()
274end) 274end)
275 275
276get = socket.protect(function(gett) 276_M.get = socket.protect(function(gett)
277 if base.type(gett) == "string" then return sget(gett) 277 if base.type(gett) == "string" then return sget(gett)
278 else return tget(gett) end 278 else return tget(gett) end
279end) 279end)
280 280
281return _M \ No newline at end of file