Keep each user's terminal settings for their next call

What a caller switches on their profile (ANSI, UTF-8, sixel, RIP, a
Commodore's width) is kept with their account as settings (users.prefs,
"key=value" pairs) and applied after the next login: ANSI and UTF-8 as
set, sixel and RIP kept off when turned off and on only where the
terminal has them.  An editor setting (full or line) is there for the
full-screen editor.

The API shows them at GET /me and changes them with PATCH /me ("":
forgets one); unknown settings and values are refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
This commit is contained in:
Claude committed 2026-10-12 14:13:25 +00:00
1 parent ff2defc805
commit de134e3308
15 files changed
+323 -14

No files matched your search

+1 -1
View File
@@ -63,7 +63,7 @@ Errors are `{"error": "...", "code": "..."}`.
| `POST /login`, `POST /logout` | a session token; ending it | (none), any |
| `GET /system` | the name, sysop, counts, features | anonymous |
| `GET /who` | who is online (with each `terminal`, as it said what it is) | anonymous |
| `GET`, `PATCH /me` | your account and access; change your location or password | read |
| `GET`, `PATCH /me` | your account, access and settings; change your location, password or settings (`{"prefs": {"ansi": "off"}}`; `""` forgets one) | read |
| `GET /me/tokens`, `DELETE /me/tokens/{id}` | your tokens; revoke one | any |
| `GET /me/keys` | your SSH keys | read |
| `POST /me/keys`, `DELETE /me/keys/{id}` | add an SSH key (`{"key": "ssh-ed25519 ..."}`, from a login's session only); delete one | any |
+2 -1
View File
@@ -99,6 +99,7 @@ What the caller gets:
- everything else as on any ANSI terminal
A caller can turn RIP off and on in their profile (`[R]IP`; on only if the
terminal answers).
terminal answers). What they choose, and a Commodore's width, is kept for
their next call ([Terminal identification](Terminal-Identification#what-a-caller-sets)).
**See also:** [Customization](Customization), [Configuration](Configuration), [Modem callers](Modem-Callers)
+2 -1
View File
@@ -7,7 +7,8 @@ without one the name drawn in a pixel font. The BBS asks every ANSI
terminal for its device attributes (`CSI c`; sixel is attribute 4) and its
character cell in pixels (`CSI 16 t`, to fit the logo to the window), so
nothing changes for terminals without sixel. A caller turns it off (or on,
if their terminal has it) in their profile.
if their terminal has it) in their profile, and it stays off on their
later calls.
`sixel = auto` is the default for telnet listeners and `no` for raw and
rlogin ones: a picture is slow over a modem.
+18
View File
@@ -72,4 +72,22 @@ vt = no
`vt = no` asks nothing (sixel is then asked for by DA1 alone, as before)
and changes nothing; `auto` (the default) asks.
## What a caller sets
What a caller switches in their profile is kept with their account and
begins their next call:
| setting | values | at the next call |
|---|---|---|
| `ansi` | `on`, `off` | ANSI as they set it, whatever the terminal answered (`off` turns RIP off too) |
| `utf8` | `on`, `off` | UTF-8, or CP437 (ASCII where there is no ANSI) |
| `sixel` | `on`, `off` | `off` keeps sixel off; `on` only where the terminal says it has it |
| `rip` | `on`, `off` | the same for RIPscrip |
| `width` | `40`, `80` | a Commodore's columns |
| `editor` | `full`, `line` | the full-screen editor or the line editor |
A Commodore and a VT52 take only what applies to them. The settings are
`prefs` at `GET /api/v1/me`, changed with `PATCH /api/v1/me`
([HTTP API](HTTP-API)).
**See also:** [PETSCII and RIP](PETSCII-and-RIP), [Sixel graphics](Sixel-Graphics), [Configuration](Configuration)
+14
View File
@@ -634,6 +634,20 @@ func TestAccess(t *testing.T) {
if acc := me["access"].(map[string]any); acc["level"] != 20.0 || acc["validated"] != true {
t.Fatalf("access: %v", acc)
}
// settings: kept, checked, forgotten
if code, m, _ := s.do("PATCH", "/api/v1/me", alice, map[string]any{"prefs": map[string]string{"ansi": "off", "editor": "line"}}); code != 200 ||
m["prefs"].(map[string]any)["ansi"] != "off" {
t.Fatalf("prefs: %d %v", code, m)
}
for _, bad := range []map[string]string{{"ansi": "maybe"}, {"colour": "on"}} {
if code, _, _ := s.do("PATCH", "/api/v1/me", alice, map[string]any{"prefs": bad}); code != 400 {
t.Errorf("prefs %v: %d", bad, code)
}
}
s.do("PATCH", "/api/v1/me", alice, map[string]any{"prefs": map[string]string{"ansi": ""}})
if u, _ := s.b.Store.UserByName("alice"); u.Prefs != "editor=line" {
t.Errorf("kept %q", u.Prefs)
}
// a sysop sets them
code, m, _ := s.do("PATCH", "/api/v1/admin/users/alice", sysop, map[string]any{"level": 5, "flags": "+F"})
if code != 200 || m["level"] != 5.0 || m["flags"] != "F" {
+4 -3
View File
@@ -19,7 +19,7 @@ var ops = []op{
{"GET", "/api/v1/system", "The system: name, sysop, counts, features", "anonymous", "", "System", 200},
{"GET", "/api/v1/who", "Who is online", "anonymous", "", "Nodes", 200},
{"GET", "/api/v1/me", "Your account", "read", "", "Me", 200},
{"PATCH", "/api/v1/me", "Change your location or password", "read", "PatchMe", "Me", 200},
{"PATCH", "/api/v1/me", "Change your location, password or settings", "read", "PatchMe", "Me", 200},
{"GET", "/api/v1/me/tokens", "Your tokens (keys and sessions)", "any", "", "Tokens", 200},
{"DELETE", "/api/v1/me/tokens/{id}", "Revoke a token of yours", "any", "", "", 204},
{"GET", "/api/v1/me/keys", "Your SSH keys", "read", "", "SSHKeys", 200},
@@ -89,8 +89,9 @@ var schemas = map[string]any{
"Nodes": arr("Node"),
"User": obj(map[string]any{"name": str, "location": str, "sysop": boo, "created": date, "last_login": date, "calls": num, "posts": num, "node": num, "locked": boo, "level": num, "flags": str}),
"Users": arr("User"),
"Me": obj(map[string]any{"user": ref("User"), "unread_mail": num, "access": map[string]any{"type": "object"}, "scopes": map[string]any{"type": "array", "items": str}}),
"PatchMe": obj(map[string]any{"location": str, "old_password": str, "new_password": str}),
"Me": obj(map[string]any{"user": ref("User"), "unread_mail": num, "access": map[string]any{"type": "object"}, "prefs": ref("Prefs"), "scopes": map[string]any{"type": "array", "items": str}}),
"PatchMe": obj(map[string]any{"location": str, "old_password": str, "new_password": str, "prefs": ref("Prefs")}),
"Prefs": map[string]any{"type": "object", "description": "settings: ansi, utf8, sixel, rip (on, off), width (40, 80), editor (full, line); \"\" forgets one", "additionalProperties": str},
"SSHKey": obj(map[string]any{"id": num, "type": str, "fingerprint": str, "comment": str, "key": str, "created": date, "last_used": date}),
"SSHKeys": arr("SSHKey"),
"NewSSHKey": obj(map[string]any{"key": str}, "key"),
+18 -4
View File
@@ -313,19 +313,33 @@ func (s *Server) me(w http.ResponseWriter, r *http.Request) {
access["minutes_per_day"] = int(lvl.Time.Minutes())
}
writeJSON(w, http.StatusOK, map[string]any{"user": j, "unread_mail": unread, "access": access,
"scopes": strings.Fields(whoOf(r).token.Scopes)})
"prefs": bbs.ParsePrefs(u.Prefs), "scopes": strings.Fields(whoOf(r).token.Scopes)})
}
func (s *Server) patchMe(w http.ResponseWriter, r *http.Request) {
var req struct {
Location *string `json:"location"`
OldPassword string `json:"old_password"`
NewPassword string `json:"new_password"`
Location *string `json:"location"`
OldPassword string `json:"old_password"`
NewPassword string `json:"new_password"`
Prefs map[string]string `json:"prefs"`
}
if !s.decode(w, r, &req) {
return
}
u := whoOf(r).user
if req.Prefs != nil {
fresh, err := s.B.Store.UserByID(u.ID)
if err != nil {
fail(w, http.StatusNotFound, "no such user")
return
}
prefs, err := bbs.ChangePrefs(fresh.Prefs, req.Prefs)
if err != nil {
fail(w, http.StatusBadRequest, err.Error())
return
}
s.B.Store.SetPrefs(u.ID, prefs)
}
if req.Location != nil {
s.B.Store.SetLocation(u.ID, bbs.Clean(*req.Location, bbs.MaxLocationLen))
}
+128
View File
@@ -0,0 +1,128 @@
package bbs
import (
"errors"
"fmt"
"sort"
"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
"width": {"40", "80"}, // a Commodore's columns
"editor": {"full", "line"}, // the full-screen editor, or the line editor
}
// PrefKeys are the settings' names, sorted.
func PrefKeys() []string {
var out []string
for k := range prefValues {
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 {
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 {
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
}
+22
View File
@@ -102,3 +102,25 @@ func TestQuote(t *testing.T) {
t.Errorf("Quote =\n%q\nwant\n%q", got, want)
}
}
func TestPrefs(t *testing.T) {
p := ParsePrefs("utf8=off width=40 bogus=1 ansi=maybe rip=on")
if p.String() != "rip=on utf8=off width=40" {
t.Errorf("parsed %q", p)
}
if on, ok := p.On("rip"); !on || !ok {
t.Error("rip")
}
if _, ok := p.On("ansi"); ok {
t.Error("ansi set")
}
got, err := ChangePrefs("utf8=off width=40", map[string]string{"width": "", "editor": "full"})
if err != nil || got != "editor=full utf8=off" {
t.Errorf("changed %q %v", got, err)
}
for _, bad := range []map[string]string{{"width": "132"}, {"nope": "on"}, {"nope": ""}} {
if _, err := ChangePrefs("", bad); err == nil {
t.Errorf("%v taken", bad)
}
}
}
+35
View File
@@ -0,0 +1,35 @@
package server
import (
"testing"
"golang.org/x/crypto/ssh"
)
// TestPrefsKept: what a caller switches on their profile is how the next
// call begins.
func TestPrefsKept(t *testing.T) {
e, addr, _ := startSSH(t, nil)
call := func() *dialog {
c, err := sshDial(t, addr, "alice", ssh.Password("alicepass1"))
if err != nil {
t.Fatal(err)
}
d := shell(t, c, "xterm-256color", 100, 40)
d.expect("Command: ")
d.send("P")
return d
}
d := call()
d.expect("Terminal: 100x40, ANSI on, UTF-8 on")
d.send("8")
d.expect("Terminal: 100x40, ANSI on, UTF-8 off")
d.send("Q")
d.expect("Command: ")
if u, _ := e.b.Store.UserByName("alice"); u.Prefs != "utf8=off" {
t.Errorf("kept %q", u.Prefs)
}
d = call()
d.expect("Terminal: 100x40, ANSI on, UTF-8 off")
}
+3
View File
@@ -83,6 +83,9 @@ func TestPETSCII(t *testing.T) {
d.expect(pet("[W]idth 40/80"))
d.send(typed("w"))
d.expect(pet("Terminal: Commodore, 80x25"))
if u, _ := e.b.Store.UserByName("alice"); u.Prefs != "width=80" {
t.Errorf("kept %q", u.Prefs)
}
d.send(typed("q"))
d.expect(pet("Command: "))
d.send(typed("w"))
+3 -1
View File
@@ -26,7 +26,8 @@ CREATE TABLE IF NOT EXISTS users (
time_day {int} NOT NULL DEFAULT 0,
time_used {int} NOT NULL DEFAULT 0,
dl_day {int} NOT NULL DEFAULT 0,
dl_count {int} NOT NULL DEFAULT 0
dl_count {int} NOT NULL DEFAULT 0,
prefs {str} NOT NULL DEFAULT ''
)`, `
CREATE TABLE IF NOT EXISTS areas (
id {id},
@@ -183,6 +184,7 @@ var added = []struct{ table, column, decl string }{
{"areas", "post_req", "{str} NOT NULL DEFAULT ''"},
{"file_areas", "download_req", "{str} NOT NULL DEFAULT ''"},
{"file_areas", "upload_req", "{str} NOT NULL DEFAULT ''"},
{"users", "prefs", "{str} NOT NULL DEFAULT ''"},
}
// migrate makes the schema, or brings an older database up to it.
+9 -2
View File
@@ -45,6 +45,8 @@ type User struct {
// today's (TimeDay, YYYYMMDD) time online in seconds, and downloads
TimeDay, TimeUsed int
DLDay, DLCount int
// the user's settings ("ansi=off width=80"): bbs.Prefs
Prefs string
}
// Area is a message area.
@@ -185,14 +187,14 @@ func HashPassword(pw string) (string, error) {
}
const userCols = `id, name, password, location, sysop, locked, created, last_login, logins, posts,
level, flags, time_day, time_used, dl_day, dl_count`
level, flags, time_day, time_used, dl_day, dl_count, prefs`
func scanUser(row interface{ Scan(...any) error }) (*User, string, error) {
var u User
var pw string
var created, last int64
err := row.Scan(&u.ID, &u.Name, &pw, &u.Location, &u.Sysop, &u.Locked,
&created, &last, &u.Logins, &u.Posts, &u.Level, &u.Flags, &u.TimeDay, &u.TimeUsed, &u.DLDay, &u.DLCount)
&created, &last, &u.Logins, &u.Posts, &u.Level, &u.Flags, &u.TimeDay, &u.TimeUsed, &u.DLDay, &u.DLCount, &u.Prefs)
if errors.Is(err, sql.ErrNoRows) {
return nil, "", ErrNotFound
}
@@ -270,6 +272,11 @@ func (s *Store) SetLocation(id int64, loc string) error {
return s.exec1(`UPDATE users SET location = ? WHERE id = ?`, loc, id)
}
// SetPrefs sets a user's settings.
func (s *Store) SetPrefs(id int64, prefs string) error {
return s.exec1(`UPDATE users SET prefs = ? WHERE id = ?`, prefs, id)
}
// SetSysop gives or takes a user's sysop rights.
func (s *Store) SetSysop(id int64, sysop bool) error {
return s.exec1(`UPDATE users SET sysop = ? WHERE id = ?`, sysop, id)
+63 -1
View File
@@ -2,6 +2,7 @@ package textui
import (
"fmt"
"strconv"
"time"
"github.com/Neo-Desktop/bbs-sec/internal/app"
@@ -55,8 +56,69 @@ func (s *session) TerminalActions() []view.Action {
{ID: "rip", Label: "RIP on/off", Key: 'R'}}
}
// TerminalAction switches what was chosen.
// applyPrefs treats the terminal as the user said to last time: ANSI and
// UTF-8 as they set them, sixel and RIP off if they turned them off (on
// only where the terminal says it has them), a Commodore's width.
func (s *session) applyPrefs() {
t := s.t
p := s.n.Prefs()
switch {
case t.PETSCII:
if w, ok := p["width"]; ok && w != strconv.Itoa(t.Cols) {
t.SetPETSCII(true, map[string]int{"40": 40, "80": 80}[w])
}
return
case t.VT52:
return
}
if on, ok := p.On("ansi"); ok {
t.ANSI = on
if !on {
t.RIP = false
}
}
if on, ok := p.On("utf8"); ok && on != t.UTF8 {
t.UTF8 = on
if on && t.DEC {
t.DEC = false
t.setTranslator(nil)
}
}
if on, ok := p.On("sixel"); ok && !on {
t.Sixel = false
}
if on, ok := p.On("rip"); ok && !on {
t.RIP = false
}
}
// savePrefs keeps how the terminal is now, for the next call.
func (s *session) savePrefs(id string) error {
t := s.t
onOff := map[bool]string{true: "on", false: "off"}
switch id {
case "width":
return s.n.SetPref("width", strconv.Itoa(t.Cols))
case "ansi":
if err := s.n.SetPref("ansi", onOff[t.ANSI]); err != nil || t.ANSI {
return err
}
return s.n.SetPref("rip", "off")
case "utf8", "sixel", "rip":
return s.n.SetPref(id, onOff[map[string]bool{"utf8": t.UTF8, "sixel": t.Sixel, "rip": t.RIP}[id]])
}
return nil
}
// TerminalAction switches what was chosen, and keeps it for next time.
func (s *session) TerminalAction(id string) error {
if err := s.switchTerminal(id); err != nil {
return err
}
return s.savePrefs(id)
}
func (s *session) switchTerminal(id string) error {
t := s.t
if t.PETSCII != (id == "width") || t.VT52 {
return nil
+1
View File
@@ -59,6 +59,7 @@ func (s *session) run() error {
if err := s.login(); err != nil {
return err
}
s.applyPrefs()
t.Notify = s.notify
defer func() { t.Notify = nil }()
err := app.Run(s.b, s.n, s)