Advertisement
1lann

Host

Feb 6th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.56 KB | None | 0 0
  1. local s
  2.  
  3. local host = {}
  4. local original = {}
  5. local buffer = {}
  6.  
  7. local function loadTermLib()
  8.   for k,v in pairs(term.native()) do
  9.     original[k] = v
  10.   end
  11.  
  12.   for k,v in pairs(original) do
  13.       if k == "current" or k == "redirect" then
  14.         host[k] = original[k]
  15.       else
  16.         host[k] = function(...)
  17.           local args = {...}
  18.           table.insert(args, 1, k)
  19.           local copy = {}
  20.           for k,v in pairs(args) do
  21.             copy[k] = v
  22.           end
  23.           table.insert(buffer, copy)
  24.           table.remove(args, 1)
  25.           return original[k](unpack(args))
  26.         end
  27.       end
  28.   end
  29.   host["native"] = function() return host end
  30. end
  31.  
  32. local function connect()
  33.   s = Socket.new("http://chuie.io:9000/", Socket.randomId())
  34.   loadTermLib()
  35.   s:on("event", function(msg)
  36.     os.queueEvent(unpack(textutils.unserialize(msg:gsub("\\u003c", "<"):gsub("\\u003e", ">"):gsub("\\u0026", "&"))))
  37.   end)
  38.   term.redirect(host)
  39. end
  40.  
  41. local function flushTerm()
  42.     if #buffer > 0 then
  43.         s:write("term", textutils.serialize(buffer):gsub("\n", ""))
  44.         buffer = {}
  45.     end
  46. end
  47.  
  48. local function errorMonitor()
  49.   while true do
  50.     local e, p1, p2, p3, p4 = os.pullEvent()
  51.     if e == "socket_failure" then
  52.       local f = io.open("/errors", "a")
  53.       f:write("socket_failure: " .. p1 .. "\n")
  54.       f:close()
  55.     end
  56.     flushTerm()
  57.   end
  58. end
  59.  
  60. connect()
  61. parallel.waitForAny(Socket.run, errorMonitor, function() shell.run("shell") end)
  62. term.clear()
  63. term.setCursorPos(1,1)
  64. term.setTextColor(colors.white)
  65. print("Exited screen share")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement