Advertisement
AquaJD

DISH

Aug 20th, 2017
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.91 KB | None | 0 0
  1. --[[
  2.  
  3.     DISH
  4.     Created by Dave-ee Jones
  5.    
  6.     Remotely control a computer.
  7.    
  8.     CREDIT:
  9.     Bomb Bloke
  10.     KingofGamesYami
  11.     The Crazy Phoenix
  12.     Lupus500
  13.    
  14.     Variable Key:
  15.     ch = INTEGER, channel
  16.     p = PERIPHERAL
  17.     s = STRING
  18.     b = BOOLEAN
  19.     k = KEY
  20.     c = COLOUR
  21.  
  22. --]]
  23.  
  24. -- CONFIGURATION --
  25.  
  26. local ch_HOST = 5317 -- DEFAULT CHANNEL | channel host speaks on
  27. local ch_CLIENT = 5318 -- DEFAULT CHANNEL | channel clients speak on
  28. local b_VIEWONLY = false -- DEFAULT MODE | makes host ignore clients
  29. local k_MENU = keys.leftCtrl -- DEFAULT KEY | opens the menu
  30. local c_MENU_BG = colors.lightBlue -- DEFAULT COLOUR | menu background
  31. local c_MENU_FG = colors.black -- DEFAULT COLOUR | menu text
  32. local c_MENU_IBG = colors.lightGray -- DEFAULT COLOUR | input background
  33. local c_MENU_IFG = colors.gray -- DEFAULT COLOUR | input text
  34. local c_MENU_BBG = colors.blue -- DEFAULT COLOUR | button background
  35. local c_MENU_BFG = colors.black -- DEFAULT COLOUR | button text
  36. local c_MENU_EBG = colors.red -- DEFAULT COLOUR | disconnect background
  37. local c_MENU_EFG = colors.white -- DEFAULT COLOUR | disconnect text
  38. local c_MENU_MBG_ON = colors.lime -- DEFAULT COLOUR | cp button background (on)
  39. local c_MENU_MBG_OFF = colors.red -- DEFAULT COLOUR | cp button background (off)
  40.  
  41. -- END CONFIGURATION --
  42.  
  43. local n_VERSION = 1.3
  44. local s_MODE = ""
  45. local b_RUNNING = true
  46. local b_MENU_VISIBLE = false
  47.  
  48. -- Load modem peripheral (if there is one)
  49. local p_MODEM = nil
  50. local t_PERIPHERALS = peripheral.getNames()
  51. if t_PERIPHERALS[1] then
  52.     if peripheral.getType(t_PERIPHERALS[1]) == "modem" then
  53.         p_MODEM = peripheral.wrap(t_PERIPHERALS[1])
  54.     else
  55.         printError("No wireless modem found.")
  56.         error()
  57.     end
  58. else
  59.     printError("No wireless modem found.")
  60.     error()
  61. end
  62.  
  63. -- Usage
  64. local function f_USAGE()
  65.     printError("Usage:")
  66.     printError(shell.getRunningProgram().." host [vo] [HostCh] [ClientCh]")
  67.     printError(shell.getRunningProgram().." join [HostCh] [ClientCh]")
  68.     printError(shell.getRunningProgram().." help")
  69.     error()
  70. end
  71.  
  72. -- Help
  73. local function f_HELP()
  74.     printError("---------")
  75.     printError("DISH v"..n_VERSION)
  76.     printError("---------")
  77.     printError("View Only: Clients cannot control host")
  78.     printError("HostCh: Channel the host speaks on")
  79.     printError("ClientCh: Channel the clients speak on")
  80.     error()
  81. end
  82.  
  83. -- Argument handling
  84. local t_ARGS = { ... }
  85. if #t_ARGS < 1 then
  86.     f_USAGE()
  87. else
  88.     if t_ARGS[1] == "help" then
  89.         f_HELP()
  90.     elseif t_ARGS[1] == "host" then
  91.         s_MODE = "host"
  92.         if t_ARGS[2] then
  93.             if t_ARGS[2] == "true" or t_ARGS[2] == "t" then
  94.                 b_VIEWONLY = true
  95.             elseif t_ARGS[2] == "false" or t_ARGS[2] == "f" then
  96.                 b_VIEWONLY = false
  97.             else
  98.                 f_USAGE()
  99.             end
  100.             if t_ARGS[3] then
  101.                 ch_HOST = tonumber(t_ARGS[3])
  102.             end
  103.             if t_ARGS[4] then
  104.                 ch_CLIENT = tonumber(t_ARGS[4])
  105.             end
  106.         end
  107.     elseif t_ARGS[1] == "join" then
  108.         s_MODE = "client"
  109.         if t_ARGS[2] then
  110.             ch_HOST = tonumber(t_ARGS[2])
  111.         end
  112.         if t_ARGS[3] then
  113.             ch_CLIENT = tonumber(t_ARGS[3])
  114.         end
  115.     else
  116.         f_USAGE()
  117.     end
  118. end
  119.  
  120. local function f_MENU_DRAW()
  121.     term.setBackgroundColor(c_MENU_BG)
  122.     term.setTextColor(c_MENU_FG)
  123.     term.clear()
  124.     term.setCursorPos(4,2)
  125.     print("DISH Menu")
  126.      term.setCursorPos(2,4)
  127.     print("Mode: ")
  128.     term.setTextColor(c_MENU_IFG)
  129.     term.setCursorPos(8,4)
  130.     print(string.upper(s_MODE))
  131.     term.setTextColor(c_MENU_FG)
  132.     term.setCursorPos(2,6)
  133.     print("Channel (H):")
  134.     term.setCursorPos(2,8)
  135.     print("Channel (C):")
  136.     term.setBackgroundColor(c_MENU_IBG)
  137.     term.setTextColor(c_MENU_IFG)
  138.     term.setCursorPos(2,7)
  139.     print("             ")
  140.     term.setCursorPos(2,7)
  141.     print(ch_HOST)
  142.     term.setCursorPos(2,9)
  143.     print("             ")
  144.     term.setCursorPos(2,9)
  145.     print(ch_CLIENT)
  146.     if s_MODE == "host" then
  147.         term.setBackgroundColor(c_MENU_IBG)
  148.         for i=1,4 do
  149.             term.setCursorPos(2,10+i)
  150.             print("             ")
  151.         end
  152.         term.setTextColor(c_MENU_FG)
  153.         term.setCursorPos(2,11)
  154.         print("Control Panel")
  155.         term.setCursorPos(3,12)
  156.         if b_VIEWONLY then
  157.             term.setBackgroundColor(c_MENU_MBG_ON)
  158.             term.setTextColor(c_MENU_FG)
  159.         else
  160.             term.setBackgroundColor(c_MENU_MBG_OFF)
  161.             term.setTextColor(c_MENU_FG)
  162.         end
  163.         print(" View-Only ")
  164.     end
  165.     term.setBackgroundColor(c_MENU_EBG)
  166.     term.setTextColor(c_MENU_EFG)
  167.     term.setCursorPos(2,16)
  168.     print("             ")
  169.     term.setCursorPos(2,17)
  170.     print("  Exit DISH  ")
  171.     term.setCursorPos(2,18)
  172.     print("             ")
  173. end
  174.  
  175. local function f_HOST()
  176.     term.clear()
  177.     term.setCursorPos(1,1)
  178.     printError("DISH - Host")
  179.     printError("Press "..keys.getName(k_MENU).." to open menu.")
  180.     local w_HOST = term.current()
  181.     local w_CLIENT = {}
  182.     for _n,_f in pairs(w_HOST) do
  183.         w_CLIENT[_n] = function(...)
  184.             p_MODEM.transmit(ch_HOST,ch_CLIENT,{p = "DISH_TMF", n = _n, a = {...}})
  185.             return _f(...)
  186.         end
  187.     end
  188.     term.redirect(w_CLIENT)
  189.     local function f_HOST_RUN()
  190.         shell.run("/rom/programs/shell")
  191.     end
  192.     local function f_HOST_LISTEN()
  193.         while b_RUNNING do
  194.             local _e,_s,_c,_cR,_m,_d = os.pullEvent()
  195.             if _e == "modem_message" and _c == ch_CLIENT and _cR == ch_HOST and _m.p == "DISH_EVENT" then
  196.                 if not b_VIEWONLY then
  197.                     if not b_MENU_VISIBLE then
  198.                         os.queueEvent(unpack(_m.e))
  199.                     end
  200.                 end
  201.             elseif _e == "key" and _s == k_MENU then
  202.                 local _b = not b_MENU_VISIBLE
  203.                 w_MENU.setVisible(_b)
  204.                 if _b then
  205.                     term.redirect(w_MENU)
  206.                     f_MENU_DRAW()
  207.                 else
  208.                     term.redirect(w_CLIENT)
  209.                     w_CLIENT.redraw()
  210.                 end
  211.                 b_MENU_VISIBLE = not b_MENU_VISIBLE
  212.             elseif _e == "mouse_click" and b_MENU_VISIBLE then
  213.                 -- Disconnect
  214.                 if _s == 1 and _c >= 38 and _c <= 50 and _cR >= 16 and _cR <= 18 then
  215.                     b_RUNNING = false
  216.                     term.redirect(w_ORIG)
  217.                     w_CLIENT.redraw()
  218.                     error()
  219.                 -- View-Only toggle
  220.                 elseif _s == 1 and _c >= 39 and _c <= 49 and _cR == 12 then
  221.                     b_VIEWONLY = not b_VIEWONLY
  222.                     f_MENU_DRAW()
  223.                 -- Host Channel
  224.                 elseif _s == 1 and _c >= 38 and _c <= 50 and _cR == 7 then
  225.                     term.setCursorPos(2,7)
  226.                     term.setBackgroundColor(c_MENU_IBG)
  227.                     term.setTextColor(c_MENU_IFG)
  228.                     print("             ")
  229.                     term.setCursorPos(2,7)
  230.                     local _cH = read()
  231.                     if tonumber(_cH) then
  232.                         ch_HOST = tonumber(_cH)
  233.                     end
  234.                     f_MENU_DRAW()
  235.                 -- Client Channel
  236.                 elseif _s == 1 and _c >= 38 and _c <= 50 and _cR == 9 then
  237.                     term.setCursorPos(2,9)
  238.                     term.setBackgroundColor(c_MENU_IBG)
  239.                     term.setTextColor(c_MENU_IFG)
  240.                     print("             ")
  241.                     term.setCursorPos(2,9)
  242.                     local _cC = read()
  243.                     if tonumber(_cC) then
  244.                         p_MODEM.close(ch_CLIENT)
  245.                         ch_CLIENT = tonumber(_cC)
  246.                         p_MODEM.open(ch_CLIENT)
  247.                     end
  248.                     f_MENU_DRAW()
  249.                 end
  250.             end
  251.         end
  252.     end
  253.     local c_RUN = coroutine.create(f_HOST_RUN)
  254.     local c_LISTEN = coroutine.create(f_HOST_LISTEN)
  255.     local t_EVENT = {}
  256.     while true do
  257.         if not b_MENU_VISIBLE then
  258.             coroutine.resume(c_RUN,unpack(t_EVENT))
  259.         end
  260.         coroutine.resume(c_LISTEN,unpack(t_EVENT))
  261.         t_EVENT = { os.pullEvent() }
  262.         if coroutine.status(c_RUN) == "dead" then
  263.             b_RUNNING = false
  264.             p_MODEM.transmit(ch_HOST,ch_CLIENT,{p = "DISH_STOP"})
  265.             term.setBackgroundColor(colors.black)
  266.             term.clear()
  267.             term.setCursorPos(1,1)
  268.             printError("Server stopped running.")
  269.             error()
  270.         elseif coroutine.status(c_LISTEN) == "dead" then
  271.             b_RUNNING = false
  272.             p_MODEM.transmit(ch_HOST,ch_CLIENT,{p = "DISH_STOP"})
  273.             term.setBackgroundColor(colors.black)
  274.             term.clear()
  275.             term.setCursorPos(1,1)
  276.             printError("Server has been stopped.")
  277.             error()
  278.         end
  279.     end
  280. end
  281.  
  282. local function f_CLIENT()
  283.     term.clear()
  284.     term.setCursorPos(1,1)
  285.     printError("DISH - Client")
  286.     printError("Press "..keys.getName(k_MENU).." to open menu.")
  287.     while b_RUNNING do
  288.         local _e,_s,_c,_cR,_m,_d = os.pullEvent()
  289.         if _e == "modem_message" then
  290.             if _c == ch_HOST and _cR == ch_CLIENT and _m.p == "DISH_TMF" then
  291.                 if term[_m.n] and not b_MENU_VISIBLE then
  292.                     term[_m.n](unpack(_m.a))
  293.                 end
  294.             elseif _c == ch_HOST and _cR == ch_CLIENT and _m.p == "DISH_STOP" then
  295.                 b_RUNNING = false
  296.                 term.setBackgroundColor(colors.black)
  297.                 term.clear()
  298.                 term.setCursorPos(1,1)
  299.                 printError("Host stopped the server.")
  300.                 term.redirect(w_ORIG)
  301.                 error()
  302.             end
  303.         elseif _e == "key" and _s == k_MENU then
  304.             local _b = not b_MENU_VISIBLE
  305.             w_MENU.setVisible(_b)
  306.             if _b then
  307.                 term.redirect(w_MENU)
  308.                 f_MENU_DRAW()
  309.             else
  310.                 term.redirect(w_MAIN)
  311.                 w_MAIN.redraw()
  312.             end
  313.             b_MENU_VISIBLE = not b_MENU_VISIBLE
  314.         elseif _e == "mouse_click" and b_MENU_VISIBLE then
  315.             -- Disconnect
  316.             if _s == 1 and _c >= 38 and _c <= 50 and _cR >= 16 and _cR <= 18 then
  317.                 b_RUNNING = false
  318.                 term.redirect(w_ORIG)
  319.                 w_MAIN.redraw()
  320.                 term.setBackgroundColor(colors.black)
  321.                 term.clear()
  322.                 term.setCursorPos(1,1)
  323.                 printError("Disconnected from server.")
  324.                 error()
  325.             -- Host Channel
  326.             elseif _s == 1 and _c >= 38 and _c <= 50 and _cR == 7 then
  327.                 term.setCursorPos(2,7)
  328.                 term.setBackgroundColor(c_MENU_IBG)
  329.                 term.setTextColor(c_MENU_IFG)
  330.                 print("             ")
  331.                 term.setCursorPos(2,7)
  332.                 local _cH = read()
  333.                 if tonumber(_cH) then
  334.                     p_MODEM.close(ch_HOST)
  335.                     ch_HOST = tonumber(_cH)
  336.                     p_MODEM.open(ch_HOST)
  337.                 end
  338.                 f_MENU_DRAW()
  339.             -- Client Channel
  340.             elseif _s == 1 and _c >= 38 and _c <= 50 and _cR == 9 then
  341.                 term.setCursorPos(2,9)
  342.                 term.setBackgroundColor(c_MENU_IBG)
  343.                 term.setTextColor(c_MENU_IFG)
  344.                 print("             ")
  345.                 term.setCursorPos(2,9)
  346.                 local _cC = read()
  347.                 if tonumber(_cC) then
  348.                     ch_CLIENT = tonumber(_cC)
  349.                 end
  350.                 f_MENU_DRAW()
  351.             end
  352.         else
  353.             if not b_MENU_VISIBLE then
  354.                 p_MODEM.transmit(ch_CLIENT,ch_HOST,{p = "DISH_EVENT",e = { _e,_s,_c,_cR,_m,_d }})
  355.             end
  356.         end
  357.     end
  358. end
  359.  
  360. w_ORIG = term.current()
  361. w_MAIN = window.create(w_ORIG,1,1,51,19,true)
  362. w_MENU = window.create(w_ORIG,37,1,15,19,false)
  363. term.redirect(w_MAIN)
  364. if s_MODE == "client" then
  365.     p_MODEM.open(ch_HOST)
  366.     f_CLIENT()
  367. elseif s_MODE == "host" then
  368.     p_MODEM.open(ch_CLIENT)
  369.     f_HOST()
  370. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement