Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local side='front' -- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement