mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-29 09:48:10 -08:00
A script (scripts_dir/NAME.js, run by a menu's script:NAME, and logon.js after each login) draws the BBS's shared screens through ui (menus, tables, forms, panels, messages, the editor, confirmations), so one script works on text, RIP and 3270 terminals. It acts as its caller, with their access: msg (areas, messages, posting, mail), node (who's online, node messages, its activity), user, bbs; and keeps JSON values in data, for everyone or (data.user) for the caller, in the database (script_data). goja runs it in strict mode, with no files, network or processes. A watchdog stops a script that computes more than two seconds between screens (the caller's time at a screen is not counted); the caller hanging up ends a script whatever it catches; failures are logged and told (to a sysop, with why). Scripts are compiled once, again when changed; bbs check refuses scripts that do not compile and menus naming missing ones. goja is pinned to its June 2026 commit (the last that builds with Go 1.24; it is changing quickly now). scripts.sample has one-liners and a poll; both are tested, the one-liners on a text terminal and on a 3270 (s3270). That test found 3270 forms broken since the shared screens: the input field began under its label's last character, whose attribute byte erased it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
60 lines
2.0 KiB
Go
60 lines
2.0 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
)
|
|
|
|
// Script data: what scripts keep, by script, and for a user (0: for all).
|
|
|
|
// ScriptGet is a script's value of name (ok false if it has none).
|
|
func (s *Store) ScriptGet(script string, user int64, name string) (string, bool, error) {
|
|
var v string
|
|
err := s.db.QueryRow(`SELECT value FROM script_data WHERE script = ? AND user_id = ? AND name = ?`,
|
|
script, user, name).Scan(&v)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return "", false, nil
|
|
}
|
|
return v, err == nil, err
|
|
}
|
|
|
|
// ScriptSet keeps a script's value of name.
|
|
func (s *Store) ScriptSet(script string, user int64, name, value string) error {
|
|
_, err := s.db.Exec(`INSERT INTO script_data (script, user_id, name, value) VALUES (?, ?, ?, ?)
|
|
ON CONFLICT (script, user_id, name) DO UPDATE SET value = excluded.value`, script, user, name, value)
|
|
return err
|
|
}
|
|
|
|
// ScriptDelete forgets a script's value of name.
|
|
func (s *Store) ScriptDelete(script string, user int64, name string) error {
|
|
_, err := s.db.Exec(`DELETE FROM script_data WHERE script = ? AND user_id = ? AND name = ?`, script, user, name)
|
|
return err
|
|
}
|
|
|
|
// ScriptNames are a script's names beginning with prefix, in order, at
|
|
// most limit.
|
|
func (s *Store) ScriptNames(script string, user int64, prefix string, limit int) ([]string, error) {
|
|
rows, err := s.db.Query(`SELECT name FROM script_data WHERE script = ? AND user_id = ? AND name LIKE ? ESCAPE '!'
|
|
ORDER BY name LIMIT ?`, script, user, likePrefix(prefix), limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []string
|
|
for rows.Next() {
|
|
var n string
|
|
if err := rows.Scan(&n); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, n)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// ScriptCount is how many values a script keeps for a user (or all).
|
|
func (s *Store) ScriptCount(script string, user int64) (int, error) {
|
|
var n int
|
|
err := s.db.QueryRow(`SELECT COUNT(*) FROM script_data WHERE script = ? AND user_id = ?`, script, user).Scan(&n)
|
|
return n, err
|
|
}
|