Advertisement
AquaJD

hsh

Aug 14th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.24 KB | None | 0 0
  1. --[[
  2.  
  3.     Created by Dave-ee Jones
  4.    
  5.     Remotely control a computer's shell. Has some small features for more
  6.     capabilities and uses.
  7.    
  8.     CREDIT:
  9.     Bomb Bloke
  10.     KingofGamesYami
  11.     The Crazy Phoenix
  12.     Lupus590
  13.    
  14.     TODO:
  15.     - Side menu for HOST
  16.     - Re-write for neater flow
  17.    
  18. --]]
  19.  
  20.  
  21. -- CONFIG: CHANGE AS NEEDED --
  22. local nChannelHost = 5317 -- channel the host speaks on
  23. local nChannelClient = 5318 -- channel the clients speak on
  24. local oModem = peripheral.wrap("top") -- side of the modem
  25. local bViewOnly = false -- makes the host view only
  26. local kMenuKey = keys.leftCtrl -- opens menu
  27. local kStopKey = keys.leftCtrl -- closes connection
  28.  
  29. local cMenu_bg = colors.lightBlue
  30. local cMenu_bg2 = colors.gray
  31. local cMenu_fg = colors.black
  32. local cMenu_fg2 = colors.lightGray
  33. local cMenu_bbg = colors.blue
  34. local cMenu_bfg = colors.black
  35. local cMenu_ebg = colors.red
  36. -- END CONFIG --
  37.  
  38. local sMode = ""
  39. local bRunning = true
  40. local bMenuVisible = false
  41.  
  42. -- The HOW I DO DIS function
  43. local function printUsage()
  44.     printError("Usage:")
  45.     printError(shell.getRunningProgram().." host [VO] [channelHost] [channelClient]")
  46.     printError(shell.getRunningProgram().." join [channelHost] [channelClient]")
  47.     printError(shell.getRunningProgram().." help")
  48.     error()
  49. end
  50.  
  51. -- The WHAT IS DIS function
  52. local function printHelp()
  53.     printError("Host: Host a session")
  54.     printError("Join: Join a session")
  55.     printError("Channel (Host): Channel that the host speaks on")
  56.     printError("Channel (Client): Channel that the client speaks on")
  57.     printError("View Only (VO): Clients cannot take control")
  58.     error()
  59. end
  60.  
  61. -- Argument handling
  62. local tArgs = { ... }
  63. if #tArgs < 1 then
  64.     printUsage()
  65. else
  66.     -- Set to host
  67.     if tArgs[1] == "host" then
  68.         sMode = "host"
  69.         -- View Only mode?
  70.         if tArgs[2] then
  71.             if tArgs[2] == "true" then
  72.                 bViewOnly = true
  73.             elseif tArgs[2] == "false" then
  74.                 bViewOnly = false
  75.             else
  76.                 printUsage()
  77.             end
  78.         end
  79.         -- Apply channels set via args (HOST)
  80.         if tArgs[3] then
  81.             nChannelHost = tonumber(tArgs[3])
  82.         end
  83.         if tArgs[4] then
  84.             nChannelClient = tonumber(tArgs[4])
  85.         end
  86.     -- Set to client
  87.     elseif tArgs[1] == "join" then
  88.         sMode = "client"
  89.         -- Apply channels set via args (JOIN)
  90.         if tArgs[2] then
  91.             nChannelHost = tonumber(tArgs[2])
  92.         end
  93.         if tArgs[3] then
  94.             nChannelClient = tonumber(tArgs[3])
  95.         end
  96.     -- Show help
  97.     elseif tArgs[1] == "help" then
  98.         printHelp()
  99.     end
  100. end
  101.  
  102. local function drawMenu()
  103.     term.setBackgroundColor(cMenu_bg)
  104.     term.setTextColor(cMenu_fg)
  105.     term.clear()
  106.     term.setCursorPos(4,2)
  107.     print("HSH Menu")
  108.     term.setCursorPos(2,17)
  109.     term.setBackgroundColor(cMenu_ebg)
  110.     term.setTextColor(cMenu_bfg)
  111.     print(" Disconnect ")
  112.     term.setBackgroundColor(cMenu_bg)
  113.     term.setCursorPos(2,5)
  114.     print("Channel (H):")
  115.     term.setCursorPos(3,6)
  116.     term.setTextColor(cMenu_fg2)
  117.     print(nChannelHost)
  118.     term.setCursorPos(2,7)
  119.     term.setTextColor(cMenu_fg)
  120.     print("Channel (C):")
  121.     term.setCursorPos(3,8)
  122.     term.setTextColor(cMenu_fg2)
  123.     print(nChannelClient)
  124. end
  125.  
  126. -- Main (CLIENT)
  127. if sMode == "client" then
  128.     local wOrig = term.current()
  129.     local wMain = window.create(term.current(),1,1,51,19,true)
  130.     local wMenu = window.create(wOrig,38,1,14,19,false)
  131.     term.redirect(wMain)
  132.     oModem.open(nChannelHost) -- listen to host
  133.     term.clear()
  134.     term.setCursorPos(1,1)
  135.     printError("Open menu by pressing the "..keys.getName(kMenuKey).." key.")
  136.     while bRunning do
  137.         local event, side, channel, channelReply, message, distance = os.pullEvent()
  138.         if event == "modem_message" then
  139.             if channel == nChannelHost and channelReply == nChannelClient and message.p == "HSH_TERM_FUNCTION" then
  140.                 term[message.n](unpack(message.a))
  141.             elseif channel == nChannelHost and channelReply == nChannelClient and message.p == "HSH_STOP" then
  142.                 bRunning = false
  143.                 term.clear()
  144.                 term.setCursorPos(1,1)
  145.                 printError("Host stopped the server.")
  146.                 term.redirect(wOrig)
  147.                 error()
  148.             end
  149.         elseif event == "mouse_click" and bMenuVisible then
  150.             -- Clicked 'Disconnect' button
  151.             if side == 1 and channel >= 39 and channel <= 50 and channelReply == 17 then
  152.                 bRunning = false
  153.                 term.redirect(wOrig)
  154.                 wMain.redraw()
  155.                 term.setBackgroundColor(colors.black)
  156.                 term.clear()
  157.                 term.setCursorPos(1,1)
  158.                 printError("Disconnected from host.")
  159.                 error()
  160.             -- Clicked the host channel
  161.             elseif side == 1 and channel >= 40 and channel <= 50 and channelReply == 6 then
  162.                 term.setCursorPos(3,6)
  163.                 term.setBackgroundColor(cMenu_bg2)
  164.                 term.setTextColor(cMenu_fg2)
  165.                 print("          ")
  166.                 term.setCursorPos(3,6)
  167.                 local _cH = read()
  168.                 if tonumber(_cH) then
  169.                     oModem.close(nChannelHost) -- close old host channel
  170.                     nChannelHost = tonumber(_cH)
  171.                     oModem.open(nChannelHost) -- listen to new host channel
  172.                 end
  173.                 drawMenu()
  174.             -- Clicked the client channel
  175.             elseif side == 1 and channel >= 40 and channel <= 50 and channelReply == 8 then
  176.                 term.setCursorPos(3,8)
  177.                 term.setBackgroundColor(cMenu_bg2)
  178.                 term.setTextColor(cMenu_fg2)
  179.                 print("          ")
  180.                 term.setCursorPos(3,8)
  181.                 local _cC = read()
  182.                 if tonumber(_cC) then
  183.                     nChannelClient = tonumber(_cC)
  184.                 end
  185.                 drawMenu()
  186.             end
  187.         elseif event == "key" and side == kMenuKey then
  188.             bMenuVisible = not bMenuVisible
  189.             wMenu.setVisible(bMenuVisible)
  190.             if bMenuVisible then
  191.                 term.redirect(wMenu)
  192.                 drawMenu()
  193.             else
  194.                 term.redirect(wMain)
  195.                 wMain.redraw()
  196.             end
  197.         else
  198.             if not bMenuVisible then
  199.                 oModem.transmit(nChannelClient, nChannelHost, {p = "HSH_EVENT", e = { event, side, channel, channelReply, message, distance }})
  200.             end
  201.         end
  202.     end
  203. -- Main (HOST)
  204. elseif sMode == "host" then
  205.     oModem.open(nChannelClient) -- listen to client/s
  206.     term.clear()
  207.     term.setCursorPos(1,1)
  208.     printError("Stop hosting by pressing the "..keys.getName(kStopKey).." key.")
  209.     local term_server = term.current()
  210.     local term_client = {}
  211.     for _name, _func in pairs(term_server) do
  212.         term_client[_name] = function(...)
  213.             oModem.transmit(nChannelHost, nChannelClient, {p = "HSH_TERM_FUNCTION", n = _name, a = {...}})
  214.             return _func(...)
  215.         end
  216.     end
  217.     term.redirect(term_client)
  218.     -- Run function
  219.     local function run()
  220.         shell.run("/rom/programs/shell")
  221.     end
  222.     -- Listen function
  223.     local function listen()
  224.         while bRunning do
  225.             local event, side, channel, channelReply, message, distance = os.pullEvent()
  226.             if event == "modem_message" and channel == nChannelClient and channelReply == nChannelHost and message.p == "HSH_EVENT" then
  227.                 if not bViewOnly then
  228.                     os.queueEvent(unpack(message.e))
  229.                 end
  230.             elseif event == "key" and side == kStopKey then
  231.                 bRunning = false
  232.             end
  233.         end
  234.     end
  235.    
  236.     -- Host Code
  237.     local cor1 = coroutine.create(run)
  238.     local cor2 = coroutine.create(listen)
  239.     local _e = {}
  240.     while true do
  241.         if bRunning then
  242.             coroutine.resume(cor2,unpack(_e))
  243.         end
  244.         coroutine.resume(cor1,unpack(_e))
  245.         _e = { os.pullEvent() }
  246.         if coroutine.status(cor1) == "dead" then
  247.             bRunning = false
  248.             oModem.transmit(nChannelHost, nChannelClient, {p = "HSH_STOP"})
  249.             printError("Server stopped running.")
  250.             error()
  251.         elseif coroutine.status(cor2) == "dead" then
  252.             bRunning = false
  253.             oModem.transmit(nChannelHost, nChannelClient, {p = "HSH_STOP"})
  254.             printError("Server has been stopped.")
  255.             error()
  256.         end
  257.     end
  258. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement