Files
Claude cfbb4be66e Line speeds: telnet, SSH and web callers may have the BBS send at a modem's speed
A caller without a modem's line picks a speed from 300 to 115200 bit/s
(or full) on the profile (B) or with Ctrl-B at any prompt, and it is
kept with their account. What the BBS sends goes at that pace, ten bits
a character, ANSI art drawn as it was; doors are told the speed and
paced too. Keys come in at once, and file transfers go at the line's
own speed so their protocols do not time out. Modem callers (and calls
a modem server reports a rate for) are never slowed again.

[general] line_speeds (may callers pick), line_speed (what they get
until they do) and line_speed_ask (asked at each login) are on bbs
config's Connections page; the sysop can set a user's speed on their
account page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRUrCtqbnHANimX37GK
2026-11-09 10:52:08 +00:00

169 lines
4.4 KiB
Go

package bbs
import (
"errors"
"fmt"
"sort"
"strconv"
"strings"
)
// Prefs are a user's settings, kept with the account as "key=value"
// pairs: how their terminal is to be treated, and their choices of how the
// BBS works for them.
type Prefs map[string]string
// prefValues are the settings there are, and the values each takes.
var prefValues = map[string][]string{
"ansi": {"on", "off"}, // ANSI color and cursor movement
"utf8": {"on", "off"}, // UTF-8 rather than CP437 (or ASCII)
"sixel": {"on", "off"}, // sixel graphics, where the terminal has them
"rip": {"on", "off"}, // RIPscrip menus, where the terminal has them
"pictures": {"on", "off"}, // pictures (the image boards'), where the terminal can show them
"width": {"40", "80"}, // a Commodore's columns
"editor": {"full", "line"}, // the full-screen editor, or the line editor
"expert": {"on", "off"}, // menus as their prompt alone (? for the rest)
"baud": nil, // the line speed (LineSpeeds, or off): lineSpeedValues
// CompuServe Mail's options (SET at "Mail!"), kept when the caller says
"cis-mailmode": {"menu", "prompt", "command"},
"cis-mailedit": {"line", "noline"},
"cis-mailpage": {"on", "off"},
}
func init() {
vs := []string{"off"}
for _, s := range LineSpeeds {
vs = append(vs, strconv.Itoa(s))
}
prefValues["baud"] = vs
}
// prefChecks are settings registered by a front end (RegisterPref), each
// with what its values may be.
var prefChecks = map[string]func(string) bool{}
// RegisterPref adds a setting whose values ok accepts: a front end's own
// (CompuServe's terminal settings). It is for package init: settings
// are read without a lock.
func RegisterPref(key string, ok func(string) bool) {
if _, dup := prefValues[key]; dup {
panic("bbs: setting " + key + " twice")
}
prefChecks[key] = ok
}
// PrefKeys are the settings' names, sorted.
func PrefKeys() []string {
var out []string
for k := range prefValues {
out = append(out, k)
}
for k := range prefChecks {
out = append(out, k)
}
sort.Strings(out)
return out
}
// CheckPref says what is wrong with a setting, or nil.
func CheckPref(key, value string) error {
if ok := prefChecks[key]; ok != nil {
if !ok(value) || strings.ContainsAny(value, " =") || value == "" {
return fmt.Errorf("%q is not a value of %s", value, key)
}
return nil
}
vs, ok := prefValues[key]
if !ok {
return fmt.Errorf("there is no setting %q", key)
}
for _, v := range vs {
if v == value {
return nil
}
}
return fmt.Errorf("%s is one of %s", key, strings.Join(vs, ", "))
}
// ParsePrefs reads a user's settings, leaving out any not known (or no
// longer).
func ParsePrefs(s string) Prefs {
p := Prefs{}
for _, f := range strings.Fields(s) {
k, v, ok := strings.Cut(f, "=")
if ok && CheckPref(k, v) == nil {
p[k] = v
}
}
return p
}
// String is the settings as they are kept.
func (p Prefs) String() string {
var out []string
for _, k := range PrefKeys() {
if v, ok := p[k]; ok {
out = append(out, k+"="+v)
}
}
return strings.Join(out, " ")
}
// On is whether a setting is on; ok false if the user has not set it.
func (p Prefs) On(key string) (on, ok bool) {
v, ok := p[key]
return v == "on", ok
}
// ChangePrefs is kept settings with changes made (a value "": forget it,
// back to what the terminal says), or what is wrong with a change.
func ChangePrefs(kept string, changes map[string]string) (string, error) {
p := ParsePrefs(kept)
for k, v := range changes {
if v == "" {
if _, ok := prefValues[k]; !ok && prefChecks[k] == nil {
return "", fmt.Errorf("there is no setting %q", k)
}
delete(p, k)
continue
}
if err := CheckPref(k, v); err != nil {
return "", err
}
p[k] = v
}
return p.String(), nil
}
// errNoUser: no one is logged in on the node.
var errNoUser = errors.New("no one is logged in")
// Prefs are the user's settings.
func (n *Node) Prefs() Prefs {
if u := n.User(); u != nil {
return ParsePrefs(u.Prefs)
}
return Prefs{}
}
// SetPref keeps one of the user's settings.
func (n *Node) SetPref(key, value string) error {
if err := CheckPref(key, value); err != nil {
return err
}
u := n.Refresh()
if u == nil {
return errNoUser
}
if ParsePrefs(u.Prefs)[key] == value {
return nil
}
prefs, _ := ChangePrefs(u.Prefs, map[string]string{key: value})
if err := n.b.Store.SetPrefs(u.ID, prefs); err != nil {
return err
}
n.Refresh()
return nil
}