Advertisement
Bmorr

Untitled

Nov 28th, 2020 (edited)
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.47 KB | None | 0 0
  1. function currentTimeString()
  2.     return textutils.formatTime(os.time("local"))
  3. end
  4.  
  5. myId = os.getComputerID()
  6. myProtocol = "ftp"
  7. startupTime = currentTimeString()
  8. parentDir = "/disk/"
  9. termWidth, termHeight = term.getSize()
  10.  
  11. function printMessage(id, message, protocol)
  12.     print(id, "("..currentTimeString().."):", message)
  13.     return id, message, protocol
  14. end
  15.  
  16. function startsWith(str, substr)
  17.     if str == nil then
  18.         print("str == nil so str doesn't start with", substr)
  19.     end
  20.     return str:find("^"..substr) ~= nil
  21. end
  22.  
  23. function shortenPath(debug, path, size, ...)
  24.     if debug then
  25.         print("in:", path)
  26.     end
  27.     local lastLen = path:len() - 1
  28.     if debug then
  29.         print("l1", lastLen)
  30.     end
  31.     while path:len() >= size and path:len() ~= lastLen do
  32.         lastLen = path:len()
  33.         local s = path:len()
  34.         local e = 1
  35.         for k, v in ipairs(arg) do
  36.             if debug then
  37.                 print(v)
  38.             end
  39.             local s2, e2 = path:find(v)
  40.             s2 = s2 or path:len()
  41.             e2 = e2 or 0
  42.             if debug then
  43.                 print(e2)
  44.             end
  45.             s = math.min(s2, s)
  46.             e = math.max(e2, e)
  47.         end
  48.         if debug then
  49.             print(e)
  50.         end
  51.         --if e ~= 0 then
  52.             path = path:sub(1, math.max(e - 1, 1))..path:sub(math.max(e, 2), path:len()):gsub("%/.-%/", "/.../", 1)
  53.         --end
  54.     end
  55.     if debug then
  56.         print("ret:", path)
  57.     end
  58.     return path
  59. end
  60.  
  61. function rfind(str, substr)
  62.     return #str - str:reverse():find(substr:reverse()) + 1
  63. end
  64.  
  65. function receiveDir(data, protocol, timeout, indentation)
  66.     timeout = timeout or 1
  67.     indentation = indentation or "    "
  68.     if startsWith(data, "dir:") then
  69.         dir = string.sub(data, 5)
  70.         fs.makeDir(parentDir..dir)
  71.         print(indentation..parentDir..dir)
  72.         id, data, protocol = rednet.receive(protocol, timeout)
  73.         while data ~= "EOD" do
  74.             print("Waiting for EOD")
  75.             receiveDir(data, protocol, timeout, indentation.."    ")
  76.             id, data, protocol = rednet.receive(protocol, timeout)
  77.         end
  78.     elseif startsWith(data, "file:") then
  79.         file = string.sub(data, 6)
  80.         print(indentation..parentDir..file)
  81.         file = fs.open(parentDir..file, "w")
  82.         id, data, protocol = rednet.receive(protocol, timeout)
  83.         while data ~= "EOF" do
  84.             file.write(data.."\n")
  85.             id, data, protocol = rednet.receive(protocol, timeout)
  86.         end
  87.         file.close()
  88.     else
  89.         print("Received unknown command \""..data.."\"!")
  90.     end
  91. end
  92.  
  93. rednet.open("back")
  94. local servers = {rednet.lookup(myProtocol)}
  95.  
  96. if servers == nil then
  97.     print("No servers were found!")
  98. else
  99.     for _, id in pairs(servers) do
  100.         response = "busy"
  101.         repeat
  102.             rednet.send(id, "u up?", myProtocol)
  103.             id, response, protocol = rednet.receive(myProtocol)
  104.             if response == "busy" then
  105.                 print("Server is busy...waiting in queue")
  106.                 sleep(3)
  107.                 term.clear()
  108.                 term.setCursorPos(1, 1)
  109.             else
  110.                 term.setTextColor(colors.green)
  111.                 write(response.."\n")
  112.                 term.setTextColor(colors.white)
  113.             end
  114.         until (response ~= "busy")
  115.         id, message, protocol = rednet.receive(myProtocol, 5)
  116.         while message ~= nil do
  117.             -- Printing the "0/ftp>" in color
  118.             term.setTextColor(colors.blue)
  119.             -- Shorten the path until it fits on one line.
  120.             local path = shortenPath(false, id.."/ftp/"..message.."> ", termWidth, "/ftp/", "/%.+/")
  121.             write(path)
  122.             -- If near the end, then return
  123.             if path:len() + 3 > termWidth then
  124.                 print()
  125.             end
  126.             term.setTextColor(colors.white)
  127.             -- Handling and forwarding user input
  128.             cmd = read()
  129.             rednet.send(id, cmd, protocol)
  130.             id, data, protocol = rednet.receive(protocol, 1)
  131.             -- command cases
  132.             if startsWith(cmd, "update ") then
  133.                 receiveDir(data)
  134.             elseif startsWith(cmd, "ls") then
  135.                 while data ~= "EOD" do
  136.                     if startsWith(data, "dir:") then
  137.                         term.setTextColor(colors.green)
  138.                         -- data = data:sub(5):gsub("/^[^/]*/", "/.../")
  139.                         print(data:sub(rfind(data, "/") + 1))
  140.                         term.setTextColor(colors.white)
  141.                     elseif startsWith(data, "file:") then
  142.                         -- shortenPath(false, data:sub(6), termWidth, "/files/", "/%.+/")
  143.                         -- data = data:sub(6):gsub("/^[^/]*/", "/.../")
  144.                         print(data:sub(rfind(data, "/") + 1))
  145.                     else
  146.                         print(data, "edge cased!")
  147.                     end
  148.                     id, data, protocol = rednet.receive(protocol, 1)
  149.                 end
  150.             end
  151.             if cmd == "" or startsWith(cmd, "e") or startsWith(cmd, "st") then
  152.                 print("Ended session. Press enter to proceed.")
  153.                 read()
  154.                 message = nil
  155.                 term.clear()
  156.                 term.setCursorPos(1, 1)
  157.             else
  158.                 id, message, protocol = rednet.receive(protocol, 3)
  159.             end
  160.         end
  161.     end
  162. end
  163. rednet.close("back")
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement