Advertisement
Lyqyd

vncd

Feb 23rd, 2014
497
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local args = {...}
  2.  
  3. local connections = {}
  4.  
  5. local nshAPI = {
  6.     connList = connections
  7. }
  8.  
  9. nshAPI.getRemoteID = function()
  10.     return false
  11. end
  12.  
  13. nshAPI.send = function(msg)
  14.     return false
  15. end
  16.  
  17. nshAPI.receive = function(timeout)
  18.     return false
  19. end
  20.  
  21. nshAPI.getClientCapabilities = function()
  22.     return false
  23. end
  24.  
  25. nshAPI.getRemoteConnections = function()
  26.     local remotes = {}
  27.     for cNum, cInfo in pairs(nshAPI.connList) do
  28.         table.insert(remotes, cNum)
  29.         if cInfo.outbound then
  30.             table.insert(remotes, cInfo.outbound)
  31.         end
  32.     end
  33.     return remotes
  34. end
  35.  
  36. nshAPI.packFile = function(path)
  37.     return false
  38. end
  39.  
  40. nshAPI.unpackAndSaveFile = function(path, data)
  41.     return false
  42. end
  43.  
  44. local packetConversion = {
  45.     query = "SQ",
  46.     response = "SR",
  47.     data = "SP",
  48.     close = "SC",
  49.     textTable = "TT",
  50.     event = "EV",
  51.     SQ = "query",
  52.     SR = "response",
  53.     SP = "data",
  54.     SC = "close",
  55.     TT = "textTable",
  56.     EV = "event",
  57. }
  58.  
  59. local function openModem()
  60.     local modemFound = false
  61.     for _, side in ipairs(rs.getSides()) do
  62.         if peripheral.getType(side) == "modem" then
  63.             if not rednet.isOpen(side) then rednet.open(side) end
  64.             modemFound = true
  65.             break
  66.         end
  67.     end
  68.     return modemFound
  69. end
  70.  
  71. local function send(id, pType, message)
  72.     if pType and message then
  73.         if term.current then
  74.             return rednet.send(id, packetConversion[pType]..":;"..message, "tror")
  75.         else
  76.             return rednet.send(id, packetConversion[pType]..":;"..message)
  77.         end
  78.     end
  79. end
  80.  
  81. local function resumeThread(co, event)
  82.     if not co.filter or event[1] == co.filter then
  83.         co.filter = nil
  84.         local _old = term.redirect(co.target)
  85.         local passback = {coroutine.resume(co.thread, unpack(event))}
  86.         if _old then term.redirect(_old) else term.restore() end
  87.         if passback[1] and passback[2] then
  88.             co.filter = passback[2]
  89.         end
  90.         if coroutine.status(co.thread) == "dead" then
  91.             for cNum, cInfo in pairs(connections) do
  92.                 send(cNum, "close", "disconnect")
  93.             end
  94.             connections = {}
  95.         end
  96.         if connections[conn] and conn ~= "localShell" and framebuffer then
  97.             for cNum, cInfo in pairs(connections) do
  98.                 send(cNum, "textTable", textutils.serialize(co.target.buffer))
  99.             end
  100.         end
  101.         framebuffer.draw(co.target.buffer)
  102.     end
  103. end
  104.  
  105. if not openModem() then error("No modem present!") end
  106.  
  107. if not framebuffer then if not shell.resolveProgram("framebuffer") or not os.loadAPI(shell.resolveProgram("framebuffer")) then error("Could not load framebuffer API") end end
  108.  
  109. term.clear()
  110. term.setCursorPos(1,1)
  111. local x, y = term.getSize()
  112.  
  113. local redirect = framebuffer.new(x, y, term.isColor())
  114.  
  115. local shellRoutine = coroutine.create(function() shell.run("/rom/programs/shell", unpack(args)) end)
  116. local co = {thread = shellRoutine, target = redirect}
  117.  
  118. resumeThread(co, {})
  119.  
  120. _G.nsh = nshAPI
  121.  
  122. while true do
  123.     event = {os.pullEventRaw()}
  124.     if event[1] == "rednet_message" then
  125.         if packetConversion[string.sub(event[3], 1, 2)] then
  126.             --this is a packet meant for us.
  127.             conn = event[2]
  128.             packetType = packetConversion[string.sub(event[3], 1, 2)]
  129.             message = string.match(event[3], ";(.*)")
  130.             if connections[conn] and connections[conn].status == "open" then
  131.                 if packetType == "event" then
  132.                     local eventTable = textutils.unserialize(message)
  133.                     resumeThread(co, eventTable)
  134.                 elseif packetType == "query" then
  135.                     connections[conn] = {status = "open"}
  136.                     send(conn, "response", "OK")
  137.                     send(conn, "textTable", textutils.serialize(co.target.buffer))
  138.                 elseif packetType == "close" then
  139.                     connections[conn] = nil
  140.                     send(conn, "close", "disconnect")
  141.                     --close connection
  142.                 end
  143.             elseif packetType ~= "query" then
  144.                 --usually, we would send a disconnect here, but this prevents one from hosting nsh and connecting to other computers.  Pass these to all shells as well.
  145.                 resumeThread(co, event)
  146.             else
  147.                 --open new connection
  148.                 connections[conn] = {status = "open"}
  149.                 send(conn, "response", "OK")
  150.                 send(conn, "textTable", textutils.serialize(co.target.buffer))
  151.             end
  152.         else
  153.             --rednet message, but not in the correct format, so pass to all shells.
  154.             resumeThread(co, event)
  155.         end
  156.     else
  157.         --dispatch all other events to all shells
  158.         resumeThread(co, event)
  159.     end
  160. end
Advertisement
RAW Paste Data Copied
Advertisement