mirror of
https://github.com/D-Modem/login.git
synced 2026-11-28 20:38:06 -08:00
As bbs config got in D-Modem/bbs#57: - Every section and key has a description of what it does for the sysop (setupdesc.go); TestEverySettingDescribed keeps one for each key the sample has. F1 adds the sample's notes and the choices. - Keys are picked from lists with a note per value: closed lists (mode, tty, after, charset, driver, hash...), suggestions with "Type another value…", and checkboxes for lists of several (auth, [menu] items, dropfile, users, groups, aol networks, ppp_pool dns). The values come from the file and the machine: the items, the users file, [groups], /etc/passwd and /etc/group, resolv.conf, the sockets in /run. - An item's key list says which letters are free and which are taken by another item or the hangup; listen addresses say when the other section uses them. - "Add a menu item" starts from one of the sample's items (or an empty one), with a free key if the sample's is taken. - tui.ChooseMulti: a list of checkboxes. - The sample gains [number] deny and [sql] timeout, which the code read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRVGabTrMWBpbnD6HKF
1226 lines
33 KiB
Go
1226 lines
33 KiB
Go
// Copyright (C) 2026 D-Modem Authors
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/D-Modem/menu/internal/confdoc"
|
|
"github.com/D-Modem/menu/internal/confedit"
|
|
"github.com/D-Modem/menu/internal/config"
|
|
"github.com/D-Modem/menu/internal/term"
|
|
"github.com/D-Modem/menu/internal/tui"
|
|
"github.com/D-Modem/menu/samples"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// The sysop's setup: modemmenu.conf on a full screen, as the BBS setup
|
|
// programs of the 1990s had theirs. Each section is a window of its
|
|
// keys, with the sample's help for the key under the bar; values are typed
|
|
// in a box or picked from a list. Saving checks the file as -check does
|
|
// first, and keeps its comments and order: only the lines of the keys
|
|
// changed are. modemmenu -setup runs it on the console (or over SSH);
|
|
// an [item] with mode = setup on the line of a sysop who calls in.
|
|
|
|
// kindGroups: the windows of the first one, under their headings (and
|
|
// the menus of the bar); a section the sample has that is in none goes
|
|
// under Other
|
|
var kindGroups = []struct {
|
|
label string
|
|
kinds []string
|
|
}{
|
|
{"Login", []string{"general", "terminal", "callerid", "number", "guest", "signup"}},
|
|
{"Users", []string{"file", "ldap", "sql", "system", "groups"}},
|
|
{"Menu", []string{"menu", "item", "dropfile"}},
|
|
{"Dial-up", []string{"ppp_pool", "autoppp", "aol"}},
|
|
{"Server", []string{"server", "authd"}},
|
|
}
|
|
|
|
// kindTitles: a section's window's title
|
|
var kindTitles = map[string]string{
|
|
"general": "Login and messages", "terminal": "Terminal detection", "callerid": "Caller ID",
|
|
"number": "Numbers by caller ID", "guest": "Guest account", "signup": "New accounts (sign-up)",
|
|
"file": "Users file", "ldap": "LDAP", "sql": "SQL database", "system": "System accounts (PAM)",
|
|
"groups": "Groups", "menu": "The menu", "item": "Menu items", "dropfile": "Door drop files",
|
|
"ppp_pool": "PPP addresses and DNS", "autoppp": "AutoPPP (dialers)", "aol": "AOL networks",
|
|
"server": "Server (-serve)", "authd": "Auth socket (authd)",
|
|
}
|
|
|
|
// setupUI is a setup session: the file, what it was, the windows up.
|
|
type setupUI struct {
|
|
d *tui.Desk
|
|
path string
|
|
doc *confedit.Doc
|
|
orig *confedit.Doc // as it was read or last saved
|
|
isNew bool // the file is not there yet: the sample to begin with
|
|
kinds []*confdoc.Kind
|
|
byKind map[string]*confdoc.Kind
|
|
stack []*page
|
|
flash string // a word for the help box, once
|
|
warn bool
|
|
size func() (rows, cols int) // the terminal's, if it can change (nil: no)
|
|
who string // for the log
|
|
}
|
|
|
|
// page is a window: the first one, a section's, or a list of the named
|
|
// sections of a kind ([item NAME]).
|
|
type page struct {
|
|
what string // "main", "section", "named"
|
|
sec string // the section ("item bbs"), or the kind of the named
|
|
kind *confdoc.Kind
|
|
l *tui.List
|
|
}
|
|
|
|
// newSetup: the setup of the file at path (the sample when there is none)
|
|
func newSetup(d *tui.Desk, path string) (*setupUI, error) {
|
|
u := &setupUI{d: d, path: path, byKind: map[string]*confdoc.Kind{}}
|
|
b, err := os.ReadFile(path)
|
|
switch {
|
|
case errors.Is(err, os.ErrNotExist):
|
|
b, u.isNew = []byte(samples.Conf), true
|
|
u.flash = path + " is not there yet: this is the sample, saved there with F10."
|
|
case err != nil:
|
|
return nil, err
|
|
}
|
|
u.doc, u.orig = confedit.Parse(string(b)), confedit.Parse(string(b))
|
|
u.kinds = confdoc.Parse(samples.Conf)
|
|
for _, k := range u.kinds {
|
|
u.byKind[k.Name] = k
|
|
}
|
|
d.Name = "modemmenu Setup"
|
|
d.Menus = u.menus()
|
|
u.stack = []*page{{what: "main", l: &tui.List{Title: "modemmenu Setup", MinW: 50}}}
|
|
return u, nil
|
|
}
|
|
|
|
// groups: kindGroups, and Other for the sample's sections in none
|
|
func (u *setupUI) groups() []struct {
|
|
label string
|
|
kinds []string
|
|
} {
|
|
out := append(kindGroups[:0:0], kindGroups...)
|
|
in := map[string]bool{}
|
|
for _, g := range kindGroups {
|
|
for _, k := range g.kinds {
|
|
in[k] = true
|
|
}
|
|
}
|
|
var other []string
|
|
for _, k := range u.kinds {
|
|
if !in[k.Name] {
|
|
other = append(other, k.Name)
|
|
}
|
|
}
|
|
if len(other) > 0 {
|
|
out = append(out, struct {
|
|
label string
|
|
kinds []string
|
|
}{"Other", other})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// menus: the bar's pull-downs
|
|
func (u *setupUI) menus() []tui.Menu {
|
|
ms := []tui.Menu{{Label: "File", Items: []tui.MenuItem{
|
|
{Label: "Save F10", ID: "save"}, {Label: "Check", ID: "check"},
|
|
{Label: "Undo all changes", ID: "revert"}, {Heading: true}, {Label: "Quit F9", ID: "quit"}}}}
|
|
for _, g := range u.groups() {
|
|
m := tui.Menu{Label: g.label}
|
|
for _, k := range g.kinds {
|
|
if u.byKind[k] != nil {
|
|
m.Items = append(m.Items, tui.MenuItem{Label: u.title(k), ID: "kind:" + k})
|
|
}
|
|
}
|
|
ms = append(ms, m)
|
|
}
|
|
return append(ms, tui.Menu{Label: "Help", Items: []tui.MenuItem{{Label: "Keys", ID: "keys"}, {Label: "About", ID: "about"}}})
|
|
}
|
|
|
|
func (u *setupUI) title(kind string) string {
|
|
if t, ok := kindTitles[kind]; ok {
|
|
return t
|
|
}
|
|
return "[" + kind + "]"
|
|
}
|
|
|
|
// named: the sections of a named kind in the file ("item bbs"...)
|
|
func (u *setupUI) named(kind string) []string {
|
|
var out []string
|
|
for _, s := range u.doc.Sections() {
|
|
if strings.HasPrefix(s, kind+" ") {
|
|
out = append(out, s)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// fill: a page's entries from the file as it is now
|
|
func (u *setupUI) fill(p *page) {
|
|
var items []tui.Entry
|
|
switch p.what {
|
|
case "main":
|
|
for _, g := range u.groups() {
|
|
items = append(items, tui.Entry{Heading: true, Label: g.label})
|
|
for _, k := range g.kinds {
|
|
kd := u.byKind[k]
|
|
if kd == nil {
|
|
continue
|
|
}
|
|
e := tui.Entry{ID: "kind:" + k, Label: u.title(k), Help: kindHelp(kd), Mark: "►"}
|
|
switch {
|
|
case kd.Named:
|
|
n := len(u.named(k))
|
|
e.Value = fmt.Sprintf("%d set", n)
|
|
case !u.doc.Has(k):
|
|
e.Value, e.Unset = "not set", true
|
|
case u.doc.Has(k) && hasKey(kd, "enabled"):
|
|
v, ok := u.doc.Get(k, "enabled")
|
|
if !ok {
|
|
v = kd.ByKey["enabled"].Value
|
|
}
|
|
e.Value = map[bool]string{true: "on", false: "off"}[yes(v)]
|
|
}
|
|
e.Changed = u.sectionChanged(k, kd.Named)
|
|
items = append(items, e)
|
|
}
|
|
}
|
|
case "named":
|
|
for _, s := range u.named(p.kind.Name) {
|
|
name := strings.TrimPrefix(s, p.kind.Name+" ")
|
|
v, _ := u.doc.Get(s, "label")
|
|
if p.kind.Name == "number" {
|
|
v, _ = u.doc.Get(s, "user")
|
|
}
|
|
items = append(items, tui.Entry{ID: "sec:" + s, Label: name, Value: v, Mark: "►",
|
|
Changed: u.changed(s, ""), Help: "Enter opens it; Del deletes it."})
|
|
}
|
|
add := tui.Entry{ID: "add", Label: "Add a new one…", Help: "A new [" + p.kind.Name + " NAME]: its name first."}
|
|
if p.kind.Name == "item" {
|
|
add.Label = "Add a menu item…"
|
|
add.Help = "A new item for the menu: start from one of the sample's (the BBS, PPP, a door, change password, last callers...) or an empty one, then give it a name. It is added to the menu's items."
|
|
}
|
|
if p.kind.Name == "number" {
|
|
add.Help = "Settings for one more phone number: type the caller's number as its name."
|
|
}
|
|
items = append(items, tui.Entry{Heading: true}, add)
|
|
case "section":
|
|
kd := p.kind
|
|
if kd.Free != "" {
|
|
for _, k := range u.doc.Keys(p.sec) {
|
|
v, _ := u.doc.Get(p.sec, k)
|
|
items = append(items, tui.Entry{ID: "key:" + k, Label: k, Value: v, Changed: u.changed(p.sec, k),
|
|
Help: "Each key is " + kd.Free + ". Del takes it out."})
|
|
}
|
|
items = append(items, tui.Entry{Heading: true}, tui.Entry{ID: "addkey", Label: "Add a new one…", Help: "Each key is " + kd.Free + "."})
|
|
break
|
|
}
|
|
known := map[string]bool{}
|
|
for _, k := range kd.Keys {
|
|
known[k.Name] = true
|
|
items = append(items, u.keyEntry(p.sec, kd, k))
|
|
}
|
|
for _, k := range u.doc.Keys(p.sec) {
|
|
if !known[k] {
|
|
v, _ := u.doc.Get(p.sec, k)
|
|
items = append(items, tui.Entry{ID: "key:" + k, Label: k, Value: v, Changed: u.changed(p.sec, k),
|
|
Help: "Not a key of [" + kd.Name + "] that modemmenu knows: a mistake, or for a newer version. Del takes it out."})
|
|
}
|
|
}
|
|
if kd.Named {
|
|
items = append(items, tui.Entry{Heading: true},
|
|
tui.Entry{ID: "rename", Label: "Rename…", Help: "A new name for this [" + p.sec + "]."},
|
|
tui.Entry{ID: "delete", Label: "Delete…", Help: "Take [" + p.sec + "] out of the file."})
|
|
}
|
|
}
|
|
p.l.Items = items
|
|
p.l.Fix()
|
|
}
|
|
|
|
// kindHelp: what a section is for
|
|
func kindHelp(kd *confdoc.Kind) string {
|
|
if d := setupDesc(kd.Name, ""); d != "" {
|
|
return d
|
|
}
|
|
return capFirst(strings.TrimSpace(kd.Help))
|
|
}
|
|
|
|
// keyEntry: a key's line in its section's window
|
|
func (u *setupUI) keyEntry(sec string, kd *confdoc.Kind, k *confdoc.Key) tui.Entry {
|
|
e := tui.Entry{ID: "key:" + k.Name, Label: k.Name, Changed: u.changed(sec, k.Name)}
|
|
v, set := u.doc.Get(sec, k.Name)
|
|
if set {
|
|
e.Value = v
|
|
if v == "" {
|
|
e.Value = `""`
|
|
}
|
|
} else {
|
|
e.Value, e.Unset = "(not set)", true
|
|
if k.Value != "" {
|
|
e.Note = "sample: " + k.Value
|
|
}
|
|
}
|
|
e.Help = setupDesc(kd.Name, k.Name)
|
|
if e.Help == "" {
|
|
e.Help = keyHelp(k)
|
|
}
|
|
if cs := u.choicesFor(sec, kd, k); cs != nil && len(cs.list) > 0 {
|
|
e.Mark = "↕"
|
|
}
|
|
return e
|
|
}
|
|
|
|
// keyHelp: a key's help as the sample has it
|
|
func keyHelp(k *confdoc.Key) string {
|
|
h := strings.TrimSpace(k.Help)
|
|
if h == "" {
|
|
h = "See the comments in the sample."
|
|
}
|
|
h = strings.ToUpper(h[:1]) + h[1:]
|
|
if !strings.HasSuffix(h, ".") && !strings.HasSuffix(h, ")") {
|
|
h += "."
|
|
}
|
|
if k.Value != "" {
|
|
h += " Sample: " + k.Value
|
|
}
|
|
return h
|
|
}
|
|
|
|
// choiceRE: a value the help names (confdoc writes them "; `name`: what")
|
|
var choiceRE = regexp.MustCompile("`([a-z0-9_]+)`:")
|
|
|
|
// choices: the values a key takes, when the sample says (yes and no for
|
|
// a yes-or-no; the names its help gives); nil when it is anything typed
|
|
func choices(k *confdoc.Key) []string {
|
|
if isBool(k.Value) {
|
|
return []string{"yes", "no"}
|
|
}
|
|
var out []string
|
|
seen := map[string]bool{}
|
|
add := func(v string) {
|
|
if v != "" && !seen[v] && !strings.ContainsAny(v, " {}/\\\"") {
|
|
seen[v] = true
|
|
out = append(out, v)
|
|
}
|
|
}
|
|
for _, m := range choiceRE.FindAllStringSubmatch(k.Help, -1) {
|
|
add(m[1])
|
|
}
|
|
if len(out) == 0 {
|
|
return nil
|
|
}
|
|
add(k.Value)
|
|
for _, e := range k.Examples {
|
|
add(e)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func isBool(v string) bool {
|
|
switch strings.ToLower(v) {
|
|
case "yes", "no", "true", "false", "on", "off":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func yes(v string) bool {
|
|
switch strings.ToLower(v) {
|
|
case "yes", "true", "on", "1":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func hasKey(k *confdoc.Kind, name string) bool { return k.ByKey[name] != nil }
|
|
|
|
// changed: whether a key (or, key "", any of a section) is not as it was
|
|
func (u *setupUI) changed(sec, key string) bool {
|
|
if key == "" {
|
|
if u.doc.Has(sec) != u.orig.Has(sec) {
|
|
return true
|
|
}
|
|
keys := append(u.doc.Keys(sec), u.orig.Keys(sec)...)
|
|
for _, k := range keys {
|
|
if u.changed(sec, k) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
a, aok := u.doc.Get(sec, key)
|
|
b, bok := u.orig.Get(sec, key)
|
|
return a != b || aok != bok
|
|
}
|
|
|
|
// sectionChanged: a section's, or with named any of the kind's
|
|
func (u *setupUI) sectionChanged(kind string, named bool) bool {
|
|
if !named {
|
|
return u.changed(kind, "")
|
|
}
|
|
all := map[string]bool{}
|
|
for _, s := range append(u.doc.Sections(), u.orig.Sections()...) {
|
|
if strings.HasPrefix(s, kind+" ") {
|
|
all[s] = true
|
|
}
|
|
}
|
|
for s := range all {
|
|
if u.changed(s, "") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// changes: how many keys are not as they were
|
|
func (u *setupUI) changes() int {
|
|
n := 0
|
|
secs := map[string]bool{}
|
|
for _, s := range append(u.doc.Sections(), u.orig.Sections()...) {
|
|
secs[s] = true
|
|
}
|
|
for s := range secs {
|
|
keys := map[string]bool{}
|
|
for _, k := range append(u.doc.Keys(s), u.orig.Keys(s)...) {
|
|
keys[k] = true
|
|
}
|
|
for k := range keys {
|
|
if u.changed(s, k) {
|
|
n++
|
|
}
|
|
}
|
|
if len(keys) == 0 && u.doc.Has(s) != u.orig.Has(s) {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
|
|
// frame: the desk with the windows up (open: a bar menu pulled down)
|
|
func (u *setupUI) frame(open int) *tui.Screen {
|
|
d := u.d
|
|
scr := d.Blank(open)
|
|
for i, p := range u.stack {
|
|
p.l.Depth = i
|
|
p.l.Draw(d, scr, i == len(u.stack)-1)
|
|
}
|
|
top := u.stack[len(u.stack)-1]
|
|
switch e := top.l.Selected(); {
|
|
case u.flash != "":
|
|
d.HelpBox(scr, "Note", u.flash, u.warn)
|
|
case e != nil:
|
|
d.HelpBox(scr, e.Label, e.Help, false)
|
|
}
|
|
status := ""
|
|
if n := u.changes(); n > 0 || u.isNew {
|
|
status = fmt.Sprintf("%d unsaved", n)
|
|
if u.isNew {
|
|
status = "not saved yet"
|
|
}
|
|
}
|
|
keys := tui.Hints("F1", "Help", "Enter", "Change", "Esc", "Back", "/", "Menu")
|
|
if top.what == "section" {
|
|
keys = append(keys, tui.Hint{Key: "Del", Label: "Default"})
|
|
}
|
|
keys = append(keys, tui.Hints("F9", "Quit", "F10", "Save")...)
|
|
d.KeyBar(scr, keys, status)
|
|
return scr
|
|
}
|
|
|
|
// run: the setup until the sysop quits (or hangs up: the error)
|
|
func (u *setupUI) run() error {
|
|
d := u.d
|
|
defer d.Close()
|
|
for {
|
|
if u.size != nil {
|
|
if r, c := u.size(); (r != d.Rows || c != d.Cols) && tui.OK(r, c) {
|
|
d.Rows, d.Cols = r, c
|
|
d.Redraw()
|
|
}
|
|
}
|
|
top := u.stack[len(u.stack)-1]
|
|
u.fill(top)
|
|
scr := u.frame(-1)
|
|
u.flash, u.warn = "", false
|
|
if err := d.Show(scr); err != nil {
|
|
return err
|
|
}
|
|
wait := time.Duration(-1) // no idle limit: the sysop's setup waits
|
|
if u.size != nil {
|
|
wait = time.Minute // the size looked at again
|
|
}
|
|
k, alt, err := d.Key(wait)
|
|
if errors.Is(err, term.ErrTimeout) && u.size != nil {
|
|
continue // at the console: no one to hang up on
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if alt || k == '/' || k == tui.F(12) {
|
|
i := 0
|
|
if alt {
|
|
if i = d.MenuKey(k); i < 0 {
|
|
continue
|
|
}
|
|
}
|
|
id, err := d.PullDown(u.frame, i)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
done, err := u.command(id)
|
|
if done || err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
switch k {
|
|
case tui.F(10):
|
|
if _, err := u.save(); err != nil {
|
|
return err
|
|
}
|
|
case tui.F(9):
|
|
if done, err := u.quit(); done || err != nil {
|
|
return err
|
|
}
|
|
case tui.KeyEsc, tui.KeyLeft:
|
|
if len(u.stack) > 1 {
|
|
u.stack = u.stack[:len(u.stack)-1]
|
|
continue
|
|
}
|
|
if done, err := u.quit(); done || err != nil {
|
|
return err
|
|
}
|
|
case tui.F(1), '?':
|
|
if err := u.help(top); err != nil {
|
|
return err
|
|
}
|
|
case tui.KeyDel, tui.KeyBackspace:
|
|
if err := u.remove(top); err != nil {
|
|
return err
|
|
}
|
|
case tui.KeyEnter, tui.KeyRight, ' ':
|
|
if err := u.pick(top, k == ' '); err != nil {
|
|
return err
|
|
}
|
|
default:
|
|
top.l.Move(d, k)
|
|
}
|
|
}
|
|
}
|
|
|
|
// command: a pull-down's item; done: the setup is over
|
|
func (u *setupUI) command(id string) (bool, error) {
|
|
switch {
|
|
case id == "":
|
|
case id == "save":
|
|
_, err := u.save()
|
|
return false, err
|
|
case id == "check":
|
|
err := checkConf(u.path, u.doc.String())
|
|
if err != nil {
|
|
_, err = u.d.Message(u.frame(-1), tui.Alert, "Check", err.Error())
|
|
return false, err
|
|
}
|
|
u.flash = "The configuration checks: " + u.summary()
|
|
case id == "revert":
|
|
if u.changes() == 0 {
|
|
return false, nil
|
|
}
|
|
ok, err := u.d.Confirm(u.frame(-1), tui.Alert, "Undo", "Undo every change since the file was read or saved?")
|
|
if ok {
|
|
u.doc = confedit.Parse(u.orig.String())
|
|
u.stack = u.stack[:1]
|
|
}
|
|
return false, err
|
|
case id == "quit":
|
|
return u.quit()
|
|
case id == "keys":
|
|
_, err := u.d.Message(u.frame(-1), tui.Info, "Keys", setupKeys)
|
|
return false, err
|
|
case id == "about":
|
|
_, err := u.d.Message(u.frame(-1), tui.Info, "About", "modemmenu Setup\n\n"+u.path+
|
|
"\n\nThe help of each key is the sample configuration's. Saving keeps the file's comments and order.")
|
|
return false, err
|
|
case strings.HasPrefix(id, "kind:"):
|
|
u.stack = u.stack[:1]
|
|
u.open(strings.TrimPrefix(id, "kind:"))
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// setupKeys: Help > Keys
|
|
const setupKeys = "↑ ↓ PgUp PgDn Home End, or a letter: move the bar.\n" +
|
|
"Enter: open a window, change a value (typed, or picked from a list).\n" +
|
|
"Space: the next of a value's choices (in a list of checkboxes: check one).\n" +
|
|
"Del: back to the default.\n" +
|
|
"Esc: back a window. / or F12 or Alt and a letter: the menus.\n" +
|
|
"F1: the help of the line. F10: save. F9: quit. ^L: draw again."
|
|
|
|
// open: the window of a kind (its section's, or its named ones')
|
|
func (u *setupUI) open(kind string) {
|
|
kd := u.byKind[kind]
|
|
if kd == nil {
|
|
return
|
|
}
|
|
if kd.Named {
|
|
u.stack = append(u.stack, &page{what: "named", sec: kind, kind: kd,
|
|
l: &tui.List{Title: u.title(kind), MinW: 44}})
|
|
return
|
|
}
|
|
u.stack = append(u.stack, &page{what: "section", sec: kind, kind: kd,
|
|
l: &tui.List{Title: "[" + kind + "] " + u.title(kind), MinW: 56}})
|
|
}
|
|
|
|
// pick: Enter (or Space: the next choice) on the bar's entry
|
|
func (u *setupUI) pick(p *page, space bool) error {
|
|
e := p.l.Selected()
|
|
if e == nil {
|
|
return nil
|
|
}
|
|
d := u.d
|
|
switch {
|
|
case strings.HasPrefix(e.ID, "kind:"):
|
|
if !space {
|
|
u.open(strings.TrimPrefix(e.ID, "kind:"))
|
|
}
|
|
case strings.HasPrefix(e.ID, "sec:"):
|
|
if !space {
|
|
s := strings.TrimPrefix(e.ID, "sec:")
|
|
u.stack = append(u.stack, &page{what: "section", sec: s, kind: p.kind,
|
|
l: &tui.List{Title: "[" + s + "]", MinW: 56}})
|
|
}
|
|
case e.ID == "add":
|
|
var tpl *itemTemplate
|
|
suggestName := ""
|
|
if p.kind.Name == "item" {
|
|
tpls := itemTemplates(samples.Conf)
|
|
values, notes := []string{"An empty item"}, []string{"set it up from scratch"}
|
|
for _, t := range tpls {
|
|
values = append(values, t.get("label"))
|
|
notes = append(notes, firstSentence(t.about))
|
|
}
|
|
i, ok, err := d.Choose(u.frame(-1), "Add a menu item: start from", values, notes, 0)
|
|
if err != nil || !ok {
|
|
return err
|
|
}
|
|
if i > 0 {
|
|
tpl = &tpls[i-1]
|
|
suggestName = tpl.name
|
|
for n := 2; u.doc.Has("item " + suggestName); n++ {
|
|
suggestName = fmt.Sprintf("%s%d", tpl.name, n)
|
|
}
|
|
}
|
|
}
|
|
name, ok, err := d.Input(u.frame(-1), "New ["+p.kind.Name+" NAME]", "Its name (one word: letters, digits, - and _"+
|
|
map[bool]string{true: "; for a number, the caller's number", false: ""}[p.kind.Name == "number"]+"):", suggestName, 32, false)
|
|
if err != nil || !ok || name == "" {
|
|
return err
|
|
}
|
|
if !nameRE.MatchString(name) || u.doc.Has(p.kind.Name+" "+name) {
|
|
u.flash, u.warn = fmt.Sprintf("%q is not a name that can be had: one word, not one there already.", name), true
|
|
return nil
|
|
}
|
|
sec := p.kind.Name + " " + name
|
|
u.doc.AddSection(sec)
|
|
if p.kind.Name == "item" {
|
|
if tpl != nil {
|
|
u.fromTemplate(sec, tpl)
|
|
} else {
|
|
u.doc.Set(sec, "label", name)
|
|
}
|
|
if v, ok := u.doc.Get(menuSection, "items"); ok {
|
|
u.doc.Set(menuSection, "items", strings.TrimSpace(v+" "+name))
|
|
}
|
|
}
|
|
u.stack = append(u.stack, &page{what: "section", sec: sec, kind: p.kind, l: &tui.List{Title: "[" + sec + "]", MinW: 56}})
|
|
case e.ID == "addkey":
|
|
name, ok, err := d.Input(u.frame(-1), "New key", "Its name (one word):", "", 32, false)
|
|
if err != nil || !ok || name == "" {
|
|
return err
|
|
}
|
|
name = strings.ToLower(name)
|
|
if !nameRE.MatchString(name) {
|
|
u.flash, u.warn = fmt.Sprintf("%q is not one word.", name), true
|
|
return nil
|
|
}
|
|
v, ok, err := d.Input(u.frame(-1), name, p.kind.Free, "", 255, false)
|
|
if err != nil || !ok {
|
|
return err
|
|
}
|
|
u.doc.Set(p.sec, name, v)
|
|
case e.ID == "rename":
|
|
kind := p.kind.Name
|
|
old := strings.TrimPrefix(p.sec, kind+" ")
|
|
name, ok, err := d.Input(u.frame(-1), "Rename", "A new name for ["+p.sec+"]:", old, 32, false)
|
|
if err != nil || !ok || name == "" || name == old {
|
|
return err
|
|
}
|
|
if !nameRE.MatchString(name) || u.doc.Has(kind+" "+name) {
|
|
u.flash, u.warn = fmt.Sprintf("%q is not a name that can be had: one word, not one there already.", name), true
|
|
return nil
|
|
}
|
|
u.doc.RenameSection(p.sec, kind+" "+name)
|
|
if kind == "item" {
|
|
u.renameInItems(old, name)
|
|
}
|
|
p.sec = kind + " " + name
|
|
p.l.Title = "[" + p.sec + "]"
|
|
case e.ID == "delete":
|
|
ok, err := d.Confirm(u.frame(-1), tui.Alert, "Delete", "Take ["+p.sec+"] out of the file?")
|
|
if err != nil || !ok {
|
|
return err
|
|
}
|
|
u.doc.RemoveSection(p.sec)
|
|
if p.kind.Name == "item" {
|
|
u.renameInItems(strings.TrimPrefix(p.sec, "item "), "")
|
|
}
|
|
u.stack = u.stack[:len(u.stack)-1]
|
|
case strings.HasPrefix(e.ID, "key:"):
|
|
return u.edit(p, strings.TrimPrefix(e.ID, "key:"), space)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// nameRE: a section's NAME, a key the sysop names
|
|
var nameRE = regexp.MustCompile(`^[A-Za-z0-9_+*#.-]{1,32}$`)
|
|
|
|
// renameInItems: an item's name in [menu] items changed (to "": out)
|
|
func (u *setupUI) renameInItems(old, name string) {
|
|
v, ok := u.doc.Get(menuSection, "items")
|
|
if !ok {
|
|
return
|
|
}
|
|
var out []string
|
|
for _, f := range strings.Fields(strings.ReplaceAll(v, ",", " ")) {
|
|
if f == old {
|
|
if name == "" {
|
|
continue
|
|
}
|
|
f = name
|
|
}
|
|
out = append(out, f)
|
|
}
|
|
u.doc.Set(menuSection, "items", strings.Join(out, " "))
|
|
}
|
|
|
|
// edit: a key's value: picked from its choices (or checked off, for a
|
|
// list of some at once), or typed
|
|
func (u *setupUI) edit(p *page, key string, space bool) error {
|
|
d := u.d
|
|
var k *confdoc.Key
|
|
if p.kind != nil {
|
|
k = p.kind.ByKey[key]
|
|
}
|
|
if k == nil {
|
|
k = &confdoc.Key{Name: key}
|
|
}
|
|
cur, set := u.doc.Get(p.sec, key)
|
|
var cs *choiceSet
|
|
if p.kind != nil {
|
|
cs = u.choicesFor(p.sec, p.kind, k)
|
|
}
|
|
if cs != nil && len(cs.list) == 0 {
|
|
cs = nil
|
|
}
|
|
switch {
|
|
case cs != nil && cs.multi:
|
|
if space {
|
|
return nil
|
|
}
|
|
return u.editMulti(p, key, cs, cur, set)
|
|
case cs != nil:
|
|
if set && !cs.has(cur) {
|
|
cs.list = append(cs.list, choice{cur, "set now"})
|
|
}
|
|
at := -1
|
|
for i, c := range cs.list {
|
|
if strings.EqualFold(c.value, cur) {
|
|
at = i
|
|
}
|
|
}
|
|
if space {
|
|
u.doc.Set(p.sec, key, cs.list[(at+1)%len(cs.list)].value)
|
|
return nil
|
|
}
|
|
values, notes := cs.values(), cs.notes()
|
|
values = append(values, "(not set)")
|
|
notes = append(notes, defaultNote(k))
|
|
if cs.open {
|
|
values = append(values, "Type another value…")
|
|
notes = append(notes, "")
|
|
}
|
|
start := at
|
|
if !set {
|
|
start = len(cs.list)
|
|
}
|
|
i, ok, err := d.Choose(u.frame(-1), key, values, notes, max(start, 0))
|
|
if err != nil || !ok {
|
|
return err
|
|
}
|
|
switch {
|
|
case i < len(cs.list):
|
|
u.doc.Set(p.sec, key, cs.list[i].value)
|
|
return nil
|
|
case i == len(cs.list):
|
|
u.doc.Unset(p.sec, key)
|
|
return nil
|
|
}
|
|
case space:
|
|
return nil
|
|
}
|
|
return u.typeValue(p, k, cur, set)
|
|
}
|
|
|
|
// defaultNote: what a key is when it is not set
|
|
func defaultNote(k *confdoc.Key) string {
|
|
if k.Value != "" {
|
|
return "the default (sample: " + k.Value + ")"
|
|
}
|
|
return "the default"
|
|
}
|
|
|
|
// editMulti: a list's values checked off; those set that the list does
|
|
// not have stay, after them
|
|
func (u *setupUI) editMulti(p *page, key string, cs *choiceSet, cur string, set bool) error {
|
|
have := config.Fields(cur)
|
|
// the ones set first, in their order (the order counts for some:
|
|
// [general] auth, [menu] items), then the rest
|
|
var list []choice
|
|
for _, v := range have {
|
|
note := "set now"
|
|
for _, c := range cs.list {
|
|
if strings.EqualFold(c.value, v) {
|
|
note = c.note
|
|
}
|
|
}
|
|
list = append(list, choice{v, note})
|
|
}
|
|
for _, c := range cs.list {
|
|
if !contains(have, c.value) {
|
|
list = append(list, c)
|
|
}
|
|
}
|
|
on := make([]bool, len(list))
|
|
for i := range have {
|
|
on[i] = true
|
|
}
|
|
all := &choiceSet{list: list}
|
|
extra := ""
|
|
if cs.open {
|
|
extra = "Type the list…"
|
|
}
|
|
out, typed, ok, err := u.d.ChooseMulti(u.frame(-1), key+": check the ones to have", all.values(), all.notes(), on, extra)
|
|
if err != nil || !ok {
|
|
return err
|
|
}
|
|
if typed {
|
|
var k *confdoc.Key
|
|
if p.kind != nil {
|
|
k = p.kind.ByKey[key]
|
|
}
|
|
if k == nil {
|
|
k = &confdoc.Key{Name: key}
|
|
}
|
|
return u.typeValue(p, k, cur, set)
|
|
}
|
|
var vs []string
|
|
for i, c := range list {
|
|
if out[i] {
|
|
vs = append(vs, c.value)
|
|
}
|
|
}
|
|
if len(vs) == 0 {
|
|
u.doc.Unset(p.sec, key)
|
|
return nil
|
|
}
|
|
u.doc.Set(p.sec, key, strings.Join(vs, " "))
|
|
return nil
|
|
}
|
|
|
|
// typeValue: a key's value typed in a box
|
|
func (u *setupUI) typeValue(p *page, k *confdoc.Key, cur string, set bool) error {
|
|
key := k.Name
|
|
prompt := ""
|
|
if p.kind != nil {
|
|
prompt = setupDesc(p.kind.Name, key)
|
|
}
|
|
if prompt == "" {
|
|
prompt = capFirst(strings.TrimSpace(k.Help))
|
|
}
|
|
if len(prompt) > 230 {
|
|
prompt = prompt[:229] + "…"
|
|
}
|
|
if !strings.HasSuffix(prompt, ".") && !strings.HasSuffix(prompt, ")") && prompt != "" {
|
|
prompt += "."
|
|
}
|
|
if !set && k.Value != "" {
|
|
prompt += " Sample: " + k.Value
|
|
}
|
|
v, ok, err := u.d.Input(u.frame(-1), "["+p.sec+"] "+key, prompt+" (Empty: not set, the default; \"\" for nothing.)", cur, 1024, false)
|
|
if err != nil || !ok {
|
|
return err
|
|
}
|
|
switch v {
|
|
case "":
|
|
u.doc.Unset(p.sec, key)
|
|
case `""`:
|
|
u.doc.Set(p.sec, key, "")
|
|
default:
|
|
u.doc.Set(p.sec, key, v)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// remove: Del on a key (its default again) or a named section (out)
|
|
func (u *setupUI) remove(p *page) error {
|
|
e := p.l.Selected()
|
|
if e == nil {
|
|
return nil
|
|
}
|
|
switch {
|
|
case strings.HasPrefix(e.ID, "key:"):
|
|
u.doc.Unset(p.sec, strings.TrimPrefix(e.ID, "key:"))
|
|
case strings.HasPrefix(e.ID, "sec:"):
|
|
s := strings.TrimPrefix(e.ID, "sec:")
|
|
ok, err := u.d.Confirm(u.frame(-1), tui.Alert, "Delete", "Take ["+s+"] out of the file?")
|
|
if err != nil || !ok {
|
|
return err
|
|
}
|
|
u.doc.RemoveSection(s)
|
|
if p.kind.Name == "item" {
|
|
u.renameInItems(strings.TrimPrefix(s, "item "), "")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// help: F1, the whole help of the bar's line: what it does, the
|
|
// sample's notes, and the values it may be picked from
|
|
func (u *setupUI) help(p *page) error {
|
|
e := p.l.Selected()
|
|
if e == nil {
|
|
return nil
|
|
}
|
|
text := e.Help
|
|
if strings.HasPrefix(e.ID, "key:") && p.kind != nil {
|
|
if k := p.kind.ByKey[strings.TrimPrefix(e.ID, "key:")]; k != nil {
|
|
text = keyFullHelp(setupDesc(p.kind.Name, k.Name), k, u.choicesFor(p.sec, p.kind, k))
|
|
}
|
|
}
|
|
if strings.HasPrefix(e.ID, "kind:") {
|
|
if kd := u.byKind[strings.TrimPrefix(e.ID, "kind:")]; kd != nil {
|
|
text = kindHelp(kd)
|
|
if h := capFirst(strings.TrimSpace(kd.Help)); h != "" && h != text {
|
|
text += "\n\nFrom the sample: " + h
|
|
}
|
|
}
|
|
}
|
|
_, err := u.d.Message(u.frame(-1), tui.Info, e.Label, text)
|
|
return err
|
|
}
|
|
|
|
// keyFullHelp: a key's description, the sample's notes, its choices
|
|
func keyFullHelp(desc string, k *confdoc.Key, cs *choiceSet) string {
|
|
text := desc
|
|
sample := keyHelp(k)
|
|
closedNotes := cs != nil && !cs.open && len(cs.list) > 2
|
|
switch {
|
|
case text == "":
|
|
text = sample
|
|
case closedNotes:
|
|
// the list below says what the sample's help does
|
|
case strings.TrimSpace(k.Help) != "":
|
|
text += "\n\nFrom the sample: " + sample
|
|
case k.Value != "":
|
|
text += " Sample: " + k.Value
|
|
}
|
|
if cs != nil && len(cs.list) > 0 {
|
|
var b strings.Builder
|
|
switch {
|
|
case cs.multi:
|
|
b.WriteString("\n\nCheck any of:")
|
|
case cs.open:
|
|
b.WriteString("\n\nSuggested (or type your own):")
|
|
default:
|
|
b.WriteString("\n\nOne of:")
|
|
}
|
|
for _, c := range cs.list {
|
|
b.WriteString("\n " + c.value)
|
|
if c.note != "" {
|
|
b.WriteString(": " + c.note)
|
|
}
|
|
}
|
|
text += b.String()
|
|
}
|
|
if len(k.Examples) > 0 && (cs == nil || cs.open) {
|
|
text += "\n\nAlso in the sample: " + strings.Join(k.Examples, ", ")
|
|
}
|
|
return text
|
|
}
|
|
|
|
// summary: what the file sets up, for a check that passed
|
|
func (u *setupUI) summary() string {
|
|
items := len(u.named("item"))
|
|
return fmt.Sprintf("%d menu items.", items)
|
|
}
|
|
|
|
// save: the file checked and written; whether it was
|
|
func (u *setupUI) save() (bool, error) {
|
|
text := u.doc.String()
|
|
if err := checkConf(u.path, text); err != nil {
|
|
i, err := u.d.Message(u.frame(-1), tui.Alert, "Check", "The configuration does not check:\n\n"+err.Error()+
|
|
"\n\nmodemmenu would not start with it.", "Back", "Save anyway")
|
|
if err != nil || i != 1 {
|
|
return false, err
|
|
}
|
|
}
|
|
if err := writeConf(u.path, text); err != nil {
|
|
_, err = u.d.Message(u.frame(-1), tui.Alert, "Save", "Not saved: "+err.Error())
|
|
return false, err
|
|
}
|
|
logf("setup: %s saved (%d changes) %s", u.path, u.changes(), u.who)
|
|
serverChanged := u.changed("server", "")
|
|
u.orig, u.isNew = confedit.Parse(text), false
|
|
u.flash = "Saved to " + u.path + ". The next call reads it."
|
|
if serverChanged {
|
|
u.flash += " [server] is read when modemmenu -serve starts: restart it."
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// quit: whether the setup ends (after asking to save what changed)
|
|
func (u *setupUI) quit() (bool, error) {
|
|
if u.changes() == 0 && !u.isNew {
|
|
return true, nil
|
|
}
|
|
i, err := u.d.Message(u.frame(-1), tui.Info, "Quit", "Save the changes first?", "Save", "Don't save", "Cancel")
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
switch i {
|
|
case 0:
|
|
saved, err := u.save()
|
|
return saved, err
|
|
case 1:
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// checkConf: whether text loads as a configuration at path would (in a
|
|
// file beside it, for its relative paths)
|
|
func checkConf(path, text string) error {
|
|
f, err := os.CreateTemp(filepath.Dir(path), ".modemmenu-check-*.conf")
|
|
if err != nil {
|
|
f, err = os.CreateTemp("", "modemmenu-check-*.conf")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
defer os.Remove(f.Name())
|
|
_, err = f.WriteString(text)
|
|
f.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = loadSettings(f.Name())
|
|
if err != nil {
|
|
return errors.New(strings.ReplaceAll(err.Error(), f.Name(), path))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// writeConf: text as the file at path: a new file renamed over it (its
|
|
// mode and owner kept), or written in place where its directory takes no
|
|
// new file
|
|
func writeConf(path, text string) error {
|
|
mode := os.FileMode(0o640)
|
|
uid, gid := -1, -1
|
|
if st, err := os.Stat(path); err == nil {
|
|
mode = st.Mode().Perm()
|
|
if sys, ok := st.Sys().(*syscall.Stat_t); ok {
|
|
uid, gid = int(sys.Uid), int(sys.Gid)
|
|
}
|
|
}
|
|
f, err := os.CreateTemp(filepath.Dir(path), ".modemmenu-*.conf")
|
|
if err != nil {
|
|
return os.WriteFile(path, []byte(text), mode)
|
|
}
|
|
tmp := f.Name()
|
|
_, err = f.WriteString(text)
|
|
if cerr := f.Close(); err == nil {
|
|
err = cerr
|
|
}
|
|
if err == nil {
|
|
err = os.Chmod(tmp, mode)
|
|
}
|
|
if err != nil {
|
|
os.Remove(tmp)
|
|
return err
|
|
}
|
|
if uid >= 0 {
|
|
os.Chown(tmp, uid, gid) // as it was, where we may
|
|
}
|
|
if err := os.Rename(tmp, path); err != nil {
|
|
os.Remove(tmp)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// runSetupLocal: modemmenu -setup on the terminal it was started on
|
|
func runSetupLocal(path string) int {
|
|
t := term.New(os.Stdin, os.Stdout)
|
|
defer t.Close()
|
|
size := func() (int, int) {
|
|
ws, err := unix.IoctlGetWinsize(int(os.Stdout.Fd()), unix.TIOCGWINSZ)
|
|
if err != nil || ws.Row == 0 || ws.Col == 0 {
|
|
return 24, 80
|
|
}
|
|
return int(ws.Row), int(ws.Col)
|
|
}
|
|
rows, cols := size()
|
|
if !tui.OK(rows, cols) {
|
|
fmt.Fprintf(os.Stderr, "modemmenu: the setup needs 40x14 at least (this is %dx%d)\n", cols, rows)
|
|
return 1
|
|
}
|
|
d := &tui.Desk{Line: t, Rows: rows, Cols: cols, Charset: localCharset(path), Mono: localMono(), Shade: true}
|
|
u, err := newSetup(d, path)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "modemmenu:", err)
|
|
return 1
|
|
}
|
|
u.size = size
|
|
d.Idle = -1 // the setup never times out
|
|
u.who = "on the console"
|
|
if err := u.run(); err != nil {
|
|
fmt.Fprintln(os.Stderr, "modemmenu:", err)
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// localCharset: [terminal] charset if it says, else UTF-8 where the
|
|
// locale is, a VT's lines on a VT, ASCII otherwise
|
|
func localCharset(path string) tui.Charset {
|
|
if b, err := os.ReadFile(path); err == nil {
|
|
if v, ok := confedit.Parse(string(b)).Get("terminal", "charset"); ok {
|
|
if cs, ok := tui.ParseCharset(v); ok {
|
|
return cs
|
|
}
|
|
}
|
|
}
|
|
for _, k := range []string{"LC_ALL", "LC_CTYPE", "LANG"} {
|
|
if v := os.Getenv(k); v != "" {
|
|
v = strings.ToLower(strings.ReplaceAll(v, "-", ""))
|
|
if strings.Contains(v, "utf8") {
|
|
return tui.UTF8
|
|
}
|
|
break
|
|
}
|
|
}
|
|
switch t := os.Getenv("TERM"); {
|
|
case strings.HasPrefix(t, "vt"), strings.HasPrefix(t, "xterm"), strings.HasPrefix(t, "screen"), strings.HasPrefix(t, "tmux"):
|
|
return tui.DEC
|
|
case t == "linux", t == "ansi", t == "pcansi", strings.HasPrefix(t, "syncterm"):
|
|
return tui.CP437
|
|
}
|
|
return tui.ASCII
|
|
}
|
|
|
|
// localMono: a terminal without colors (a VT's TERM)
|
|
func localMono() bool {
|
|
t := os.Getenv("TERM")
|
|
return strings.HasPrefix(t, "vt1") || strings.HasPrefix(t, "vt2") || strings.HasPrefix(t, "vt3") || t == "dumb"
|
|
}
|
|
|
|
// setupItem: an [item] of mode = setup: the setup on the caller's line
|
|
func setupItem(t *term.Term, cfg *settings, it *menuItem, s *session, deadline time.Time) error {
|
|
d := deskForSetup(t, cfg, s)
|
|
if d == nil {
|
|
return t.Write("\r\nThe setup needs an ANSI terminal of 80x24 (40x14 at least).\r\n")
|
|
}
|
|
u, err := newSetup(d, cfg.path)
|
|
if err != nil {
|
|
return t.Write("\r\nThe setup cannot read " + cfg.path + ": " + err.Error() + "\r\n")
|
|
}
|
|
u.who = "user=" + s.User
|
|
logf("setup: %s by user=%s", cfg.path, s.User)
|
|
// no idle limit for the sysop's setup: only the session's time limit
|
|
// (time_limit) ends it, or a hangup
|
|
d.Idle = -1
|
|
d.Deadline = deadline
|
|
err = u.run()
|
|
if errors.Is(err, term.ErrTimeout) {
|
|
return errTimeLimit
|
|
}
|
|
return err
|
|
}
|
|
|
|
// deskForSetup: the full screen for the setup, whether or not the menu
|
|
// is full screen
|
|
func deskForSetup(t *term.Term, cfg *settings, s *session) *tui.Desk {
|
|
c := *cfg
|
|
c.fullscreen = true
|
|
return deskFor(t, &c, s)
|
|
}
|
|
|
|
// capFirst: s with its first letter a capital
|
|
func capFirst(s string) string {
|
|
if s == "" {
|
|
return s
|
|
}
|
|
return strings.ToUpper(s[:1]) + s[1:]
|
|
}
|
|
|
|
// menuSection: [menu], whose items = the setup keeps up with the items'
|
|
const menuSection = "menu"
|
|
|
|
// fromTemplate: a new [item NAME] given a sample item's keys; its key a
|
|
// free one when the sample's is taken
|
|
func (u *setupUI) fromTemplate(sec string, t *itemTemplate) {
|
|
for _, kv := range t.keys {
|
|
u.doc.Set(sec, kv[0], kv[1])
|
|
}
|
|
key := strings.ToUpper(t.get("key"))
|
|
by, taken := u.takenKeys(sec)[key]
|
|
if key == "" || !taken {
|
|
return
|
|
}
|
|
for _, c := range u.keyChoices(sec).list {
|
|
if c.note == "free" {
|
|
u.doc.Set(sec, "key", c.value)
|
|
u.flash = "The sample's key " + key + " is " + by + ", so this item has " + c.value + "."
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// firstSentence: s up to its first stop (". ", ": " or " ("), where
|
|
// that leaves a few words
|
|
func firstSentence(s string) string {
|
|
cut := len(s)
|
|
for _, sep := range []string{". ", ": ", " ("} {
|
|
for from := 0; ; {
|
|
i := strings.Index(s[from:], sep)
|
|
if i < 0 {
|
|
break
|
|
}
|
|
if from+i >= 12 {
|
|
cut = min(cut, from+i)
|
|
break
|
|
}
|
|
from += i + len(sep)
|
|
}
|
|
}
|
|
return s[:cut]
|
|
}
|