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