Files
bbs/internal/server/customconfig.go
T
Claude b41f59ec55 Add access levels: levels, flags, requirements, limits, validation
Users have a level (0-255) and flags (A-Z).  [level N] sections say what
a range of levels may do: time online a day (warned at 5 and 1 minutes,
hung up at the end; doors' limits cut to what is left), downloads a day
(counted by every way: the BBS, 3270, FTP, the API), and whether they
may post, mail (else only the sysop), chat, page, download, upload and
open doors.

Requirements ("30 F !X", "sysop") are put on message areas (reading and
posting), file areas (downloading and uploading), doors, menus and menu
items ("x = menu:extras Extras | 50 X").  They are checked in the core,
so the text BBS, 3270s, FTP, NNTP and the API keep them alike: an area a
user may not read is not listed, opened or found by number or
Message-ID.

New users start at new_user_level and are told they wait; the sysop
validates them on the BBS (V on the main menu), by bbs user
waiting/validate/level/flags, or by PATCH /api/v1/admin/users.  Users
see their access in the profile and /api/v1/me; no one changes their
own.  Drop files carry the caller's level.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
2026-10-12 08:49:46 +00:00

115 lines
3.1 KiB
Go

package server
import (
"errors"
"fmt"
"os"
"sort"
"strings"
"github.com/Neo-Desktop/bbs-sec/internal/bbs"
"github.com/Neo-Desktop/bbs-sec/internal/config"
"github.com/Neo-Desktop/bbs-sec/internal/screens"
)
// customSections reads screens_dir, the [menu NAME]s and [strings].
func (c *Config) customSections(cf *config.File) error {
if dir := cf.Get("general", "").String("screens_dir", ""); dir != "" {
if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {
c.Warnings = append(c.Warnings, fmt.Sprintf("[general] screens_dir %s is not a directory: no screens", dir))
}
c.Screens = &screens.Set{Dir: dir}
}
c.Menus = map[string]*bbs.Menu{}
for _, s := range cf.All("menu") {
m, err := menu(s)
if err != nil {
return fmt.Errorf("[menu %s] %w", s.Name, err)
}
if c.Menus[m.Name] != nil {
return fmt.Errorf("[menu %s] given twice", m.Name)
}
c.Menus[m.Name] = m
}
// what menus name must be there
doors := map[string]bool{}
for _, d := range c.Doors {
doors[strings.ToLower(d.Name)] = true
}
for _, m := range c.Menus {
for _, it := range m.Items {
switch it.Action {
case "menu":
if c.Menus[strings.ToLower(it.Arg)] == nil {
return fmt.Errorf("[menu %s] %s: there is no [menu %s]", m.Name, it.Key, it.Arg)
}
case "door":
if !doors[strings.ToLower(it.Arg)] {
return fmt.Errorf("[menu %s] %s: there is no [door %s]", m.Name, it.Key, it.Arg)
}
case "screen":
if c.Screens == nil {
return fmt.Errorf("[menu %s] %s: screen:%s, but no screens_dir", m.Name, it.Key, it.Arg)
}
}
}
if m.Screen != "" && c.Screens == nil {
return fmt.Errorf("[menu %s] screen = %s, but no screens_dir", m.Name, m.Screen)
}
}
c.Text = map[string]string{}
if s := cf.Get("strings", ""); s != nil {
for _, k := range s.Keys() {
if _, ok := bbs.Strings[k]; !ok {
return fmt.Errorf("[strings] %s: no such string (%s)", k, strings.Join(stringKeys(), ", "))
}
c.Text[k] = s.String(k, "")
}
}
return nil
}
func stringKeys() []string {
var out []string
for k := range bbs.Strings {
out = append(out, k)
}
sort.Strings(out)
return out
}
func menu(s *config.Section) (*bbs.Menu, error) {
name := strings.ToLower(s.Name)
if name == "" {
return nil, errors.New("a menu needs a name: [menu main]")
}
m := &bbs.Menu{Name: name, Title: s.String("title", s.Name), Screen: s.String("screen", ""),
Prompt: s.String("prompt", ""), Require: s.String("require", "")}
if _, err := bbs.ParseRequire(m.Require); err != nil {
return nil, fmt.Errorf("require: %v", err)
}
if m.Screen != "" && !screens.ValidName(m.Screen) {
return nil, errors.New("screen: a screen's name is lower case letters, digits, _ and -")
}
seen := map[string]bool{}
for _, k := range s.Keys() {
switch k {
case "title", "screen", "prompt", "require":
continue
}
it, err := bbs.ParseMenuItem(k, s.String(k, ""))
if err != nil {
return nil, err
}
if seen[it.Key] {
return nil, fmt.Errorf("%s given twice", it.Key)
}
seen[it.Key] = true
m.Items = append(m.Items, it)
}
if len(m.Items) == 0 {
return nil, errors.New("no items: KEY = ACTION Label, e.g. m = areas Message areas")
}
return m, nil
}