From 3ba5b602879514f07e69ca8e04f2f5ca044e2b91 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 18 Oct 2026 03:37:21 +0000 Subject: [PATCH] Drop TN3270 detection and handoff The telnet terminal-type question and the tn3270 server handoff are gone; ANSI, VT, VT52 and PETSCII detection stay as they were. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01RvtV8Dg9kqRaakbTEfyH3f --- README.md | 23 ++--- main.go | 6 +- modemmenu.conf.sample | 5 - terminal.go | 207 +++--------------------------------------- terminal_test.go | 68 -------------- 5 files changed, 21 insertions(+), 288 deletions(-) diff --git a/README.md b/README.md index 8896511..65f0723 100644 --- a/README.md +++ b/README.md @@ -349,25 +349,16 @@ Before the banner, modemmenu asks the caller's terminal what it is, as [bbs-sec](https://github.com/Neo-Desktop/bbs-sec) does, and in this order: -1. **TN3270** (only with `[terminal] tn3270` set): telnet's `DO TTYPE`. - A client that speaks telnet answers with its terminal type; a 3270's - (`IBM-3278-2-E`, `IBM-DYNAMIC`...) is handed to the tn3270 server - there (`tcp:host:port`, or a unix socket's path) instead of the login, - which asks for the terminal type again from the start. A modem line - has no telnet of its own: this is for a TN3270 client that reaches - the modem through a telnet modem (a WiFi modem, tcpser) or a - serial-to-telnet bridge. A terminal that is no telnet client shows - the question as two characters, which are wiped, after `timeout`. -2. **ANSI**: the cursor sent as far down and right as it goes, and its +1. **ANSI**: the cursor sent as far down and right as it goes, and its position asked for (`CSI 6n`). An ANSI terminal answers, and the answer is its size. Nothing shows on one that does; the question is wiped on one that does not. -3. **VT52**, if not ANSI: `ESC Z`, which a VT52 answers `ESC / K`. -4. **What an ANSI terminal is**: DEC's questions (the answerback, the +2. **VT52**, if not ANSI: `ESC Z`, which a VT52 answers `ESC / K`. +3. **What an ANSI terminal is**: DEC's questions (the answerback, the primary and secondary device attributes), xterm's version (XTVERSION) and SyncTERM's CTerm attributes. A DEC VT220 says it is one; xterm says `XTerm(390)`; SyncTERM its CTerm version. -5. **PETSCII**, if not ANSI: a Commodore 64 or 128 asks nothing and +4. **PETSCII**, if not ANSI: a Commodore 64 or 128 asks nothing and answers nothing, so the login prompt watches its keys. DEL, the cursor keys, the function keys and the shifted letters are bytes only a Commodore sends: one of them, and the prompt starts again in @@ -387,7 +378,7 @@ and is the selection's: |---|---|---| | `{terminal}` | `MODEM_TERMINAL` | `vt` (an ANSI terminal that said what it is), `ansi`, `vt52`, `petscii`, `ascii`; empty when not asked | | `{term}` | `MODEM_TERM`, and `TERM` of exec items | a terminal type for it: `xterm`, `syncterm`, `vt100`, `vt220`... `vt520`, `ansi`, `vt52`, `dumb` | -| `{term_name}` | `MODEM_TERM_NAME` | what it said it is: `XTerm(390)`, `VT220`, `SyncTERM (CTerm 1.316)`, its answerback, a telnet client's terminal type | +| `{term_name}` | `MODEM_TERM_NAME` | what it said it is: `XTerm(390)`, `VT220`, `SyncTERM (CTerm 1.316)`, or its answerback | | `{rows}`, `{cols}` | `MODEM_ROWS`, `MODEM_COLS` | its size (40 by 25 for a Commodore), also set on the line for exec items (`stty size`) | E.g. `rlogin_terminal = {term}/{baud}` for a BBS that takes the type @@ -401,13 +392,11 @@ downstream `modemmenu -serve` does not ask again. detect = yes ; ask: ANSI, VT52, what an ANSI terminal is petscii = auto ; auto: watch the login for a Commodore; yes: every caller is one; no timeout = 2s ; for each question -;tn3270 = tcp:127.0.0.1:3270 ; TN3270 clients to this server (empty: not asked) -;tn3270_header = no ; the session header ahead of them ``` The questions cost a moment on a terminal that answers (each ends with its answer) and `timeout` for each one it does not (2 s for ANSI, 1 s -for VT52, and the `tn3270` question first when set). +for VT52). ### Message of the day diff --git a/main.go b/main.go index ce838b7..3e55359 100644 --- a/main.go +++ b/main.go @@ -180,17 +180,13 @@ func run(t *term, s *settings, sess *session) int { return 1 } // what the terminal is, before anything is written for it - is3270, err := detectTerminal(t, s, sess) - if err != nil { + if err := detectTerminal(t, s, sess); err != nil { logf("no login: %v (%v)", err, sess) return 1 } if sess.Terminal != "" { logf("terminal %s %v", sess.describeTerminal(), sess) } - if is3270 { - return tn3270(t, s, sess) - } if t.write(expand(s.banner, sess.vars(false), nil)) != nil { return 1 } diff --git a/modemmenu.conf.sample b/modemmenu.conf.sample index 38938f5..985e1a6 100644 --- a/modemmenu.conf.sample +++ b/modemmenu.conf.sample @@ -13,11 +13,6 @@ detect = yes petscii = auto ; for each question a terminal may not answer timeout = 2s -; TN3270 clients (by telnet's terminal type, through a telnet modem or a -; serial-to-telnet bridge) to a tn3270 server: tcp:host:port or a unix -; socket's path; empty: not asked -;tn3270 = tcp:127.0.0.1:3270 -;tn3270_header = no [menu] ; mode = exec diff --git a/terminal.go b/terminal.go index 08721ca..bab928c 100644 --- a/terminal.go +++ b/terminal.go @@ -18,9 +18,6 @@ import ( // What the caller's terminal is, asked as bbs-sec asks it, before the // banner: // -// - TN3270 ([terminal] tn3270 set): telnet's DO TTYPE. A client that -// speaks telnet answers with its terminal type; IBM-3278-2-E and the -// like are 3270s, which get the tn3270 server instead of the login. // - ANSI: the cursor moved as far as it goes and its position asked for // (CSI 6n). An ANSI terminal answers, and the answer is its size. // - VT52, when not ANSI: ESC Z, which a VT52 answers with ESC / and a @@ -33,25 +30,19 @@ import ( // terminalSettings: [terminal] type terminalSettings struct { - detect bool // ask: ANSI, VT52, an ANSI terminal's identity - petscii string // auto (a terminal that is not ANSI may be one), yes (every caller), no - tn3270 string // where a TN3270 client goes: tcp:host:port or a socket's path ("": not asked) - tn3270Header bool // the session header ahead of it - timeout time.Duration + detect bool // ask: ANSI, VT52, an ANSI terminal's identity + petscii string // auto (a terminal that is not ANSI may be one), yes (every caller), no + timeout time.Duration } func loadTerminal(sec *section) (terminalSettings, error) { var err error ts := terminalSettings{ petscii: strings.ToLower(sec.get("petscii", "auto")), - tn3270: sec.get("tn3270", ""), } if ts.detect, err = sec.bool("detect", true); err != nil { return ts, err } - if ts.tn3270Header, err = sec.bool("tn3270_header", false); err != nil { - return ts, err - } if ts.timeout, err = sec.duration("timeout", 2*time.Second); err != nil { return ts, err } @@ -60,46 +51,9 @@ func loadTerminal(sec *section) (terminalSettings, error) { default: return ts, fmt.Errorf("[terminal] petscii: %q (auto, yes or no)", ts.petscii) } - if ts.tn3270 != "" { - if _, err := tn3270Item(ts); err != nil { - return ts, err - } - } return ts, nil } -// tn3270Item: the tn3270 server as an item of mode tcp, to hand the -// line to -func tn3270Item(ts terminalSettings) (*menuItem, error) { - it := &menuItem{name: "tn3270", mode: "tcp", header: ts.tn3270Header, timeout: 10 * time.Second} - if addr, ok := strings.CutPrefix(ts.tn3270, "tcp:"); ok { - host, port, err := splitHostPort(addr) - if err != nil { - return nil, fmt.Errorf("[terminal] tn3270: %v", err) - } - it.host, it.port = host, port - return it, nil - } - if !strings.HasPrefix(ts.tn3270, "/") { - return nil, fmt.Errorf("[terminal] tn3270: %q (tcp:host:port, or a unix socket's absolute path)", ts.tn3270) - } - it.path = ts.tn3270 - return it, nil -} - -func splitHostPort(addr string) (string, int, error) { - i := strings.LastIndexByte(addr, ':') - if i < 0 { - return "", 0, fmt.Errorf("%q: no port", addr) - } - port, err := strconv.Atoi(addr[i+1:]) - if err != nil || port <= 0 || port > 65535 { - return "", 0, fmt.Errorf("%q: bad port", addr) - } - host := strings.TrimSuffix(strings.TrimPrefix(addr[:i], "["), "]") - return host, port, nil -} - // the questions, and the patterns of their answers const ( // the cursor saved, as far down and right as it goes, its position @@ -323,19 +277,6 @@ func petsciiTelltale(b byte) bool { return false } -// telnet's terminal type of a 3270 (IBM-3278-2-E, IBM-DYNAMIC...), not -// IBM's ASCII terminals (IBM-3151) -var tn3270Pattern = regexp.MustCompile(`^IBM-(327[789]|DYNAMIC)`) - -// petscii terminal types, as telnet clients and upstream menus name them -func petsciiName(tt string) bool { - switch strings.ToLower(tt) { - case "petscii", "c64", "c128", "commodore", "cbm": - return true - } - return false -} - // prober: the questions of one call, and what came that was no answer // (what the caller typed, a PPP frame) to be read again after type prober struct { @@ -450,78 +391,9 @@ func (p *prober) identify(timeout time.Duration) (vtInfo, error) { return in, nil } -// telnet: a telnet client's terminal type, "" when what is on the line -// does not speak telnet -func (p *prober) ttype(timeout time.Duration) (string, error) { - if err := p.t.writeRaw(string([]byte{iac, do, optTType})); err != nil { - return "", err - } - reply := func(cmd byte) []byte { return []byte{iac, cmd, optTType} } - got, err := p.collect(timeout, 64, func(b []byte) bool { - return bytes.Contains(b, reply(will)) || bytes.Contains(b, reply(wont)) - }) - if err != nil { - return "", err - } - if !bytes.Contains(got, reply(will)) { - if !bytes.Contains(got, reply(wont)) { - // not telnet: IAC DO showed as two characters - p.t.writeRaw("\r \r") - } - p.keep(stripTelnet(got)) - return "", nil - } - p.keep(stripTelnet(got)) - // SB TTYPE SEND; the answer SB TTYPE IS SE - if err := p.t.writeRaw(string([]byte{iac, sb, optTType, 1, iac, se})); err != nil { - return "", err - } - got, err = p.collect(timeout, 128, func(b []byte) bool { return bytes.Contains(b, []byte{iac, se}) }) - if err != nil { - return "", err - } - var tt string - if i := bytes.Index(got, []byte{iac, sb, optTType, 0}); i >= 0 { - rest := got[i+4:] - if j := bytes.Index(rest, []byte{iac, se}); j >= 0 { - tt = printable(string(rest[:j]), 40) - } - } - p.keep(stripTelnet(got)) - return tt, nil -} - -// stripTelnet: the bytes without telnet's commands -func stripTelnet(b []byte) []byte { - var out []byte - for i := 0; i < len(b); i++ { - if b[i] != iac || i+1 >= len(b) { - out = append(out, b[i]) - continue - } - switch c := b[i+1]; { - case c == iac: - out = append(out, iac) - i++ - case c >= will && c <= dont: - i += 2 - case c == sb: - j := bytes.Index(b[i:], []byte{iac, se}) - if j < 0 { - return out - } - i += j + 1 - default: - i++ - } - } - return out -} - // detectTerminal: what the caller's terminal is, in sess (Terminal, Term, -// TermName, Rows, Cols), the line set for it; true for a TN3270 client, -// the tn3270 server's -func detectTerminal(t *term, s *settings, sess *session) (bool, error) { +// TermName, Rows, Cols), the line set for it +func detectTerminal(t *term, s *settings, sess *session) error { ts := s.terminal defer func() { if sess.Terminal == "petscii" { @@ -537,52 +409,35 @@ func detectTerminal(t *term, s *settings, sess *session) (bool, error) { switch { case sess.Terminal != "": // an upstream menu said - return false, nil + return nil case ts.petscii == "yes": sess.Terminal, sess.Term, sess.Rows, sess.Cols = "petscii", "dumb", "25", "40" - return false, nil + return nil } p := &prober{t: t} defer p.done() - if ts.tn3270 != "" { - tt, err := p.ttype(ts.timeout) - if err != nil { - return false, err - } - switch { - case tt == "": - case tn3270Pattern.MatchString(strings.ToUpper(tt)): - sess.Terminal, sess.TermName = "tn3270", tt - return true, nil - case petsciiName(tt): - sess.Terminal, sess.Term, sess.TermName, sess.Rows, sess.Cols = "petscii", "dumb", tt, "25", "40" - return false, nil - default: - sess.TermName = tt - } - } if !ts.detect || p.ppp { // not asked: perhaps a Commodore, whose keys will tell t.watchPETSCII = ts.petscii == "auto" && !p.ppp - return false, nil + return nil } ansi, rows, cols, err := p.ansi(ts.timeout) if err != nil || p.ppp { - return false, err + return err } if !ansi { is52, err := p.vt52(ts.timeout / 2) if err != nil || p.ppp { - return false, err + return err } if is52 { sess.Terminal, sess.Term, sess.TermName, sess.Rows, sess.Cols = "vt52", "vt52", "VT52", "24", "80" - return false, nil + return nil } sess.Terminal, sess.Term = "ascii", "dumb" // perhaps a Commodore: its keys will tell t.watchPETSCII = ts.petscii == "auto" - return false, nil + return nil } sess.Terminal, sess.Term = "ansi", "ansi" if rows > 0 && cols > 0 { @@ -590,7 +445,7 @@ func detectTerminal(t *term, s *settings, sess *session) (bool, error) { } in, err := p.identify(ts.timeout) if err != nil { - return false, err + return err } if n := in.name(); n != "" { sess.TermName = n @@ -599,7 +454,7 @@ func detectTerminal(t *term, s *settings, sess *session) (bool, error) { if in.level() > 0 || in.emulator() { sess.Terminal = "vt" } - return false, nil + return nil } // becomePETSCII: the caller has a Commodore after all @@ -723,37 +578,3 @@ func petsciiIn(c byte) byte { } return c } - -// tn3270: a TN3270 client to the tn3270 server, until either goes; the -// server asks for the terminal type again, as from the start -func tn3270(t *term, s *settings, sess *session) int { - it, err := tn3270Item(s.terminal) - if err != nil { - logf("tn3270: %v", err) - return 1 - } - logf("tn3270 %s to %s %v", sess.TermName, s.terminal.tn3270, sess) - // TTYPE off again (its WONT is the server's to ignore) - p := &prober{t: t} - if err := t.writeRaw(string([]byte{iac, dont, optTType})); err != nil { - return 1 - } - got, err := p.collect(s.terminal.timeout, 64, func(b []byte) bool { - return bytes.Contains(b, []byte{iac, wont, optTType}) - }) - if err != nil { - return 1 - } - if i := bytes.Index(got, []byte{iac, wont, optTType}); i >= 0 { - got = append(got[:i:i], got[i+3:]...) - } - t.unread(got...) - before := t.setMode("raw") - defer t.restoreMode(before) - if err := runTCP(t, it, sess, time.Time{}); err != nil && !errors.Is(err, errHangup) { - logf("tn3270: %v", err) - return 1 - } - logf("tn3270 ended %v", sess) - return 0 -} diff --git a/terminal_test.go b/terminal_test.go index 0c74940..657f0a5 100644 --- a/terminal_test.go +++ b/terminal_test.go @@ -6,7 +6,6 @@ package main import ( "bytes" "fmt" - "net" "os" "os/exec" "path/filepath" @@ -38,13 +37,6 @@ func TestParseVT(t *testing.T) { } } -func TestStripTelnet(t *testing.T) { - in := []byte("a\xff\xfb\x18b\xff\xfa\x18\x00ANSI\xff\xf0c\xff\xffd") - if got := string(stripTelnet(in)); got != "abc\xffd" { - t.Errorf("%q", got) - } -} - func TestPETSCIIText(t *testing.T) { if got := string(petsciiOut("Hi you\r\n\x1b[1;33mX\b \b\n")); got != "\xc8I YOU\r\xd8\x9d \x9d\r" { t.Errorf("out %q", got) @@ -262,66 +254,6 @@ func TestDetectPETSCII(t *testing.T) { m.Close() } -func TestTN3270(t *testing.T) { - ln, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatal(err) - } - defer ln.Close() - go func() { - for { - conn, err := ln.Accept() - if err != nil { - return - } - conn.Write([]byte("3270 HERE")) - b := make([]byte, 4) - for got := 0; got < 4; { - n, err := conn.Read(b[got:]) - if err != nil { - break - } - got += n - } - if string(b) == "ping" { - conn.Write([]byte("pong")) - } - conn.Close() - } - }() - dir := t.TempDir() - bin, conf := terminalConf(t, dir, "tn3270 = tcp:"+ln.Addr().String()) - - // a 3270: to the server - m, s := testPty(t) - cmd := startOn(t, bin, conf, s) - cl := newTermCaller(t, m, replies("\xff\xfd\x18", "\xff\xfb\x18", - "\xff\xfa\x18\x01\xff\xf0", "\xff\xfa\x18\x00IBM-3278-2-E\xff\xf0", "\xff\xfe\x18", "\xff\xfc\x18")) - if got := cl.expect("3270 HERE"); strings.Contains(got, "login") { - t.Errorf("%q", got) - } - cl.send("ping") - cl.expect("pong") - waitExit(t, cmd) - m.Close() - - // a telnet client of another terminal type, and a terminal that is - // no telnet client: the login - for _, answer := range []func([]byte) []byte{ - replies("\xff\xfd\x18", "\xff\xfb\x18", "\xff\xfa\x18\x01\xff\xf0", "\xff\xfa\x18\x00ANSI\xff\xf0", "\x1b[6n", "\x1b[24;80R"), - replies(), - } { - m, s := testPty(t) - cmd := startOn(t, bin, conf, s) - cl := newTermCaller(t, m, answer) - cl.expect("login: ") - cl.send("alice\r") - cl.expect("Password: ") - cmd.Process.Kill() - m.Close() - } -} - // startOn: modemmenu on the slave of a pty func startOn(t *testing.T, bin, conf string, s *os.File, env ...string) *exec.Cmd { cmd := exec.Command(bin, "-config", conf)