1
0
mirror of https://github.com/kepler155c/opus synced 2025-02-06 20:20:03 +00:00
opus/sys/apps/telnet.lua

87 lines
1.5 KiB
Lua
Raw Normal View History

2018-01-24 17:39:38 -05:00
_G.requireInjector(_ENV)
local Event = require('event')
local Socket = require('socket')
2016-12-11 14:24:52 -05:00
local Terminal = require('terminal')
local Util = require('util')
2016-12-11 14:24:52 -05:00
local multishell = _ENV.multishell
local os = _G.os
local read = _G.read
local term = _G.term
2018-10-15 16:05:43 -04:00
local args = { ... }
2018-01-13 15:17:26 -05:00
local remoteId = tonumber(table.remove(args, 1) or '')
if not remoteId then
2018-01-24 17:39:38 -05:00
print('Enter host ID')
remoteId = tonumber(read())
2016-12-11 14:24:52 -05:00
end
if not remoteId then
2018-10-15 16:05:43 -04:00
error('Syntax: telnet ID [PROGRAM] [ARGS]')
end
2018-10-15 16:05:43 -04:00
if multishell then
multishell.setTitle(multishell.getCurrent(), 'Telnet ' .. remoteId)
2016-12-11 14:24:52 -05:00
end
2017-10-09 13:08:38 -04:00
local socket, msg = Socket.connect(remoteId, 23)
2016-12-11 14:24:52 -05:00
if not socket then
2018-01-24 17:39:38 -05:00
error(msg)
2016-12-11 14:24:52 -05:00
end
local ct = Util.shallowCopy(term.current())
if not ct.isColor() then
2018-01-24 17:39:38 -05:00
Terminal.toGrayscale(ct)
2016-12-11 14:24:52 -05:00
end
local w, h = ct.getSize()
socket:write({
2018-01-24 17:39:38 -05:00
width = w,
height = h,
isColor = ct.isColor(),
program = args,
pos = { ct.getCursorPos() },
2016-12-11 14:24:52 -05:00
})
2017-07-23 22:37:07 -04:00
Event.addRoutine(function()
2018-01-24 17:39:38 -05:00
while true do
local data = socket:read()
if not data then
break
end
for _,v in ipairs(data) do
ct[v.f](table.unpack(v.args))
end
end
2016-12-11 14:24:52 -05:00
end)
2018-01-13 23:40:53 -05:00
--ct.clear()
--ct.setCursorPos(1, 1)
2016-12-11 14:24:52 -05:00
2018-01-13 15:17:26 -05:00
local filter = Util.transpose {
2018-01-24 17:39:38 -05:00
'char', 'paste', 'key', 'key_up', 'terminate',
'mouse_scroll', 'mouse_click', 'mouse_drag', 'mouse_up',
2018-01-13 15:17:26 -05:00
}
2017-05-10 06:11:25 -04:00
2016-12-11 14:24:52 -05:00
while true do
2018-01-24 17:39:38 -05:00
local e = { os.pullEventRaw() }
local event = e[1]
2016-12-11 14:24:52 -05:00
2018-01-24 17:39:38 -05:00
if filter[event] then
socket:write(e)
else
Event.processEvent(e)
end
2017-08-09 10:19:00 -04:00
2018-01-24 17:39:38 -05:00
if not socket.connected then
2018-01-13 23:40:53 -05:00
-- print()
-- print('Connection lost')
-- print('Press enter to exit')
-- pcall(read)
2018-01-24 17:39:38 -05:00
break
end
2016-12-11 14:24:52 -05:00
end