Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Created by Dave-ee Jones
- Remotely control a computer's shell. Has some small features for more
- capabilities and uses.
- CREDIT:
- Bomb Bloke
- KingofGamesYami
- The Crazy Phoenix
- Lupus590
- TODO:
- - Side menu for HOST
- - Re-write for neater flow
- --]]
- -- CONFIG: CHANGE AS NEEDED --
- local nChannelHost = 5317 -- channel the host speaks on
- local nChannelClient = 5318 -- channel the clients speak on
- local oModem = peripheral.wrap("top") -- side of the modem
- local bViewOnly = false -- makes the host view only
- local kMenuKey = keys.leftCtrl -- opens menu
- local kStopKey = keys.leftCtrl -- closes connection
- local cMenu_bg = colors.lightBlue
- local cMenu_bg2 = colors.gray
- local cMenu_fg = colors.black
- local cMenu_fg2 = colors.lightGray
- local cMenu_bbg = colors.blue
- local cMenu_bfg = colors.black
- local cMenu_ebg = colors.red
- -- END CONFIG --
- local sMode = ""
- local bRunning = true
- local bMenuVisible = false
- -- The HOW I DO DIS function
- local function printUsage()
- printError("Usage:")
- printError(shell.getRunningProgram().." host [VO] [channelHost] [channelClient]")
- printError(shell.getRunningProgram().." join [channelHost] [channelClient]")
- printError(shell.getRunningProgram().." help")
- error()
- end
- -- The WHAT IS DIS function
- local function printHelp()
- printError("Host: Host a session")
- printError("Join: Join a session")
- printError("Channel (Host): Channel that the host speaks on")
- printError("Channel (Client): Channel that the client speaks on")
- printError("View Only (VO): Clients cannot take control")
- error()
- end
- -- Argument handling
- local tArgs = { ... }
- if #tArgs < 1 then
- printUsage()
- else
- -- Set to host
- if tArgs[1] == "host" then
- sMode = "host"
- -- View Only mode?
- if tArgs[2] then
- if tArgs[2] == "true" then
- bViewOnly = true
- elseif tArgs[2] == "false" then
- bViewOnly = false
- else
- printUsage()
- end
- end
- -- Apply channels set via args (HOST)
- if tArgs[3] then
- nChannelHost = tonumber(tArgs[3])
- end
- if tArgs[4] then
- nChannelClient = tonumber(tArgs[4])
- end
- -- Set to client
- elseif tArgs[1] == "join" then
- sMode = "client"
- -- Apply channels set via args (JOIN)
- if tArgs[2] then
- nChannelHost = tonumber(tArgs[2])
- end
- if tArgs[3] then
- nChannelClient = tonumber(tArgs[3])
- end
- -- Show help
- elseif tArgs[1] == "help" then
- printHelp()
- end
- end
- local function drawMenu()
- term.setBackgroundColor(cMenu_bg)
- term.setTextColor(cMenu_fg)
- term.clear()
- term.setCursorPos(4,2)
- print("HSH Menu")
- term.setCursorPos(2,17)
- term.setBackgroundColor(cMenu_ebg)
- term.setTextColor(cMenu_bfg)
- print(" Disconnect ")
- term.setBackgroundColor(cMenu_bg)
- term.setCursorPos(2,5)
- print("Channel (H):")
- term.setCursorPos(3,6)
- term.setTextColor(cMenu_fg2)
- print(nChannelHost)
- term.setCursorPos(2,7)
- term.setTextColor(cMenu_fg)
- print("Channel (C):")
- term.setCursorPos(3,8)
- term.setTextColor(cMenu_fg2)
- print(nChannelClient)
- end
- -- Main (CLIENT)
- if sMode == "client" then
- local wOrig = term.current()
- local wMain = window.create(term.current(),1,1,51,19,true)
- local wMenu = window.create(wOrig,38,1,14,19,false)
- term.redirect(wMain)
- oModem.open(nChannelHost) -- listen to host
- term.clear()
- term.setCursorPos(1,1)
- printError("Open menu by pressing the "..keys.getName(kMenuKey).." key.")
- while bRunning do
- local event, side, channel, channelReply, message, distance = os.pullEvent()
- if event == "modem_message" then
- if channel == nChannelHost and channelReply == nChannelClient and message.p == "HSH_TERM_FUNCTION" then
- term[message.n](unpack(message.a))
- elseif channel == nChannelHost and channelReply == nChannelClient and message.p == "HSH_STOP" then
- bRunning = false
- term.clear()
- term.setCursorPos(1,1)
- printError("Host stopped the server.")
- term.redirect(wOrig)
- error()
- end
- elseif event == "mouse_click" and bMenuVisible then
- -- Clicked 'Disconnect' button
- if side == 1 and channel >= 39 and channel <= 50 and channelReply == 17 then
- bRunning = false
- term.redirect(wOrig)
- wMain.redraw()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- printError("Disconnected from host.")
- error()
- -- Clicked the host channel
- elseif side == 1 and channel >= 40 and channel <= 50 and channelReply == 6 then
- term.setCursorPos(3,6)
- term.setBackgroundColor(cMenu_bg2)
- term.setTextColor(cMenu_fg2)
- print(" ")
- term.setCursorPos(3,6)
- local _cH = read()
- if tonumber(_cH) then
- oModem.close(nChannelHost) -- close old host channel
- nChannelHost = tonumber(_cH)
- oModem.open(nChannelHost) -- listen to new host channel
- end
- drawMenu()
- -- Clicked the client channel
- elseif side == 1 and channel >= 40 and channel <= 50 and channelReply == 8 then
- term.setCursorPos(3,8)
- term.setBackgroundColor(cMenu_bg2)
- term.setTextColor(cMenu_fg2)
- print(" ")
- term.setCursorPos(3,8)
- local _cC = read()
- if tonumber(_cC) then
- nChannelClient = tonumber(_cC)
- end
- drawMenu()
- end
- elseif event == "key" and side == kMenuKey then
- bMenuVisible = not bMenuVisible
- wMenu.setVisible(bMenuVisible)
- if bMenuVisible then
- term.redirect(wMenu)
- drawMenu()
- else
- term.redirect(wMain)
- wMain.redraw()
- end
- else
- if not bMenuVisible then
- oModem.transmit(nChannelClient, nChannelHost, {p = "HSH_EVENT", e = { event, side, channel, channelReply, message, distance }})
- end
- end
- end
- -- Main (HOST)
- elseif sMode == "host" then
- oModem.open(nChannelClient) -- listen to client/s
- term.clear()
- term.setCursorPos(1,1)
- printError("Stop hosting by pressing the "..keys.getName(kStopKey).." key.")
- local term_server = term.current()
- local term_client = {}
- for _name, _func in pairs(term_server) do
- term_client[_name] = function(...)
- oModem.transmit(nChannelHost, nChannelClient, {p = "HSH_TERM_FUNCTION", n = _name, a = {...}})
- return _func(...)
- end
- end
- term.redirect(term_client)
- -- Run function
- local function run()
- shell.run("/rom/programs/shell")
- end
- -- Listen function
- local function listen()
- while bRunning do
- local event, side, channel, channelReply, message, distance = os.pullEvent()
- if event == "modem_message" and channel == nChannelClient and channelReply == nChannelHost and message.p == "HSH_EVENT" then
- if not bViewOnly then
- os.queueEvent(unpack(message.e))
- end
- elseif event == "key" and side == kStopKey then
- bRunning = false
- end
- end
- end
- -- Host Code
- local cor1 = coroutine.create(run)
- local cor2 = coroutine.create(listen)
- local _e = {}
- while true do
- if bRunning then
- coroutine.resume(cor2,unpack(_e))
- end
- coroutine.resume(cor1,unpack(_e))
- _e = { os.pullEvent() }
- if coroutine.status(cor1) == "dead" then
- bRunning = false
- oModem.transmit(nChannelHost, nChannelClient, {p = "HSH_STOP"})
- printError("Server stopped running.")
- error()
- elseif coroutine.status(cor2) == "dead" then
- bRunning = false
- oModem.transmit(nChannelHost, nChannelClient, {p = "HSH_STOP"})
- printError("Server has been stopped.")
- error()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement