Bmorr

Untitled

Nov 28th, 2020 (edited)
784
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 receiveDir(data, protocol, timeout, indentation)
  62.     timeout = timeout or 1
  63.     indentation = indentation or "    "
  64.     if startsWith(data, "dir:") then
  65.         dir = string.sub(data, 5)
  66.         fs.makeDir(parentDir..dir)
  67.         print(indentation..parentDir..dir)
  68.         id, data, protocol = rednet.receive(protocol, timeout)
  69.         while data ~= "EOD" do
  70.             print("Waiting for EOD")
  71.             receiveDir(data, protocol, timeout, indentation.."    ")
  72.             id, data, protocol = rednet.receive(protocol, timeout)
  73.         end
  74.     elseif startsWith(data, "file:") then
  75.         file = string.sub(data, 6)
  76.         print(indentation..parentDir..file)
  77.         file = fs.open(parentDir..file, "w")
  78.         id, data, protocol = rednet.receive(protocol, timeout)
  79.         while data ~= "EOF" do
  80.             file.write(data.."\n")
  81.             id, data, protocol = rednet.receive(protocol, timeout)
  82.         end
  83.         file.close()
  84.     else
  85.         print("Received unknown command \""..data.."\"!")
  86.     end
  87. end
  88.  
  89. rednet.open("back")
  90. local servers = {rednet.lookup(myProtocol)}
  91.  
  92. if servers == nil then
  93.     print("No servers were found!")
  94. else
  95.     for _, id in pairs(servers) do
  96.         response = "busy"
  97.         repeat
  98.             rednet.send(id, "u up?", myProtocol)
  99.             id, response, protocol = rednet.receive(myProtocol)
  100.             if response == "busy" then
  101.                 print("Server is busy...waiting in queue")
  102.                 sleep(3)
  103.                 term.clear()
  104.                 term.setCursorPos(1, 1)
  105.             else
  106.                 term.setTextColor(colors.green)
  107.                 write(response.."\n")
  108.                 term.setTextColor(colors.white)
  109.             end
  110.         until (response ~= "busy")
  111.         id, message, protocol = rednet.receive(myProtocol, 5)
  112.         while message ~= nil do
  113.             -- Printing the "0/ftp>" in color
  114.             term.setTextColor(colors.blue)
  115.             -- Shorten the path until it fits on one line.
  116.             local path = shortenPath(false, id.."/ftp/"..message.."> ", termWidth, "/ftp/", "/%.+/")
  117.             write(path)
  118.             -- If near the end, then return
  119.             if path:len() + 3 > termWidth then
  120.                 print()
  121.             end
  122.             term.setTextColor(colors.white)
  123.             -- Handling and forwarding user input
  124.             cmd = read()
  125.             rednet.send(id, cmd, protocol)
  126.             id, data, protocol = rednet.receive(protocol, 1)
  127.             -- command cases
  128.             if startsWith(cmd, "update ") then
  129.                 receiveDir(data)
  130.             elseif startsWith(cmd, "ls") then
  131.                 while data ~= "EOD" do
  132.                     if startsWith(data, "dir:") then
  133.                         term.setTextColor(colors.green)
  134.                         -- data = data:sub(5):gsub("/^[^/]*/", "/.../")
  135.                         print(shortenPath(false, data:sub(5), termWidth, "/files/", "/%.+/"))
  136.                         term.setTextColor(colors.white)
  137.                     elseif startsWith(data, "file:") then
  138.                         -- shortenPath(false, data:sub(6), termWidth, "/files/", "/%.+/")
  139.                         -- data = data:sub(6):gsub("/^[^/]*/", "/.../")
  140.                         print(shortenPath(false, data:sub(6), termWidth, "/files/", "/%.+/"))
  141.                     else
  142.                         print(data, "edge cased!")
  143.                     end
  144.                     id, data, protocol = rednet.receive(protocol, 1)
  145.                 end
  146.             end
  147.             if cmd == "" or startsWith(cmd, "e") or startsWith(cmd, "st") then
  148.                 print("Ended session. Press enter to proceed.")
  149.                 read()
  150.                 message = nil
  151.                 term.clear()
  152.                 term.setCursorPos(1, 1)
  153.             else
  154.                 id, message, protocol = rednet.receive(protocol, 3)
  155.             end
  156.         end
  157.     end
  158. end
  159. rednet.close("back")
  160.  
RAW Paste Data