Advertisement
EnterYourName

CCWInstaller

Jan 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.68 KB | None | 0 0
  1.  
  2. local modem = peripheral.find("modem")
  3. if modem == nil then
  4.  
  5.     term.clear()
  6.     print("Please attach a wireless modem to use the ccw.")
  7.     while true do
  8.         sleep(1)
  9.     end
  10.  
  11. end
  12.  
  13. function install(programName, programCode)
  14.     local h = fs.open("startup","w")
  15.     h.writeLine("shell.run(\"delete "..programName.."\")")
  16.     h.writeLine("shell.run(\"pastebin get "..programCode.." "..programName.."\")")
  17.     h.writeLine("shell.run(\""..programName.."\")")
  18.     h.close()
  19. end
  20.  
  21. function dhcpInstaller()
  22.  
  23.     install("DHCP","3bgBhh1Y")
  24.  
  25. end
  26.  
  27. function routerInstaller()
  28.  
  29.     install("router","AWeF7DZP")
  30.  
  31. end
  32.  
  33. function clientInstaller()
  34.  
  35.     getIP()
  36.     install("client","X0sW3SHg")
  37.  
  38. end
  39.  
  40. function webserverInstaller()
  41.  
  42.     getIP()
  43.     install("server","VQ2mnKHc")
  44.  
  45. end
  46.  
  47. -------------------------------------------------------------------------------------
  48.  
  49. function getIP()
  50.  
  51.     local label = os.getComputerLabel();
  52.  
  53.     function send(channel, replychannel, table)
  54.         modem.transmit(channel, replychannel, textutils.serialize(table))
  55.     end
  56.  
  57.     function deserialize(message)
  58.         return textutils.unserialize(message)
  59.     end
  60.  
  61.     local function getMinecraftTimestamp()
  62.         return string.format("%d:%d:%d", os.day(), os.time(), os.clock())
  63.     end
  64.  
  65.     if label == nil or label == "" then
  66.         label = getMinecraftTimestamp()
  67.         os.setComputerLabel(label)
  68.     end
  69.  
  70.     local function setupEventHandler()
  71.  
  72.         local _, _, senderChannel, _, message, _ = os.pullEvent("modem_message")
  73.         local table
  74.  
  75.         print("-- Received Event --")
  76.         print("SenderChannel: "..(senderChannel or "nil"))
  77.         print("Message: "..(message or "nil"))
  78.  
  79.         if pcall(function() table = deserialize(message) end) and table.id == label and table["status"] == "reply" then
  80.  
  81.             local ip = table["ip"]
  82.             os.setComputerLabel(ip.."")
  83.  
  84.         end
  85.  
  86.     end
  87.  
  88.     local function timeout()
  89.         sleep(5)
  90.     end
  91.  
  92.     local function setupChannel()
  93.  
  94.         local attempt = 0
  95.         modem.open(31254)
  96.  
  97.         while os.getComputerLabel() == label do
  98.             attempt = attempt+1
  99.             term.clear()
  100.             term.setCursorPos(1,1)
  101.             print("Setting up communication channel.")
  102.             print("Attempt #"..attempt)
  103.  
  104.             local toSend = {["id"]=label}
  105.  
  106.             send(31255,31254,toSend)
  107.  
  108.             parallel.waitForAny(timeout, setupEventHandler)
  109.  
  110.             sleep(0.1)
  111.         end
  112.  
  113.         modem.close(31254)
  114.         print("Received ip: "..os.getComputerLabel())
  115.  
  116.     end
  117.  
  118.     setupChannel()
  119.  
  120. end
  121.  
  122. -------------------------------------------------------------------------------------
  123.  
  124. local label = (os.getComputerLabel() or "")
  125.  
  126. if pocket then
  127.     clientInstaller();
  128. elseif label == "DHCP" then
  129.     dhcpInstaller()
  130. elseif label == "router" then
  131.     routerInstaller()
  132. else
  133.     term.clear()
  134.     term.setCursorPos(1,1)
  135.     while true do
  136.  
  137.         print("Please enter the type you want to configure this device as")
  138.         local input = string.lower(read())
  139.         if input == "dhcp" then
  140.             os.setComputerLabel("DHCP")
  141.             dhcpInstaller()
  142.             break
  143.         elseif input == "router" then
  144.             os.setComputerLabel("router")
  145.             routerInstaller()
  146.             break
  147.         elseif input == "client" then
  148.             clientInstaller()
  149.             break
  150.         elseif input == "server" or input == "webserver" then
  151.             webserverInstaller()
  152.             break
  153.         end
  154.         term.clear()
  155.         term.setCursorPos(1,1)
  156.         print("Invalid input. Type 'Client' to setup a client")
  157.  
  158.     end
  159. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement