Advertisement
MrHG

FTPClient 3.1

Jan 20th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.58 KB | None | 0 0
  1. -- RedNet FTP Client
  2. protocol = "FTP"
  3. serverhostname = "FTPServer"
  4. programName = "File Transfer Client "
  5. versionNumber = "3.1"
  6. modemSide = ""
  7.  
  8. function FindModem()
  9.  modemFound = false
  10.  side = ""
  11.  local pList = peripheral.getNames()
  12.  for i = 1, #pList do
  13.   if peripheral.getType(pList[i]) == "modem" then
  14.    modemFound = true
  15.    side = pList[i]
  16.   end
  17.  end
  18.  if modemFound then
  19.   return side
  20.  else
  21.   return false
  22.  end
  23. end
  24.  
  25. function WriteFile(name,filecontents)
  26.  file = fs.open(name,"w")
  27.  file.write(filecontents)
  28.  file.close()
  29. end
  30.  
  31. function TransmitFile(id,filename)
  32.  file = fs.open(filename,"r")
  33.  rednet.send(id,file.readAll(),protocol)
  34.  file.close()
  35. end
  36.  
  37. -- START PROGRAM
  38. term.clear()
  39. term.setCursorPos(1,1)
  40. if FindModem() == false then
  41.  print("Modem not Found")
  42.  return
  43. else
  44.  modemSide = FindModem()
  45. end
  46.  
  47. -- Attempting to connect to server.
  48. rednet.open(modemSide)
  49. serverid = rednet.lookup(protocol,serverhostname)
  50. if serverid then
  51.  print("Found Server.")
  52. else
  53.  print("Failed to Find Server")
  54.  return
  55. end
  56.  
  57. while true do
  58.  print("'new', 'update', 'list', 'send' or 'exit'")
  59.  ans = read()
  60.  if ans == "exit" then
  61.   break
  62.  end
  63.  
  64.  -- Getting New files
  65.  if ans == "new" then
  66.   while true do
  67.    print("Enter the name of the file")
  68.    progName = read()
  69.    if fs.exists(progName) then
  70.      print("File already exists on device.")
  71.     return
  72.    end
  73.    rednet.send(serverid,progName,protocol)
  74.    print("Request Sent. Awaiting Response.")
  75.    id, msg = rednet.receive(protocol)
  76.    if id == serverid then
  77.     if msg == "true" then
  78.      print("File found on server. Downloading.")
  79.      -- Start file Download.
  80.      id2, msg2 = rednet.receive(protocol)
  81.      WriteFile(progName,msg2)
  82.      break
  83.     elseif msg == "false" then
  84.      print("File not found.")
  85.     else
  86.      print("Unexpected Response from server")
  87.     end
  88.    else
  89.     print("Unexpected Sender")
  90.    end
  91.   end
  92.  end
  93.  -- Updating Files
  94.  if ans == "update" then
  95.   term.clear()
  96.   term.setCursorPos(1,1)
  97.   Flist = fs.list("")
  98.   for i=1, #Flist do
  99.    if not fs.isDir(Flist[i]) then
  100.     rednet.send(serverid,Flist[i],protocol)
  101.     id, msg = rednet.receive(protocol)
  102.     if id == serverid and msg == "true" then
  103.      print("Updating "..Flist[i])
  104.      id2, msg2 = rednet.receive(protocol)
  105.      fs.delete(Flist[i])
  106.      WriteFile(Flist[i],msg2)
  107.     end
  108.    end
  109.   end
  110.   print("All files Updated.")
  111.  end
  112.  -- Uploading Files to Server
  113.  if ans == "send" then
  114.   term.clear()
  115.   term.setCursorPos(1,1)
  116.   print("Enter the name of the File you'd like to upload.")
  117.   sProgName = read()
  118.   if sProgName == "startup" or sProgName == "startup.lua" then
  119.    print("Rename file before uploading.")
  120.    return
  121.   end
  122.   if fs.exists(sProgName) then
  123.    print("Enter Password: ")
  124.    sPass = read()
  125.    term.clear()
  126.    term.setCursorPos(1,1)
  127.    -- ANSWER 1
  128.    rednet.send(serverid,"SEND",protocol)
  129.    -- ANSWER 2
  130.    rednet.send(serverid,sPass,protocol)
  131.    -- ANSWER 3
  132.    id, perm = rednet.receive(protocol)
  133.    if perm == "GRANTED" then
  134.    -- ANSWER 4
  135.     rednet.send(serverid,sProgName,protocol)
  136.    -- ANSWER 5
  137.     TransmitFile(serverid,sProgName)
  138.    elseif perm == "DENIED" then
  139.     print("Wrong Password.")
  140.     return
  141.    else
  142.     print("Unexpected Response.")
  143.     return
  144.    end
  145.    print("Uploaded file: "..sProgName.." to the server.")
  146.   else
  147.    print("File does not exist.")
  148.   end
  149.  end
  150.  -- Getting a list of all files
  151.  if ans == "list" then
  152.   rednet.send(serverid,"LIST",protocol)
  153.   id, msg = rednet.receive(protocol)
  154.   term.clear()
  155.   term.setCursorPos(1,1)
  156.   print("Files on Server: "..msg)
  157.  end
  158. end
  159. rednet.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement