Commands: (client, server has no commands)
get <filename> – downloads a file
send <filename> – sends a file
ls [dir] – lists files, dir is optional
exit – self explanatory
cftp 0.2: (Client)
Spoiler
local side='left' -- Rednet modem/wire
-- Dont change this!
local server=nil;
local servername='';
errorMsg={'Unable to connect','Unable to get directory listing',''}
leave=false
function err(num)
return 'ERROR '..num..' '..errorMsg[num]
end
function ls(dir)
if dir==nil then
dir=cdir
else
dir=cdir..dir
end
rednet.send(server, 'ftp:'..servername..':ls:'..dir)
id,value=rednet.receive(10)
if value==nil then
value=err(2)
end
print(value)
end
function get(file)
if file==nil then
print("No File Specified! Aborting!")
else
rednet.send(server, 'ftp:'..servername..':get:'..cdir..file)
id,value=rednet.receive(10)
if value==nil or value=='File Not Found' then
print('File Not Found')
else
write('Downloading... ')
file=fs.open('/'..fs.getName(file),'w')
file.write(value)
file.close()
print('DONE')
print('File saved')
end
end
end
function send(file)
if file==nil then
print("No File Specified! Aborting!")
else
rednet.send(server, 'ftp:'..servername..':set:'..cdir..file)
id,value=rednet.receive(10)
if value==nil then
print('No reply from server')
else
if value=='OK' then
write('File accepted.\n Sending... ')
file=fs.open('/'..fs.getName(file),'r')
rednet.send(server,file.readAll())
file.close()
print('DONE')
print('File sent.')
end
end
end
end
function close(unused)
leave=true
end
function split(pString, pPattern)
local Table = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table,cap)
end
last_end = e+1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
end
cdir=''
com={exit=close,ls=ls,get=get,send=send}
rednet.open(side)
print('CraftFTP Client 0.2')
write('Please enter a server: ')
res=io.read()
servername=res
rednet.broadcast('ftp:'..res..':id')
res=rednet.receive(10)
if res==nil then
print(err(1))
else
server=res
print('Connected!')
while not leave do
write('\nFTP:'..cdir..'>')
res=io.read()
res=split(res,' ')
c=com[res[1]]
c(res[2])
--[[if res[1]=='exit' then
break
elseif res[1]=='ls' then
ls(res[2])
elseif res[1]=='get' then
get(res[2])
end]]
end
end
cftd 0.2 (server)
Spoiler
local side='left' -- Rednet side
local name='test' -- Server name
wait=5
if fs.exists('/ftp')==false then
fs.makeDir('/ftp')
end
function split(pString, pPattern)
local Table = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table,cap)
end
last_end = e+1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
end
term.clear()
term.setCursorPos((term.getSize()/2)-13,1)
print('CraftFTP Server 0.2 running')
rednet.open(side)
while true do
id,res=rednet.receive(wait)
if res == nil then
if wait==5 then
print('Server adjusting idle mode')
wait=wait+60
end
else
wait=5
print(id..'->'..res)
res=split(res,'\:')
if res[1]=='ftp' and res[2]==name then
if res[3]=='id' then
rednet.send(id,'')
elseif res[3]=='get' then
fl=''
fnf='File Not Found'
if res[4]==nil then
out=fnf
else
fl='/ftp/'..res[4]
end
if fs.exists(fl) and fs.isDir(fl)==false then
file=fs.open(fl,'r')
out=file.readAll()
file.close()
else
out=fnf
end
rednet.send(id,out)
elseif res[3]=='set' then
fl=''
if res[4]==nil then
rednet.send(id,'INVALID')
else
fl='/ftp/'..res[4]
end
if fs.exists(fl)==false then
rednet.send(id,'OK')
id2,rec=rednet.receive(10)
if id2==id then
file=fs.open(fl,'w')
out=file.write()
file.close()
end
else
rednet.send(id,'EXISTS')
end
elseif res[3]=='ls' then
lst={}
out=''
if res[4]==nil then
lst='/ftp'
else
lst='/ftp/'..res[4]
end
if fs.exists(lst) and fs.isDir(lst) then
lst2=fs.list(lst)
for k,v in ipairs(lst2) do
if fs.isDir(lst..'/'..v) then
out=out..'\nD '
else
out=out..'\nF '
end
out=out..v
end
else
out='\nDirectory does not exist'
end
if out=='' then out='\nNo Files Found' end
if not res[4]==nil then
out=res[4]..out
end
rednet.send(id,'File listing: '..out)
end
end
end
end
Beta 0.4 versions
Spoiler
This is the 0.4 beta versions of CraftFTPThis is the server (cftpd) and the client API (cftpAPI)
The server has limited user support, it will seperate user files but not check passwords yet. That will be added in 0.5
The server also now uses a config, run once then edit cftpd.conf for options. You can also run lua code therefore having a dynamic config. :mellow:/>/>
I will add the client when Wolvan finishes it. For now, feel free to add auto-update to your own programs by using the API ;)/>/>
Credits go to tomass1996 for his StrUtils API (No need to install, its been included in the top of each script, tomass1996: If you dont like me doing like this, please let me know and Ill change it)
cftpd 0.4 http://pastie.org/3578267
cftpAPI http://pastie.org/3578276
cftp 0.4 WIP, Will post soon!
cftpAPI functions: (All will return true if it succeeded or false if failed)
Spoiler
function connect(side,serverName)function needAuth()
function auth(user,pass)
function ls(dir)
function cd(dir)
function rm(item)
function exists(item)
function mv(src,dest)
function cp(src,dest)
function mkdir(dir)
function get(remoteFile,localFile)
function send(localFile,remoteFile)
function close()
Changelog:
Spoiler
- 3-12-12
- Removed old beta API and uploaded latest
- Added cftpd 0.4
- 3-9-12
- Added Beta API
- Updated API (Still untested)
- 3-7-12
- Initial upload of v0.2