Advertisement
fyrkantis

News

Aug 7th, 2024 (edited)
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.52 KB | None | 0 0
  1. function split(input, maxSplits, sep)
  2.     if sep == nil then
  3.         sep = ";"
  4.     end
  5.     splits = {}
  6.     splitCount = 0
  7.     for str in string.gmatch(input, "([^"..sep.."]+)") do
  8.         if maxSplits ~= nil and splitCount > maxSplits then
  9.             splits[splitCount] = splits[splitCount]..sep..str
  10.         else
  11.             table.insert(splits, str)
  12.             splitCount = splitCount + 1
  13.         end
  14.     end
  15.     return splits
  16. end
  17.  
  18. peripheral.find("modem", rednet.open)
  19.  
  20. write("Finding database server ID...")
  21. rednet.broadcast("database", "dns-lookup")
  22. _, body = rednet.receive("dns-record-database")
  23. serverId = tonumber(body)
  24. print(" Done!")
  25.  
  26. while true do
  27.     rednet.send(serverId, "news", "database-dir-request")
  28.     _, body = rednet.receive("database-dir-response")
  29.     code, body = table.unpack(split(body, 2))
  30.     filenames = split(body)
  31.     term.clear()
  32.     term.setCursorPos(1, 1)
  33.     if code == "200" then
  34.         for i, filename in pairs(filenames) do
  35.             local path = "news/"..filename
  36.             rednet.send(serverId, path, "database-file-request")
  37.             _, file_response = rednet.receive("database-file-response", 5)
  38.             file_code, file_body = table.unpack(split(file_response, 2))
  39.             if file_code == "200" then
  40.                 print(file_body)
  41.             else
  42.                 print("ERROR "..file_code..": "..file_body.." ("..path..")")
  43.             end
  44.         end
  45.     else
  46.         print("ERROR "..code..": "..body)
  47.     end
  48.    
  49.     rednet.receive("news-update")
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement