diff --git a/internal/confui/confui.go b/internal/confui/confui.go index 2fe2fa8..58f34d9 100644 --- a/internal/confui/confui.go +++ b/internal/confui/confui.go @@ -59,6 +59,9 @@ func (e *Editor) Run() error { if se, ok := e.UI.(view.SheetEnder); ok { defer se.EndSheets() } + if u, ok := e.UI.(view.Unhurried); ok { + defer u.HoldIdle()() // the sysop is never timed out of the setup + } if ok, err := e.load(); err != nil || !ok { return err } diff --git a/internal/textui/idle_test.go b/internal/textui/idle_test.go new file mode 100644 index 0000000..da26816 --- /dev/null +++ b/internal/textui/idle_test.go @@ -0,0 +1,42 @@ +package textui + +import ( + "errors" + "net" + "testing" + "time" +) + +// TestHoldIdle: the sysop's configuration waits past the idle timeout +// for a key; the timeout is back after it +func TestHoldIdle(t *testing.T) { + a, b := net.Pipe() + defer a.Close() + defer b.Close() + go func() { + buf := make([]byte, 64) + for { + if _, err := b.Read(buf); err != nil { + return + } + } + }() + term := NewTerm(a) + term.Idle = 20 * time.Millisecond + s := &session{t: term} + restore := s.HoldIdle() + go func() { + time.Sleep(100 * time.Millisecond) + b.Write([]byte("x")) + }() + if k, err := term.EditKey(); err != nil || k != 'x' { + t.Fatalf("held: %q, %v; want 'x'", k, err) + } + restore() + if term.Idle != 20*time.Millisecond { + t.Fatalf("Idle %v after restore", term.Idle) + } + if _, err := term.EditKey(); !errors.Is(err, ErrIdle) { + t.Fatalf("restored: %v, want ErrIdle", err) + } +} diff --git a/internal/textui/term.go b/internal/textui/term.go index 5406d49..b60d608 100644 --- a/internal/textui/term.go +++ b/internal/textui/term.go @@ -27,6 +27,9 @@ import ( // ErrIdle: the caller typed nothing for too long. var ErrIdle = errors.New("idle timeout") +// noIdle: how long a read waits with no Idle (as good as for ever) +const noIdle = 100 * 365 * 24 * time.Hour + // ErrPETSCII: while WatchPETSCII, a key only a Commodore sends. var ErrPETSCII = errors.New("a Commodore's key") @@ -382,7 +385,7 @@ var fkeys = map[string]rune{"11": KeyF1, "12": KeyF1 + 1, "13": KeyF1 + 2, "14": func (t *Term) EditKey() (rune, error) { return t.read(true) } // read reads a key for readRune (keys false) or EditKey (true), waiting -// at most Idle (or to the deadline, if sooner): ErrIdle then. A LF after +// at most Idle (0: no limit) or to the deadline, if sooner: ErrIdle then. A LF after // a CR is dropped; an escape sequence is dropped, or, for EditKey, the // key it is. func (t *Term) read(keys bool) (rune, error) { @@ -392,8 +395,12 @@ func (t *Term) read(keys bool) (rune, error) { t.line.skip.Store(false) // a skipped screen ends at its prompt t.line.noPoll.Store(false) start := time.Now() + idle := t.Idle + if idle <= 0 { + idle = noIdle + } for { - limit := start.Add(t.Idle) + limit := start.Add(idle) if !t.deadline.IsZero() && t.deadline.Before(limit) { limit = t.deadline } diff --git a/internal/textui/ui.go b/internal/textui/ui.go index 9ac17d1..2e42b65 100644 --- a/internal/textui/ui.go +++ b/internal/textui/ui.go @@ -389,6 +389,14 @@ func (s *session) edit(lines []string) ([]string, error) { } } +// HoldIdle: no idle timeout until the func returned is called (the +// sysop's configuration); the login's deadline still holds. +func (s *session) HoldIdle() func() { + old := s.t.Idle + s.t.Idle = 0 + return func() { s.t.Idle = old } +} + // Console is the screens on a terminal of the machine's own, with no // login: for bbs config's screens (internal/confui), which want a // view.UI and nothing of a caller's. diff --git a/internal/ui3270/ui.go b/internal/ui3270/ui.go index 1b810e7..3b90faf 100644 --- a/internal/ui3270/ui.go +++ b/internal/ui3270/ui.go @@ -67,6 +67,7 @@ type ui struct { o Options user *store.User deadline time.Time // the login's limit + noIdle bool // no idle timeout (HoldIdle: the sysop's configuration) logo *logo // nil: no graphics logo logoAt [2]int // where the vector logo goes on this screen drawn bool // vector graphics are on the screen @@ -146,7 +147,10 @@ func (u *ui) show(scr go3270.Screen, vals map[string]string) (go3270.Response, m } u.g.n = 0 d := time.Now().Add(u.b.Conf().IdleTimeout) - if !u.deadline.IsZero() && u.deadline.Before(d) { + if u.noIdle { + d = time.Time{} // none + } + if !u.deadline.IsZero() && (d.IsZero() || u.deadline.Before(d)) { d = u.deadline } u.g.SetReadDeadline(d) @@ -500,3 +504,11 @@ func (u *ui) listColored(title, heading string, lines []string, colors []go3270. } } } + +// HoldIdle: no idle timeout until the func returned is called (the +// sysop's configuration); the login's deadline still holds. +func (u *ui) HoldIdle() func() { + old := u.noIdle + u.noIdle = true + return func() { u.noIdle = old } +} diff --git a/internal/view/sheet.go b/internal/view/sheet.go index 6c02999..4a07ce1 100644 --- a/internal/view/sheet.go +++ b/internal/view/sheet.go @@ -93,6 +93,13 @@ type SheetEnder interface { EndSheets() } +// Unhurried is a front end whose idle timeout can be held off, for the +// sysop's own screens (the configuration): HoldIdle lifts it, and the +// func it returns puts it back. The login's hard limit still holds. +type Unhurried interface { + HoldIdle() (restore func()) +} + // SheetUI is a front end that draws sheets full-screen. type SheetUI interface { Sheet(s *Sheet) (SheetResult, error)