mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
menus: menu files, a command set and a menu stack
The first phase of customizable menus, as in Mystic BBS and Synchronet. - menus_dir: NAME.menu files, with a [menu] section and an [item KEY] section for each item. An item does one or more commands (with ";" between them), can be hidden, and can have a requirement. A key is a character, a word (the caller types a line) or a special key: @first, @every, @enter. A menu can have a fail menu for callers without access. - A catalog of commands (bbs.Commands) with data and checks, and the menu commands menu.gosub, menu.goto, menu.return and menu.main on a stack of menus. The [menu] lines of bbs.conf keep their syntax. - The built-in main menu is now a menu file of the Classic theme (internal/app/menus/main.menu), run by the same engine. An item whose command is not available to the caller is not shown, and @NOTE@ gives the counts of new mail, pages and users that wait. - Text and 3270 front ends: hidden keys and typed words. - docs/wiki/Customization.md: the menus section, in STE. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRQvYyBEEXAiGy2xSd1 Signed-off-by: Claude <noreply@anthropic.com>
This commit is contained in:
18 files changed
+1482
-393
No files matched your search
+12
-2
@@ -47,6 +47,9 @@ log = syslog
|
||||
; the sysop's JavaScript: NAME.js runs from a menu's script:NAME, logon.js
|
||||
; after each login; scripts.sample has some to start from
|
||||
;scripts_dir = /etc/bbs/scripts
|
||||
; the sysop's menu files: NAME.menu is the menu NAME (main.menu replaces
|
||||
; the built-in main menu). docs/wiki/Customization.md gives the format
|
||||
;menus_dir = /etc/bbs/menus
|
||||
; a caller pages the sysop and no sysop is on: tell them this way ({user},
|
||||
; {node} and {reason} filled in; no shell). E.g. a push by ntfy.sh:
|
||||
;page_command = curl -s -d "{user} on node {node}: {reason}" https://ntfy.sh/my-bbs-pages
|
||||
@@ -1292,8 +1295,15 @@ address = 127.0.0.1:2513
|
||||
; Menus of the sysop's. [menu main] takes the place of the built-in main
|
||||
; menu (on 3270s too); others are reached from it. Each item is
|
||||
; KEY = ACTION Label: one character (not ; or #), then what it does:
|
||||
; areas mail who users profile info files doors chat page terminal
|
||||
; logoff back, or door:NAME menu:NAME screen:NAME area:NAME
|
||||
; areas mail who users profile info files boards doors chat page
|
||||
; callers feedback qwk news headlines weather quotes validate sysop
|
||||
; terminal logoff back menu.main, or door:NAME menu:NAME menu.goto:NAME
|
||||
; screen:NAME area:NAME script:NAME; "| REQUIREMENT" after the label
|
||||
; shows the item only to those who meet it. @NOTE@ in a label: the
|
||||
; new mail of a mail item, the callers that page a sysop on chat.
|
||||
; A menus_dir file NAME.menu does the same, with more: items that do
|
||||
; more than one command, words as keys, @first, @every and @enter, hidden
|
||||
; items (docs/wiki/Customization.md)
|
||||
; Labels, titles and prompts take @-codes (@USER@, @NEWMAIL@, @BBS@ ...)
|
||||
; and tags ({yellow}, {bright}, {reset} ...: docs/wiki/Customization.md).
|
||||
; screen = NAME shows that display file instead of the list of items
|
||||
|
||||
@@ -85,6 +85,7 @@ The system: its name, database, log, logins, and what callers see.
|
||||
| `motd_file` | | yes | the message of the day, after the login: motd, then the file (read at each login) Examples: `/etc/bbs/motd.txt`. |
|
||||
| `screens_dir` | | | the sysop's screens (display files): logon, newuser, welcome, goodbye and those menus name; NAME.ans (ANSI, CP437), NAME.asc (text), NAME.3270. screens.sample has some to start from. Examples: `/etc/bbs/screens`. |
|
||||
| `scripts_dir` | | | the sysop's JavaScript: NAME.js runs from a menu's script:NAME, logon.js after each login; scripts.sample has some to start from. Examples: `/etc/bbs/scripts`. |
|
||||
| `menus_dir` | | | the sysop's menu files: NAME.menu is the menu NAME (main.menu replaces the built-in main menu). docs/wiki/Customization.md gives the format. Examples: `/etc/bbs/menus`. |
|
||||
| `page_command` | | yes | a caller pages the sysop and no sysop is on: tell them this way ({user}, {node} and {reason} filled in; no shell). E.g. a push by ntfy.sh: Examples: `curl -s -d "{user} on node {node}: {reason}" https://ntfy.sh/my-bbs-pages`. |
|
||||
| `goodbye` | | yes | shown when a caller logs off. Examples: `Thanks for calling.`. |
|
||||
| `qwk_id` | | yes | QWK offline mail: the packets' name (BBSID.QWK), up to 8 letters and digits; by default made of the name. See docs/wiki/Offline-Mail.md. Examples: `DMODEM`. |
|
||||
|
||||
+174
-13
@@ -144,9 +144,25 @@ there.
|
||||
|
||||
## Menus
|
||||
|
||||
Each `[menu NAME]` section makes a menu. `[menu main]` replaces the
|
||||
built-in main menu on text terminals and on 3270s. Users get to the
|
||||
other menus from the main menu and from each other.
|
||||
The sysop can replace the main menu and add other menus. Callers go
|
||||
from the main menu to the other menus, and from these menus to more
|
||||
menus. If the sysop does not give a main menu, the BBS uses its built-in
|
||||
main menu. The built-in main menu is a menu file of the Classic theme
|
||||
([`internal/app/menus/main.menu`](https://github.com/D-Modem/bbs/tree/HEAD/internal/app/menus/main.menu)).
|
||||
You can copy it to `menus_dir` and change it.
|
||||
|
||||
There are two ways to write a menu:
|
||||
|
||||
- A `[menu NAME]` section in `bbs.conf`, with one line for each item.
|
||||
This way is short. Each item does one command.
|
||||
- A menu file `NAME.menu` in `menus_dir`. This way gives all the
|
||||
functions: an item can do more than one command, a key can be a word,
|
||||
and there are special keys and hidden items.
|
||||
|
||||
A menu name is lower-case letters, digits, `_` and `-`. If two menus
|
||||
have the same name, `bbs check` refuses the configuration.
|
||||
|
||||
### Menus in bbs.conf
|
||||
|
||||
```
|
||||
[menu main]
|
||||
@@ -154,12 +170,13 @@ title = {cyan}{bright}@BBS@{reset}
|
||||
screen = mainmenu ; shown instead of the list of items (optional)
|
||||
prompt = "Command: " ; (optional)
|
||||
m = areas Message areas
|
||||
e = mail E-mail ({yellow}@NEWMAIL@{default} new)
|
||||
e = mail E-mail@NOTE@
|
||||
d = doors Doors
|
||||
l = door:lord Legend of the Red Dragon
|
||||
b = screen:bulletins Bulletins
|
||||
g = area:general Straight to General
|
||||
x = menu:extras Extras
|
||||
s = sysop Sysop tools | sysop
|
||||
q = logoff Goodbye
|
||||
|
||||
[menu extras]
|
||||
@@ -168,20 +185,162 @@ u = users User list
|
||||
z = back Back
|
||||
```
|
||||
|
||||
Each item is `KEY = ACTION Label`. The key is one character (not `;` or
|
||||
`#`, which start comments). The action is what the item does:
|
||||
Each item is `KEY = COMMAND Label`. The key is one character (not `;` or
|
||||
`#`, which start comments). The command is one of the
|
||||
[commands](#commands). Write its data after a colon: `door:lord`. After
|
||||
`|`, an item can have an [access requirement](Access-Levels). Only
|
||||
callers who meet the requirement see the item.
|
||||
|
||||
| Action | What it does |
|
||||
### Menu files
|
||||
|
||||
Set the directory of the menu files:
|
||||
|
||||
```
|
||||
[general]
|
||||
menus_dir = /etc/bbs/menus
|
||||
```
|
||||
|
||||
The file `NAME.menu` is the menu `NAME`. Thus, `main.menu` replaces the
|
||||
built-in main menu. A menu file has one `[menu]` section and one
|
||||
`[item KEY]` section for each item:
|
||||
|
||||
```
|
||||
; /etc/bbs/menus/messages.menu
|
||||
[menu]
|
||||
title = {cyan}Messages{reset}
|
||||
prompt = "{yellow}@USER@{default}> "
|
||||
screen = messages ; shown instead of the list of items (optional)
|
||||
require = 20 ; who can go into the menu (optional)
|
||||
fail = main ; where other callers go (optional)
|
||||
|
||||
[item @first] ; done one time, when the menu opens
|
||||
do = screen:msgnews
|
||||
|
||||
[item r]
|
||||
label = Read the areas
|
||||
do = areas
|
||||
|
||||
[item n]
|
||||
label = Areas, then mail
|
||||
do = areas ; mail
|
||||
|
||||
[item JOIN] ; a word: the caller types it, then Enter
|
||||
label = Join an area
|
||||
do = area:general
|
||||
|
||||
[item q]
|
||||
label = Back
|
||||
do = menu.return
|
||||
|
||||
[item x] ; the key operates, but it is not in the list
|
||||
hidden = yes
|
||||
do = menu.main
|
||||
```
|
||||
|
||||
The `[menu]` section can have these keys. All of them are optional.
|
||||
|
||||
| Key | What it is |
|
||||
|---|---|
|
||||
| `areas` `mail` `who` `users` `profile` `info` `files` `boards` `doors` `chat` `page` `callers` `feedback` `qwk` `sysop` | the built-in parts (`callers`: the last callers. `feedback`: mail to the sysop. `qwk`: [offline mail](Offline-Mail). `boards`: the [image boards](Image-Boards). `sysop`: the [sysop tools](Sysop-Tools), for sysops) |
|
||||
| `title` | the title of the menu, with @-codes and tags. The default is the name of the menu |
|
||||
| `prompt` | the prompt. The default is the `command_prompt` string |
|
||||
| `screen` | a screen that the BBS shows instead of the list of items |
|
||||
| `require` | the [access requirement](Access-Levels) to go into the menu. The main menu does not use it |
|
||||
| `fail` | the menu for callers who do not meet `require`. If there is no `fail`, the caller stays in the menu before |
|
||||
|
||||
An `[item KEY]` section can have these keys:
|
||||
|
||||
| Key | What it is |
|
||||
|---|---|
|
||||
| `do` | the [commands](#commands), with `;` between them. This key is necessary |
|
||||
| `label` | the text in the list, with @-codes and tags. It is necessary, but not for a special key or a hidden item |
|
||||
| `require` | the [access requirement](Access-Levels) to see and use the item |
|
||||
| `hidden` | `yes`: the key operates, but the BBS does not show it in the list |
|
||||
|
||||
The key of an item is one of these:
|
||||
|
||||
- One character, for example `r` or `*`. The case of a letter is not
|
||||
important.
|
||||
- A word of 2 to 12 letters and digits, for example `JOIN`. If a menu
|
||||
has a word, the caller types a line and pushes Enter. Then a key of one
|
||||
character also needs Enter.
|
||||
- A special key:
|
||||
- `@first`: the BBS does it one time, when the caller goes into the
|
||||
menu.
|
||||
- `@every`: the BBS does it each time before it shows the menu.
|
||||
- `@enter`: the BBS does it when the caller pushes Enter and types no
|
||||
key.
|
||||
|
||||
The BBS does the commands of an item in sequence. A command that goes
|
||||
to a different menu (`menu.gosub`, `menu.goto`, `menu.return`,
|
||||
`menu.main`) is the last command that the BBS does.
|
||||
|
||||
### Commands
|
||||
|
||||
| Command | What it does |
|
||||
|---|---|
|
||||
| `areas` | the message areas |
|
||||
| `mail` | e-mail |
|
||||
| `who` | who is online |
|
||||
| `users` | the user list |
|
||||
| `profile` | the profile of the caller |
|
||||
| `info` | system information |
|
||||
| `files` | the file areas |
|
||||
| `boards` | the [image boards](Image-Boards) |
|
||||
| `doors` | the list of doors |
|
||||
| `chat` | chat |
|
||||
| `page` | pages the sysop |
|
||||
| `callers` | the last callers |
|
||||
| `feedback` | mail to the sysop |
|
||||
| `qwk` | [offline mail](Offline-Mail) |
|
||||
| `news` | news, weather and stock quotes |
|
||||
| `headlines` | the news headlines |
|
||||
| `weather` | the weather of the caller |
|
||||
| `quotes` | stock quotes |
|
||||
| `validate` | the users that wait for validation (for sysops) |
|
||||
| `sysop` | the [sysop tools](Sysop-Tools) (for sysops) |
|
||||
| `terminal` | the terminal details of a 3270 |
|
||||
| `logoff` | logs out (on a text terminal, it asks first) |
|
||||
| `back` | goes back to the previous menu |
|
||||
| `door:NAME` | goes directly into a door |
|
||||
| `menu:NAME` | goes to a different menu |
|
||||
| `screen:NAME` | shows a screen |
|
||||
| `area:NAME` | goes directly into a message area |
|
||||
| `script:NAME` | runs a script of `scripts_dir` ([Scripting](Scripting)) |
|
||||
| `menu.gosub NAME` | goes to the menu `NAME`. `menu.return` in that menu comes back. The short name is `menu:NAME` |
|
||||
| `menu.goto NAME` | goes to the menu `NAME`, in place of this menu |
|
||||
| `menu.return` | goes back to the menu before. The short name is `back` |
|
||||
| `menu.main` | goes back to the main menu |
|
||||
|
||||
In a menu file, write the data after a space or after a colon:
|
||||
`menu.gosub extras` and `menu.gosub:extras` are the same.
|
||||
|
||||
These names are also correct: `msg.areas` (`areas`), `mail.read`
|
||||
(`mail`), `mail.feedback` (`feedback`), `info.who` (`who`), `info.users`
|
||||
(`users`), `info.system` (`info`), `info.callers` (`callers`),
|
||||
`user.profile` (`profile`), `file.areas` (`files`).
|
||||
|
||||
Some commands are not always available. The BBS does not show an item
|
||||
if its first command is not available to the caller:
|
||||
|
||||
- `files`: only if there are file areas.
|
||||
- `doors`: only if a door is open to the caller.
|
||||
- `boards` and `news`: only if the BBS has them.
|
||||
- `chat` and `page`: only if the [level](Access-Levels) of the caller
|
||||
lets the caller chat or page.
|
||||
- `sysop`: only for sysops.
|
||||
- `validate`: only for sysops, and only if users wait for validation.
|
||||
- `terminal`: only on a 3270.
|
||||
|
||||
`@NOTE@` in a label gives a note from the first command of the item:
|
||||
|
||||
| Command | `@NOTE@` |
|
||||
|---|---|
|
||||
| `mail` | ` (3 new)`: the new mail. Nothing if there is no new mail |
|
||||
| `chat` | ` (2 paging)`: callers who page the sysop. Only for sysops |
|
||||
| `validate` | ` (4)`: the users that wait |
|
||||
| `news` | the name of what there is, for example `News and weather` |
|
||||
|
||||
A caller can go into a maximum of 10 menus, each from the menu before.
|
||||
|
||||
### Menus on a 3270 and on RIP terminals
|
||||
|
||||
With `screen =`, a text terminal shows the screen, which shows the keys
|
||||
itself. A 3270 shows the `.3270` (or `.asc` or `.txt`) version of the
|
||||
@@ -190,9 +349,11 @@ Thus, a menu that is drawn only as ANSI art still operates on a 3270.
|
||||
|
||||
On a 3270, the caller types the key in the option field. PF3 goes back
|
||||
(on the main menu, it logs out if there is a `logoff` item). Doors are
|
||||
only for text terminals. On a 3270, a door item tells this. `bbs check`
|
||||
refuses a menu that names a menu, door or screen that does not exist. It
|
||||
lists the menus that it read.
|
||||
only for text terminals. On a 3270, a door item tells this. A RIP
|
||||
terminal shows the items as buttons.
|
||||
|
||||
`bbs check` refuses a menu that names a command, menu, door, screen or
|
||||
script that does not exist. It lists the menus that it read.
|
||||
|
||||
## Strings
|
||||
|
||||
|
||||
+1
-285
@@ -14,7 +14,6 @@ import (
|
||||
|
||||
"github.com/D-Modem/bbs/internal/bbs"
|
||||
"github.com/D-Modem/bbs/internal/door"
|
||||
"github.com/D-Modem/bbs/internal/screens"
|
||||
"github.com/D-Modem/bbs/internal/script"
|
||||
"github.com/D-Modem/bbs/internal/store"
|
||||
"github.com/D-Modem/bbs/internal/view"
|
||||
@@ -117,10 +116,7 @@ func Run(b *bbs.BBS, n *bbs.Node, ui Frontend) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if m := b.Menu("main"); m != nil {
|
||||
return s.runMenu(m, 0, intro)
|
||||
}
|
||||
return s.mainMenu(intro)
|
||||
return s.runMenus(intro)
|
||||
}
|
||||
|
||||
// refresh is the caller's user read again (kept in s.User), for what the sysop
|
||||
@@ -165,131 +161,6 @@ func (s *Session) welcome(shown bool) []view.Line {
|
||||
return out
|
||||
}
|
||||
|
||||
// mainMenu is the built-in main menu.
|
||||
func (s *Session) mainMenu(intro []view.Line) error {
|
||||
for {
|
||||
if err := s.summoned(); err != nil {
|
||||
return err
|
||||
}
|
||||
u := s.refresh()
|
||||
s.N.Doing("Main menu")
|
||||
unread, _ := s.B.Store.UnreadMail(u.ID)
|
||||
mail := "E-mail"
|
||||
if unread > 0 {
|
||||
mail += fmt.Sprintf(" (%d new)", unread)
|
||||
}
|
||||
items := []view.Item{{Key: "M", Label: "Message areas"}, {Key: "E", Label: mail},
|
||||
{Key: "W", Label: "Who's online"}, {Key: "U", Label: "User list"},
|
||||
{Key: "P", Label: "Your profile"}, {Key: "I", Label: "System info"}}
|
||||
if s.B.Files != nil {
|
||||
items = append(items, view.Item{Key: "F", Label: "File areas"})
|
||||
}
|
||||
if len(s.UI.Doors()) > 0 {
|
||||
items = append(items, view.Item{Key: "D", Label: "Doors"})
|
||||
}
|
||||
if s.hasBoards() {
|
||||
items = append(items, view.Item{Key: "B", Label: "Image boards"})
|
||||
}
|
||||
if s.hasNews() {
|
||||
items = append(items, view.Item{Key: "N", Label: s.newsLabel()})
|
||||
}
|
||||
lvl := s.B.LevelOf(u)
|
||||
if lvl.Chat {
|
||||
chat := "Chat"
|
||||
if u.Sysop {
|
||||
if ps := s.B.Pages(); len(ps) > 0 {
|
||||
chat += fmt.Sprintf(" (%d paging)", len(ps))
|
||||
}
|
||||
}
|
||||
items = append(items, view.Item{Key: "C", Label: chat})
|
||||
}
|
||||
if lvl.Page {
|
||||
items = append(items, view.Item{Key: "S", Label: "Page the sysop"})
|
||||
}
|
||||
waiting := 0
|
||||
if u.Sysop && s.B.Conf().ValidatedLevel > s.B.Conf().NewUserLevel {
|
||||
w, _ := s.B.Store.UsersBelow(s.B.Conf().ValidatedLevel)
|
||||
if waiting = len(w); waiting > 0 {
|
||||
items = append(items, view.Item{Key: "V", Label: fmt.Sprintf("Validate users (%d)", waiting)})
|
||||
}
|
||||
}
|
||||
items = append(items, view.Item{Key: "O", Label: "Offline mail (QWK)"},
|
||||
view.Item{Key: "L", Label: "Last callers"},
|
||||
view.Item{Key: "K", Label: "Feedback to the sysop"})
|
||||
if u.Sysop {
|
||||
items = append(items, view.Item{Key: "*", Label: "Sysop tools"})
|
||||
}
|
||||
if s.UI.Kind() == TN3270 {
|
||||
items = append(items, view.Item{Key: "T", Label: "Your terminal"})
|
||||
}
|
||||
items = append(items, view.Item{Key: "G", Label: "Goodbye"})
|
||||
k, err := s.UI.Menu(&view.Menu{Title: "Main menu", Intro: intro, Items: items, Brief: s.expert()})
|
||||
intro = nil
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch strings.ToUpper(k) {
|
||||
case "M":
|
||||
err = s.areas()
|
||||
case "B":
|
||||
if s.hasBoards() {
|
||||
err = s.imageBoards()
|
||||
}
|
||||
case "N":
|
||||
if s.hasNews() {
|
||||
err = s.newsMenu()
|
||||
}
|
||||
case "E":
|
||||
err = s.mail()
|
||||
case "O":
|
||||
err = s.offline()
|
||||
case "L":
|
||||
err = s.lastCallers()
|
||||
case "*":
|
||||
if u.Sysop {
|
||||
err = s.sysopMenu()
|
||||
}
|
||||
case "K":
|
||||
err = s.feedback()
|
||||
case "W":
|
||||
err = s.who()
|
||||
case "U":
|
||||
err = s.users()
|
||||
case "P":
|
||||
err = s.profile()
|
||||
case "I":
|
||||
err = s.info()
|
||||
case "F":
|
||||
if s.B.Files != nil {
|
||||
err = s.fileAreas()
|
||||
}
|
||||
case "D":
|
||||
err = s.doors()
|
||||
case "C":
|
||||
if lvl.Chat {
|
||||
err = s.UI.Chat("", "")
|
||||
}
|
||||
case "S":
|
||||
if lvl.Page {
|
||||
err = s.page()
|
||||
}
|
||||
case "V":
|
||||
if waiting > 0 {
|
||||
err = s.validate()
|
||||
}
|
||||
case "T":
|
||||
if s.UI.Kind() == TN3270 {
|
||||
err = s.UI.TerminalInfo()
|
||||
}
|
||||
case "G", "Q", view.Back:
|
||||
err = s.logoff()
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// expert: the caller wants their menus brief.
|
||||
func (s *Session) expert() bool { return s.N.Prefs()["expert"] == "on" }
|
||||
|
||||
@@ -303,161 +174,6 @@ func (s *Session) logoff() error {
|
||||
return ErrLogoff
|
||||
}
|
||||
|
||||
// runMenu runs a menu of the sysop's until it is left (back) or the caller
|
||||
// logs off.
|
||||
func (s *Session) runMenu(full *bbs.Menu, depth int, intro []view.Line) error {
|
||||
vars := s.N.Vars()
|
||||
for {
|
||||
if err := s.summoned(); err != nil {
|
||||
return err
|
||||
}
|
||||
u := s.refresh()
|
||||
m := s.B.MenuFor(full, u)
|
||||
// the title, labels and prompt with their tags marked, for the
|
||||
// front ends to show (or drop); Who's online gets the title plain
|
||||
title := screens.Marked(m.Title, vars)
|
||||
s.N.Doing(screens.Unmark(title))
|
||||
vm := &view.Menu{Title: title, Intro: intro, Screen: m.Screen, Brief: s.expert()}
|
||||
intro = nil
|
||||
for _, it := range m.Items {
|
||||
vm.Items = append(vm.Items, view.Item{Key: it.Key, Label: screens.Marked(it.Label, vars)})
|
||||
}
|
||||
if m.Prompt != "" {
|
||||
vm.Prompt = screens.Marked(m.Prompt, vars)
|
||||
}
|
||||
k, err := s.UI.Menu(vm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if k == view.Back {
|
||||
// a 3270's PF3: up a menu, or off from the top one
|
||||
if depth > 0 {
|
||||
return nil
|
||||
}
|
||||
for _, it := range m.Items {
|
||||
if it.Action == "logoff" {
|
||||
k = it.Key
|
||||
}
|
||||
}
|
||||
}
|
||||
it := m.Item(k)
|
||||
if it == nil {
|
||||
continue
|
||||
}
|
||||
if it.Action == "back" {
|
||||
if depth > 0 {
|
||||
return nil
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err := s.do(it, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// do does a menu item.
|
||||
func (s *Session) do(it *bbs.MenuItem, depth int) error {
|
||||
switch it.Action {
|
||||
case "areas":
|
||||
return s.areas()
|
||||
case "mail":
|
||||
return s.mail()
|
||||
case "who":
|
||||
return s.who()
|
||||
case "users":
|
||||
return s.users()
|
||||
case "profile":
|
||||
return s.profile()
|
||||
case "info":
|
||||
return s.info()
|
||||
case "terminal":
|
||||
if s.UI.Kind() != TN3270 {
|
||||
s.UI.Say(view.Plain, "That is for 3270 terminals.")
|
||||
return nil
|
||||
}
|
||||
return s.UI.TerminalInfo()
|
||||
case "files":
|
||||
if s.B.Files == nil {
|
||||
s.UI.Say(view.Plain, "There are no file areas.")
|
||||
return nil
|
||||
}
|
||||
return s.fileAreas()
|
||||
case "doors":
|
||||
return s.doors()
|
||||
case "boards":
|
||||
if !s.hasBoards() {
|
||||
s.UI.Say(view.Plain, "There are no image boards.")
|
||||
return nil
|
||||
}
|
||||
return s.imageBoards()
|
||||
case "door":
|
||||
for _, d := range s.UI.Doors() {
|
||||
if strings.EqualFold(d.Name, it.Arg) {
|
||||
return s.UI.OpenDoor(d)
|
||||
}
|
||||
}
|
||||
s.UI.Say(view.Bad, "That door is not open to you.")
|
||||
case "chat":
|
||||
return s.UI.Chat("", "")
|
||||
case "callers":
|
||||
return s.lastCallers()
|
||||
case "news":
|
||||
if !s.hasNews() {
|
||||
s.UI.Say(view.Plain, "There is no news here.")
|
||||
return nil
|
||||
}
|
||||
return s.newsMenu()
|
||||
case "headlines":
|
||||
return s.headlines()
|
||||
case "weather":
|
||||
return s.weather()
|
||||
case "quotes":
|
||||
return s.stockQuotes()
|
||||
case "sysop":
|
||||
if !s.User.Sysop {
|
||||
s.UI.Say(view.Bad, "That is for the sysop.")
|
||||
return nil
|
||||
}
|
||||
return s.sysopMenu()
|
||||
case "feedback":
|
||||
return s.feedback()
|
||||
case "qwk":
|
||||
return s.offline()
|
||||
case "page":
|
||||
return s.page()
|
||||
case "logoff":
|
||||
return s.logoff()
|
||||
case "menu":
|
||||
if depth >= 10 {
|
||||
s.UI.Say(view.Bad, "Menus nest too deep.")
|
||||
return nil
|
||||
}
|
||||
sub := s.B.Menu(strings.ToLower(it.Arg))
|
||||
if !bbs.MustRequire(sub.Require).Allows(s.User) {
|
||||
s.UI.Say(view.Bad, "That menu is not for you.")
|
||||
return nil
|
||||
}
|
||||
return s.runMenu(sub, depth+1, nil)
|
||||
case "screen":
|
||||
shown, err := s.UI.ShowScreen(it.Arg, true)
|
||||
if err == nil && !shown {
|
||||
s.UI.Say(view.Plain, "There is nothing to show.")
|
||||
}
|
||||
return err
|
||||
case "script":
|
||||
return s.runScript(it.Arg)
|
||||
case "area":
|
||||
a, err := s.B.Store.AreaByName(it.Arg)
|
||||
if err != nil || !s.B.CanRead(s.User, a) {
|
||||
s.UI.Say(view.Bad, "There is no area "+it.Arg+".")
|
||||
return nil
|
||||
}
|
||||
return s.area(a)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// runScript runs a script of the sysop's for the caller.
|
||||
func (s *Session) runScript(name string) error {
|
||||
if s.B.ScriptsDir == "" {
|
||||
|
||||
@@ -0,0 +1,403 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/D-Modem/bbs/internal/bbs"
|
||||
"github.com/D-Modem/bbs/internal/config"
|
||||
"github.com/D-Modem/bbs/internal/screens"
|
||||
"github.com/D-Modem/bbs/internal/store"
|
||||
"github.com/D-Modem/bbs/internal/view"
|
||||
)
|
||||
|
||||
// The menus: the sysop's (bbs.conf's [menu NAME]s and menus_dir's files)
|
||||
// over the built-in ones of the Classic theme (menus/*.menu), run as a
|
||||
// stack: menu.gosub puts a menu on it, menu.return takes it off,
|
||||
// menu.goto puts one in the place of the top one, menu.main goes back to
|
||||
// the main menu.
|
||||
|
||||
//go:embed menus/*.menu
|
||||
var classicFiles embed.FS
|
||||
|
||||
// classic is the built-in menus, by name.
|
||||
var classic = func() map[string]*bbs.Menu {
|
||||
out := map[string]*bbs.Menu{}
|
||||
ents, err := classicFiles.ReadDir("menus")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, e := range ents {
|
||||
f, err := classicFiles.Open("menus/" + e.Name())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
cf, err := config.Parse(f)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("menus/%s: %v", e.Name(), err))
|
||||
}
|
||||
name := strings.TrimSuffix(e.Name(), ".menu")
|
||||
m, err := bbs.ParseMenuFile(name, cf)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("menus/%s: %v", e.Name(), err))
|
||||
}
|
||||
out[name] = m
|
||||
}
|
||||
return out
|
||||
}()
|
||||
|
||||
// BuiltinMenu is the built-in menu of a name, or nil.
|
||||
func BuiltinMenu(name string) *bbs.Menu { return classic[name] }
|
||||
|
||||
// maxMenuDepth is how many menus the stack holds at most.
|
||||
const maxMenuDepth = 10
|
||||
|
||||
// menuFrame is a menu on the stack.
|
||||
type menuFrame struct {
|
||||
name string
|
||||
started bool // its @FIRST is done
|
||||
}
|
||||
|
||||
// menuRun is the stack of menus of a session.
|
||||
type menuRun struct {
|
||||
stack []menuFrame
|
||||
moved bool // the stack changed: the rest of an item's commands is not done
|
||||
}
|
||||
|
||||
func (r *menuRun) top() *menuFrame { return &r.stack[len(r.stack)-1] }
|
||||
|
||||
// menuNamed is the sysop's menu of a name, else the built-in one, else nil.
|
||||
func (s *Session) menuNamed(name string) *bbs.Menu {
|
||||
if m := s.B.Menu(name); m != nil {
|
||||
return m
|
||||
}
|
||||
return classic[name]
|
||||
}
|
||||
|
||||
// menuCommand is how the session does a command, and when.
|
||||
type menuCommand struct {
|
||||
run func(s *Session, c bbs.Command, r *menuRun) error
|
||||
// avail: whether the caller can use it now; an item whose first
|
||||
// command is not available is not shown (nil: always)
|
||||
avail func(s *Session, u *store.User) bool
|
||||
// note: what @NOTE@ in the item's label becomes (nil: nothing)
|
||||
note func(s *Session, u *store.User) string
|
||||
}
|
||||
|
||||
// plain is a command that is a function of the session's.
|
||||
func plain(f func(s *Session) error) func(*Session, bbs.Command, *menuRun) error {
|
||||
return func(s *Session, _ bbs.Command, _ *menuRun) error { return f(s) }
|
||||
}
|
||||
|
||||
// menuCommands are the commands of bbs.Commands, done.
|
||||
var menuCommands map[string]menuCommand
|
||||
|
||||
func init() {
|
||||
menuCommands = map[string]menuCommand{
|
||||
"areas": {run: plain((*Session).areas)},
|
||||
"mail": {run: plain((*Session).mail), note: func(s *Session, u *store.User) string {
|
||||
if n, _ := s.B.Store.UnreadMail(u.ID); n > 0 {
|
||||
return fmt.Sprintf(" (%d new)", n)
|
||||
}
|
||||
return ""
|
||||
}},
|
||||
"who": {run: plain((*Session).who)},
|
||||
"users": {run: plain((*Session).users)},
|
||||
"profile": {run: plain((*Session).profile)},
|
||||
"info": {run: plain((*Session).info)},
|
||||
"files": {run: func(s *Session, _ bbs.Command, _ *menuRun) error {
|
||||
if s.B.Files == nil {
|
||||
s.UI.Say(view.Plain, "There are no file areas.")
|
||||
return nil
|
||||
}
|
||||
return s.fileAreas()
|
||||
}, avail: func(s *Session, _ *store.User) bool { return s.B.Files != nil }},
|
||||
"boards": {run: func(s *Session, _ bbs.Command, _ *menuRun) error {
|
||||
if !s.hasBoards() {
|
||||
s.UI.Say(view.Plain, "There are no image boards.")
|
||||
return nil
|
||||
}
|
||||
return s.imageBoards()
|
||||
}, avail: func(s *Session, _ *store.User) bool { return s.hasBoards() }},
|
||||
"doors": {run: plain((*Session).doors),
|
||||
avail: func(s *Session, _ *store.User) bool { return len(s.UI.Doors()) > 0 }},
|
||||
"chat": {run: func(s *Session, _ bbs.Command, _ *menuRun) error {
|
||||
if !s.B.LevelOf(s.User).Chat {
|
||||
s.UI.Say(view.Bad, "Chat is not open to you.")
|
||||
return nil
|
||||
}
|
||||
return s.UI.Chat("", "")
|
||||
}, avail: func(s *Session, u *store.User) bool { return s.B.LevelOf(u).Chat },
|
||||
note: func(s *Session, u *store.User) string {
|
||||
if u.Sysop {
|
||||
if ps := s.B.Pages(); len(ps) > 0 {
|
||||
return fmt.Sprintf(" (%d paging)", len(ps))
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}},
|
||||
"page": {run: func(s *Session, _ bbs.Command, _ *menuRun) error {
|
||||
if !s.B.LevelOf(s.User).Page {
|
||||
s.UI.Say(view.Bad, "Paging is not open to you.")
|
||||
return nil
|
||||
}
|
||||
return s.page()
|
||||
}, avail: func(s *Session, u *store.User) bool { return s.B.LevelOf(u).Page }},
|
||||
"callers": {run: plain((*Session).lastCallers)},
|
||||
"feedback": {run: plain((*Session).feedback)},
|
||||
"qwk": {run: plain((*Session).offline)},
|
||||
"news": {run: func(s *Session, _ bbs.Command, _ *menuRun) error {
|
||||
if !s.hasNews() {
|
||||
s.UI.Say(view.Plain, "There is no news here.")
|
||||
return nil
|
||||
}
|
||||
return s.newsMenu()
|
||||
}, avail: func(s *Session, _ *store.User) bool { return s.hasNews() },
|
||||
note: func(s *Session, _ *store.User) string { return s.newsLabel() }},
|
||||
"headlines": {run: plain((*Session).headlines)},
|
||||
"weather": {run: plain((*Session).weather)},
|
||||
"quotes": {run: plain((*Session).stockQuotes)},
|
||||
"validate": {run: func(s *Session, _ bbs.Command, _ *menuRun) error {
|
||||
if !s.User.Sysop {
|
||||
s.UI.Say(view.Bad, "That is for the sysop.")
|
||||
return nil
|
||||
}
|
||||
return s.validate()
|
||||
}, avail: func(s *Session, u *store.User) bool { return s.waiting(u) > 0 },
|
||||
note: func(s *Session, u *store.User) string { return fmt.Sprintf(" (%d)", s.waiting(u)) }},
|
||||
"sysop": {run: func(s *Session, _ bbs.Command, _ *menuRun) error {
|
||||
if !s.User.Sysop {
|
||||
s.UI.Say(view.Bad, "That is for the sysop.")
|
||||
return nil
|
||||
}
|
||||
return s.sysopMenu()
|
||||
}, avail: func(_ *Session, u *store.User) bool { return u.Sysop }},
|
||||
"terminal": {run: func(s *Session, _ bbs.Command, _ *menuRun) error {
|
||||
if s.UI.Kind() != TN3270 {
|
||||
s.UI.Say(view.Plain, "That is for 3270 terminals.")
|
||||
return nil
|
||||
}
|
||||
return s.UI.TerminalInfo()
|
||||
}, avail: func(s *Session, _ *store.User) bool { return s.UI.Kind() == TN3270 }},
|
||||
"logoff": {run: plain((*Session).logoff)},
|
||||
"door": {run: func(s *Session, c bbs.Command, _ *menuRun) error {
|
||||
for _, d := range s.UI.Doors() {
|
||||
if strings.EqualFold(d.Name, c.Arg) {
|
||||
return s.UI.OpenDoor(d)
|
||||
}
|
||||
}
|
||||
s.UI.Say(view.Bad, "That door is not open to you.")
|
||||
return nil
|
||||
}},
|
||||
"screen": {run: func(s *Session, c bbs.Command, _ *menuRun) error {
|
||||
shown, err := s.UI.ShowScreen(c.Arg, true)
|
||||
if err == nil && !shown {
|
||||
s.UI.Say(view.Plain, "There is nothing to show.")
|
||||
}
|
||||
return err
|
||||
}},
|
||||
"script": {run: func(s *Session, c bbs.Command, _ *menuRun) error { return s.runScript(c.Arg) }},
|
||||
"area": {run: func(s *Session, c bbs.Command, _ *menuRun) error {
|
||||
a, err := s.B.Store.AreaByName(c.Arg)
|
||||
if err != nil || !s.B.CanRead(s.User, a) {
|
||||
s.UI.Say(view.Bad, "There is no area "+c.Arg+".")
|
||||
return nil
|
||||
}
|
||||
return s.area(a)
|
||||
}},
|
||||
"menu.gosub": {run: func(s *Session, c bbs.Command, r *menuRun) error {
|
||||
if len(r.stack) >= maxMenuDepth {
|
||||
s.UI.Say(view.Bad, "Menus nest too deep.")
|
||||
return nil
|
||||
}
|
||||
r.stack = append(r.stack, menuFrame{name: c.Arg})
|
||||
r.moved = true
|
||||
return nil
|
||||
}},
|
||||
"menu.goto": {run: func(s *Session, c bbs.Command, r *menuRun) error {
|
||||
*r.top() = menuFrame{name: c.Arg}
|
||||
r.moved = true
|
||||
return nil
|
||||
}},
|
||||
"menu.return": {run: func(s *Session, c bbs.Command, r *menuRun) error {
|
||||
if len(r.stack) > 1 {
|
||||
r.stack = r.stack[:len(r.stack)-1]
|
||||
r.moved = true
|
||||
}
|
||||
return nil
|
||||
}},
|
||||
"menu.main": {run: func(s *Session, c bbs.Command, r *menuRun) error {
|
||||
r.stack = []menuFrame{{name: "main"}}
|
||||
r.moved = true
|
||||
return nil
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
// waiting is how many users wait for a sysop's validation (0 for others).
|
||||
func (s *Session) waiting(u *store.User) int {
|
||||
if !u.Sysop || s.B.Conf().ValidatedLevel <= s.B.Conf().NewUserLevel {
|
||||
return 0
|
||||
}
|
||||
w, _ := s.B.Store.UsersBelow(s.B.Conf().ValidatedLevel)
|
||||
return len(w)
|
||||
}
|
||||
|
||||
// itemAvail is whether u may choose the item now: its requirement, and
|
||||
// its first command's being available.
|
||||
func (s *Session) itemAvail(it *bbs.MenuItem, u *store.User) bool {
|
||||
if !bbs.MustRequire(it.Require).Allows(u) {
|
||||
return false
|
||||
}
|
||||
if c, ok := menuCommands[it.Do[0].Name]; ok && c.avail != nil {
|
||||
return c.avail(s, u)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// runMenus runs the menus from the main menu until the caller logs off.
|
||||
func (s *Session) runMenus(intro []view.Line) error {
|
||||
r := &menuRun{stack: []menuFrame{{name: "main"}}}
|
||||
for {
|
||||
if err := s.summoned(); err != nil {
|
||||
return err
|
||||
}
|
||||
u := s.refresh()
|
||||
top := r.top()
|
||||
full := s.menuNamed(top.name)
|
||||
if full == nil {
|
||||
// gone in a reload
|
||||
s.UI.Say(view.Bad, "There is no menu "+top.name+".")
|
||||
r.stack = []menuFrame{{name: "main"}}
|
||||
continue
|
||||
}
|
||||
if len(r.stack) > 1 && !bbs.MustRequire(full.Require).Allows(u) {
|
||||
s.UI.Say(view.Bad, "That menu is not for you.")
|
||||
if full.Fail != "" && full.Fail != top.name && s.menuNamed(full.Fail) != nil {
|
||||
*top = menuFrame{name: full.Fail}
|
||||
} else {
|
||||
r.stack = r.stack[:len(r.stack)-1]
|
||||
}
|
||||
continue
|
||||
}
|
||||
var items []*bbs.MenuItem
|
||||
for i := range full.Items {
|
||||
if s.itemAvail(&full.Items[i], u) {
|
||||
items = append(items, &full.Items[i])
|
||||
}
|
||||
}
|
||||
special := func(key string) *bbs.MenuItem {
|
||||
for _, it := range items {
|
||||
if it.Key == key {
|
||||
return it
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !top.started {
|
||||
top.started = true
|
||||
if it := special("@FIRST"); it != nil {
|
||||
if moved, err := s.doItem(it, r); err != nil || moved {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
if it := special("@EVERY"); it != nil {
|
||||
if moved, err := s.doItem(it, r); err != nil || moved {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// the title, labels and prompt with their tags marked, for the
|
||||
// front ends to show (or drop); Who's online gets the title plain
|
||||
vars := s.N.Vars()
|
||||
title := screens.Marked(full.Title, vars)
|
||||
s.N.Doing(screens.Unmark(title))
|
||||
vm := &view.Menu{Title: title, Intro: intro, Screen: full.Screen, Brief: s.expert(), Line: full.Words()}
|
||||
intro = nil
|
||||
for _, it := range items {
|
||||
switch {
|
||||
case it.Special():
|
||||
case it.Hidden:
|
||||
vm.Hidden = append(vm.Hidden, it.Key)
|
||||
default:
|
||||
vm.Items = append(vm.Items, view.Item{Key: it.Key, Label: screens.Marked(it.Label, s.itemVars(it, u, vars))})
|
||||
}
|
||||
}
|
||||
if full.Prompt != "" {
|
||||
vm.Prompt = screens.Marked(full.Prompt, vars)
|
||||
}
|
||||
k, err := s.UI.Menu(vm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var it *bbs.MenuItem
|
||||
switch k {
|
||||
case view.Back:
|
||||
// a 3270's PF3: up a menu, or off from the main menu
|
||||
if len(r.stack) > 1 {
|
||||
r.stack = r.stack[:len(r.stack)-1]
|
||||
continue
|
||||
}
|
||||
for _, x := range items {
|
||||
if !x.Special() && x.Do[0].Name == "logoff" {
|
||||
it = x
|
||||
}
|
||||
}
|
||||
case "":
|
||||
it = special("@ENTER")
|
||||
default:
|
||||
for _, x := range items {
|
||||
if !x.Special() && strings.EqualFold(x.Key, k) {
|
||||
it = x
|
||||
}
|
||||
}
|
||||
}
|
||||
if it == nil {
|
||||
continue
|
||||
}
|
||||
if _, err := s.doItem(it, r); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// itemVars are the @-codes of an item's label: the node's, and @NOTE@.
|
||||
func (s *Session) itemVars(it *bbs.MenuItem, u *store.User, vars screens.Vars) screens.Vars {
|
||||
return func(code string) (string, bool) {
|
||||
if code == "NOTE" {
|
||||
if c, ok := menuCommands[it.Do[0].Name]; ok && c.note != nil {
|
||||
return c.note(s, u), true // made here: no caller's text
|
||||
}
|
||||
return "", true
|
||||
}
|
||||
return vars(code)
|
||||
}
|
||||
}
|
||||
|
||||
// doItem does an item's commands in turn, until one moves the menus;
|
||||
// it reports whether one did.
|
||||
func (s *Session) doItem(it *bbs.MenuItem, r *menuRun) (bool, error) {
|
||||
r.moved = false
|
||||
for _, c := range it.Do {
|
||||
mc, ok := menuCommands[c.Name]
|
||||
if !ok {
|
||||
return false, fmt.Errorf("menu command %s not done", c.Name)
|
||||
}
|
||||
if err := mc.run(s, c, r); err != nil {
|
||||
return r.moved, err
|
||||
}
|
||||
if r.moved {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
; The main menu of the Classic theme: the built-in main menu. A
|
||||
; [menu main] in bbs.conf, or a main.menu in menus_dir, replaces it.
|
||||
; An item whose command is not available to the caller is not shown
|
||||
; (no file areas, no doors, chat not allowed for the caller's level).
|
||||
|
||||
[menu]
|
||||
title = Main menu
|
||||
|
||||
[item M]
|
||||
label = Message areas
|
||||
do = areas
|
||||
|
||||
[item E]
|
||||
label = E-mail@NOTE@
|
||||
do = mail
|
||||
|
||||
[item W]
|
||||
label = Who's online
|
||||
do = who
|
||||
|
||||
[item U]
|
||||
label = User list
|
||||
do = users
|
||||
|
||||
[item P]
|
||||
label = Your profile
|
||||
do = profile
|
||||
|
||||
[item I]
|
||||
label = System info
|
||||
do = info
|
||||
|
||||
[item F]
|
||||
label = File areas
|
||||
do = files
|
||||
|
||||
[item D]
|
||||
label = Doors
|
||||
do = doors
|
||||
|
||||
[item B]
|
||||
label = Image boards
|
||||
do = boards
|
||||
|
||||
[item N]
|
||||
label = @NOTE@
|
||||
do = news
|
||||
|
||||
[item C]
|
||||
label = Chat@NOTE@
|
||||
do = chat
|
||||
|
||||
[item S]
|
||||
label = Page the sysop
|
||||
do = page
|
||||
|
||||
[item V]
|
||||
label = Validate users@NOTE@
|
||||
do = validate
|
||||
|
||||
[item O]
|
||||
label = Offline mail (QWK)
|
||||
do = qwk
|
||||
|
||||
[item L]
|
||||
label = Last callers
|
||||
do = callers
|
||||
|
||||
[item K]
|
||||
label = Feedback to the sysop
|
||||
do = feedback
|
||||
|
||||
[item *]
|
||||
label = Sysop tools
|
||||
do = sysop
|
||||
|
||||
[item T]
|
||||
label = Your terminal
|
||||
do = terminal
|
||||
|
||||
[item G]
|
||||
label = Goodbye
|
||||
do = logoff
|
||||
|
||||
; Q also logs off, as G does
|
||||
[item Q]
|
||||
hidden = yes
|
||||
do = logoff
|
||||
@@ -0,0 +1,191 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/D-Modem/bbs/internal/bbs"
|
||||
"github.com/D-Modem/bbs/internal/config"
|
||||
"github.com/D-Modem/bbs/internal/store"
|
||||
"github.com/D-Modem/bbs/internal/view"
|
||||
)
|
||||
|
||||
// TestCommandsDone: each command that a menu can name is one that the
|
||||
// session does, and no other.
|
||||
func TestCommandsDone(t *testing.T) {
|
||||
for name := range bbs.Commands {
|
||||
if _, ok := menuCommands[name]; !ok {
|
||||
t.Errorf("%s: in bbs.Commands, not done", name)
|
||||
}
|
||||
}
|
||||
for name := range menuCommands {
|
||||
if _, ok := bbs.Commands[name]; !ok {
|
||||
t.Errorf("%s: done, not in bbs.Commands", name)
|
||||
}
|
||||
}
|
||||
if classic["main"] == nil {
|
||||
t.Fatal("no built-in main menu")
|
||||
}
|
||||
}
|
||||
|
||||
// menuFake is the fake, which keeps the menus that it was shown whole.
|
||||
type menuFake struct {
|
||||
*fake
|
||||
menus []*view.Menu
|
||||
}
|
||||
|
||||
func (f *menuFake) Menu(m *view.Menu) (string, error) {
|
||||
f.menus = append(f.menus, m)
|
||||
return f.fake.Menu(m)
|
||||
}
|
||||
|
||||
// runMenusFor is run, with the menus kept.
|
||||
func runMenusFor(t *testing.T, b *bbs.BBS, n *bbs.Node, script ...any) *menuFake {
|
||||
t.Helper()
|
||||
f := &menuFake{fake: &fake{t: t, b: b, script: script}}
|
||||
if err := Run(b, n, f); err != ErrLogoff {
|
||||
t.Fatalf("Run: %v; shown %q", err, f.shown)
|
||||
}
|
||||
if len(f.script) > 0 {
|
||||
t.Errorf("left of the script: %v; shown %q", f.script, f.shown)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// labels is the labels of a menu's items.
|
||||
func labels(m *view.Menu) []string {
|
||||
var out []string
|
||||
for _, it := range m.Items {
|
||||
out = append(out, it.Label)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// TestBuiltinMain: the Classic main menu shows only what the caller can
|
||||
// use: no file areas without files_dir, the sysop's tools for the sysop
|
||||
// alone; Q logs off, but is not in the list.
|
||||
func TestBuiltinMain(t *testing.T) {
|
||||
b, alice, sysop := setup(t)
|
||||
f := runMenusFor(t, b, alice, "Q")
|
||||
m := f.menus[0]
|
||||
if m.Title != "Main menu" || !slices.Contains(m.Hidden, "Q") || m.Line {
|
||||
t.Fatalf("%+v", m)
|
||||
}
|
||||
got := strings.Join(labels(m), ",")
|
||||
if got != "Message areas,E-mail,Who's online,User list,Your profile,System info,Chat,Page the sysop,"+
|
||||
"Offline mail (QWK),Last callers,Feedback to the sysop,Goodbye" {
|
||||
t.Errorf("alice's main menu: %s", got)
|
||||
}
|
||||
f = runMenusFor(t, b, sysop)
|
||||
if l := labels(f.menus[0]); !slices.Contains(l, "Sysop tools") {
|
||||
t.Errorf("the sysop's main menu: %q", l)
|
||||
}
|
||||
}
|
||||
|
||||
// parseMenu is a menu file's menu.
|
||||
func parseMenu(t *testing.T, name, body string) *bbs.Menu {
|
||||
t.Helper()
|
||||
cf, err := config.Parse(strings.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m, err := bbs.ParseMenuFile(name, cf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// TestMenuStack: gosub, goto, return, main and PF3 move between menus; a
|
||||
// chain stops at a command that moves; @FIRST is done once when a menu
|
||||
// opens, @EVERY each time it shows; a word is a key; a menu that the
|
||||
// caller may not have goes to its fail menu.
|
||||
func TestMenuStack(t *testing.T) {
|
||||
b, alice, _ := setup(t)
|
||||
b.Menus = map[string]*bbs.Menu{
|
||||
"main": parseMenu(t, "main", `
|
||||
[menu]
|
||||
title = Main
|
||||
[item X]
|
||||
label = Extras
|
||||
do = menu.gosub extras ; logoff
|
||||
[item S]
|
||||
label = Secret
|
||||
do = menu.gosub secret
|
||||
[item G]
|
||||
label = Goodbye
|
||||
do = logoff
|
||||
`),
|
||||
"extras": parseMenu(t, "extras", `
|
||||
[menu]
|
||||
title = Extras
|
||||
[item @first]
|
||||
do = screen:first
|
||||
[item @every]
|
||||
do = screen:every
|
||||
[item JUMP]
|
||||
label = Jump
|
||||
do = menu.goto other
|
||||
[item R]
|
||||
label = Back
|
||||
do = menu.return
|
||||
`),
|
||||
"other": parseMenu(t, "other", `
|
||||
[menu]
|
||||
title = Other
|
||||
[item M]
|
||||
label = Main
|
||||
do = menu.main
|
||||
`),
|
||||
"secret": parseMenu(t, "secret", `
|
||||
[menu]
|
||||
title = Secret
|
||||
require = 200
|
||||
fail = other
|
||||
[item R]
|
||||
hidden = yes
|
||||
do = menu.return
|
||||
`),
|
||||
}
|
||||
f := runMenusFor(t, b, alice,
|
||||
"X", "R", // into Extras (no logoff after it), back
|
||||
"X", "jump", "M", // Extras, Other in its place, Main
|
||||
"X", view.Back, // Extras, PF3 back
|
||||
"S", view.Back, // Secret is not for alice: Other in its place
|
||||
"G")
|
||||
var titles []string
|
||||
for _, m := range f.menus {
|
||||
titles = append(titles, m.Title)
|
||||
}
|
||||
want := "Main Extras Main Extras Other Main Extras Main Other Main"
|
||||
if got := strings.Join(titles, " "); got != want {
|
||||
t.Errorf("menus %s, want %s", got, want)
|
||||
}
|
||||
if !f.menus[1].Line || f.menus[0].Line {
|
||||
t.Error("Extras has a word key: the caller types a line; Main has none")
|
||||
}
|
||||
// the screens are not there: each is "nothing to show"; @first and
|
||||
// @every three times each (Extras opened three times), and "not for
|
||||
// you" once
|
||||
if n := strings.Count(strings.Join(f.said, "\n"), "There is nothing to show."); n != 3+3 {
|
||||
t.Errorf("%d screens shown; said %q", n, f.said)
|
||||
}
|
||||
has(t, "said", f.said, "That menu is not for you.")
|
||||
}
|
||||
|
||||
// TestMenuNotes: @NOTE@ in a label is what its command adds: the count of
|
||||
// new mail.
|
||||
func TestMenuNotes(t *testing.T) {
|
||||
b, alice, sysop := setup(t)
|
||||
m := &store.Message{FromID: sysop.User().ID, From: "sysop", ToID: alice.User().ID, To: "alice",
|
||||
Subject: "Hi", Body: "Hello.", Created: time.Now()}
|
||||
if _, err := b.Store.SendMail(m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f := runMenusFor(t, b, alice)
|
||||
if l := labels(f.menus[0]); !slices.Contains(l, "E-mail (1 new)") {
|
||||
t.Errorf("%q", l)
|
||||
}
|
||||
}
|
||||
+338
-49
@@ -1,24 +1,52 @@
|
||||
package bbs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/D-Modem/bbs/internal/config"
|
||||
"github.com/D-Modem/bbs/internal/screens"
|
||||
"github.com/D-Modem/bbs/internal/store"
|
||||
)
|
||||
|
||||
// MenuItem is a key of a sysop's menu and what it does.
|
||||
type MenuItem struct {
|
||||
Key string // one character, upper case
|
||||
Action string // see MenuActions; or door, menu, screen, area with Arg
|
||||
Arg string
|
||||
Label string // with @-codes
|
||||
Require string // the access requirement ("": anyone)
|
||||
// Command is one thing a menu item does: a command's name (Commands)
|
||||
// and its data ("" for none).
|
||||
type Command struct {
|
||||
Name string
|
||||
Arg string
|
||||
}
|
||||
|
||||
// Menu is a sysop's menu ([menu NAME]); "main" replaces the built-in one.
|
||||
func (c Command) String() string {
|
||||
if c.Arg == "" {
|
||||
return c.Name
|
||||
}
|
||||
return c.Name + " " + c.Arg
|
||||
}
|
||||
|
||||
// MenuItem is a key of a sysop's menu and what it does.
|
||||
type MenuItem struct {
|
||||
// Key is one character or a word (upper case), or a special key:
|
||||
// @FIRST (done when the menu opens), @EVERY (done each time that the
|
||||
// menu shows), @ENTER (Enter on an empty prompt).
|
||||
Key string
|
||||
Do []Command // done in turn
|
||||
Label string // with @-codes; "" for a special key
|
||||
Require string // the access requirement ("": anyone)
|
||||
Hidden bool // its key works, but it is not in the list
|
||||
// Action and Arg are Do[0]'s name and data, as a short [menu] line
|
||||
// gives them.
|
||||
Action string
|
||||
Arg string
|
||||
}
|
||||
|
||||
// Special is a key that no caller types as it is (@FIRST, @EVERY, @ENTER).
|
||||
func (it *MenuItem) Special() bool { return strings.HasPrefix(it.Key, "@") }
|
||||
|
||||
// Menu is a sysop's menu ([menu NAME] or NAME.menu); "main" replaces the
|
||||
// built-in one.
|
||||
type Menu struct {
|
||||
Name string
|
||||
Title string // with @-codes
|
||||
@@ -26,6 +54,18 @@ type Menu struct {
|
||||
Prompt string // ("": the command prompt)
|
||||
Items []MenuItem
|
||||
Require string // to be taken into it ("": anyone)
|
||||
Fail string // the menu for a caller without access ("": stay)
|
||||
}
|
||||
|
||||
// Words is whether a menu has a key of more than one character: then the
|
||||
// caller types a line, not one key.
|
||||
func (m *Menu) Words() bool {
|
||||
for _, it := range m.Items {
|
||||
if !it.Special() && len(it.Key) > 1 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// MenuFor is m with only the items u may choose.
|
||||
@@ -40,27 +80,174 @@ func (b *BBS) MenuFor(m *Menu, u *store.User) *Menu {
|
||||
return &out
|
||||
}
|
||||
|
||||
// MenuActions are what a menu item may do by itself; door:NAME,
|
||||
// menu:NAME, screen:NAME and area:NAME take a name.
|
||||
var MenuActions = map[string]string{
|
||||
"areas": "the message areas",
|
||||
"mail": "e-mail",
|
||||
"who": "who's online",
|
||||
"users": "the user list",
|
||||
"profile": "the caller's profile",
|
||||
"info": "system info",
|
||||
"files": "the file areas",
|
||||
"boards": "the image boards",
|
||||
"doors": "the doors",
|
||||
"chat": "chat",
|
||||
"page": "page the sysop",
|
||||
"callers": "the last callers",
|
||||
"feedback": "mail to the sysop",
|
||||
"qwk": "offline mail (QWK packets)",
|
||||
"sysop": "the sysop's tools (sysops only)",
|
||||
"terminal": "a 3270's terminal details (nothing elsewhere)",
|
||||
"back": "back to the menu before",
|
||||
"logoff": "log off",
|
||||
// ArgKind is what a command's data is.
|
||||
type ArgKind int
|
||||
|
||||
const (
|
||||
NoArg ArgKind = iota
|
||||
MenuArg // the name of a menu
|
||||
DoorArg // the name of a [door]
|
||||
ScreenArg // the name of a screen in screens_dir
|
||||
ScriptArg // the name of a script in scripts_dir
|
||||
AreaArg // the name of a message area
|
||||
OptionalArg // a word, or nothing
|
||||
)
|
||||
|
||||
// CommandInfo is what a command does, and its data.
|
||||
type CommandInfo struct {
|
||||
Help string
|
||||
Arg ArgKind
|
||||
// Words are the data that the command takes, if it takes only these
|
||||
// (OptionalArg)
|
||||
Words []string
|
||||
}
|
||||
|
||||
// Commands are what menu items can do.
|
||||
var Commands = map[string]CommandInfo{
|
||||
"areas": {Help: "the message areas"},
|
||||
"mail": {Help: "e-mail"},
|
||||
"who": {Help: "who's online"},
|
||||
"users": {Help: "the user list"},
|
||||
"profile": {Help: "the caller's profile"},
|
||||
"info": {Help: "system info"},
|
||||
"files": {Help: "the file areas"},
|
||||
"boards": {Help: "the image boards"},
|
||||
"doors": {Help: "the doors"},
|
||||
"chat": {Help: "chat"},
|
||||
"page": {Help: "page the sysop"},
|
||||
"callers": {Help: "the last callers"},
|
||||
"feedback": {Help: "mail to the sysop"},
|
||||
"qwk": {Help: "offline mail (QWK packets)"},
|
||||
"news": {Help: "news, weather and quotes"},
|
||||
"headlines": {Help: "the news headlines"},
|
||||
"weather": {Help: "the caller's weather"},
|
||||
"quotes": {Help: "stock quotes"},
|
||||
"validate": {Help: "validate new users (sysops only)"},
|
||||
"sysop": {Help: "the sysop's tools (sysops only)"},
|
||||
"terminal": {Help: "a 3270's terminal details (nothing elsewhere)"},
|
||||
"logoff": {Help: "log off"},
|
||||
"door": {Help: "a door", Arg: DoorArg},
|
||||
"screen": {Help: "a display file", Arg: ScreenArg},
|
||||
"script": {Help: "a script", Arg: ScriptArg},
|
||||
"area": {Help: "a message area", Arg: AreaArg},
|
||||
|
||||
"menu.gosub": {Help: "a menu, and back to this one after it", Arg: MenuArg},
|
||||
"menu.goto": {Help: "a menu in place of this one", Arg: MenuArg},
|
||||
"menu.return": {Help: "back to the menu before"},
|
||||
"menu.main": {Help: "back to the main menu"},
|
||||
}
|
||||
|
||||
// commandAliases are other names of commands: the short names of a
|
||||
// [menu] line, and names in groups.
|
||||
var commandAliases = map[string]string{
|
||||
"menu": "menu.gosub",
|
||||
"back": "menu.return",
|
||||
"msg.areas": "areas",
|
||||
"mail.read": "mail",
|
||||
"mail.feedback": "feedback",
|
||||
"info.who": "who",
|
||||
"info.users": "users",
|
||||
"info.system": "info",
|
||||
"info.callers": "callers",
|
||||
"user.profile": "profile",
|
||||
"file.areas": "files",
|
||||
}
|
||||
|
||||
// MenuActions are the commands that take no data, and what they do.
|
||||
var MenuActions = func() map[string]string {
|
||||
m := map[string]string{"back": "back to the menu before"}
|
||||
for k, c := range Commands {
|
||||
if c.Arg == NoArg || c.Arg == OptionalArg {
|
||||
m[k] = c.Help
|
||||
}
|
||||
}
|
||||
return m
|
||||
}()
|
||||
|
||||
// ParseCommand reads one command: "NAME", "NAME DATA" or "NAME:DATA".
|
||||
func ParseCommand(s string) (Command, error) {
|
||||
s = strings.TrimSpace(s)
|
||||
name, arg, _ := strings.Cut(s, " ")
|
||||
if n, a, ok := strings.Cut(name, ":"); ok {
|
||||
name, arg = n, a+" "+arg
|
||||
}
|
||||
name, arg = strings.ToLower(name), strings.TrimSpace(arg)
|
||||
if c, ok := commandAliases[name]; ok {
|
||||
name = c
|
||||
}
|
||||
info, ok := Commands[name]
|
||||
if !ok {
|
||||
return Command{}, fmt.Errorf("%q: no such command", name)
|
||||
}
|
||||
switch info.Arg {
|
||||
case NoArg:
|
||||
if arg != "" {
|
||||
return Command{}, fmt.Errorf("%s takes no data", name)
|
||||
}
|
||||
case OptionalArg:
|
||||
arg = strings.ToLower(arg)
|
||||
if arg != "" && !slices.Contains(info.Words, arg) {
|
||||
return Command{}, fmt.Errorf("%s %s: the data is one of %s", name, arg, strings.Join(info.Words, ", "))
|
||||
}
|
||||
default:
|
||||
if arg == "" {
|
||||
return Command{}, fmt.Errorf("%s needs a name", name)
|
||||
}
|
||||
if (info.Arg == ScreenArg || info.Arg == ScriptArg) && !screens.ValidName(arg) {
|
||||
return Command{}, fmt.Errorf("%s %s: a %s's name is lower case letters, digits, _ and -", name, arg, name)
|
||||
}
|
||||
if info.Arg == MenuArg {
|
||||
arg = strings.ToLower(arg)
|
||||
}
|
||||
}
|
||||
return Command{Name: name, Arg: arg}, nil
|
||||
}
|
||||
|
||||
// ParseCommands reads commands separated by semicolons.
|
||||
func ParseCommands(s string) ([]Command, error) {
|
||||
var out []Command
|
||||
for _, part := range strings.Split(s, ";") {
|
||||
if strings.TrimSpace(part) == "" {
|
||||
continue
|
||||
}
|
||||
c, err := ParseCommand(part)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, c)
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil, errors.New("no command")
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// specialKeys are the keys that a caller does not type as they are.
|
||||
var specialKeys = []string{"@FIRST", "@EVERY", "@ENTER"}
|
||||
|
||||
// ParseMenuKey reads a menu key: one character (not a space), a word of
|
||||
// letters and digits (at most 12), or a special key.
|
||||
func ParseMenuKey(key string) (string, error) {
|
||||
k := strings.ToUpper(strings.TrimSpace(key))
|
||||
if strings.HasPrefix(k, "@") {
|
||||
if slices.Contains(specialKeys, k) {
|
||||
return k, nil
|
||||
}
|
||||
return "", fmt.Errorf("%q: the special keys are %s", key, strings.ToLower(strings.Join(specialKeys, ", ")))
|
||||
}
|
||||
r := []rune(k)
|
||||
if len(r) == 1 && r[0] >= '!' && r[0] <= '~' {
|
||||
return k, nil
|
||||
}
|
||||
if len(r) < 2 || len(r) > 12 {
|
||||
return "", fmt.Errorf("%q: a menu key is one character or a word of 2 to 12 letters and digits", key)
|
||||
}
|
||||
for _, c := range r {
|
||||
if (c < 'A' || c > 'Z') && (c < '0' || c > '9') {
|
||||
return "", fmt.Errorf("%q: a menu key is one character or a word of 2 to 12 letters and digits", key)
|
||||
}
|
||||
}
|
||||
return k, nil
|
||||
}
|
||||
|
||||
// Strings are the prompts and messages a sysop may change ([strings]),
|
||||
@@ -171,7 +358,8 @@ func (n *Node) Vars() screens.Vars {
|
||||
}
|
||||
}
|
||||
|
||||
// ParseMenuItem reads a menu line: "ACTION Label words".
|
||||
// ParseMenuItem reads a [menu] line: KEY = "ACTION Label words",
|
||||
// perhaps with "| requirement" after it.
|
||||
func ParseMenuItem(key, value string) (MenuItem, error) {
|
||||
k := []rune(strings.ToUpper(key))
|
||||
if len(k) != 1 || k[0] < '!' || k[0] > '~' {
|
||||
@@ -182,36 +370,137 @@ func ParseMenuItem(key, value string) (MenuItem, error) {
|
||||
return MenuItem{}, fmt.Errorf("%s: %v", key, err)
|
||||
}
|
||||
action, label, _ := strings.Cut(strings.TrimSpace(value), " ")
|
||||
it := MenuItem{Key: string(k), Label: strings.TrimSpace(label), Require: strings.TrimSpace(req)}
|
||||
if a, arg, ok := strings.Cut(action, ":"); ok {
|
||||
switch a {
|
||||
case "door", "menu", "area":
|
||||
case "screen", "script":
|
||||
if !screens.ValidName(arg) {
|
||||
return MenuItem{}, fmt.Errorf("%s: a %s's name is lower case letters, digits, _ and -", action, a)
|
||||
}
|
||||
default:
|
||||
return MenuItem{}, fmt.Errorf("%s: no such action", action)
|
||||
}
|
||||
if arg == "" {
|
||||
return MenuItem{}, fmt.Errorf("%s: a name after the colon", action)
|
||||
}
|
||||
it.Action, it.Arg = a, arg
|
||||
} else {
|
||||
if _, ok := MenuActions[action]; !ok {
|
||||
c, err := ParseCommand(action)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no such command") {
|
||||
return MenuItem{}, fmt.Errorf("%q: no such action", action)
|
||||
}
|
||||
it.Action = action
|
||||
return MenuItem{}, fmt.Errorf("%s: %v", key, err)
|
||||
}
|
||||
if strings.Contains(action, ":") {
|
||||
// door:LORD keeps its case, menu:extras is lower case
|
||||
_, c.Arg, _ = strings.Cut(action, ":")
|
||||
if Commands[c.Name].Arg == MenuArg {
|
||||
c.Arg = strings.ToLower(c.Arg)
|
||||
}
|
||||
if c.Arg == "" {
|
||||
return MenuItem{}, fmt.Errorf("%s: a name after the colon", action)
|
||||
}
|
||||
}
|
||||
it := MenuItem{Key: string(k), Label: strings.TrimSpace(label), Require: strings.TrimSpace(req),
|
||||
Do: []Command{c}}
|
||||
it.Action, it.Arg = shortAction(c)
|
||||
if it.Label == "" {
|
||||
return MenuItem{}, fmt.Errorf("%s: a label after the action", key)
|
||||
}
|
||||
return it, nil
|
||||
}
|
||||
|
||||
// Item finds a menu's item by its key.
|
||||
// shortAction is a command's name as a [menu] line gives it: menu for
|
||||
// menu.gosub, back for menu.return.
|
||||
func shortAction(c Command) (string, string) {
|
||||
switch c.Name {
|
||||
case "menu.gosub":
|
||||
return "menu", c.Arg
|
||||
case "menu.return":
|
||||
return "back", ""
|
||||
}
|
||||
return c.Name, c.Arg
|
||||
}
|
||||
|
||||
// ParseMenuFile reads a menu file (NAME.menu): a [menu] section with
|
||||
// title, prompt, screen, require and fail, then an [item KEY] section for
|
||||
// each item, with label, do, require and hidden.
|
||||
func ParseMenuFile(name string, cf *config.File) (*Menu, error) {
|
||||
name = strings.ToLower(name)
|
||||
if !screens.ValidName(name) {
|
||||
return nil, fmt.Errorf("%s: a menu's name is lower case letters, digits, _ and -", name)
|
||||
}
|
||||
m := &Menu{Name: name, Title: name}
|
||||
seen := map[string]bool{}
|
||||
for _, s := range cf.Sections {
|
||||
switch s.Kind {
|
||||
case "menu":
|
||||
if s.Name != "" {
|
||||
return nil, fmt.Errorf("line %d: [menu] has no name in a menu file", s.Line)
|
||||
}
|
||||
m.Title = s.String("title", name)
|
||||
m.Screen = s.String("screen", "")
|
||||
m.Prompt = s.String("prompt", "")
|
||||
m.Require = s.String("require", "")
|
||||
m.Fail = strings.ToLower(s.String("fail", ""))
|
||||
if _, err := ParseRequire(m.Require); err != nil {
|
||||
return nil, fmt.Errorf("line %d: require: %v", s.Line, err)
|
||||
}
|
||||
if m.Screen != "" && !screens.ValidName(m.Screen) {
|
||||
return nil, fmt.Errorf("line %d: screen: a screen's name is lower case letters, digits, _ and -", s.Line)
|
||||
}
|
||||
if bad := unknownKeys(s, "title", "screen", "prompt", "require", "fail"); bad != "" {
|
||||
return nil, fmt.Errorf("line %d: [menu] has no %s", s.Line, bad)
|
||||
}
|
||||
case "item":
|
||||
key, err := ParseMenuKey(s.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("line %d: %v", s.Line, err)
|
||||
}
|
||||
if seen[key] {
|
||||
return nil, fmt.Errorf("line %d: [item %s] given twice", s.Line, s.Name)
|
||||
}
|
||||
seen[key] = true
|
||||
it := MenuItem{Key: key, Label: s.String("label", ""), Require: s.String("require", "")}
|
||||
if _, err := ParseRequire(it.Require); err != nil {
|
||||
return nil, fmt.Errorf("line %d: require: %v", s.Line, err)
|
||||
}
|
||||
if it.Hidden, err = s.Bool("hidden", false); err != nil {
|
||||
return nil, fmt.Errorf("line %d: %v", s.Line, err)
|
||||
}
|
||||
if it.Do, err = ParseCommands(s.String("do", "")); err != nil {
|
||||
return nil, fmt.Errorf("line %d: [item %s] do: %v", s.Line, s.Name, err)
|
||||
}
|
||||
it.Action, it.Arg = shortAction(it.Do[0])
|
||||
if it.Label == "" && !it.Special() && !it.Hidden {
|
||||
return nil, fmt.Errorf("line %d: [item %s] needs a label (or hidden = yes)", s.Line, s.Name)
|
||||
}
|
||||
if bad := unknownKeys(s, "label", "do", "require", "hidden"); bad != "" {
|
||||
return nil, fmt.Errorf("line %d: [item %s] has no %s", s.Line, s.Name, bad)
|
||||
}
|
||||
m.Items = append(m.Items, it)
|
||||
default:
|
||||
return nil, fmt.Errorf("line %d: [%s]: a menu file has [menu] and [item KEY] sections", s.Line, s.Kind)
|
||||
}
|
||||
}
|
||||
if len(m.Items) == 0 {
|
||||
return nil, errors.New("no items: [item KEY] with label and do")
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// unknownKeys is the first key of s not in known ("": none).
|
||||
func unknownKeys(s *config.Section, known ...string) string {
|
||||
for _, k := range s.Keys() {
|
||||
if !slices.Contains(known, k) {
|
||||
return k
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Item finds a menu's item by its key (not a special key).
|
||||
func (m *Menu) Item(key string) *MenuItem {
|
||||
key = strings.ToUpper(strings.TrimSpace(key))
|
||||
if strings.HasPrefix(key, "@") {
|
||||
return nil
|
||||
}
|
||||
for i := range m.Items {
|
||||
if m.Items[i].Key == key {
|
||||
return &m.Items[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Special finds a menu's special item (@FIRST, @EVERY, @ENTER), or nil.
|
||||
func (m *Menu) Special(key string) *MenuItem {
|
||||
for i := range m.Items {
|
||||
if m.Items[i].Key == key {
|
||||
return &m.Items[i]
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package bbs
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/D-Modem/bbs/internal/config"
|
||||
)
|
||||
|
||||
// TestParseMenuFile: a menu file's menu, items, chains and special keys;
|
||||
// what it refuses.
|
||||
func TestParseMenuFile(t *testing.T) {
|
||||
parse := func(body string) (*Menu, error) {
|
||||
cf, err := config.Parse(strings.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ParseMenuFile("msg", cf)
|
||||
}
|
||||
m, err := parse(`
|
||||
[menu]
|
||||
title = {cyan}Messages{reset}
|
||||
prompt = "@USER@> "
|
||||
require = 20
|
||||
fail = main
|
||||
[item r]
|
||||
label = Read
|
||||
do = areas
|
||||
[item new]
|
||||
label = New, then mail
|
||||
do = areas ; mail.read
|
||||
[item @first]
|
||||
do = screen:msgnews
|
||||
[item x]
|
||||
hidden = yes
|
||||
do = menu:Extras
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m.Name != "msg" || m.Prompt != "@USER@> " || m.Fail != "main" || len(m.Items) != 4 || !m.Words() {
|
||||
t.Fatalf("%+v", m)
|
||||
}
|
||||
if it := m.Item("new"); it == nil || len(it.Do) != 2 || it.Do[1] != (Command{Name: "mail"}) {
|
||||
t.Errorf("NEW: %+v", it)
|
||||
}
|
||||
if it := m.Special("@FIRST"); it == nil || it.Do[0] != (Command{Name: "screen", Arg: "msgnews"}) || m.Item("@first") != nil {
|
||||
t.Errorf("@FIRST: %+v", it)
|
||||
}
|
||||
if it := m.Item("X"); it == nil || !it.Hidden || it.Do[0] != (Command{Name: "menu.gosub", Arg: "extras"}) ||
|
||||
it.Action != "menu" || it.Arg != "extras" {
|
||||
t.Errorf("X: %+v", it)
|
||||
}
|
||||
for body, want := range map[string]string{
|
||||
"[item a]\nlabel = A\n": "no command",
|
||||
"[item a]\nlabel = A\ndo = nothing\n": "no such command",
|
||||
"[item a]\ndo = areas\n": "needs a label",
|
||||
"[item a]\nlabel = A\ndo = areas now\n": "takes no data",
|
||||
"[item a]\nlabel = A\ndo = door\n": "needs a name",
|
||||
"[item a]\nlabel = A\ndo = screen ../x\n": "a screen's name",
|
||||
"[item @last]\ndo = areas\n": "special keys",
|
||||
"[item toolongforakey]\nlabel = A\ndo = areas\n": "a word of 2 to 12",
|
||||
"[item a-b]\nlabel = A\ndo = areas\n": "a word of 2 to 12",
|
||||
"[item a]\nlabel = A\ndo = areas\n[item A]\ndo = mail": "given twice",
|
||||
"[item a]\nlabel = A\ndo = areas\ncolour = red\n": "has no colour",
|
||||
"[menu]\nsize = 2\n[item a]\nlabel = A\ndo = areas\n": "has no size",
|
||||
"[door x]\n": "[menu] and [item KEY]",
|
||||
"[menu]\ntitle = Empty\n": "no items",
|
||||
} {
|
||||
if _, err := parse(body); err == nil || !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("%q: %v, want %q", body, err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,7 +73,7 @@ var labels = map[string]string{
|
||||
"general.logo": "Logo (PNG)", "general.database": "Database", "general.log": "Log",
|
||||
"general.files_dir": "File areas' files", "general.max_file_size": "Largest file",
|
||||
"general.upload_quota": "Uploads a day (each)", "general.screens_dir": "Display files",
|
||||
"general.scripts_dir": "Scripts", "general.max_nodes": "Callers at once",
|
||||
"general.scripts_dir": "Scripts", "general.menus_dir": "Menu files", "general.max_nodes": "Callers at once",
|
||||
"general.attempts": "Login attempts", "general.fail_delay": "Delay after a wrong one (s)",
|
||||
"general.login_timeout": "Time to log in (s)", "general.idle_timeout": "Idle time out (s)",
|
||||
"general.throttle_failures": "Failures before a lock-out", "general.throttle_time": "Within (s)",
|
||||
|
||||
@@ -249,7 +249,7 @@ func (e *Editor) systemMenu() page {
|
||||
return page{title: "System configuration", items: func() []item {
|
||||
out := e.fields(g, "database", "log")
|
||||
out = append(out, heading("File locations"))
|
||||
out = append(out, e.fields(g, "files_dir", "max_file_size", "upload_quota", "screens_dir", "scripts_dir")...)
|
||||
out = append(out, e.fields(g, "files_dir", "max_file_size", "upload_quota", "screens_dir", "scripts_dir", "menus_dir")...)
|
||||
out = append(out, e.field(sec("chan"), "dir"), e.field(sec("fido"), "dir"))
|
||||
out = append(out, heading("Security"))
|
||||
out = append(out, e.fields(sec("security"), "deny_file", "name_deny_file", "hack_log", "ban_minutes", "ntlm")...)
|
||||
|
||||
@@ -76,6 +76,55 @@ pause = (more)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMenusDir: menus_dir's NAME.menu files read beside [menu NAME]s; a
|
||||
// menu given in both places, a bad file, and a command that names what
|
||||
// is not there refused.
|
||||
func TestMenusDir(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
menus := filepath.Join(dir, "menus")
|
||||
os.Mkdir(menus, 0755)
|
||||
write := func(name, body string) { os.WriteFile(filepath.Join(menus, name), []byte(body), 0644) }
|
||||
load := func(body string) (*Config, error) {
|
||||
p := filepath.Join(dir, "bbs.conf")
|
||||
os.WriteFile(p, []byte("[listen telnet]\ntype = telnet\naddress = :2300\n[general]\nmenus_dir = "+menus+"\n"+body), 0600)
|
||||
return Load(p)
|
||||
}
|
||||
write("main.menu", "[menu]\ntitle = Main\n[item x]\nlabel = Extras\ndo = menu.gosub extras\n[item g]\nlabel = Bye\ndo = logoff\n")
|
||||
c, err := load("[menu extras]\nq = back Back\n")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.Menus["main"] == nil || c.Menus["main"].Items[0].Do[0].Arg != "extras" || c.Menus["extras"] == nil {
|
||||
t.Fatalf("%+v", c.Menus)
|
||||
}
|
||||
for body, want := range map[string]string{
|
||||
"[menu main]\nq = back Back\n": "also given as [menu main]",
|
||||
"": "there is no [menu extras]",
|
||||
} {
|
||||
if _, err := load(body); err == nil || !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("%q: %v, want %q", body, err, want)
|
||||
}
|
||||
}
|
||||
write("extras.menu", "[menu]\nfail = nowhere\n[item q]\nlabel = Back\ndo = menu.return\n")
|
||||
if _, err := load(""); err == nil || !strings.Contains(err.Error(), "extras.menu: fail = nowhere") {
|
||||
t.Errorf("fail: %v", err)
|
||||
}
|
||||
write("extras.menu", "[item q]\nlabel = Back\ndo = menu.main ; door:lord\n")
|
||||
if _, err := load(""); err == nil || !strings.Contains(err.Error(), "extras.menu Q: there is no [door lord]") {
|
||||
t.Errorf("door: %v", err)
|
||||
}
|
||||
write("extras.menu", "[item q]\nlabel = Back\n")
|
||||
if _, err := load(""); err == nil || !strings.Contains(err.Error(), "extras.menu: line 1: [item q] do: no command") {
|
||||
t.Errorf("no do: %v", err)
|
||||
}
|
||||
// a menu may name a built-in menu
|
||||
write("extras.menu", "[item q]\nlabel = Main\ndo = menu.goto main\n")
|
||||
os.Remove(filepath.Join(menus, "main.menu"))
|
||||
if _, err := load(""); err != nil {
|
||||
t.Errorf("menu.goto main, built in: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// customize gives a BBS screens, menus and strings (startFull's prep).
|
||||
func customize(t *testing.T, files map[string]string) func(*bbs.BBS, string) {
|
||||
return func(b *bbs.BBS, top string) { customizeBBS(t, b, top, files) }
|
||||
|
||||
@@ -4,17 +4,19 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/D-Modem/bbs/internal/app"
|
||||
"github.com/D-Modem/bbs/internal/bbs"
|
||||
"github.com/D-Modem/bbs/internal/config"
|
||||
"github.com/D-Modem/bbs/internal/screens"
|
||||
"github.com/D-Modem/bbs/internal/script"
|
||||
)
|
||||
|
||||
// customSections reads screens_dir, scripts_dir, the [menu NAME]s and
|
||||
// [strings].
|
||||
// customSections reads screens_dir, scripts_dir, the [menu NAME]s,
|
||||
// menus_dir's NAME.menu files 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() {
|
||||
@@ -36,6 +38,7 @@ func (c *Config) customSections(cf *config.File) error {
|
||||
}
|
||||
}
|
||||
c.Menus = map[string]*bbs.Menu{}
|
||||
where := map[string]string{} // where each menu is given, for messages
|
||||
for _, s := range cf.All("menu") {
|
||||
m, err := menu(s)
|
||||
if err != nil {
|
||||
@@ -45,38 +48,50 @@ func (c *Config) customSections(cf *config.File) error {
|
||||
return fmt.Errorf("[menu %s] given twice", m.Name)
|
||||
}
|
||||
c.Menus[m.Name] = m
|
||||
where[m.Name] = "[menu " + m.Name + "]"
|
||||
}
|
||||
if dir := cf.Get("general", "").String("menus_dir", ""); dir != "" {
|
||||
files, err := filepath.Glob(filepath.Join(dir, "*.menu"))
|
||||
if fi, serr := os.Stat(dir); serr != nil || !fi.IsDir() || err != nil {
|
||||
return fmt.Errorf("[general] menus_dir %s is not a directory", dir)
|
||||
}
|
||||
for _, p := range files {
|
||||
name := strings.ToLower(strings.TrimSuffix(filepath.Base(p), ".menu"))
|
||||
mf, err := config.Load(p)
|
||||
if err != nil {
|
||||
return fmt.Errorf("menus_dir: %v", err)
|
||||
}
|
||||
m, err := bbs.ParseMenuFile(name, mf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("menus_dir: %s: %v", filepath.Base(p), err)
|
||||
}
|
||||
if c.Menus[m.Name] != nil {
|
||||
return fmt.Errorf("menus_dir: %s: the menu %s is also given as [menu %s] in the configuration", filepath.Base(p), m.Name, m.Name)
|
||||
}
|
||||
c.Menus[m.Name] = m
|
||||
where[m.Name] = filepath.Base(p)
|
||||
}
|
||||
}
|
||||
// what menus name must be there
|
||||
doors := map[string]bool{}
|
||||
for _, d := range c.Doors {
|
||||
doors[strings.ToLower(d.Name)] = true
|
||||
}
|
||||
hasMenu := func(name string) bool { return c.Menus[name] != nil || app.BuiltinMenu(name) != nil }
|
||||
for _, m := range c.Menus {
|
||||
at := where[m.Name]
|
||||
if m.Fail != "" && !hasMenu(m.Fail) {
|
||||
return fmt.Errorf("%s: fail = %s, but there is no menu %s", at, m.Fail, m.Fail)
|
||||
}
|
||||
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)
|
||||
}
|
||||
case "script":
|
||||
if c.ScriptsDir == "" {
|
||||
return fmt.Errorf("[menu %s] %s: script:%s, but no scripts_dir", m.Name, it.Key, it.Arg)
|
||||
}
|
||||
if !script.For(c.ScriptsDir).Has(it.Arg) {
|
||||
return fmt.Errorf("[menu %s] %s: there is no %s.js in %s", m.Name, it.Key, it.Arg, c.ScriptsDir)
|
||||
for _, cmd := range it.Do {
|
||||
if err := c.checkCommand(cmd, hasMenu, doors); err != nil {
|
||||
return fmt.Errorf("%s %s: %v", at, it.Key, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if m.Screen != "" && c.Screens == nil {
|
||||
return fmt.Errorf("[menu %s] screen = %s, but no screens_dir", m.Name, m.Screen)
|
||||
return fmt.Errorf("%s screen = %s, but no screens_dir", at, m.Screen)
|
||||
}
|
||||
}
|
||||
c.Text = map[string]string{}
|
||||
@@ -91,6 +106,32 @@ func (c *Config) customSections(cf *config.File) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkCommand: what a menu command names is there.
|
||||
func (c *Config) checkCommand(cmd bbs.Command, hasMenu func(string) bool, doors map[string]bool) error {
|
||||
switch bbs.Commands[cmd.Name].Arg {
|
||||
case bbs.MenuArg:
|
||||
if !hasMenu(cmd.Arg) {
|
||||
return fmt.Errorf("there is no [menu %s]", cmd.Arg)
|
||||
}
|
||||
case bbs.DoorArg:
|
||||
if !doors[strings.ToLower(cmd.Arg)] {
|
||||
return fmt.Errorf("there is no [door %s]", cmd.Arg)
|
||||
}
|
||||
case bbs.ScreenArg:
|
||||
if c.Screens == nil {
|
||||
return fmt.Errorf("screen:%s, but no screens_dir", cmd.Arg)
|
||||
}
|
||||
case bbs.ScriptArg:
|
||||
if c.ScriptsDir == "" {
|
||||
return fmt.Errorf("script:%s, but no scripts_dir", cmd.Arg)
|
||||
}
|
||||
if !script.For(c.ScriptsDir).Has(cmd.Arg) {
|
||||
return fmt.Errorf("there is no %s.js in %s", cmd.Arg, c.ScriptsDir)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// stringKeys is the names of the strings [strings] may set, sorted.
|
||||
func stringKeys() []string {
|
||||
var out []string
|
||||
|
||||
+19
-5
@@ -13,14 +13,15 @@ type choice struct{ key, label, help string }
|
||||
// menu shows a menu and reads its key: the title and items (unless list
|
||||
// is false: a screen of the sysop's showed them) in two columns, or one
|
||||
// on a narrow screen, or as buttons on a RIP terminal; then the prompt.
|
||||
// Enter is '\r'.
|
||||
func (s *session) menu(title string, items []choice, prompt string, list bool) (rune, error) {
|
||||
// Enter is "\r". With line, the caller types a line (a word, or one
|
||||
// character and Enter), not one key.
|
||||
func (s *session) menu(title string, items []choice, prompt string, list, line bool) (string, error) {
|
||||
t := s.t
|
||||
if t.RIP && list {
|
||||
// what the last choice printed is read before the buttons cover it
|
||||
if t.sinceKey > 0 {
|
||||
if err := t.Pause(); err != nil {
|
||||
return 0, err
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
ri := make([]rip.Item, len(items))
|
||||
@@ -60,9 +61,22 @@ func (s *session) menu(title string, items []choice, prompt string, list bool) (
|
||||
}
|
||||
t.Print(prompt)
|
||||
}
|
||||
if line {
|
||||
l, err := t.Input("", 12)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if t.ripScene {
|
||||
t.RIPClear()
|
||||
}
|
||||
if l = strings.TrimSpace(l); l == "" {
|
||||
return "\r", nil
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
k, err := t.Key()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return "", err
|
||||
}
|
||||
if t.ripScene {
|
||||
t.RIPClear()
|
||||
@@ -71,5 +85,5 @@ func (s *session) menu(title string, items []choice, prompt string, list bool) (
|
||||
} else {
|
||||
t.Println("")
|
||||
}
|
||||
return k, nil
|
||||
return string(k), nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package textui
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/D-Modem/bbs/internal/view"
|
||||
)
|
||||
|
||||
// TestMenuKeys: a hidden key is taken but not shown; a key that is not
|
||||
// the menu's asks again; with Line, the caller types a word and Enter.
|
||||
func TestMenuKeys(t *testing.T) {
|
||||
m := &view.Menu{Title: "Main", Prompt: "Choice: ", Hidden: []string{"Q"},
|
||||
Items: []view.Item{{Key: "M", Label: "Messages"}}}
|
||||
s, c := deskTerm("zq", false)
|
||||
if k, err := s.Menu(m); err != nil || k != "Q" {
|
||||
t.Fatalf("%q %v", k, err)
|
||||
}
|
||||
if out := c.out.String(); !strings.Contains(out, "Messages") || strings.Count(out, "Choice: ") != 2 {
|
||||
t.Errorf("%q", out)
|
||||
}
|
||||
m.Line = true
|
||||
m.Items = append(m.Items, view.Item{Key: "JOIN", Label: "Join"})
|
||||
s, _ = deskTerm("nope\rjoin\r", false)
|
||||
if k, err := s.Menu(m); err != nil || k != "JOIN" {
|
||||
t.Fatalf("%q %v", k, err)
|
||||
}
|
||||
s, _ = deskTerm("\r", false)
|
||||
if k, err := s.Menu(m); err != nil || k != "" {
|
||||
t.Fatalf("Enter: %q %v", k, err)
|
||||
}
|
||||
}
|
||||
@@ -142,21 +142,19 @@ func (s *session) Menu(m *view.Menu) (string, error) {
|
||||
prompt = p + " (? for the menu) "
|
||||
}
|
||||
}
|
||||
k, err := s.menu(m.Title, items, "\n"+prompt, !shown && !brief)
|
||||
if err == nil && brief && k == '?' {
|
||||
k, err = s.menu(m.Title, items, "\n"+prompt, true)
|
||||
k, err := s.menu(m.Title, items, "\n"+prompt, !shown && !brief, m.Line)
|
||||
if err == nil && brief && k == "?" {
|
||||
k, err = s.menu(m.Title, items, "\n"+prompt, true, m.Line)
|
||||
}
|
||||
for {
|
||||
if err != nil || k == '\r' {
|
||||
if err != nil || k == "\r" {
|
||||
return "", err
|
||||
}
|
||||
for _, it := range m.Items {
|
||||
if strings.EqualFold(it.Key, string(k)) {
|
||||
return it.Key, nil
|
||||
}
|
||||
if key, ok := m.Has(k); ok {
|
||||
return key, nil
|
||||
}
|
||||
// not an item's: asked again, as a 3270 does
|
||||
k, err = s.menu(m.Title, items, "\n"+prompt, false)
|
||||
k, err = s.menu(m.Title, items, "\n"+prompt, false, m.Line)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -170,7 +170,11 @@ func (u *ui) Menu(m *view.Menu) (string, error) {
|
||||
prompt = bbs.Trunc(screens.Unmark(m.Prompt), 20) // filled in already, tags marked for a text terminal
|
||||
}
|
||||
scr = append(scr, u.text(u.rows-3, 2, prompt, go3270.Turquoise, false))
|
||||
scr = append(scr, u.input(u.rows-3, 4+len([]rune(prompt)), 2, "opt", false)...)
|
||||
width := 2
|
||||
if m.Line {
|
||||
width = 12 // a word
|
||||
}
|
||||
scr = append(scr, u.input(u.rows-3, 4+len([]rune(prompt)), width, "opt", false)...)
|
||||
resp, v, err := u.show(scr, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -186,10 +190,8 @@ func (u *ui) Menu(m *view.Menu) (string, error) {
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
for _, it := range m.Items {
|
||||
if strings.EqualFold(it.Key, key) {
|
||||
return it.Key, nil
|
||||
}
|
||||
if k, ok := m.Has(key); ok {
|
||||
return k, nil
|
||||
}
|
||||
msg = "No such option."
|
||||
}
|
||||
|
||||
+21
-1
@@ -68,7 +68,7 @@ const (
|
||||
|
||||
// Item is a menu's choice.
|
||||
type Item struct {
|
||||
Key string // one character
|
||||
Key string // one character, or a word (Menu.Line)
|
||||
Label string
|
||||
Help string // what it does, beside it where the screen has room ("": none)
|
||||
}
|
||||
@@ -84,6 +84,26 @@ type Menu struct {
|
||||
// Brief: the prompt alone, the items when the caller asks (?): an
|
||||
// expert's menus
|
||||
Brief bool
|
||||
// Hidden are keys that are not shown but may be chosen
|
||||
Hidden []string
|
||||
// Line: the caller types a line (a key may be a word), not one key
|
||||
Line bool
|
||||
}
|
||||
|
||||
// Has is whether key (any case) is a key of the menu's, shown or hidden;
|
||||
// it is that key as the menu gives it.
|
||||
func (m *Menu) Has(key string) (string, bool) {
|
||||
for _, it := range m.Items {
|
||||
if strings.EqualFold(it.Key, key) {
|
||||
return it.Key, true
|
||||
}
|
||||
}
|
||||
for _, k := range m.Hidden {
|
||||
if strings.EqualFold(k, key) {
|
||||
return k, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Column is a table's column.
|
||||
|
||||
Reference in new issue
Block a user