From d9232eabb4ddba2b860921657cf20fe7f3cbd830 Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Tue, 28 Apr 2026 22:47:52 +0200 Subject: [PATCH] upgrade for our main bbs server --- lib/bbs/config.rb | 2 +- lib/bbs/flow.rb | 4 ++++ lib/bbs/flow_runner.rb | 17 ++++++++++++++--- lib/bbs/server.rb | 11 ++++++++++- lib/bbs/session.rb | 2 ++ 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/bbs/config.rb b/lib/bbs/config.rb index c8efa15..29a1b26 100644 --- a/lib/bbs/config.rb +++ b/lib/bbs/config.rb @@ -2,6 +2,6 @@ module BBS class Config - attr_accessor :screens_dir, :flow + attr_accessor :screens_dir, :flow, :on_session_end end end diff --git a/lib/bbs/flow.rb b/lib/bbs/flow.rb index 4f1bd54..40dd691 100644 --- a/lib/bbs/flow.rb +++ b/lib/bbs/flow.rb @@ -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 diff --git a/lib/bbs/flow_runner.rb b/lib/bbs/flow_runner.rb index 1609e69..5936a04 100644 --- a/lib/bbs/flow_runner.rb +++ b/lib/bbs/flow_runner.rb @@ -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 diff --git a/lib/bbs/server.rb b/lib/bbs/server.rb index 6296c82..9bd7025 100644 --- a/lib/bbs/server.rb +++ b/lib/bbs/server.rb @@ -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." diff --git a/lib/bbs/session.rb b/lib/bbs/session.rb index 4af9a0b..e19d374 100644 --- a/lib/bbs/session.rb +++ b/lib/bbs/session.rb @@ -6,6 +6,8 @@ module BBS class Session include Telnet + attr_reader :session_id + def initialize(client) @client = client @session_id = SecureRandom.hex(8)