PaymentOption

downloadClient

Apr 25th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.01 KB | None | 0 0
  1. -- Download Manager Client by PaymentOption for NeXuS
  2. VERSION = 1.4
  3. -----------------------------------------------------
  4.  
  5. rednet.open("top")
  6. rednet.open("right")
  7. rednet.open("left")
  8. rednet.open("back")
  9. rednet.open("bottom")
  10.  
  11. shell.run("mkdir", "NeXuS/Downloads")
  12. shell.run("mkdir", "NeXuS/Files")
  13. shell.run("mkdir", "NeXuS/Settings/DownloadServer/")
  14.  
  15. selection = 1
  16. serverID = 0
  17.  
  18. if fs.exists("NeXuS/Settings/DownloadServer/serverID") then
  19.     local file = fs.open("NeXuS/Settings/DownloadServer/serverID", "r")
  20.     local fileContents = file.readAll()
  21.     file.close()
  22.    
  23.     serverID = tonumber(fileContents)
  24. else
  25.     local file = fs.open("NeXuS/Settings/DownloadServer/serverID", "w")
  26.     file.write(tonumber(serverID))
  27.     file.close()
  28. end
  29.  
  30. function cPrint(height, string)
  31.     local w, h = term.getSize()
  32.     local xPosition = w/2 - string.len(string)/2
  33.    
  34.     term.setCursorPos(xPosition, height)
  35.     term.write(string)
  36. end
  37.  
  38. function rPrint(height, string)
  39.     local w,h = term.getSize()
  40.     local xPos = w - string.len(string)
  41.    
  42.     term.setCursorPos(xPos, height)
  43.     term.write(string)
  44. end
  45.  
  46. function clear()
  47.     term.clear()
  48.     term.setCursorPos(1,1)
  49. end
  50.  
  51. function printMenu()
  52.     printHeader()
  53.    
  54.     if selection == 1 then cPrint(7, "  [ Download ]  ")
  55.     else                   cPrint(7, "    Download    ") end
  56.    
  57.     if selection == 2 then cPrint(8, "   [ Upload ]   ")
  58.     else                   cPrint(8, "     Upload     ") end
  59.    
  60.     if selection == 3 then cPrint(9, "  [ Downloads ] ")
  61.     else                   cPrint(9, "    Downloads   ") end
  62.    
  63.     if selection == 4 then cPrint(10, " [ New Server ]")
  64.     else                   cPrint(10, "   New Server  ") end
  65.    
  66.     if selection == 5 then cPrint(11, "    [ Exit ]   ")
  67.     else                   cPrint(11, "      Exit     ") end
  68. end
  69.  
  70. function printHeader()
  71.     cPrint(1, "NeXuS Download Manager")
  72.     rPrint(1, "Version: "..VERSION)
  73. end
  74.  
  75. function changeServer()
  76.     clear()
  77.     cPrint(6, "Enter new ServerID: ")
  78.     serverID = tonumber(read())
  79.     clear()
  80.    
  81.     fs.delete("NeXuS/Settings/DownloadServer/serverID")
  82.     local file = fs.open("NeXuS/Settings/DownloadServer/serverID", "w")
  83.     file.write(serverID)
  84.     file.close()
  85. end
  86.  
  87. function download()
  88.     clear()
  89.     rednet.send(serverID, "files")
  90.     sender, files = rednet.receive(0.8)
  91.     term.setCursorPos(1,1)
  92.     print("Available Files::")
  93.     print(files)
  94.    
  95.     cPrint(6, "FileName: ")
  96.     local filename = read()
  97.     clear()
  98.    
  99.     cPrint(6, "Download As: ")
  100.     local newfilename = read()
  101.     newfilename = "NeXuS/Downloads/"..newfilename
  102.     clear()
  103.    
  104.     cPrint(6, "Sending request...")
  105.     sleep(0.5)
  106.     rednet.send(serverID, "download")
  107.     sender, message = rednet.receive(5)
  108.     if sender == serverID and message == "ready" then
  109.         rednet.send(serverID, filename)
  110.         sender, fileContents = rednet.receive(0.8)
  111.         if sender == serverID and fileContents ~= "failed" then
  112.             file = fs.open(newfilename, "w")
  113.             file.write(fileContents)
  114.             file.close()
  115.             cPrint(7, "Success!")
  116.             sleep(1.3)
  117.             clear()
  118.         end
  119.     else cPrint(7, "Failure: Request not acknowledged."); sleep(1.3); clear() end
  120. end
  121.  
  122. function upload()
  123.     clear()
  124.     term.setCursorPos(1, 17)
  125.     write("*Must be in: NeXuS/Downloads!")
  126.     cPrint(6, "FileName: ")
  127.     local filename = read()
  128.     clear()
  129.    
  130.     cPrint(6, "Sending request...")
  131.     rednet.send(serverID, "upload")
  132.     sender, message = rednet.receive(0.8)
  133.    
  134.     if sender == serverID and message == "ready" then
  135.         rednet.send(serverID, tostring(filename))
  136.         sender, message = rednet.receive(0.8)
  137.        
  138.         if sender == serverID and message == "ready" then
  139.             local file = fs.open("NeXuS/Downloads/"..filename, "r")
  140.             local fileContents = file.readAll()
  141.             file.close()
  142.            
  143.             rednet.send(serverID, tostring(fileContents))
  144.             cPrint(7, "Success!"); sleep(1.3)
  145.         else cPrint(7, "Failure: Request no acknowledged."); sleep(1.3); end
  146.     else cPrint(7, "Failure: Request no acknowledged."); sleep(1.3); end
  147. end
  148.  
  149. function viewDownloads()
  150.     clear()
  151.     print("Download Folder: \n\n")
  152.     rPrint(14, "[ Exit ]")
  153.     term.setCursorPos(1, 3)
  154.     for k, v in ipairs(fs.list("NeXuS/Downloads")) do
  155.         print(v)
  156.     end
  157.    
  158.     event, key = os.pullEvent("key")
  159.     if key == 28 then end
  160. end
  161.  
  162. function printLogo()
  163.     term.clear(); term.setCursorPos(1,1)
  164.     print("     _______          ____  ___      _________")
  165.     print("     \\      \\   ____  \\   \\/  /__ __/   _____/")
  166.     print("     /   |   \\_/ __ \\  \\     /|  |  \\_____  \\ ")
  167.     print("    /    |    \\  ___/  /     \\|  |  /        \\")
  168.     print("    \\____|__  /\\___  >/___/\\  \\____/_______  /")
  169.     print("            \\/     \\/       \\_/            \\/ ")
  170.     cPrint(15, "File Client by PaymentOption"); sleep(3)
  171. end
  172.  
  173. printLogo()
  174. while true do
  175.     clear()
  176.    
  177.     printHeader()
  178.     printMenu()
  179.    
  180.     event, key = os.pullEvent("key")
  181.     if key == 200 and selection > 1 then selection = selection-1
  182.     elseif key == 208 and selection < 5 then selection = selection+1
  183.     end
  184.    
  185.     if selection == 1 and key == 28 then download()
  186.     elseif selection == 2 and key == 28 then upload()
  187.     elseif selection == 3 and key == 28 then viewDownloads()
  188.     elseif selection == 4 and key == 28 then changeServer()
  189.     elseif selection == 5 and key == 28 then clear(); break
  190.     end
  191. end
Add Comment
Please, Sign In to add comment