Skip to content

Instantly share code, notes, and snippets.

@denzuko
Created July 27, 2020 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denzuko/0f5506eddcf2f7b56e546bf276a257a6 to your computer and use it in GitHub Desktop.
Save denzuko/0f5506eddcf2f7b56e546bf276a257a6 to your computer and use it in GitHub Desktop.
telnetbbsguide.com client in opencomputers lua (defaults to xmcore bbs, usage: bbs <host:port | "" for bbs.dapla.net:23>
local component = require("component")
local internet = require("internet")
local shell = require("shell")
local term = require("term")
local text = require("text")
local event = require("event")
local gpu = component.gpu
local w, h = gpu.getResolution()
local args = shell.parse(...)
local host = args[1]
local hist = {}
if not host then
host = "bbs.dapla.net:23"
io.stderr.write("Defaulting to xmcore bbs: " .. host .. "\n")
end
local sock, reason = internet.open(host)
if not sock then
io.stderr:write(reason .. "\n")
return
end
sock:setTimeout(0.05)
--Function from the built in IRC program
local function print(message, overwrite)
local w, h = component.gpu.getResolution()
local line
repeat
line, message = text.wrap(text.trim(message), w, w)
if not overwrite then
component.gpu.copy(1, 1, w, h - 1, 0, -1)
end
overwrite = false
component.gpu.fill(1, h - 1, w, 1, " ")
component.gpu.set(1, h - 1, line)
until not message or message == ""
end
local function draw()
if not sock then
return false
end
repeat
local ok, line = pcall(sock.read, sock)
if ok then
if not line then
print("Connection lost.")
sock:close()
sock = nil
return false
end
print(line)
end
until not ok
end
local function uin()
term.setCursor(1,h)
line = term.read(hist)
line2 = text.trim(line)
if line2 == "/exit" then
return false
else
sock:write(line2.."\r\n")
end
return true
end
local going = true
local dLoop = event.timer(0.5, draw, math.huge)
repeat
r = uin()
until not r
if dLoop then
event.cancel(dLoop)
end
if sock then
sock:write("QUIT")
sock:close()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment