mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
Customization: screens of the sysop's, menus, @-codes, strings
screens_dir holds display files by name: NAME.ans (ANSI art in CP437,
SAUCE dropped; UTF-8 to UTF-8 terminals, CP437 bytes to the rest),
NAME.asc/.txt (text, paged) and NAME.3270 (text with {color} tags).
logon, newuser, welcome and goodbye are shown by themselves, on text
terminals and 3270s; others by menus. @-codes (@USER@, @NEWMAIL@,
@NODE@, @ONLINE@ ... with @CODE:20@ widths, @CLS@, @PAUSE@) are filled
in, their values cleaned.
[menu NAME] sections: KEY = ACTION Label, actions the built-in parts or
door:, menu:, screen:, area:; [menu main] replaces the built-in main menu
on both kinds of terminal, with an optional screen in place of its list.
[strings]: the login and command prompts, the pause, the logoff question
and the welcome. bbs check refuses menus naming what is not there.
screens.sample: a set of screens to start from. Tested: the screens
package; menus and strings in the configuration; a text caller through
ANSI art, strings, menus and screens; a 3270 (s3270) through colored
screens and the sysop's main menu. Wiki page Customization.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
This commit is contained in:
35 files changed
+1671
-42
No files matched your search
@@ -66,6 +66,7 @@ What there is:
|
||||
and the usual drop files, or any Linux program, for text callers
|
||||
- [chat](https://github.com/Neo-Desktop/bbs-sec/wiki/Chat-and-Paging): rooms, messages to a node, paging the sysop
|
||||
- [finger and gopher](https://github.com/Neo-Desktop/bbs-sec/wiki/Finger-and-Gopher): who is on, and chosen areas in gopherspace
|
||||
- [customization](https://github.com/Neo-Desktop/bbs-sec/wiki/Customization): ANSI art and 3270 screens, menus of the sysop's, @-codes, the prompts' words
|
||||
- new users sign themselves up (or not: `new_users = no`)
|
||||
- SQLite storage, one file
|
||||
|
||||
@@ -96,6 +97,7 @@ The documentation is in the [wiki](https://github.com/Neo-Desktop/bbs-sec/wiki/)
|
||||
| [Running](https://github.com/Neo-Desktop/bbs-sec/wiki/Running) | first start, systemd, the defaults |
|
||||
| [Administration](https://github.com/Neo-Desktop/bbs-sec/wiki/Administration) | the bbs command: users, areas, files, mail exchange |
|
||||
| [Configuration](https://github.com/Neo-Desktop/bbs-sec/wiki/Configuration) | bbs.conf: its sections, listeners |
|
||||
| [Customization](https://github.com/Neo-Desktop/bbs-sec/wiki/Customization) | screens (ANSI art, 3270), menus, prompts |
|
||||
| [Modem callers](https://github.com/Neo-Desktop/bbs-sec/wiki/Modem-Callers) | app_dmodem and modemmenu |
|
||||
| [TN3270](https://github.com/Neo-Desktop/bbs-sec/wiki/TN3270) | 3270 terminals, PF keys, 3270 graphics |
|
||||
| [Sixel graphics](https://github.com/Neo-Desktop/bbs-sec/wiki/Sixel-Graphics) | the logo on sixel terminals |
|
||||
|
||||
@@ -22,6 +22,10 @@ log = syslog
|
||||
; each login)
|
||||
;motd = "The line is down for maintenance on Sunday."
|
||||
;motd_file = /etc/bbs/motd.txt
|
||||
; 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
|
||||
;screens_dir = /etc/bbs/screens
|
||||
; 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
|
||||
@@ -282,3 +286,43 @@ address = 127.0.0.1:2513
|
||||
;areas = general,announcements
|
||||
; file areas listed and downloadable
|
||||
;files = no
|
||||
|
||||
; 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
|
||||
; Labels, titles and prompts take @-codes (@USER@, @NEWMAIL@, @BBS@ ...).
|
||||
; screen = NAME shows that display file instead of the list of items
|
||||
;[menu main]
|
||||
;title = @BBS@
|
||||
;screen = mainmenu
|
||||
;m = areas Message areas
|
||||
;e = mail E-mail (@NEWMAIL@ new)
|
||||
;f = files File areas
|
||||
;d = doors Doors
|
||||
;c = chat Chat
|
||||
;s = page Page the sysop
|
||||
;w = who Who's online
|
||||
;b = screen:bulletins Bulletins
|
||||
;x = menu:extras Extras
|
||||
;g = logoff Goodbye
|
||||
;
|
||||
;[menu extras]
|
||||
;title = Extras
|
||||
;prompt = "Extras> "
|
||||
;u = users User list
|
||||
;p = profile Your profile
|
||||
;i = info System info
|
||||
;t = terminal Your terminal (3270)
|
||||
;q = back Back
|
||||
|
||||
; The prompts and messages, with @-codes:
|
||||
;[strings]
|
||||
;name_prompt = "Name: "
|
||||
;new_name_prompt = "Name (or NEW): "
|
||||
;password_prompt = "Password: "
|
||||
;command_prompt = "Command: "
|
||||
;pause = "[Press a key]"
|
||||
;logoff_confirm = "Log off?"
|
||||
;welcome = "Welcome, @USER@."
|
||||
@@ -136,6 +136,16 @@ func main() {
|
||||
fmt.Printf("[link %s] %s, host %q, echoes %s, route %v\n", l.Name, l.Addr, l.Host, l.Echoes, l.Route)
|
||||
}
|
||||
}
|
||||
if cfg.Screens != nil {
|
||||
fmt.Println("screens:", cfg.Screens.Dir)
|
||||
}
|
||||
for _, m := range cfg.Menus {
|
||||
keys := ""
|
||||
for _, it := range m.Items {
|
||||
keys += it.Key
|
||||
}
|
||||
fmt.Printf("[menu %s] %q, keys %s\n", m.Name, m.Title, keys)
|
||||
}
|
||||
if cfg.Finger != nil {
|
||||
fmt.Printf("finger: %s (online %v, users %v)\n", cfg.Finger.Address, cfg.Finger.Online, cfg.Finger.Users)
|
||||
}
|
||||
@@ -229,6 +239,7 @@ func serve(cfg *server.Config) int {
|
||||
b := bbs.New(cfg.Settings, st, lg)
|
||||
b.News = cfg.News
|
||||
b.Doors = cfg.Doors
|
||||
b.Screens, b.Menus, b.Text = cfg.Screens, cfg.Menus, cfg.Text
|
||||
if b.Files, err = files.Open(st, cfg.FilesDir, cfg.MaxFileSize, cfg.UploadQuota); err != nil {
|
||||
lg.Printf("files: %v", err)
|
||||
return 1
|
||||
|
||||
@@ -23,6 +23,7 @@ yes`), and with `trust_user = yes` take the caller as logged in already: see
|
||||
| `[news]`, `[nntp]`, `[peer NAME]` | newsgroups, the NNTP server, news peers | [Newsgroups and NNTP](Newsgroups-and-NNTP) |
|
||||
| `[fido]`, `[binkp]`, `[link NAME]` | FidoNet: our address, the binkp mailer, links | [FidoNet](FidoNet) |
|
||||
| `[door NAME]` | a door program | [Doors](Doors) |
|
||||
| `screens_dir` (in `[general]`), `[menu NAME]`, `[strings]` | the sysop's screens, menus and prompts | [Customization](Customization) |
|
||||
| `[finger]`, `[gopher]` | the finger and gopher servers | [Finger and gopher](Finger-and-Gopher) |
|
||||
| `page_command` (in `[general]`) | telling the sysop of a page when they are not on | [Chat and paging](Chat-and-Paging) |
|
||||
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
The BBS's look is the sysop's: screens of their own (ANSI art, text, 3270
|
||||
screens in color), menus of their own, and the prompts' words. None of it
|
||||
is needed: without it the built-in screens and menus are used.
|
||||
|
||||
## Screens
|
||||
|
||||
Display files go in one directory:
|
||||
|
||||
```
|
||||
[general]
|
||||
screens_dir = /etc/bbs/screens
|
||||
```
|
||||
|
||||
A screen is a name (lower case letters, digits, `_` and `-`) and one or
|
||||
more files for different terminals:
|
||||
|
||||
| file | what | shown to |
|
||||
|---|---|---|
|
||||
| `NAME.ans` | ANSI art in CP437, as ANSI editors (PabloDraw, Moebius, TheDraw) save it; a SAUCE record after ^Z is dropped | ANSI terminals |
|
||||
| `NAME.asc` | plain text (UTF-8 or ASCII), a page at a time | any text terminal; 3270s when there is no `.3270` |
|
||||
| `NAME.txt` | the same as `.asc` | the same |
|
||||
| `NAME.3270` | text with color tags (below) | 3270s |
|
||||
|
||||
An ANSI terminal gets `.ans`, else `.asc`, else `.txt`; a plain one `.asc`
|
||||
or `.txt`; a 3270 `.3270`, `.asc` or `.txt`. ANSI art goes to a UTF-8
|
||||
terminal as UTF-8, and to any other as the CP437 bytes it is (what
|
||||
SyncTERM, NetRunner and DOS terminal programs want). Files are read
|
||||
each time they are shown, so they can be changed while the BBS runs; the
|
||||
largest read is 256 KB.
|
||||
|
||||
These are shown by themselves:
|
||||
|
||||
| name | when | in place of |
|
||||
|---|---|---|
|
||||
| `logon` | when a caller connects, before the name prompt (on a 3270, in the logon screen's upper part) | the system's name, banner and how they came |
|
||||
| `newuser` | before a new user's questions | (added) |
|
||||
| `welcome` | after the login | the welcome lines (the message of the day still follows) |
|
||||
| `goodbye` | at a logoff | the `goodbye` line |
|
||||
|
||||
Others are shown by menus: `screen = NAME` on a menu, or a `screen:NAME`
|
||||
item. [screens.sample](https://github.com/Neo-Desktop/bbs-sec/tree/HEAD/screens.sample)
|
||||
has a set to start from.
|
||||
|
||||
### @-codes
|
||||
|
||||
Screens, menu titles, labels and prompts, and the strings take codes
|
||||
that are filled in when shown:
|
||||
|
||||
| code | |
|
||||
|---|---|
|
||||
| `@BBS@` `@SYSOP@` `@LOCATION@` | the system |
|
||||
| `@USER@` `@USERLOC@` | the caller's name, where they call from |
|
||||
| `@CALLS@` `@POSTS@` `@LASTON@` `@SINCE@` | their calls, posts, last call, when they joined |
|
||||
| `@NEWMAIL@` | their unread mail |
|
||||
| `@NODE@` `@VIA@` `@BAUD@` | this call: node, how (telnet, modem, tn3270), a modem's rate |
|
||||
| `@ONLINE@` | callers on now |
|
||||
| `@DATE@` `@TIME@` | now |
|
||||
| `@CLS@` | clear the screen |
|
||||
| `@PAUSE@` | wait for a key (on a 3270, a new page) |
|
||||
|
||||
`@CODE:20@` pads (or cuts) the value to 20 characters, `@CODE:-20@`
|
||||
aligns it right: art keeps its shape whatever a name's length. Anything
|
||||
else between @s (an e-mail address) is left as it is. The values are
|
||||
cleaned like anything a caller writes: no escape sequences get into art
|
||||
through a user's name.
|
||||
|
||||
### 3270 screens
|
||||
|
||||
A `.3270` file is lines of text with tags that change the color for the
|
||||
rest of the line:
|
||||
|
||||
```
|
||||
{turquoise}{bright}*** @BBS@ ***
|
||||
{blue}A bulletin board, by telnet, modem and TN3270.{default} plain again
|
||||
```
|
||||
|
||||
`{blue}` `{red}` `{pink}` `{green}` `{turquoise}` `{yellow}` `{white}`
|
||||
`{default}`, and `{bright}` / `{normal}`. On a 3270 each change of color
|
||||
takes a blank column (an attribute byte, as 3270 fields work).
|
||||
|
||||
## Menus
|
||||
|
||||
`[menu NAME]` sections make menus; `[menu main]` takes the place of the
|
||||
built-in main menu, on text terminals and 3270s alike, and the others are
|
||||
reached from it (and from each other).
|
||||
|
||||
```
|
||||
[menu main]
|
||||
title = @BBS@
|
||||
screen = mainmenu ; shown instead of the list of items (optional)
|
||||
prompt = "Command: " ; (optional)
|
||||
m = areas Message areas
|
||||
e = mail E-mail (@NEWMAIL@ new)
|
||||
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
|
||||
q = logoff Goodbye
|
||||
|
||||
[menu extras]
|
||||
title = Extras
|
||||
u = users User list
|
||||
z = back Back
|
||||
```
|
||||
|
||||
Each item is `KEY = ACTION Label`: one character (not `;` or `#`, which
|
||||
begin comments), then what it does:
|
||||
|
||||
| action | |
|
||||
|---|---|
|
||||
| `areas` `mail` `who` `users` `profile` `info` `files` `doors` `chat` `page` | the built-in parts |
|
||||
| `terminal` | a 3270's terminal details |
|
||||
| `logoff` | log off (asking first on a text terminal) |
|
||||
| `back` | back to the menu before |
|
||||
| `door:NAME` | straight into a door |
|
||||
| `menu:NAME` | another menu |
|
||||
| `screen:NAME` | show a screen |
|
||||
| `area:NAME` | straight into a message area |
|
||||
|
||||
With `screen =`, a text terminal sees the screen (which shows the keys
|
||||
itself), and a 3270 the screen's `.3270` (or `.asc`, `.txt`) version if
|
||||
it has one, else the list of items in a box: a menu drawn as ANSI art
|
||||
alone still works on a 3270. On a 3270 the key is typed in the option field;
|
||||
PF3 goes back (on the main menu, logs off if a `logoff` item is there).
|
||||
Doors are for text terminals: on a 3270, a door item says so. `bbs
|
||||
check` refuses a menu that names a menu, door or screen that is not
|
||||
there, and lists the menus it read.
|
||||
|
||||
## Strings
|
||||
|
||||
```
|
||||
[strings]
|
||||
name_prompt = "Handle: "
|
||||
new_name_prompt = "Handle (or NEW): "
|
||||
password_prompt = "Password: "
|
||||
command_prompt = "@USER@ @ @BBS@ > "
|
||||
pause = "-- more --"
|
||||
logoff_confirm = "Leaving so soon, @USER@?"
|
||||
welcome = "Hi @USER@, @NEWMAIL@ new letters."
|
||||
```
|
||||
|
||||
Each takes @-codes; quote a value to keep spaces at its end. `welcome`
|
||||
is the 3270 main menu's greeting too.
|
||||
|
||||
**See also:** [Configuration](Configuration), [Doors](Doors)
|
||||
@@ -19,6 +19,7 @@
|
||||
| `internal/finger` | the finger server |
|
||||
| `internal/gopher` | the gopher server |
|
||||
| `internal/tcpserv` | what finger and gopher share: limits, timeouts |
|
||||
| `internal/screens` | the sysop's display files: finding them, @-codes, 3270 color tags |
|
||||
| `internal/xfer` | XMODEM, YMODEM, ZMODEM and Kermit, both ways |
|
||||
| `internal/logo` | the logo: a PNG, or the name in a pixel font |
|
||||
| `internal/bbs` | the BBS itself: nodes, logins, new users, posting, cleaning text |
|
||||
@@ -65,6 +66,11 @@ and answering, the page command (and that it runs no shell); two callers
|
||||
chatting and paging on the text BBS; a 3270 (s3270) chatting with a text
|
||||
caller and getting a node message on its message line. Finger and
|
||||
gopher: their answers and refusals, with the finger program and curl as
|
||||
clients.
|
||||
clients. Customization: finding display files by terminal, SAUCE,
|
||||
@-codes and widths, color tags; menus and strings in the configuration
|
||||
and what it refuses; a text caller through an ANSI logon (CP437 as it
|
||||
is), the strings, a welcome screen, the sysop's menus and submenus, a
|
||||
screen with a pause and a goodbye screen; and a 3270 (s3270) through a
|
||||
colored logon screen, welcome, the sysop's main menu and a screen.
|
||||
|
||||
**See also:** [Building](Building)
|
||||
@@ -64,6 +64,7 @@ What there is:
|
||||
and the usual drop files, or any Linux program, for text callers
|
||||
- [chat](Chat-and-Paging): rooms, messages to a node, paging the sysop
|
||||
- [finger and gopher](Finger-and-Gopher): who is on, and chosen areas in gopherspace
|
||||
- [customization](Customization): ANSI art and 3270 screens, menus of the sysop's, @-codes, the prompts' words
|
||||
- new users sign themselves up (or not: `new_users = no`)
|
||||
- SQLite storage, one file
|
||||
|
||||
@@ -75,6 +76,7 @@ What there is:
|
||||
| [Running](Running) | first start, systemd, the defaults |
|
||||
| [Administration](Administration) | the `bbs` command: users, areas, files, mail exchange |
|
||||
| [Configuration](Configuration) | `bbs.conf`: its sections, listeners |
|
||||
| [Customization](Customization) | screens (ANSI art, 3270), menus, prompts |
|
||||
| [Modem callers](Modem-Callers) | app_dmodem and modemmenu |
|
||||
| [TN3270](TN3270) | 3270 terminals, PF keys, 3270 graphics |
|
||||
| [Sixel graphics](Sixel-Graphics) | the logo on sixel terminals |
|
||||
|
||||
@@ -53,5 +53,9 @@
|
||||
check` warns when they do), and its selectors are database records, not
|
||||
paths. Both limit connections at once and from one address, and time
|
||||
out.
|
||||
- Screens: display files are read only from `screens_dir`, by names of
|
||||
letters, digits, `_` and `-` (no paths), at most 256 KB. The values of
|
||||
@-codes are cleaned, so no escape sequence gets into a screen through
|
||||
a user's name or location.
|
||||
- The log has logins, failures, posts and mail sent (never passwords or
|
||||
message text).
|
||||
@@ -5,6 +5,7 @@
|
||||
- [Running](Running)
|
||||
- [Administration](Administration)
|
||||
- [Configuration](Configuration)
|
||||
- [Customization](Customization)
|
||||
|
||||
**Callers**
|
||||
- [Modem callers](Modem-Callers)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/door"
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/screens"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
@@ -107,6 +108,10 @@ type BBS struct {
|
||||
Netmail NetmailFunc
|
||||
// Doors are the door programs (for text callers)
|
||||
Doors []*door.Door
|
||||
// the sysop's screens, menus and strings
|
||||
Screens *screens.Set
|
||||
Menus map[string]*Menu
|
||||
Text map[string]string
|
||||
|
||||
mu sync.Mutex
|
||||
nodes map[int]*Node
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
package bbs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/screens"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// Menu is a sysop's menu ([menu NAME]); "main" replaces the built-in one.
|
||||
type Menu struct {
|
||||
Name string
|
||||
Title string // with @-codes
|
||||
Screen string // a display file shown instead of the item list ("": none)
|
||||
Prompt string // ("": the command prompt)
|
||||
Items []MenuItem
|
||||
}
|
||||
|
||||
// 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",
|
||||
"doors": "the doors",
|
||||
"chat": "chat",
|
||||
"page": "page the sysop",
|
||||
"terminal": "a 3270's terminal details (nothing elsewhere)",
|
||||
"back": "back to the menu before",
|
||||
"logoff": "log off",
|
||||
}
|
||||
|
||||
// Strings are the prompts and messages a sysop may change ([strings]),
|
||||
// with their defaults; @-codes are filled in.
|
||||
var Strings = map[string]string{
|
||||
"name_prompt": "Name: ",
|
||||
"new_name_prompt": "Name (or NEW): ",
|
||||
"password_prompt": "Password: ",
|
||||
"command_prompt": "Command: ",
|
||||
"pause": "[Press a key]",
|
||||
"logoff_confirm": "Log off?",
|
||||
"welcome": "Welcome, @USER@.",
|
||||
}
|
||||
|
||||
// Str is a string, the sysop's or the default, its @-codes filled in for
|
||||
// the node; and whether the sysop set it.
|
||||
func (n *Node) Str(key string) (string, bool) {
|
||||
v, set := n.b.Text[key]
|
||||
if !set {
|
||||
v = Strings[key]
|
||||
}
|
||||
return screens.String(v, n.Vars()), set
|
||||
}
|
||||
|
||||
// Vars are the @-codes' values for the node's caller:
|
||||
//
|
||||
// BBS SYSOP LOCATION the system
|
||||
// USER USERLOC CALLS POSTS the caller
|
||||
// LASTON SINCE NEWMAIL
|
||||
// NODE VIA BAUD ONLINE this call, and how many are on
|
||||
// DATE TIME
|
||||
func (n *Node) Vars() screens.Vars {
|
||||
return func(code string) (string, bool) {
|
||||
u := n.User()
|
||||
v := ""
|
||||
switch code {
|
||||
case "BBS":
|
||||
v = n.b.Name
|
||||
case "SYSOP":
|
||||
v = n.b.Sysop
|
||||
case "LOCATION":
|
||||
v = n.b.Location
|
||||
case "NODE":
|
||||
v = fmt.Sprint(n.Num)
|
||||
case "VIA":
|
||||
v = n.Caller.Via
|
||||
case "BAUD":
|
||||
v = n.Caller.Baud()
|
||||
case "ONLINE":
|
||||
on := 0
|
||||
for _, x := range n.b.Nodes() {
|
||||
if x.User != "" {
|
||||
on++
|
||||
}
|
||||
}
|
||||
v = fmt.Sprint(on)
|
||||
case "DATE":
|
||||
v = time.Now().Format("2006-01-02")
|
||||
case "TIME":
|
||||
v = time.Now().Format("15:04")
|
||||
case "USER", "USERLOC", "CALLS", "POSTS", "LASTON", "SINCE", "NEWMAIL":
|
||||
if u == nil {
|
||||
return "", true
|
||||
}
|
||||
switch code {
|
||||
case "USER":
|
||||
v = u.Name
|
||||
case "USERLOC":
|
||||
v = u.Location
|
||||
case "CALLS":
|
||||
v = fmt.Sprint(u.Logins)
|
||||
case "POSTS":
|
||||
v = fmt.Sprint(u.Posts)
|
||||
case "LASTON":
|
||||
if u.LastLogin.IsZero() {
|
||||
v = "never"
|
||||
} else {
|
||||
v = u.LastLogin.Format("2006-01-02 15:04")
|
||||
}
|
||||
case "SINCE":
|
||||
v = u.Created.Format("2006-01-02")
|
||||
case "NEWMAIL":
|
||||
c, _ := n.b.Store.UnreadMail(u.ID)
|
||||
v = fmt.Sprint(c)
|
||||
}
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
return Clean(v, 200), true
|
||||
}
|
||||
}
|
||||
|
||||
// ParseMenuItem reads a menu line: "ACTION Label words".
|
||||
func ParseMenuItem(key, value string) (MenuItem, error) {
|
||||
k := []rune(strings.ToUpper(key))
|
||||
if len(k) != 1 || k[0] < '!' || k[0] > '~' {
|
||||
return MenuItem{}, fmt.Errorf("%q: a menu key is one character", key)
|
||||
}
|
||||
action, label, _ := strings.Cut(strings.TrimSpace(value), " ")
|
||||
it := MenuItem{Key: string(k), Label: strings.TrimSpace(label)}
|
||||
if a, arg, ok := strings.Cut(action, ":"); ok {
|
||||
switch a {
|
||||
case "door", "menu", "area":
|
||||
case "screen":
|
||||
if !screens.ValidName(arg) {
|
||||
return MenuItem{}, fmt.Errorf("%s: a screen's name is lower case letters, digits, _ and -", action)
|
||||
}
|
||||
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 {
|
||||
return MenuItem{}, fmt.Errorf("%q: no such action", action)
|
||||
}
|
||||
it.Action = action
|
||||
}
|
||||
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.
|
||||
func (m *Menu) Item(key string) *MenuItem {
|
||||
key = strings.ToUpper(strings.TrimSpace(key))
|
||||
for i := range m.Items {
|
||||
if m.Items[i].Key == key {
|
||||
return &m.Items[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -241,3 +241,11 @@ func (s *Section) Seconds(key string, def, min, max int) (time.Duration, error)
|
||||
n, err := s.Int(key, def, min, max)
|
||||
return time.Duration(n) * time.Second, err
|
||||
}
|
||||
|
||||
// Keys lists the section's keys in the order they came.
|
||||
func (s *Section) Keys() []string {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
return append([]string(nil), s.ord...)
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
// Package screens is the sysop's own screens: display files in a
|
||||
// directory, chosen by name and by the caller's terminal, with @-codes
|
||||
// filled in.
|
||||
//
|
||||
// NAME.ans ANSI art, in CP437 (a SAUCE record after ^Z is dropped)
|
||||
// NAME.asc plain text (UTF-8 or ASCII), paged
|
||||
// NAME.txt the same
|
||||
// NAME.3270 text for 3270s, with {color} tags
|
||||
//
|
||||
// An ANSI terminal gets .ans, else .asc, else .txt; a plain one .asc or
|
||||
// .txt; a 3270 .3270, .asc or .txt.
|
||||
package screens
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/text/encoding/charmap"
|
||||
)
|
||||
|
||||
// Kind is the caller's terminal.
|
||||
type Kind int
|
||||
|
||||
// Kinds of terminal.
|
||||
const (
|
||||
Plain Kind = iota
|
||||
ANSI
|
||||
TN3270
|
||||
)
|
||||
|
||||
// Format is a display file's.
|
||||
type Format int
|
||||
|
||||
// Formats of display files.
|
||||
const (
|
||||
Text Format = iota // .asc, .txt
|
||||
Art // .ans
|
||||
Color // .3270
|
||||
)
|
||||
|
||||
// MaxSize is the largest display file read.
|
||||
const MaxSize = 256 << 10
|
||||
|
||||
// Set is a directory of screens ("": none).
|
||||
type Set struct{ Dir string }
|
||||
|
||||
var validName = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,39}$`)
|
||||
|
||||
// ValidName checks a screen's name: lower case letters, digits, _ and -.
|
||||
func ValidName(name string) bool { return validName.MatchString(name) }
|
||||
|
||||
// Screen is a display file read.
|
||||
type Screen struct {
|
||||
Name string
|
||||
Format Format
|
||||
Text string // as UTF-8 (an .ans decoded from CP437)
|
||||
}
|
||||
|
||||
// Find reads the screen of that name for the terminal; false if there is
|
||||
// none (or no directory).
|
||||
func (s *Set) Find(name string, kind Kind) (*Screen, bool) {
|
||||
if s == nil || s.Dir == "" || !ValidName(name) {
|
||||
return nil, false
|
||||
}
|
||||
var tries []string
|
||||
switch kind {
|
||||
case ANSI:
|
||||
tries = []string{".ans", ".asc", ".txt"}
|
||||
case Plain:
|
||||
tries = []string{".asc", ".txt"}
|
||||
case TN3270:
|
||||
tries = []string{".3270", ".asc", ".txt"}
|
||||
}
|
||||
for _, ext := range tries {
|
||||
b, err := readFile(filepath.Join(s.Dir, name+ext))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
sc := &Screen{Name: name}
|
||||
switch ext {
|
||||
case ".ans":
|
||||
sc.Format = Art
|
||||
sc.Text = decodeCP437(stripSAUCE(b))
|
||||
case ".3270":
|
||||
sc.Format = Color
|
||||
sc.Text = strings.ToValidUTF8(string(b), "?")
|
||||
default:
|
||||
sc.Text = strings.ToValidUTF8(string(b), "?")
|
||||
}
|
||||
sc.Text = strings.ReplaceAll(sc.Text, "\r\n", "\n")
|
||||
return sc, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func readFile(path string) ([]byte, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
fi, err := f.Stat()
|
||||
if err != nil || !fi.Mode().IsRegular() {
|
||||
return nil, errors.New("not a file")
|
||||
}
|
||||
if fi.Size() > MaxSize {
|
||||
return nil, errors.New("too big")
|
||||
}
|
||||
b := make([]byte, fi.Size())
|
||||
n, err := f.Read(b)
|
||||
return b[:n], err
|
||||
}
|
||||
|
||||
// stripSAUCE drops what follows ^Z: a SAUCE record, comments.
|
||||
func stripSAUCE(b []byte) []byte {
|
||||
for i, c := range b {
|
||||
if c == 0x1a {
|
||||
return b[:i]
|
||||
}
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func decodeCP437(b []byte) string {
|
||||
var sb strings.Builder
|
||||
for _, c := range b {
|
||||
if c < 0x80 {
|
||||
sb.WriteByte(c)
|
||||
} else {
|
||||
sb.WriteRune(charmap.CodePage437.DecodeByte(c))
|
||||
}
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// EncodeCP437 turns text back into CP437 bytes for a CP437 terminal
|
||||
// ('?' for what CP437 lacks).
|
||||
func EncodeCP437(s string) []byte {
|
||||
out := make([]byte, 0, len(s))
|
||||
for _, r := range s {
|
||||
if r < 0x80 {
|
||||
out = append(out, byte(r))
|
||||
} else if c, ok := charmap.CodePage437.EncodeRune(r); ok {
|
||||
out = append(out, c)
|
||||
} else {
|
||||
out = append(out, '?')
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Vars gives the value of an @-code (USER, NODE, ...), false if it is not
|
||||
// one.
|
||||
type Vars func(code string) (string, bool)
|
||||
|
||||
// Piece is part of a screen: text, or a clear or a pause.
|
||||
type Piece struct {
|
||||
Text string
|
||||
Clear bool
|
||||
Pause bool
|
||||
}
|
||||
|
||||
// @CODE@ or @CODE:WIDTH@ (negative: right-aligned)
|
||||
var code = regexp.MustCompile(`@([A-Z][A-Z0-9]*)(?::(-?[0-9]{1,3}))?@`)
|
||||
|
||||
// Expand fills in a text's @-codes, and splits it at @CLS@ and @PAUSE@.
|
||||
// A code that is not one stays as it was.
|
||||
func Expand(text string, vars Vars) []Piece {
|
||||
var out []Piece
|
||||
var cur strings.Builder
|
||||
last := 0
|
||||
for _, m := range code.FindAllStringSubmatchIndex(text, -1) {
|
||||
cur.WriteString(text[last:m[0]])
|
||||
last = m[1]
|
||||
name := text[m[2]:m[3]]
|
||||
switch name {
|
||||
case "CLS", "PAUSE":
|
||||
if cur.Len() > 0 {
|
||||
out = append(out, Piece{Text: cur.String()})
|
||||
cur.Reset()
|
||||
}
|
||||
out = append(out, Piece{Clear: name == "CLS", Pause: name == "PAUSE"})
|
||||
continue
|
||||
}
|
||||
v, ok := "", false
|
||||
if vars != nil {
|
||||
v, ok = vars(name)
|
||||
}
|
||||
if !ok {
|
||||
cur.WriteString(text[m[0]:m[1]])
|
||||
continue
|
||||
}
|
||||
if m[4] >= 0 {
|
||||
w, _ := strconv.Atoi(text[m[4]:m[5]])
|
||||
v = pad(v, w)
|
||||
}
|
||||
cur.WriteString(v)
|
||||
}
|
||||
cur.WriteString(text[last:])
|
||||
if cur.Len() > 0 {
|
||||
out = append(out, Piece{Text: cur.String()})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// String is Expand's text alone (clears and pauses dropped).
|
||||
func String(text string, vars Vars) string {
|
||||
var b strings.Builder
|
||||
for _, p := range Expand(text, vars) {
|
||||
b.WriteString(p.Text)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// pad fits v to |w| runes: cut, or padded with spaces (on the left if w
|
||||
// is negative).
|
||||
func pad(v string, w int) string {
|
||||
right := w < 0
|
||||
if right {
|
||||
w = -w
|
||||
}
|
||||
if w == 0 {
|
||||
return v
|
||||
}
|
||||
n := utf8.RuneCountInString(v)
|
||||
if n > w {
|
||||
return string([]rune(v)[:w])
|
||||
}
|
||||
if right {
|
||||
return strings.Repeat(" ", w-n) + v
|
||||
}
|
||||
return v + strings.Repeat(" ", w-n)
|
||||
}
|
||||
|
||||
// Span is a run of a .3270 line in one color.
|
||||
type Span struct {
|
||||
Text string
|
||||
Color string // blue red pink green turquoise yellow white ("": default)
|
||||
Bright bool
|
||||
}
|
||||
|
||||
var tag = regexp.MustCompile(`\{(blue|red|pink|green|turquoise|yellow|white|default|bright|normal)\}`)
|
||||
|
||||
// Spans splits a .3270 line at its {color} tags: {blue} ... {white},
|
||||
// {default}, and {bright} or {normal} intensity.
|
||||
func Spans(line string) []Span {
|
||||
var out []Span
|
||||
color, bright := "", false
|
||||
last := 0
|
||||
for _, m := range tag.FindAllStringSubmatchIndex(line, -1) {
|
||||
if m[0] > last {
|
||||
out = append(out, Span{line[last:m[0]], color, bright})
|
||||
}
|
||||
last = m[1]
|
||||
switch t := line[m[2]:m[3]]; t {
|
||||
case "bright":
|
||||
bright = true
|
||||
case "normal":
|
||||
bright = false
|
||||
case "default":
|
||||
color = ""
|
||||
default:
|
||||
color = t
|
||||
}
|
||||
}
|
||||
if last < len(line) {
|
||||
out = append(out, Span{line[last:], color, bright})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// StripTags is a .3270 line without its tags.
|
||||
func StripTags(line string) string { return tag.ReplaceAllString(line, "") }
|
||||
@@ -0,0 +1,81 @@
|
||||
package screens
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFind(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
os.WriteFile(filepath.Join(dir, "welcome.ans"), []byte("\x1b[1;33m\xc9\xcd\xbb Hi @USER@\r\n\x1aSAUCE00 junk"), 0644)
|
||||
os.WriteFile(filepath.Join(dir, "welcome.asc"), []byte("Hi @USER@, plain"), 0644)
|
||||
os.WriteFile(filepath.Join(dir, "welcome.3270"), []byte("{yellow}Hi{default} @USER@"), 0644)
|
||||
os.WriteFile(filepath.Join(dir, "only.txt"), []byte("text"), 0644)
|
||||
os.WriteFile(filepath.Join(dir, "big.asc"), make([]byte, MaxSize+1), 0644)
|
||||
s := &Set{Dir: dir}
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
kind Kind
|
||||
want string
|
||||
f Format
|
||||
}{
|
||||
{"welcome", ANSI, "\x1b[1;33m╔═╗ Hi @USER@\n", Art},
|
||||
{"welcome", Plain, "Hi @USER@, plain", Text},
|
||||
{"welcome", TN3270, "{yellow}Hi{default} @USER@", Color},
|
||||
{"only", TN3270, "text", Text},
|
||||
{"only", ANSI, "text", Text},
|
||||
} {
|
||||
sc, ok := s.Find(c.name, c.kind)
|
||||
if !ok || sc.Text != c.want || sc.Format != c.f {
|
||||
t.Errorf("%s/%d: %+v %v", c.name, c.kind, sc, ok)
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"none", "big", "../etc/passwd", "Welcome", ""} {
|
||||
if _, ok := s.Find(name, ANSI); ok {
|
||||
t.Errorf("%q found", name)
|
||||
}
|
||||
}
|
||||
if _, ok := (*Set)(nil).Find("welcome", ANSI); ok {
|
||||
t.Error("found in no set")
|
||||
}
|
||||
if got := string(EncodeCP437("╔═╗ é ☃")); got != "\xc9\xcd\xbb \x82 ?" {
|
||||
t.Errorf("%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpand(t *testing.T) {
|
||||
vars := func(c string) (string, bool) {
|
||||
switch c {
|
||||
case "USER":
|
||||
return "alice", true
|
||||
case "NODE":
|
||||
return "7", true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
got := Expand("@CLS@Hi @USER:8@|@NODE:-3@|@USER:2@ mail me@example.com @NOPE@@PAUSE@bye", vars)
|
||||
want := []Piece{{Clear: true}, {Text: "Hi alice | 7|al mail me@example.com @NOPE@"}, {Pause: true}, {Text: "bye"}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("%+v", got)
|
||||
}
|
||||
if s := String("a@CLS@b@PAUSE@c", nil); s != "abc" {
|
||||
t.Error(s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSpans(t *testing.T) {
|
||||
got := Spans("plain {red}red {bright}hot{normal}{default} end")
|
||||
want := []Span{{"plain ", "", false}, {"red ", "red", false}, {"hot", "red", true}, {" end", "", false}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("%+v", got)
|
||||
}
|
||||
if s := StripTags("{blue}a{bright}b"); s != "ab" {
|
||||
t.Error(s)
|
||||
}
|
||||
if !strings.Contains(StripTags("{nope}"), "{nope}") {
|
||||
t.Error("an unknown tag was taken")
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/gopher"
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/logo"
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/nntp"
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/screens"
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/ui3270"
|
||||
)
|
||||
|
||||
@@ -41,7 +42,10 @@ type Config struct {
|
||||
Binkp *BinkpSettings // nil: no binkp answering
|
||||
Doors []*door.Door
|
||||
Finger *finger.Settings // nil: no finger server
|
||||
Gopher *gopher.Settings // nil: no gopher server
|
||||
Screens *screens.Set // nil: no screens of the sysop's
|
||||
Menus map[string]*bbs.Menu
|
||||
Text map[string]string // [strings]
|
||||
Gopher *gopher.Settings // nil: no gopher server
|
||||
Database string
|
||||
Log string // syslog, stderr, none, or a file
|
||||
Listeners []*Listener
|
||||
@@ -88,6 +92,9 @@ func Load(path string) (*Config, error) {
|
||||
if err := c.doorSections(cf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := c.customSections(cf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := c.ftpSection(cf.Get("ftp", "")); err != nil {
|
||||
return nil, fmt.Errorf("[ftp] %w", err)
|
||||
}
|
||||
@@ -112,7 +119,7 @@ func Load(path string) (*Config, error) {
|
||||
}
|
||||
for _, s := range cf.Sections {
|
||||
switch s.Kind {
|
||||
case "general", "listen", "ftp", "news", "nntp", "peer", "fido", "binkp", "link", "door", "finger", "gopher":
|
||||
case "general", "listen", "ftp", "news", "nntp", "peer", "fido", "binkp", "link", "door", "finger", "gopher", "menu", "strings":
|
||||
default:
|
||||
return nil, fmt.Errorf("line %d: unknown section [%s]", s.Line, s.Kind)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/bbs"
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/screens"
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/ui3270"
|
||||
)
|
||||
|
||||
func TestCustomConfig(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
load := func(body string) (*Config, error) {
|
||||
p := filepath.Join(dir, "bbs.conf")
|
||||
os.WriteFile(p, []byte("[listen telnet]\ntype = telnet\naddress = :2300\n"+body), 0600)
|
||||
return Load(p)
|
||||
}
|
||||
c, err := load(`
|
||||
[general]
|
||||
screens_dir = ` + dir + `
|
||||
[door lord]
|
||||
command = /bin/true
|
||||
[menu main]
|
||||
title = @BBS@ main menu
|
||||
screen = mainmenu
|
||||
m = areas Message areas
|
||||
l = door:LORD Legend of the Red Dragon
|
||||
x = menu:extras More...
|
||||
g = logoff Goodbye
|
||||
[menu Extras]
|
||||
b = screen:bulletins Bulletins
|
||||
q = back Back
|
||||
[strings]
|
||||
command_prompt = "Your choice, @USER@: "
|
||||
pause = (more)
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := c.Menus["main"]
|
||||
if m == nil || m.Screen != "mainmenu" || len(m.Items) != 4 || m.Items[1].Key != "L" ||
|
||||
m.Items[1].Action != "door" || m.Items[1].Arg != "LORD" || m.Items[2].Label != "More..." {
|
||||
t.Fatalf("%+v", m)
|
||||
}
|
||||
if c.Menus["extras"] == nil || c.Text["command_prompt"] != "Your choice, @USER@: " || c.Screens.Dir != dir {
|
||||
t.Fatalf("%+v %+v", c.Menus, c.Text)
|
||||
}
|
||||
for body, want := range map[string]string{
|
||||
"[menu main]\nm = nothing Hi\n": "no such action",
|
||||
"[menu main]\nm = areas\n": "a label",
|
||||
"[menu main]\nmm = areas Areas\n": "one character",
|
||||
"[menu main]\nm = menu:nope Nope\n": "there is no [menu nope]",
|
||||
"[menu main]\nm = door:nope Nope\n": "there is no [door nope]",
|
||||
"[menu main]\nm = screen:bulletins Bulletins\n": "no screens_dir",
|
||||
"[menu main]\nm = screen:../x X\n": "a screen's name",
|
||||
"[menu main]\ntitle = x\n": "no items",
|
||||
"[menu]\nm = areas A\n": "needs a name",
|
||||
"[strings]\nnope = x\n": "no such string",
|
||||
"[menu a]\nm = areas A\n[menu A]\nm = areas A\n": "given twice",
|
||||
} {
|
||||
if _, err := load(body); err == nil || !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("%q: %v, want %q", body, err, want)
|
||||
}
|
||||
}
|
||||
c, err = load("[general]\nscreens_dir = " + filepath.Join(dir, "nope") + "\n")
|
||||
if err != nil || !strings.Contains(strings.Join(c.Warnings, "\n"), "is not a directory") {
|
||||
t.Errorf("%v %v", c.Warnings, 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) }
|
||||
}
|
||||
|
||||
func customizeBBS(t *testing.T, b *bbs.BBS, top string, files map[string]string) {
|
||||
dir := filepath.Join(top, "screens")
|
||||
os.Mkdir(dir, 0755)
|
||||
for name, body := range files {
|
||||
os.WriteFile(filepath.Join(dir, name), []byte(body), 0644)
|
||||
}
|
||||
b.Screens = &screens.Set{Dir: dir}
|
||||
item := func(k, v string) bbs.MenuItem {
|
||||
it, err := bbs.ParseMenuItem(k, v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return it
|
||||
}
|
||||
b.Menus = map[string]*bbs.Menu{
|
||||
"main": {Name: "main", Title: "@BBS@ main", Items: []bbs.MenuItem{
|
||||
item("m", "areas Message areas"), item("b", "screen:bulletin Bulletin"),
|
||||
item("x", "menu:extras Extras"), item("g", "logoff Goodbye")}},
|
||||
"extras": {Name: "extras", Title: "Extras", Prompt: "Extras> ", Items: []bbs.MenuItem{
|
||||
item("w", "who Who's online"), item("q", "back Back")}},
|
||||
}
|
||||
b.Text = map[string]string{"name_prompt": "Who are you? ", "new_name_prompt": "Who are you (or NEW)? ",
|
||||
"password_prompt": "Secret word: ", "pause": "(more)", "logoff_confirm": "Leave now, @USER@?"}
|
||||
}
|
||||
|
||||
// TestCustomText: the sysop's screens, menus and strings on the text BBS.
|
||||
func TestCustomText(t *testing.T) {
|
||||
e := startFull(t, ui3270.Graphics{}, nil, customize(t, map[string]string{
|
||||
"logon.ans": "\x1b[1;36m\xc9\xcd\xcd\xbb @BBS@ \xc8\xcd\xcd\xbc\x1b[0m\r\n\x1aSAUCE00 not shown",
|
||||
"welcome.asc": "Hello @USER@, @NEWMAIL@ new mail, node @NODE:3@.\n",
|
||||
"bulletin.asc": "Bulletin one\n@PAUSE@Bulletin two\n",
|
||||
"goodbye.asc": "Come back soon, @USER@!\n",
|
||||
}), &Listener{Name: "modem", Kind: "raw", Address: "modem.sock", ANSI: "yes", UTF8: "no"})
|
||||
d := e.dial("modem.sock")
|
||||
d.expect("\xc9\xcd\xcd\xbb Test BBS \xc8\xcd\xcd\xbc") // CP437, as it is
|
||||
d.expect("Who are you (or NEW)? ")
|
||||
d.send("alice\r")
|
||||
d.expect("Secret word: ")
|
||||
d.send("alicepass1\r")
|
||||
d.expect("Hello alice, 0 new mail, node 1 .")
|
||||
d.expect("Test BBS main")
|
||||
d.expect("[B]")
|
||||
d.expect("Command: ")
|
||||
d.send("B")
|
||||
d.expect("Bulletin one")
|
||||
d.expect("(more)")
|
||||
d.send(" ")
|
||||
d.expect("Bulletin two")
|
||||
d.expect("(more)")
|
||||
d.send(" ")
|
||||
d.expect("Command: ")
|
||||
d.send("X")
|
||||
d.expect("Extras> ")
|
||||
d.send("Q")
|
||||
d.expect("Test BBS main")
|
||||
d.expect("Command: ")
|
||||
d.send("G")
|
||||
d.expect("Leave now, alice?")
|
||||
d.send("Y")
|
||||
d.expect("Come back soon, alice!")
|
||||
if strings.Contains(string(d.got), "SAUCE") {
|
||||
t.Error("SAUCE shown")
|
||||
}
|
||||
}
|
||||
|
||||
// TestCustom3270: the same on a 3270 (s3270): a .3270 logon screen in
|
||||
// colors, the welcome screen, the sysop's main menu, a screen from it.
|
||||
func TestCustom3270(t *testing.T) {
|
||||
if _, err := exec.LookPath("s3270"); err != nil {
|
||||
t.Skip("no s3270")
|
||||
}
|
||||
addr := freeTCP(t)
|
||||
startFull(t, ui3270.Graphics{}, nil, customize(t, map[string]string{
|
||||
"logon.3270": "{yellow}{bright}Welcome to @BBS@{normal} {blue}on a mainframe\n",
|
||||
"welcome.3270": "{green}Hello @USER@.\n",
|
||||
"bulletin.asc": "A plain bulletin\n",
|
||||
"goodbye.3270": "{white}So long, @USER@.\n",
|
||||
}), &Listener{Name: "telnet", Kind: "telnet", Address: addr, TN3270: true})
|
||||
script := strings.Join([]string{
|
||||
"Connect(" + addr + ")", "Wait(10,InputField)", "Ascii()",
|
||||
`String("alice")`, "Tab()", `String("alicepass1")`, "Enter()", "Wait(5,InputField)", "Ascii()",
|
||||
"Enter()", "Wait(5,InputField)", "Ascii()",
|
||||
`String("b")`, "Enter()", "Wait(5,InputField)", "Ascii()",
|
||||
"Enter()", "Wait(5,InputField)",
|
||||
`String("g")`, "Enter()", "Wait(3,Seconds)", "Ascii()", "Quit()", ""}, "\n")
|
||||
cmd := exec.Command("s3270", "-model", "3279-2")
|
||||
cmd.Stdin = strings.NewReader(script)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("s3270: %v\n%s", err, out)
|
||||
}
|
||||
for _, want := range []string{"Welcome to Test BBS", "on a mainframe", "Hello alice.",
|
||||
"Test BBS main", "B Bulletin", "A plain bulletin", "So long, alice."} {
|
||||
if !strings.Contains(string(out), want) {
|
||||
t.Errorf("no %q:\n%s", want, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func freeTCP(t *testing.T) string {
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ln.Close()
|
||||
return ln.Addr().String()
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
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", "")}
|
||||
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":
|
||||
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
|
||||
}
|
||||
@@ -39,6 +39,13 @@ func start(t *testing.T, tweak func(*bbs.Settings), ls ...*Listener) *env {
|
||||
|
||||
// startGraphics is start with 3270 graphics settings.
|
||||
func startGraphics(t *testing.T, g ui3270.Graphics, tweak func(*bbs.Settings), ls ...*Listener) *env {
|
||||
t.Helper()
|
||||
return startFull(t, g, tweak, nil, ls...)
|
||||
}
|
||||
|
||||
// startFull is start with graphics settings, and prep to set the BBS up
|
||||
// before it answers (dir: the test's directory).
|
||||
func startFull(t *testing.T, g ui3270.Graphics, tweak func(*bbs.Settings), prep func(b *bbs.BBS, dir string), ls ...*Listener) *env {
|
||||
t.Helper()
|
||||
dir, err := os.MkdirTemp("", "bbs")
|
||||
if err != nil {
|
||||
@@ -85,6 +92,9 @@ func startGraphics(t *testing.T, g ui3270.Graphics, tweak func(*bbs.Settings), l
|
||||
l.Via = l.Kind
|
||||
}
|
||||
}
|
||||
if prep != nil {
|
||||
prep(b, dir)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
package textui
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/bbs"
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/screens"
|
||||
)
|
||||
|
||||
// screen shows the sysop's display file of that name, if there is one
|
||||
// for this terminal: ANSI art as it is, text a page at a time, @CLS@ and
|
||||
// @PAUSE@ done. It reports whether there was one.
|
||||
func (s *session) screen(name string) (bool, error) {
|
||||
t := s.t
|
||||
kind := screens.Plain
|
||||
if t.ANSI {
|
||||
kind = screens.ANSI
|
||||
}
|
||||
sc, ok := s.b.Screens.Find(name, kind)
|
||||
if !ok {
|
||||
return false, nil
|
||||
}
|
||||
for _, p := range screens.Expand(sc.Text, s.n.Vars()) {
|
||||
switch {
|
||||
case p.Clear:
|
||||
t.Clear()
|
||||
case p.Pause:
|
||||
if err := t.Pause(); err != nil {
|
||||
return true, err
|
||||
}
|
||||
case sc.Format == screens.Art:
|
||||
t.Art(p.Text)
|
||||
default:
|
||||
lines := strings.Split(strings.TrimSuffix(p.Text, "\n"), "\n")
|
||||
for i, l := range lines {
|
||||
lines[i] = bbs.Clean(l, 400)
|
||||
}
|
||||
if more, err := t.Page(lines); err != nil || !more {
|
||||
return true, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, t.Flush()
|
||||
}
|
||||
|
||||
// str is a string of the sysop's (or the default), filled in.
|
||||
func (s *session) str(key string) string {
|
||||
v, _ := s.n.Str(key)
|
||||
return v
|
||||
}
|
||||
|
||||
// logoff asks, says goodbye and hangs up; nil if the caller stays.
|
||||
func (s *session) logoff() error {
|
||||
t := s.t
|
||||
ok, err := t.YesNo(s.str("logoff_confirm"), true)
|
||||
if err != nil || !ok {
|
||||
return err
|
||||
}
|
||||
t.Println("")
|
||||
if shown, err := s.screen("goodbye"); err != nil || !shown {
|
||||
t.Println(s.b.Goodbye)
|
||||
}
|
||||
s.b.Log.Printf("node %d: %s logged off", s.n.Num, s.user.Name)
|
||||
return errHangup
|
||||
}
|
||||
|
||||
// runMenu runs a menu of the sysop's until it is left (back) or the
|
||||
// caller logs off.
|
||||
func (s *session) runMenu(m *bbs.Menu, depth int) error {
|
||||
t := s.t
|
||||
vars := s.n.Vars()
|
||||
for {
|
||||
s.user = s.n.Refresh()
|
||||
title := screens.String(m.Title, vars)
|
||||
s.n.Doing(title)
|
||||
shown := false
|
||||
if m.Screen != "" {
|
||||
var err error
|
||||
if shown, err = s.screen(m.Screen); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !shown {
|
||||
t.Println("")
|
||||
t.Println(t.C(Cyan, title))
|
||||
var items []string
|
||||
for _, it := range m.Items {
|
||||
items = append(items, t.C(Yellow, "["+it.Key+"]")+" "+bbs.Fit(screens.String(it.Label, vars), 22))
|
||||
}
|
||||
for i := 0; i < len(items); i += 2 {
|
||||
t.Println(" " + strings.Join(items[i:min(i+2, len(items))], ""))
|
||||
}
|
||||
}
|
||||
prompt := s.str("command_prompt")
|
||||
if m.Prompt != "" {
|
||||
prompt = screens.String(m.Prompt, vars)
|
||||
}
|
||||
t.Print("\n" + prompt)
|
||||
k, err := t.Key()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if k == '\r' {
|
||||
t.Println("")
|
||||
continue
|
||||
}
|
||||
t.Println(string(k))
|
||||
it := m.Item(string(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 {
|
||||
t := s.t
|
||||
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 "files":
|
||||
if s.b.Files == nil {
|
||||
t.Println("There are no file areas.")
|
||||
return nil
|
||||
}
|
||||
return s.fileAreas()
|
||||
case "doors":
|
||||
return s.doors()
|
||||
case "chat":
|
||||
return s.chat("")
|
||||
case "page":
|
||||
return s.page()
|
||||
case "terminal":
|
||||
t.Println("That is for 3270 terminals.")
|
||||
return nil
|
||||
case "logoff":
|
||||
return s.logoff()
|
||||
case "menu":
|
||||
if depth >= 10 {
|
||||
t.Println("Menus nest too deep.")
|
||||
return nil
|
||||
}
|
||||
return s.runMenu(s.b.Menus[strings.ToLower(it.Arg)], depth+1)
|
||||
case "screen":
|
||||
shown, err := s.screen(it.Arg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !shown {
|
||||
t.Println("There is nothing to show.")
|
||||
return nil
|
||||
}
|
||||
return t.Pause()
|
||||
case "area":
|
||||
a, err := s.b.Store.AreaByName(it.Arg)
|
||||
if err != nil {
|
||||
t.Println(t.C(Red, "There is no area "+it.Arg+"."))
|
||||
return nil
|
||||
}
|
||||
return s.area(a)
|
||||
case "door":
|
||||
for _, d := range s.doorsFor() {
|
||||
if strings.EqualFold(d.Name, it.Arg) {
|
||||
return s.openDoor(d)
|
||||
}
|
||||
}
|
||||
t.Println("That door is not open to you.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+24
-2
@@ -8,6 +8,7 @@ import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/screens"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
@@ -52,6 +53,9 @@ type Term struct {
|
||||
// the line being typed, which is then drawn again
|
||||
Notify func() []string
|
||||
cur []rune // what has been printed of the line the cursor is on
|
||||
|
||||
// PausePrompt is Pause's ("": "[Press a key]")
|
||||
PausePrompt string
|
||||
}
|
||||
|
||||
// NewTerm makes a terminal on c, 80x24 plain ASCII until told otherwise.
|
||||
@@ -426,12 +430,30 @@ func (t *Term) YesNo(prompt string, def bool) (bool, error) {
|
||||
|
||||
// Pause waits for a key.
|
||||
func (t *Term) Pause() error {
|
||||
t.Print(t.C(Dim, "[Press a key]"))
|
||||
p := t.PausePrompt
|
||||
if p == "" {
|
||||
p = "[Press a key]"
|
||||
}
|
||||
t.Print(t.C(Dim, p))
|
||||
_, err := t.Key()
|
||||
t.Print("\r" + strings.Repeat(" ", 13) + "\r")
|
||||
t.Print("\r" + strings.Repeat(" ", min(utf8.RuneCountInString(p), 79)) + "\r")
|
||||
return err
|
||||
}
|
||||
|
||||
// Art writes ANSI art as it is (its escape sequences too): UTF-8 to a
|
||||
// UTF-8 terminal, CP437 to any other; \n as CR LF.
|
||||
func (t *Term) Art(s string) {
|
||||
s = strings.ReplaceAll(s, "\n", "\r\n")
|
||||
if t.UTF8 {
|
||||
t.w.WriteString(s)
|
||||
} else {
|
||||
t.w.Write(screens.EncodeCP437(s))
|
||||
}
|
||||
if i := strings.LastIndex(s, "\n"); i >= 0 {
|
||||
t.cur = t.cur[:0]
|
||||
}
|
||||
}
|
||||
|
||||
// Page shows lines a screen at a time; false if the caller quit.
|
||||
func (t *Term) Page(lines []string) (bool, error) {
|
||||
per := t.Rows - 2
|
||||
|
||||
+44
-28
@@ -52,19 +52,24 @@ func Run(b *bbs.BBS, n *bbs.Node, t *Term, o Options) error {
|
||||
func (s *session) run() error {
|
||||
t := s.t
|
||||
t.Clear()
|
||||
if t.Sixel {
|
||||
t.Picture(sixelLogo(t, s.b.Name, s.o.LogoFile))
|
||||
t.PausePrompt = s.str("pause")
|
||||
if shown, err := s.screen("logon"); err != nil {
|
||||
return err
|
||||
} else if !shown {
|
||||
if t.Sixel {
|
||||
t.Picture(sixelLogo(t, s.b.Name, s.o.LogoFile))
|
||||
}
|
||||
t.Println(t.C(Cyan, s.b.Name))
|
||||
if s.b.Banner != "" {
|
||||
t.Println(s.b.Banner)
|
||||
}
|
||||
via := "Node " + strconv.Itoa(s.n.Num) + ", " + s.c.Via
|
||||
if baud := s.c.Baud(); baud != "" {
|
||||
via += " at " + baud + " bit/s"
|
||||
}
|
||||
t.Println(t.C(Dim, via))
|
||||
t.Println("")
|
||||
}
|
||||
t.Println(t.C(Cyan, s.b.Name))
|
||||
if s.b.Banner != "" {
|
||||
t.Println(s.b.Banner)
|
||||
}
|
||||
via := "Node " + strconv.Itoa(s.n.Num) + ", " + s.c.Via
|
||||
if baud := s.c.Baud(); baud != "" {
|
||||
via += " at " + baud + " bit/s"
|
||||
}
|
||||
t.Println(t.C(Dim, via))
|
||||
t.Println("")
|
||||
|
||||
if err := s.login(); err != nil {
|
||||
return err
|
||||
@@ -95,12 +100,18 @@ func (s *session) login() error {
|
||||
|
||||
t.SetDeadline(time.Now().Add(s.b.LoginTimeout))
|
||||
defer t.SetDeadline(time.Time{})
|
||||
newHint := ""
|
||||
namePrompt := "Name: "
|
||||
if v, set := s.n.Str("name_prompt"); set {
|
||||
namePrompt = v
|
||||
}
|
||||
if s.b.NewUsers {
|
||||
newHint = t.C(Dim, " (or NEW)")
|
||||
namePrompt = "Name" + t.C(Dim, " (or NEW)") + ": "
|
||||
if v, set := s.n.Str("new_name_prompt"); set {
|
||||
namePrompt = v
|
||||
}
|
||||
}
|
||||
for try := 0; try < s.b.Attempts; try++ {
|
||||
t.Print("Name" + newHint + ": ")
|
||||
t.Print(namePrompt)
|
||||
l, err := t.ReadLine(LineOpts{Max: bbs.MaxNameLen, Initial: s.c.LoginUser})
|
||||
s.c.LoginUser = ""
|
||||
if err != nil {
|
||||
@@ -122,7 +133,7 @@ func (s *session) login() error {
|
||||
}
|
||||
continue
|
||||
}
|
||||
pw, err := t.Password("Password: ")
|
||||
pw, err := t.Password(s.str("password_prompt"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -147,6 +158,9 @@ func (s *session) login() error {
|
||||
func (s *session) register() (bool, error) {
|
||||
t := s.t
|
||||
t.Println("")
|
||||
if _, err := s.screen("newuser"); err != nil {
|
||||
return false, err
|
||||
}
|
||||
t.Println(t.C(Cyan, "New user"))
|
||||
t.Println("Pick a name: letters, digits, spaces, dots and dashes, 2 to 24 long.")
|
||||
var name string
|
||||
@@ -207,9 +221,15 @@ func (s *session) welcome() {
|
||||
t := s.t
|
||||
u := s.user
|
||||
t.Println("")
|
||||
t.Println("Welcome, " + t.C(Yellow, u.Name) + ".")
|
||||
if !u.LastLogin.IsZero() {
|
||||
t.Println(t.C(Dim, "Your last call was "+u.LastLogin.Format("2006-01-02 15:04")+"."))
|
||||
if shown, _ := s.screen("welcome"); !shown {
|
||||
if v, set := s.n.Str("welcome"); set {
|
||||
t.Println(v)
|
||||
} else {
|
||||
t.Println("Welcome, " + t.C(Yellow, u.Name) + ".")
|
||||
}
|
||||
if !u.LastLogin.IsZero() {
|
||||
t.Println(t.C(Dim, "Your last call was "+u.LastLogin.Format("2006-01-02 15:04")+"."))
|
||||
}
|
||||
}
|
||||
if motd := s.b.MOTDText(); motd != "" {
|
||||
t.Println("")
|
||||
@@ -226,6 +246,9 @@ func (s *session) mainMenu() error {
|
||||
t := s.t
|
||||
t.Notify = s.notify
|
||||
defer func() { t.Notify = nil }()
|
||||
if m := s.b.Menus["main"]; m != nil {
|
||||
return s.runMenu(m, 0)
|
||||
}
|
||||
for {
|
||||
u := s.n.Refresh()
|
||||
s.user = u
|
||||
@@ -260,7 +283,7 @@ func (s *session) mainMenu() error {
|
||||
for i := 0; i < len(items); i += 2 {
|
||||
t.Println(" " + strings.Join(items[i:min(i+2, len(items))], ""))
|
||||
}
|
||||
t.Print("\nCommand: ")
|
||||
t.Print("\n" + s.str("command_prompt"))
|
||||
k, err := t.Key()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -296,16 +319,9 @@ func (s *session) mainMenu() error {
|
||||
case 'I':
|
||||
err = s.info()
|
||||
case 'G', 'Q':
|
||||
ok, err := t.YesNo("Log off?", true)
|
||||
if err != nil {
|
||||
if err := s.logoff(); err != nil {
|
||||
return err
|
||||
}
|
||||
if ok {
|
||||
t.Println("")
|
||||
t.Println(s.b.Goodbye)
|
||||
s.b.Log.Printf("node %d: %s logged off", s.n.Num, u.Name)
|
||||
return errHangup
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -0,0 +1,241 @@
|
||||
package ui3270
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/racingmars/go3270"
|
||||
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/bbs"
|
||||
"github.com/Neo-Desktop/bbs-sec/internal/screens"
|
||||
)
|
||||
|
||||
var colors = map[string]go3270.Color{
|
||||
"": go3270.DefaultColor, "blue": go3270.Blue, "red": go3270.Red, "pink": go3270.Pink,
|
||||
"green": go3270.Green, "turquoise": go3270.Turquoise, "yellow": go3270.Yellow, "white": go3270.White,
|
||||
}
|
||||
|
||||
// screenPages is the sysop's display file of that name for a 3270, as
|
||||
// pages of lines (split at @CLS@ and @PAUSE@ and where a page is full).
|
||||
func (u *ui) screenPages(name string, per int) ([][]string, *screens.Screen, bool) {
|
||||
sc, ok := u.b.Screens.Find(name, screens.TN3270)
|
||||
if !ok {
|
||||
return nil, nil, false
|
||||
}
|
||||
var pages [][]string
|
||||
var cur []string
|
||||
flush := func() {
|
||||
if len(cur) > 0 {
|
||||
pages = append(pages, cur)
|
||||
cur = nil
|
||||
}
|
||||
}
|
||||
for _, p := range screens.Expand(sc.Text, u.n.Vars()) {
|
||||
if p.Clear || p.Pause {
|
||||
flush()
|
||||
continue
|
||||
}
|
||||
for _, l := range strings.Split(strings.TrimSuffix(p.Text, "\n"), "\n") {
|
||||
cur = append(cur, l)
|
||||
if len(cur) == per {
|
||||
flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
flush()
|
||||
return pages, sc, true
|
||||
}
|
||||
|
||||
// lineFields draws a display file's line at row: a .3270 line's colors
|
||||
// (each change of color takes a column, as 3270 attributes do), or text.
|
||||
func (u *ui) lineFields(sc *screens.Screen, row int, line string) go3270.Screen {
|
||||
if sc.Format != screens.Color {
|
||||
return go3270.Screen{u.text(row, 1, bbs.Clean(line, u.cols-3), go3270.Green, false)}
|
||||
}
|
||||
var out go3270.Screen
|
||||
col := 1
|
||||
for _, sp := range screens.Spans(line) {
|
||||
if col >= u.cols-1 {
|
||||
break
|
||||
}
|
||||
t := bbs.Clean(sp.Text, u.cols-col-2)
|
||||
if t == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, u.text(row, col, t, colors[sp.Color], sp.Bright))
|
||||
col += len([]rune(t)) + 1
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// screen shows the sysop's display file of that name, a page at a time;
|
||||
// false if there is none for a 3270.
|
||||
func (u *ui) screen(name string) (bool, error) {
|
||||
pages, sc, ok := u.screenPages(name, u.rows-4)
|
||||
if !ok {
|
||||
return false, nil
|
||||
}
|
||||
for i, page := range pages {
|
||||
keys := "Enter=Continue PF3=Skip"
|
||||
if i == len(pages)-1 {
|
||||
keys = "Enter=Continue"
|
||||
}
|
||||
scr := u.frame("", keys, "")
|
||||
for j, l := range page {
|
||||
scr = append(scr, u.lineFields(sc, 1+j, l)...)
|
||||
}
|
||||
resp, _, err := u.show(scr, nil)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
if resp.AID == go3270.AIDPF3 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// bye is the goodbye at a logoff: the sysop's goodbye screen, or the
|
||||
// goodbye line.
|
||||
func (u *ui) bye() {
|
||||
pages, sc, ok := u.screenPages("goodbye", u.rows-3)
|
||||
if !ok || len(pages) == 0 {
|
||||
u.goodbye(u.b.Goodbye)
|
||||
return
|
||||
}
|
||||
scr := u.frame("Goodbye", "", "")
|
||||
for j, l := range pages[0] {
|
||||
scr = append(scr, u.lineFields(sc, 1+j, l)...)
|
||||
}
|
||||
u.sendLast(scr)
|
||||
}
|
||||
|
||||
// runMenu runs a menu of the sysop's until it is left (back) or the
|
||||
// caller logs off.
|
||||
func (u *ui) runMenu(m *bbs.Menu, depth int) error {
|
||||
msg := ""
|
||||
vars := u.n.Vars()
|
||||
for {
|
||||
u.user = u.n.Refresh()
|
||||
title := screens.String(m.Title, vars)
|
||||
u.n.Doing(title)
|
||||
scr := u.frame(title, "Enter=Select PF3=Back", msg)
|
||||
msg = ""
|
||||
pages, sc, shown := []([]string)(nil), (*screens.Screen)(nil), false
|
||||
if m.Screen != "" {
|
||||
pages, sc, shown = u.screenPages(m.Screen, u.rows-5)
|
||||
}
|
||||
if shown && len(pages) > 0 {
|
||||
for j, l := range pages[0] {
|
||||
scr = append(scr, u.lineFields(sc, 1+j, l)...)
|
||||
}
|
||||
} else {
|
||||
scr = append(scr, u.boxed(2, 2, 3+len(m.Items)+1, 44, go3270.Blue)...)
|
||||
for i, it := range m.Items {
|
||||
scr = append(scr, u.text(3+i, 4, it.Key, go3270.Yellow, true),
|
||||
u.text(3+i, 7, screens.String(it.Label, vars), go3270.Green, false))
|
||||
}
|
||||
}
|
||||
prompt := "Option ===>"
|
||||
if m.Prompt != "" {
|
||||
prompt = bbs.Trunc(screens.String(m.Prompt, vars), 20)
|
||||
}
|
||||
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)...)
|
||||
resp, v, err := u.show(scr, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.AID == go3270.AIDPF3 {
|
||||
if depth > 0 {
|
||||
return nil
|
||||
}
|
||||
v["opt"] = ""
|
||||
if it := u.logoffItem(m); it != nil {
|
||||
v["opt"] = it.Key
|
||||
}
|
||||
} else if resp.AID != go3270.AIDEnter {
|
||||
continue
|
||||
}
|
||||
key := strings.TrimSpace(v["opt"])
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
it := m.Item(key)
|
||||
if it == nil {
|
||||
msg = "No such option."
|
||||
continue
|
||||
}
|
||||
if it.Action == "back" {
|
||||
if depth > 0 {
|
||||
return nil
|
||||
}
|
||||
continue
|
||||
}
|
||||
if msg, err = u.do(it, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// logoffItem is a menu's log off item, for PF3 on the top menu.
|
||||
func (u *ui) logoffItem(m *bbs.Menu) *bbs.MenuItem {
|
||||
for i := range m.Items {
|
||||
if m.Items[i].Action == "logoff" {
|
||||
return &m.Items[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// do does a menu item; the message for the menu's screen after.
|
||||
func (u *ui) do(it *bbs.MenuItem, depth int) (string, error) {
|
||||
switch it.Action {
|
||||
case "areas":
|
||||
return "", u.areas()
|
||||
case "mail":
|
||||
return "", u.mail()
|
||||
case "who":
|
||||
return "", u.who()
|
||||
case "users":
|
||||
return "", u.users()
|
||||
case "profile":
|
||||
return "", u.profile()
|
||||
case "info":
|
||||
return "", u.info()
|
||||
case "terminal":
|
||||
return "", u.terminal()
|
||||
case "files":
|
||||
if u.b.Files == nil {
|
||||
return "There are no file areas.", nil
|
||||
}
|
||||
return "", u.fileAreas()
|
||||
case "doors", "door":
|
||||
return "Doors are for text terminals (telnet, modem).", nil
|
||||
case "chat":
|
||||
return "", u.chat("", "")
|
||||
case "page":
|
||||
return "", u.page()
|
||||
case "logoff":
|
||||
u.b.Log.Printf("node %d: %s logged off", u.n.Num, u.user.Name)
|
||||
u.bye()
|
||||
return "", errLogoff
|
||||
case "menu":
|
||||
if depth >= 10 {
|
||||
return "Menus nest too deep.", nil
|
||||
}
|
||||
return "", u.runMenu(u.b.Menus[strings.ToLower(it.Arg)], depth+1)
|
||||
case "screen":
|
||||
shown, err := u.screen(it.Arg)
|
||||
if !shown {
|
||||
return "There is nothing to show.", err
|
||||
}
|
||||
return "", err
|
||||
case "area":
|
||||
a, err := u.b.Store.AreaByName(it.Arg)
|
||||
if err != nil {
|
||||
return "There is no area " + it.Arg + ".", nil
|
||||
}
|
||||
return "", u.area(a)
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
+28
-9
@@ -95,6 +95,12 @@ func (u *ui) run() error {
|
||||
if err := u.logon(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := u.screen("welcome"); err != nil {
|
||||
return err
|
||||
}
|
||||
if m := u.b.Menus["main"]; m != nil {
|
||||
return u.runMenu(m, 0)
|
||||
}
|
||||
return u.mainMenu()
|
||||
}
|
||||
|
||||
@@ -158,6 +164,11 @@ func (u *ui) show(scr go3270.Screen, vals map[string]string) (go3270.Response, m
|
||||
func (u *ui) goodbye(msg string) {
|
||||
scr := u.frame("Goodbye", "", "")
|
||||
scr = append(scr, u.text(u.rows/2, 2, msg, go3270.White, true))
|
||||
u.sendLast(scr)
|
||||
}
|
||||
|
||||
// sendLast sends a screen that is answered by nothing: the last.
|
||||
func (u *ui) sendLast(scr go3270.Screen) {
|
||||
u.g.SetWriteDeadline(time.Now().Add(10 * time.Second))
|
||||
go3270.ShowScreenOpts(scr, nil, u.g, go3270.ScreenOpts{AltScreen: u.dev,
|
||||
Codepage: u.dev.Codepage(), NoResponse: true})
|
||||
@@ -236,14 +247,21 @@ func (u *ui) logon() error {
|
||||
scr = append(scr, lf...)
|
||||
row += n + 1
|
||||
}
|
||||
for _, l := range strings.Split(strings.TrimRight(u.b.Banner, "\n"), "\n") {
|
||||
if row > u.rows-10 {
|
||||
break
|
||||
}
|
||||
if l = bbs.Clean(l, u.cols-4); l != "" || row > 2 {
|
||||
scr = append(scr, u.text(row, 2, l, go3270.White, false))
|
||||
if pages, sc, ok := u.screenPages("logon", u.rows-11); ok && len(pages) > 0 {
|
||||
for _, l := range pages[0] {
|
||||
scr = append(scr, u.lineFields(sc, row, l)...)
|
||||
row++
|
||||
}
|
||||
} else {
|
||||
for _, l := range strings.Split(strings.TrimRight(u.b.Banner, "\n"), "\n") {
|
||||
if row > u.rows-10 {
|
||||
break
|
||||
}
|
||||
if l = bbs.Clean(l, u.cols-4); l != "" || row > 2 {
|
||||
scr = append(scr, u.text(row, 2, l, go3270.White, false))
|
||||
row++
|
||||
}
|
||||
}
|
||||
}
|
||||
via := "Connected by " + c.Via
|
||||
if u.dev.TerminalType() != "" {
|
||||
@@ -266,7 +284,7 @@ func (u *ui) logon() error {
|
||||
vals = map[string]string{"name": v["name"]}
|
||||
switch resp.AID {
|
||||
case go3270.AIDPF3:
|
||||
u.goodbye(u.b.Goodbye)
|
||||
u.bye()
|
||||
return errLogoff
|
||||
case go3270.AIDPF5:
|
||||
if !u.b.NewUsers {
|
||||
@@ -358,7 +376,8 @@ func (u *ui) mainMenu() error {
|
||||
}
|
||||
scr := u.frame("Main menu", "Enter=Select PF3=Log off", msg)
|
||||
msg = ""
|
||||
scr = append(scr, u.text(2, 2, "Welcome, "+u.user.Name+".", go3270.White, true))
|
||||
welcome, _ := u.n.Str("welcome")
|
||||
scr = append(scr, u.text(2, 2, welcome, go3270.White, true))
|
||||
if first && !u.user.LastLogin.IsZero() {
|
||||
scr = append(scr, u.text(3, 2, "Your last call was "+
|
||||
u.user.LastLogin.Format("2006-01-02 15:04")+".", go3270.Blue, false))
|
||||
@@ -437,7 +456,7 @@ func (u *ui) mainMenu() error {
|
||||
}
|
||||
case "X":
|
||||
u.b.Log.Printf("node %d: %s logged off", u.n.Num, u.user.Name)
|
||||
u.goodbye(u.b.Goodbye)
|
||||
u.bye()
|
||||
return errLogoff
|
||||
default:
|
||||
msg = fmt.Sprintf("Choose 1 to %d, or X.", len(items))
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
Example screens for screens_dir: copy this directory to /etc/bbs/screens
|
||||
and set [general] screens_dir = /etc/bbs/screens. The [menu main] in
|
||||
bbs.conf.sample uses mainmenu and bulletins.
|
||||
|
||||
NAME.ans ANSI art, CP437 (ANSI terminals)
|
||||
NAME.asc plain text (any text terminal; 3270s too)
|
||||
NAME.3270 text with {color} (3270s)
|
||||
|
||||
logon, newuser, welcome and goodbye are shown by themselves; others by
|
||||
the menus that name them. See the wiki's Customization page.
|
||||
@@ -0,0 +1,7 @@
|
||||
Bulletins
|
||||
=========
|
||||
|
||||
1. The board is open. Say hello in General.
|
||||
2. Doors are on the D key; chat is on C.
|
||||
|
||||
@PAUSE@Have a look at the file areas too.
|
||||
@@ -0,0 +1 @@
|
||||
{turquoise}{bright}Thanks for calling @BBS@, @USER@. Come back soon!
|
||||
@@ -0,0 +1,2 @@
|
||||
|
||||
[1;36mThanks for calling @BBS@, [1;33m@USER@[1;36m. Come back soon![0m
|
||||
@@ -0,0 +1,2 @@
|
||||
|
||||
Thanks for calling @BBS@, @USER@. Come back soon!
|
||||
@@ -0,0 +1,3 @@
|
||||
{turquoise}{bright}*** @BBS@ ***
|
||||
{blue}A bulletin board, by telnet, modem and TN3270.
|
||||
{green}@ONLINE@ on now.
|
||||
@@ -0,0 +1,10 @@
|
||||
[2J[H[0m[1;36mÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»[0m
|
||||
[1;36mº[0m[1;37m [0m[1;36mº[0m
|
||||
[1;36mº[0m[1;33m °±²Û @BBS:24@ Û²±° [0m[1;36mº[0m
|
||||
[1;36mº[0m[1;37m [0m[1;36mº[0m
|
||||
[1;36mº[0m[0;36m telnet ú modem ú TN3270 [0m[1;36mº[0m
|
||||
[1;36mº[0m[1;37m [0m[1;36mº[0m
|
||||
[1;36mÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ[0m
|
||||
|
||||
[0;37mNode @NODE@ ú @VIA@ ú @ONLINE@ on now[0m
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
@BBS@
|
||||
----------------------------------------
|
||||
Node @NODE@, @VIA@. @ONLINE@ on now.
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
[2J[H[1;36mÄÄÄ @BBS@ ÄÄÄ[0m [0;37m@TIME@ node @NODE@[0m
|
||||
|
||||
[1;33m[M][0;37m Message areas [1;33m[E][0;37m E-mail (@NEWMAIL@ new)[0m
|
||||
[1;33m[F][0;37m File areas [1;33m[D][0;37m Doors [0m
|
||||
[1;33m[C][0;37m Chat [1;33m[S][0;37m Page the sysop [0m
|
||||
[1;33m[W][0;37m Who's online [1;33m[B][0;37m Bulletins [0m
|
||||
[1;33m[X][0;37m Extras [1;33m[G][0;37m Goodbye [0m
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
{white}{bright}Welcome back, @USER@.{normal}
|
||||
{blue}Last call: @LASTON@ Calls: @CALLS@ Posts: @POSTS@
|
||||
{green}New mail: @NEWMAIL@
|
||||
@@ -0,0 +1,4 @@
|
||||
[0m
|
||||
[1;37mWelcome back, [1;33m@USER@[1;37m.[0m
|
||||
[0;37mLast call: @LASTON@ ú Calls: @CALLS@ ú Posts: @POSTS@[0m
|
||||
[1;32mNew mail: @NEWMAIL@[0m
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
Welcome back, @USER@.
|
||||
Last call: @LASTON@ Calls: @CALLS@ Posts: @POSTS@
|
||||
New mail: @NEWMAIL@
|
||||
Reference in new issue
Block a user