upgrade for our main bbs server
This commit is contained in:
@@ -2,6 +2,6 @@
|
||||
|
||||
module BBS
|
||||
class Config
|
||||
attr_accessor :screens_dir, :flow
|
||||
attr_accessor :screens_dir, :flow, :on_session_end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -71,6 +71,10 @@ module BBS
|
||||
def exit_menu
|
||||
@steps << { type: :exit_menu }
|
||||
end
|
||||
|
||||
def call(&block)
|
||||
@steps << { type: :call, block: block }
|
||||
end
|
||||
end
|
||||
|
||||
class MenuBuilder
|
||||
|
||||
@@ -11,6 +11,8 @@ module BBS
|
||||
confirm: "\e[1;36m",
|
||||
}.freeze
|
||||
|
||||
attr_reader :session_id
|
||||
|
||||
def initialize(session, session_id, flow)
|
||||
@session = session
|
||||
@session_id = session_id
|
||||
@@ -22,6 +24,9 @@ module BBS
|
||||
execute(@flow.steps)
|
||||
end
|
||||
|
||||
def write(data) = @session.write(data)
|
||||
def readline = @session.readline
|
||||
|
||||
private
|
||||
|
||||
def execute(steps)
|
||||
@@ -47,6 +52,7 @@ module BBS
|
||||
when :confirm_block then run_confirm_block(step)
|
||||
when :menu then run_menu(step)
|
||||
when :exit_menu then :exit_menu
|
||||
when :call then run_call(step)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -159,12 +165,20 @@ module BBS
|
||||
nil
|
||||
end
|
||||
|
||||
def run_call(step)
|
||||
result = step[:block].call(@ctx, self)
|
||||
result == :halt ? :halt : nil
|
||||
rescue IOError, Errno::EPIPE, Errno::ECONNRESET
|
||||
:halt
|
||||
end
|
||||
|
||||
# ── helpers ────────────────────────────────────────────────────────────────
|
||||
|
||||
def apply_transform(value, transform)
|
||||
case transform
|
||||
when :upcase then value.upcase
|
||||
when :downcase then value.downcase
|
||||
when Proc then transform.call(value)
|
||||
else value
|
||||
end
|
||||
end
|
||||
@@ -183,8 +197,5 @@ module BBS
|
||||
else "Invalid input. Farewell."
|
||||
end
|
||||
end
|
||||
|
||||
def write(data) = @session.write(data)
|
||||
def readline = @session.readline
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,7 +13,16 @@ module BBS
|
||||
puts "BBS listening on port #{@port} — connect with: telnet localhost #{@port}"
|
||||
loop do
|
||||
client = server.accept
|
||||
Thread.new(client) { |c| Session.new(c).run rescue nil }
|
||||
Thread.new(client) do |c|
|
||||
session = Session.new(c)
|
||||
begin
|
||||
session.run
|
||||
rescue => e
|
||||
nil
|
||||
ensure
|
||||
BBS.config.on_session_end&.call(session)
|
||||
end
|
||||
end
|
||||
end
|
||||
rescue Interrupt
|
||||
puts "\nServer stopped."
|
||||
|
||||
@@ -6,6 +6,8 @@ module BBS
|
||||
class Session
|
||||
include Telnet
|
||||
|
||||
attr_reader :session_id
|
||||
|
||||
def initialize(client)
|
||||
@client = client
|
||||
@session_id = SecureRandom.hex(8)
|
||||
|
||||
Reference in New Issue
Block a user