osmarks

GICR v2

Dec 11th, 2019
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.11 KB | None | 0 0
  1. local key = settings.get "gicr.key"
  2. if not key then error "No SPUDNET key provided" end
  3. local c = peripheral.find "chat_box"
  4.  
  5. local prefix = "\167bgollark (via GICR)\167r"
  6.  
  7. local ws
  8. local function connect()
  9.     if ws then pcall(ws.close) end
  10.     local error_count = 0
  11.     while true do
  12.         print "Connecting to SPUDNET..."
  13.         ws, err = http.websocket("wss://osmarks.tk/wsthing/GICR/comm", { authorization = "Key " .. key })
  14.         if not ws then
  15.             printError("Connection Error: " .. tostring(err))
  16.             error_count = error_count + 1
  17.             delay = math.pow(2, error_count)
  18.             print(("Exponential backoff: waiting %d seconds."):format(delay))
  19.             sleep(delay)
  20.             print "Attempting reconnection..."
  21.         else
  22.             return
  23.         end
  24.     end
  25. end
  26.  
  27. local function receive_ws()
  28.     local ok, result = pcall(ws.receive)
  29.     if not ok then
  30.         printError "Receive Failure"
  31.         printError(result)
  32.         connect()
  33.         return ws.receive()
  34.     end
  35.     return result
  36. end
  37.  
  38. local function send_ws(message)
  39.     local ok, result = pcall(ws.send, message)
  40.     if not ok then
  41.         printError "Send Failure"
  42.         printError(result)
  43.         connect()
  44.         ws.send(message)
  45.     end
  46.     return result
  47. end
  48.  
  49. local function chat_listener()
  50.     send_ws "Connected."
  51.     while true do
  52.         local ev, p1, p2, p3 = os.pullEvent()
  53.         if ev == "chat" then
  54.             print("Chat message:", p1, p2)
  55.             send_ws(("%s: %s"):format(p1, p2))
  56.         elseif ev == "death" then
  57.             print("Death:", p1, p2, p3)
  58.             send_ws(("%s died due to entity %s cause %s"):format(p1, p2 or "[none]", p3 or "[none]"))
  59.         elseif ev == "join" then
  60.             print("Join:", p1)
  61.             send_ws("+ " .. p1)
  62.         elseif ev == "leave" then
  63.             print("leave:", p1)
  64.             send_ws("- " .. p1)
  65.         end
  66.     end
  67. end
  68.  
  69. local function splitspace(str)
  70.     local tokens = {}
  71.     for token in string.gmatch(str, "[^%s]+") do
  72.         table.insert(tokens, token)
  73.     end
  74.     return tokens
  75. end
  76.  
  77. local function handle_command(tokens)
  78.     local t = tokens[1]
  79.     if t == "update" then
  80.         local h = http.get("https://pastebin.com/raw/70w12805?" .. tostring(math.random(0, 100000)))
  81.         local code = h.readAll()
  82.         h.close()
  83.         local ok, err = load(code, "@<code>")
  84.         if err then error("syntax error in update: " .. err) end
  85.         local f = fs.open("startup", "w")
  86.         f.write(code)
  87.         f.close()
  88.         os.reboot()
  89.     elseif t == "tell" then
  90.         table.remove(tokens, 1)
  91.         local user = table.remove(tokens, 1)
  92.         local message = table.concat(tokens, " ")
  93.         c.tell(user, message, prefix)
  94.     elseif t == "prefix" then
  95.         table.remove(tokens, 1)
  96.         local prefix = table.remove(tokens, 1)
  97.         local message = table.concat(tokens, " ")
  98.         c.say(message, prefix)
  99.     elseif t == "list" then
  100.         local list = c.getPlayerList()
  101.         send_ws(("Player list: %s"):format(table.concat(list, " ")))
  102.     end
  103. end
  104.  
  105. local function ws_listener()
  106.     while true do
  107.         local message = receive_ws()
  108.         print("Received", message)
  109.         local fst = message:sub(1, 1)
  110.         if fst == "/" then
  111.             local rest = message:sub(2)
  112.             print("Executing", rest)
  113.             local tokens = splitspace(rest)
  114.             local ok, err = pcall(handle_command, tokens)
  115.             if not ok then printError(err) end
  116.         else
  117.             c.say(message, prefix)
  118.         end
  119.     end
  120. end
  121.  
  122. connect()
  123.  
  124. parallel.waitForAll(chat_listener, ws_listener)
Add Comment
Please, Sign In to add comment