From 01c5a27f76463ad1057017b0aeb61f5858373881 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 11:34:30 +0000 Subject: [PATCH] CompuServe forums on the message areas GO FORUMS lists a forum for each group of message areas (and one for those in none), each area a section, at most 18 a forum. The forum is in command mode at Forum !, as TAPCIS needs: REA, SCA (and QUICK), BRO with NEW/ALL/WAI/REV/THR and FROM:/TO:/SUB:/SEC:/DAYS:/STA:, POST with EDIT, replies, OPT;TYP N for nonstop reading, MES, DES, NEW; message headers in forum software 4's form (#:, Sb: with thread marks, Fm:/To: with User IDs, * Reply: footers), and the entry and exit texts. TAPCIS's command strings are in the test. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRRPWCkk1Ro91AuzXFM --- docs/wiki/CompuServe.md | 39 +- internal/cis/ascii.go | 37 +- internal/cis/cis.go | 1 + internal/cis/forum.go | 832 +++++++++++++++++++++++++++++++ internal/cis/pages.txt | 15 +- internal/server/cisforum_test.go | 119 +++++ 6 files changed, 1027 insertions(+), 16 deletions(-) create mode 100644 internal/cis/forum.go create mode 100644 internal/server/cisforum_test.go diff --git a/docs/wiki/CompuServe.md b/docs/wiki/CompuServe.md index eff853c..d299b10 100644 --- a/docs/wiki/CompuServe.md +++ b/docs/wiki/CompuServe.md @@ -67,11 +67,48 @@ with their identification, which shows in who's online. CIM clients Commands chain with `;`, as in `SET BRI ON;GO RATES`. +## Forums + +`GO FORUMS` lists the forums. Each forum is a group of the BBS's message +areas, and each area in it is a section (S0, S1, ...). Areas in no group +make up a forum named after the BBS. A group with more than 18 areas is +split into several forums, because CompuServe's forums had at most 18 +sections. A forum's GO word is its name in capitals, letters and digits +only (`Amiga Tech` is `GO AMIGATECH`). Message numbers are the BBS's own, +so they rise across the forum with gaps. + +The forum works in command mode, at `Forum !`, which is the mode TAPCIS +and the other offline readers need: + +| Command | Does | +|---|---| +| `REA [NEW\|ALL\|WAI\|REV\|THR] [FROM:x] [TO:x] [SUB:x] [SEC:n,n] [DAYS:n] [STA:n] [n]` | read (NEW by default); `Read action !` after each: `RE` reply, `RER` reread, `P` parent, CR next | +| `SCA ...` / `SCA QUI ...` | the headers, or one line each | +| `BRO ...` | a header at a time, read on CR | +| `POST [SEC:n] [TO:x] [SUB:x]` | write a message (EDIT: `/EX` then `S` to store, or `/POST`) | +| `DES` / `NEW` | the sections; the new messages in each | +| `OPT;TYP N` | read nonstop (`TYP P` pauses again); the rest of OPT is taken | +| `MES` / `M` | the `Messages !` prompt and back | +| `EXIT`, `M`, `T`, `GO x`, `OFF` | leave the forum | + +Headers follow forum software 4's: + +``` +#: 2 S1/Software + 08-Nov-26 11:30:05 +Sb: #1-Kickstart 1.3 +Fm: sysop 72000,2 +To: alice 72000,1 +``` + +Membership (JOIN), the libraries and conferencing are not there yet. + ## What is there and what is coming | Done | Coming, in order | |---|---| -| The logon, `ESC I`, the menus and articles (`internal/cis/pages.txt`), GO, FIND, SET | Forums on the message areas, with the forum command language | +| The logon, `ESC I`, the menus and articles (`internal/cis/pages.txt`), GO, FIND, SET | Forum JOIN and member options | +| Forums on the message areas, with the forum command language | | | The CompuServe B, Quick B and B Plus protocols (`internal/xfer/cisb.go`) | Libraries on the file areas, with B+ downloads | | | CompuServe Mail, the CB Simulator, then CIM's HMI service | diff --git a/internal/cis/ascii.go b/internal/cis/ascii.go index 63d20e7..3b547b1 100644 --- a/internal/cis/ascii.go +++ b/internal/cis/ascii.go @@ -78,7 +78,7 @@ func (s *session) command(c string) error { s.out(verb + " is not a choice !\n") return nil } - s.goTo(s.pages.Get(s.page.Items[n-1].Target)) + return s.goWord(s.page.Items[n-1].Target) case verb == "T" || verb == "TOP": s.goTo(s.pages.Get("TOP")) case verb == "M" || verb == "MENU": @@ -109,12 +109,11 @@ func (s *session) command(c string) error { s.out("GO where? Type GO and a GO word or page ID.\n") return nil } - p := s.pages.Get(arg) - if p == nil { + if !s.known(arg) { s.out(strings.ToUpper(arg) + " is unrecognized !\n") return nil } - s.goTo(p) + return s.goWord(arg) case verb == "FIND": s.find(arg) case verb == "SET": @@ -127,9 +126,8 @@ func (s *session) command(c string) error { return errRelog default: // a GO word alone takes you there too - if p := s.pages.Get(c); p != nil { - s.goTo(p) - return nil + if s.known(c) { + return s.goWord(c) } s.out(strings.ToUpper(c) + " is unrecognized !\n") } @@ -148,11 +146,30 @@ func isNumber(s string) bool { return true } +// known reports whether a GO word is a page's or a forum's. +func (s *session) known(word string) bool { + return s.pages.Get(word) != nil || s.forum(word) != nil +} + +// goWord goes to a page or into a forum. +func (s *session) goWord(word string) error { + if f := s.forum(word); f != nil && s.pages.Get(word) == nil { + return s.enterForum(f) + } + s.goTo(s.pages.Get(word)) + return nil +} + // goTo shows a page and remembers it for B and M. func (s *session) goTo(p *Page) { if p == nil { return } + if p.ID == "FORUMS" { + // made afresh each time it is gone to: the forums there are now + s.fpage = s.forumsPage() + p = s.fpage + } if p.Menu() { s.menus = append(s.menus, p) } @@ -161,6 +178,12 @@ func (s *session) goTo(p *Page) { // show shows a page. func (s *session) show(p *Page) { + if p.ID == "FORUMS" { + if s.fpage == nil { + s.fpage = s.forumsPage() + } + p = s.fpage + } s.page = p s.more = nil if len(s.stack) == 0 || s.stack[len(s.stack)-1] != p { diff --git a/internal/cis/cis.go b/internal/cis/cis.go index 87dbb03..91cb75b 100644 --- a/internal/cis/cis.go +++ b/internal/cis/cis.go @@ -64,6 +64,7 @@ type session struct { more []string firstGo string // /GO:word at the User ID byName bool // logged on with a BBS name, not a User ID + fpage *Page // the FORUMS menu, as last made pushed []byte // input put back cr bool // the last input byte was a CR (drop a LF or NUL after it) } diff --git a/internal/cis/forum.go b/internal/cis/forum.go new file mode 100644 index 0000000..900d4ce --- /dev/null +++ b/internal/cis/forum.go @@ -0,0 +1,832 @@ +package cis + +import ( + "errors" + "fmt" + "sort" + "strconv" + "strings" + "time" + + "github.com/D-Modem/bbs/internal/store" +) + +// Forum is a CompuServe forum: a group of the BBS's message areas, each a +// section (CompuServe's forums had up to 18, numbered from 0). Its +// message numbers are the BBS's: forum-wide and rising, with gaps. +type Forum struct { + Go string // its GO word + Name string // "Amiga Tech Forum" + Sections []*store.Area +} + +// maxSections is how many sections a forum has at most. +const maxSections = 18 + +// Forums are the forums a user can read: one for each group of message +// areas (and one, named after the BBS, for those in none), split where a +// group has more than 18. +func (s *session) forums() ([]*Forum, error) { + areas, err := s.b.Store.Areas() + if err != nil { + return nil, err + } + byGroup := map[string][]*store.Area{} + var groups []string + for _, a := range areas { + if !s.b.CanRead(s.user, a) { + continue + } + g := a.Group + if _, ok := byGroup[g]; !ok { + groups = append(groups, g) + } + byGroup[g] = append(byGroup[g], a) + } + var out []*Forum + used := map[string]bool{} + for _, g := range groups { + name := g + if name == "" { + name = s.b.Conf().Name + } + as := byGroup[g] + for part := 0; len(as) > 0; part++ { + n := min(len(as), maxSections) + f := &Forum{Name: forumName(name, part), Sections: as[:n]} + f.Go = goWord(name, part, used) + out = append(out, f) + as = as[n:] + } + } + return out, nil +} + +func forumName(name string, part int) string { + if !strings.HasSuffix(strings.ToLower(name), "forum") { + name += " Forum" + } + if part > 0 { + name += fmt.Sprintf(" %d", part+1) + } + return name +} + +// goWord is a forum's GO word: its name's letters and digits, upper case, +// at most 10, made unique. +func goWord(name string, part int, used map[string]bool) string { + var b strings.Builder + for _, r := range strings.ToUpper(name) { + if (r >= 'A' && r <= 'Z' || r >= '0' && r <= '9') && b.Len() < 10 { + b.WriteRune(r) + } + } + w := b.String() + if w == "" { + w = "FORUM" + } + if part > 0 { + w += strconv.Itoa(part + 1) + } + base := w + for i := 2; used[w]; i++ { + w = base + strconv.Itoa(i) + } + used[w] = true + return w +} + +// forum finds a forum by GO word. +func (s *session) forum(word string) *Forum { + fs, err := s.forums() + if err != nil { + return nil + } + word = strings.ToUpper(strings.TrimSpace(word)) + for _, f := range fs { + if f.Go == word { + return f + } + } + return nil +} + +// forumsPage is the FORUMS menu, made from the forums there are. +func (s *session) forumsPage() *Page { + p := &Page{ID: "FORUMS", Title: "CompuServe"} + fs, _ := s.forums() + for _, f := range fs { + p.Items = append(p.Items, Item{Text: f.Name, Target: f.Go}) + } + if len(fs) == 0 { + p.Text = []string{" There are no forums here yet."} + } + return p +} + +// section is the section number of an area in f, or -1. +func (f *Forum) section(area int64) int { + for i, a := range f.Sections { + if a.ID == area { + return i + } + } + return -1 +} + +func sectionName(a *store.Area) string { + n := a.Name + if len(n) > 20 { + n = n[:20] + } + return n +} + +// forumState is a caller's time in a forum. +type forumState struct { + f *Forum + messages bool // at "Messages !" rather than "Forum !" + nonstop bool // OPT TYPE NONSTOP: no pause after each message + read map[int64]int64 // the highest read here, by area + last *store.Message // the message last shown +} + +// enterForum is GO to a forum: its welcome, then its command prompt until +// the caller leaves. +func (s *session) enterForum(f *Forum) error { + st := &forumState{f: f, read: map[int64]int64{}} + s.node.Doing("CompuServe: " + f.Name) + lo, hi := int64(0), int64(0) + var lastRead int64 + for _, a := range f.Sections { + if a.Messages > 0 { + if lo == 0 || a.FirstID < lo { + lo = a.FirstID + } + hi = max(hi, a.LastID) + } + lr, _ := s.b.Store.LastRead(s.user.ID, a.ID) + st.read[a.ID] = lr + lastRead = max(lastRead, lr) + } + s.out("One moment please...\n") + s.outf("Welcome to %s, V. 4D(1)\n\n", f.Name) + s.outf("Hello, %s\n", s.user.Name) + if !s.user.LastLogin.IsZero() { + s.outf("Last visit: %s\n", s.user.LastLogin.Format("02-Jan-06 15:04:05")) + } + s.outf("Forum messages: %6d to %6d\n", lo, hi) + s.outf("Last message you've read: %6d\n", lastRead) + s.out("Subtopic(s) Selected:\n All Accessible\n") + s.out("No members are in conference.\n") + if n := s.waiting(st); n > 0 { + s.outf("You have %d message(s) waiting.\n", n) + } + for { + p := "Forum !" + if st.messages { + p = "Messages !" + } + line, err := s.prompt(p) + if errors.Is(err, errAbort) { + continue + } + if err != nil { + return err + } + leave, err := s.forumCommands(st, line) + if err != nil { + return err + } + if leave != nil { + s.exitForum(st, hi) + return leave() + } + } +} + +// exitForum is the leaving text. +func (s *session) exitForum(st *forumState, hi int64) { + var lr int64 + for _, id := range st.read { + lr = max(lr, id) + } + s.outf("Exiting at %s\n", s.now().Format("02-Jan-06 15:04:05")) + s.outf("Last message in forum: %10d\n", hi) + s.outf("Last message you've read: %6d\n", lr) + s.outf("Thank you for visiting %s\n", st.f.Name) +} + +// forumCommands does a line of forum commands. A command that leaves the +// forum returns what to do after the leaving text. +func (s *session) forumCommands(st *forumState, line string) (func() error, error) { + cmds := strings.Split(line, ";") + for i := 0; i < len(cmds); i++ { + c := strings.TrimSpace(cmds[i]) + verb, arg := splitVerb(c) + switch { + case c == "": + if len(cmds) == 1 { + continue + } + case is(verb, "OPTIONS", 2): + // OPT's settings follow it on the line: OPT;TYP N;SKI N;... + rest := cmds[i+1:] + i = len(cmds) + s.options(st, arg, rest) + case is(verb, "MESSAGES", 3): + st.messages = true + if arg != "" { + cmds = append(cmds[:i+1], append([]string{arg}, cmds[i+1:]...)...) + } + case verb == "M" || is(verb, "MENU", 4): + if st.messages { + st.messages = false + continue + } + return func() error { s.show(s.page); return nil }, nil + case is(verb, "READ", 3) || verb == "R" || verb == "RS" || verb == "RN" || verb == "RT", + is(verb, "BROWSE", 2): + rest, err := s.readCmd(st, verb, arg, is(verb, "BROWSE", 2)) + if err != nil || rest == "" { + if err != nil { + return nil, err + } + continue + } + if leave, err := s.forumCommands(st, rest); leave != nil || err != nil { + return leave, err + } + case is(verb, "SCAN", 2): + s.scan(st, arg) + case is(verb, "POST", 3) || is(verb, "COMPOSE", 3) || is(verb, "LEAVE", 3) || verb == "L": + if err := s.post(st, arg, nil); err != nil { + return nil, err + } + case is(verb, "DESCRIPTIONS", 2) || is(verb, "SECTIONS", 3): + s.sections(st) + case is(verb, "NEW", 2): + s.newCounts(st) + case is(verb, "LIBRARY", 3) || is(verb, "DL", 2): + s.out("The libraries of this forum are not open yet.\n") + case is(verb, "CONFERENCE", 2): + s.out("Conferencing is not open yet.\n") + case is(verb, "HELP", 1) || verb == "?": + s.forumHelp() + case is(verb, "EXIT", 2) || verb == "T" || is(verb, "TOP", 3): + if verb == "T" || is(verb, "TOP", 3) { + return func() error { s.goTo(s.pages.Get("TOP")); return nil }, nil + } + return func() error { s.show(s.page); return nil }, nil + case verb == "G" || is(verb, "GO", 2): + w := arg + return func() error { return s.command("GO " + w) }, nil + case is(verb, "OFF", 3) || is(verb, "BYE", 3): + return func() error { s.logoff(); return errHangup }, nil + case is(verb, "LOG", 3): + return func() error { s.logoff(); return errRelog }, nil + case is(verb, "SET", 3): + s.set(arg) + default: + s.outf("%s is not a forum command !\n", strings.ToUpper(c)) + } + } + return nil, nil +} + +// splitVerb is a command's first word and the rest; "BRO/LONG" is +// "BRO", "/LONG". +func splitVerb(c string) (string, string) { + i := strings.IndexAny(c, " /") + if i < 0 { + return strings.ToUpper(c), "" + } + return strings.ToUpper(c[:i]), strings.TrimSpace(c[i:]) +} + +// is reports whether v abbreviates word, in at least n letters. +func is(v, word string, n int) bool { + return len(v) >= n && len(v) <= len(word) && strings.HasPrefix(word, v) +} + +// options is OPT: its settings, given after it. +func (s *session) options(st *forumState, arg string, rest []string) { + if arg != "" { + rest = append([]string{arg}, rest...) + } + if len(rest) == 0 { + s.out("TYPE (N)onstop or (P)ause, MODE (COM)mand -- e.g. OPT;TYP N\n") + return + } + for _, r := range rest { + verb, val := splitVerb(strings.TrimSpace(r)) + val = strings.ToUpper(strings.TrimSpace(val)) + switch { + case is(verb, "TYPE", 2): + st.nonstop = strings.HasPrefix(val, "N") + } + // the rest (SKIP, MODE, INITIAL, CHARACTER, PERMANENT...) are + // taken: this forum is always in command mode + } + s.out("Options set\n") +} + +// query is what REA, SCA and BRO choose. +type query struct { + newOnly bool + waiting bool + reverse bool + thread bool + from, to string + subject string + sections map[int]bool + start int64 + days int + numbers []int64 +} + +func parseQuery(arg string) query { + q := query{newOnly: true} + for _, f := range strings.FieldsFunc(arg, func(r rune) bool { return r == ' ' || r == '/' }) { + k, v, hasV := strings.Cut(f, ":") + K := strings.ToUpper(k) + switch { + case isNumber(K): + n, _ := strconv.ParseInt(K, 10, 64) + q.numbers = append(q.numbers, n) + q.newOnly = false + case !hasV && is(K, "NEW", 3): + q.newOnly = true + case !hasV && is(K, "ALL", 3): + q.newOnly = false + case !hasV && is(K, "WAITING", 3): + q.waiting = true + case !hasV && is(K, "REVERSE", 3): + q.reverse, q.newOnly = true, false + case !hasV && is(K, "THREAD", 3): + q.thread = true + case !hasV && is(K, "FORWARD", 3): + q.reverse = false + case hasV && is(K, "FROM", 3): + q.from = v + case hasV && K == "TO": + q.to = v + case hasV && is(K, "SUBJECT", 3): + q.subject = v + case hasV && is(K, "SECTIONS", 3): + q.sections = map[int]bool{} + for _, n := range strings.Split(v, ",") { + if k, err := strconv.Atoi(strings.TrimSpace(n)); err == nil { + q.sections[k] = true + } + } + case hasV && is(K, "START", 3): + q.start, _ = strconv.ParseInt(v, 10, 64) + q.newOnly = false + case hasV && K == "DAYS": + q.days, _ = strconv.Atoi(v) + q.newOnly = false + case hasV && is(K, "NUMBER", 3): + if n, err := strconv.ParseInt(v, 10, 64); err == nil { + q.numbers = append(q.numbers, n) + q.newOnly = false + } + } + } + return q +} + +// findMessages is the forum's messages q chooses, oldest first (newest, REVERSE). +func (s *session) findMessages(st *forumState, q query) []*store.Message { + var out []*store.Message + if len(q.numbers) > 0 { + for _, n := range q.numbers { + m, err := s.b.Store.Message(n) + if err == nil && st.f.section(m.AreaID) >= 0 { + out = append(out, m) + } + } + return out + } + cut := time.Time{} + if q.days > 0 { + cut = s.now().AddDate(0, 0, -q.days) + } + for i, a := range st.f.Sections { + if q.sections != nil && !q.sections[i] { + continue + } + after := q.start - 1 + if q.newOnly || q.waiting { + after = max(after, st.read[a.ID]) + } + ms, err := s.b.Store.Messages(a.ID, max(after, 0), 5000) + if err != nil { + continue + } + for _, m := range ms { + switch { + case !cut.IsZero() && m.Created.Before(cut): + case q.waiting && !strings.EqualFold(m.To, s.user.Name): + case q.from != "" && !matches(m.From, q.from) && PPN(m.FromID) != q.from: + case q.to != "" && !matches(m.To, q.to): + case q.subject != "" && !matches(m.Subject, q.subject): + default: + out = append(out, m) + } + } + } + sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID }) + if q.thread { + // each topic together, in the order its first message came + root := func(m *store.Message) int64 { + if m.Thread != 0 { + return m.Thread + } + return m.ID + } + sort.SliceStable(out, func(i, j int) bool { return root(out[i]) < root(out[j]) }) + } + if q.reverse { + for i, j := 0, len(out)-1; i < j; i, j = i+1, j-1 { + out[i], out[j] = out[j], out[i] + } + } + return out +} + +func matches(field, want string) bool { + return strings.Contains(strings.ToUpper(field), strings.ToUpper(strings.TrimSpace(want))) +} + +// waiting counts the new messages to the caller. +func (s *session) waiting(st *forumState) int { + return len(s.findMessages(st, query{waiting: true})) +} + +// replies are the numbers of the messages that answer m. +func (s *session) replies(m *store.Message) []int64 { + ms, _, err := s.b.Store.Thread(m) + if err != nil { + return nil + } + var out []int64 + for _, r := range ms { + if r.ReplyTo == m.ID { + out = append(out, r.ID) + } + } + return out +} + +// header is a message's header, as forum software 4 printed it: +// +// #: 210094 S10/Views:Zachmann +// 04-Jul-92 12:33:25 +// Sb: #210070-#Independence Declaration +// Fm: Bruce Biermann 72241,261 +// To: William F. Zachmann 72241,43 +func (s *session) header(st *forumState, m *store.Message, replies []int64) []string { + sec := st.f.section(m.AreaID) + sb := m.Subject + if len(replies) > 0 { + sb = "#" + sb + } + if m.ReplyTo != 0 { + sb = fmt.Sprintf("#%d-%s", m.ReplyTo, sb) + } + to := m.To + if to == "" { + to = "All" + } + if u, err := s.b.Store.UserByName(m.To); err == nil { + to += " " + PPN(u.ID) + } + from := m.From + if m.FromID != 0 { + from += " " + PPN(m.FromID) + } + return []string{ + fmt.Sprintf("#: %d S%d/%s", m.ID, sec, sectionName(st.f.Sections[sec])), + " " + m.Created.Format("02-Jan-06 15:04:05"), + "Sb: " + sb, + "Fm: " + from, + "To: " + to, + } +} + +// replyLine is the footer: "* Reply: n" or "* Replies: n, n". +func replyLine(replies []int64) []string { + if len(replies) == 0 { + return nil + } + label := "* Reply: " + if len(replies) > 1 { + label = "* Replies: " + } + var out []string + line := label + for i, r := range replies { + item := strconv.FormatInt(r, 10) + if i < len(replies)-1 { + item += "," + } + if len(line)+len(item)+1 > 72 { + out = append(out, line) + line = strings.Repeat(" ", 12) + } else if i > 0 { + line += " " + } + line += item + } + return append(out, line) +} + +// showMessage prints a message whole. +func (s *session) showMessage(st *forumState, m *store.Message) { + replies := s.replies(m) + lines := append(s.header(st, m, replies), "") + body := strings.Split(strings.TrimRight(strings.ReplaceAll(m.Body, "\r\n", "\n"), "\n"), "\n") + lines = append(lines, body...) + if r := replyLine(replies); r != nil { + lines = append(append(lines, ""), r...) + } + s.out("\n") + for _, l := range lines { + s.out(l + "\n") + } + st.last = m + if m.ID > st.read[m.AreaID] { + st.read[m.AreaID] = m.ID + s.b.Store.SetLastRead(s.user.ID, m.AreaID, m.ID) + } +} + +// readCmd is READ (or BROWSE: its subjects first, each read on a CR). +// What the caller types at a read prompt that is not for it ends the +// reading, and is returned to be done as a forum command. +func (s *session) readCmd(st *forumState, verb, arg string, browse bool) (string, error) { + q := parseQuery(arg) + switch verb { + case "RS", "RT": + q.thread = true + } + ms := s.findMessages(st, q) + if len(ms) == 0 { + s.out("No messages\n") + return "", nil + } + for i := 0; i < len(ms); i++ { + m := ms[i] + if browse { + s.out("\n") + for _, l := range s.header(st, m, s.replies(m)) { + s.out(l + "\n") + } + line, err := s.prompt("Press to read, N for next, or type CHOICES !") + if errors.Is(err, errAbort) { + return "", nil + } + if err != nil { + return "", err + } + switch strings.ToUpper(strings.TrimSpace(line)) { + case "": + case "N", "NEXT": + continue + default: + return line, nil + } + } + s.showMessage(st, m) + if st.nonstop { + continue + } + for { + line, err := s.prompt("Read action !") + if errors.Is(err, errAbort) { + return "", nil + } + if err != nil { + return "", err + } + verb, rarg := splitVerb(strings.TrimSpace(line)) + switch { + case verb == "" || is(verb, "NEXT", 3): + case is(verb, "REPLY", 2): + if err := s.post(st, rarg, m); err != nil { + return "", err + } + continue + case is(verb, "REREAD", 3): + s.showMessage(st, m) + continue + case verb == "P" || is(verb, "PARENT", 3): + if m.ReplyTo == 0 { + s.out("This message has no parent.\n") + continue + } + if pm, err := s.b.Store.Message(m.ReplyTo); err == nil { + s.showMessage(st, pm) + } + continue + case is(verb, "CHOICES", 2): + s.out(" RE Reply RER Reread P Parent\n NEX or Next STOP Stop reading\n") + continue + case is(verb, "STOP", 2): + return "", nil + default: + return line, nil + } + break + } + } + return "", nil +} + +// scan is SCAN: the headers of the messages chosen, without their text; +// QUICK, one line each. +func (s *session) scan(st *forumState, arg string) { + quick := false + for _, f := range strings.Fields(strings.ToUpper(arg)) { + if is(f, "QUICK", 3) { + quick = true + } + } + ms := s.findMessages(st, parseQuery(arg)) + if len(ms) == 0 { + s.out("No messages\n") + return + } + for _, m := range ms { + if quick { + s.outf("%7d S%-2d %s\n", m.ID, st.f.section(m.AreaID), m.Subject) + continue + } + s.out("\n") + for _, l := range s.header(st, m, s.replies(m)) { + s.out(l + "\n") + } + } +} + +// sections lists the forum's sections. +func (s *session) sections(st *forumState) { + s.out("\nSections\n") + for i, a := range st.f.Sections { + s.outf("%2d %s\n", i, sectionName(a)) + } +} + +// newCounts is NEW: the new messages in each section. +func (s *session) newCounts(st *forumState) { + s.out("\nSection New\n") + for i, a := range st.f.Sections { + n, _ := s.b.Store.NewCount(s.user.ID, a.ID) + s.outf("%2d %-20s %4d\n", i, sectionName(a), n) + } +} + +func (s *session) forumHelp() { + s.out(` +Forum commands: + REA [NEW|ALL|WAI|REV|THR] [FROM:x] [TO:x] [SUB:x] [SEC:n,n] [n] + SCA [QUI] ... (as READ, headers only) BRO ... (as READ, a subject at a time) + POST [SEC:n] [TO:x] [SUB:x] DES (sections) NEW (new messages) + OPT;TYP N (nonstop) MES (Messages !) M or EXIT (leave) OFF +`) +} + +// post is POST (or REPLY to re, in its section with its subject). +func (s *session) post(st *forumState, arg string, re *store.Message) error { + var to, subject string + section := -1 + for _, f := range strings.FieldsFunc(arg, func(r rune) bool { return r == '/' }) { + k, v, ok := strings.Cut(strings.TrimSpace(f), ":") + K := strings.ToUpper(k) + switch { + case ok && K == "TO": + to = v + case ok && is(K, "SUBJECT", 3): + subject = v + case ok && is(K, "SECTIONS", 3): + if n, err := strconv.Atoi(v); err == nil { + section = n + } + } + } + var replyTo int64 + if re != nil { + replyTo = re.ID + section = st.f.section(re.AreaID) + subject = re.Subject + if to == "" { + to = re.From + } + } + var err error + if to == "" { + if to, err = s.prompt("To: "); err != nil { + return s.abortOK(err) + } + if strings.TrimSpace(to) == "" { + to = "All" + } + } + if subject == "" { + if subject, err = s.prompt("Subject: "); err != nil { + return s.abortOK(err) + } + if strings.TrimSpace(subject) == "" { + s.out("Message not stored\n") + return nil + } + } + body, done, err := s.edit() + if err != nil { + return s.abortOK(err) + } + for !done { + a, err := s.prompt("Post action !") + if err != nil { + return s.abortOK(err) + } + v, _ := splitVerb(strings.TrimSpace(a)) + switch { + case v == "S" || is(v, "STORE", 2) || is(v, "POST", 2): + done = true + case v == "A" || is(v, "ABORT", 2): + s.out("Message not stored\n") + return nil + case v == "C" || is(v, "CONTINUE", 2): + more, st2, err := s.edit() + if err != nil { + return s.abortOK(err) + } + body, done = append(body, more...), st2 + default: + s.out(" S Store the message\n C Continue entering text\n A Abort\n") + } + } + for section < 0 || section >= len(st.f.Sections) || !s.b.CanPost(s.user, st.f.Sections[section]) { + s.out("SUBTOPIC # REQUIRED\n") + for i, a := range st.f.Sections { + if s.b.CanPost(s.user, a) { + s.outf("%2d %s\n", i, sectionName(a)) + } + } + a, err := s.prompt("Enter choice : ") + if err != nil { + return s.abortOK(err) + } + if strings.TrimSpace(a) == "" { + s.out("Message not stored\n") + return nil + } + section, _ = strconv.Atoi(strings.TrimSpace(a)) + if section < 0 || section >= len(st.f.Sections) { + section = -1 + } + } + id, err := s.node.Post(st.f.Sections[section], strings.TrimSpace(to), strings.TrimSpace(subject), body, replyTo) + if err != nil { + s.outf("Message not stored: %v\n", err) + return nil + } + s.outf("Message # %d Stored\n", id) + return nil +} + +// abortOK is an error of a prompt: ^C gives up quietly. +func (s *session) abortOK(err error) error { + if errors.Is(err, errAbort) { + s.out("Message not stored\n") + return nil + } + return err +} + +// edit is CompuServe's EDIT for a new message: lines until /EX (asks what +// to do) or /POST (stores it). +func (s *session) edit() (lines []string, done bool, err error) { + s.out("Enter /EX to exit EDIT\n") + for { + l, err := s.input(true, s.b.Conf().IdleTimeout) + if err != nil { + if errors.Is(err, errIdle) { + return nil, false, errHangup + } + return nil, false, err + } + switch strings.ToUpper(strings.TrimSpace(l)) { + case "/EX", "/EXIT": + return lines, false, nil + case "/POST", "/STORE": + return lines, true, nil + } + lines = append(lines, l) + if len(lines) > 1000 { + return lines, false, nil + } + } +} diff --git a/internal/cis/pages.txt b/internal/cis/pages.txt index 9ea0ec2..2837145 100644 --- a/internal/cis/pages.txt +++ b/internal/cis/pages.txt @@ -51,12 +51,13 @@ log on with your BBS name or your User ID (shown at logon) and your BBS password. - Here now: the menus and articles, GO words and FIND, and the - terminal settings of SET. + Here now: the menus and articles, GO words and FIND, the terminal + settings of SET, and the forums (GO FORUMS): the BBS's message + areas, a forum for each group of them, with the forum command + language (REA, SCA, BRO, POST, OPT). - On their way, in this order: forums (the BBS's message areas, with - the forum command language), libraries and the CompuServe B and - B Plus protocols, CompuServe Mail, the CB Simulator, and the + On their way, in this order: forum libraries with the CompuServe B + and B Plus protocols, CompuServe Mail, the CB Simulator, and the CompuServe Information Manager (CIM) graphical service. :RATES CHARGES BILLING NEWBIL @@ -134,9 +135,7 @@ :FORUMS SIGS =CompuServe - Forums are on their way: each of the BBS's message areas is to be a - forum section, with the forum command language (REA, SCA, BRO, - LIB...). Until then, read and post on the BBS itself. + (This menu is made from the BBS's message areas when shown.) :CB CB-1 CHAT =CB Simulator diff --git a/internal/server/cisforum_test.go b/internal/server/cisforum_test.go new file mode 100644 index 0000000..1189e50 --- /dev/null +++ b/internal/server/cisforum_test.go @@ -0,0 +1,119 @@ +package server + +import ( + "strings" + "testing" + + "github.com/D-Modem/bbs/internal/bbs" + "github.com/D-Modem/bbs/internal/ui3270" +) + +// cisLogin logs a user on to the CompuServe listener, to the TOP menu. +func cisLogin(e *env, id, pw string) *dialog { + d := e.dial("cis.sock") + d.expect("Host Name: ") + d.send("CIS\r") + d.expect("User ID: ") + d.send(id + "\r") + d.expect("Password: ") + d.send(pw + "\r") + d.expect("Enter choice !") + return d +} + +// TestCompuServeForums: the message areas as forums: a post from one +// caller, read, replied to and scanned by another in the period's format. +func TestCompuServeForums(t *testing.T) { + e := startFull(t, ui3270.Graphics{}, nil, func(b *bbs.BBS, dir string) { + for _, name := range []string{"Hardware", "Software"} { + a, err := b.Store.CreateArea(name, "", false) + if err != nil { + t.Fatal(err) + } + b.Store.SetAreaGroup(a.ID, "Amiga Tech") + } + }, cisListener()) + + d := cisLogin(e, "72000,1", "alicepass1") + d.send("GO FORUMS\r") + d.expect(" 1 Test BBS Forum") + d.expect(" 2 Amiga Tech Forum") + d.expect("Enter choice !") + d.send("2\r") + d.expect("Welcome to Amiga Tech Forum, V. 4D(1)") + d.expect("Hello, alice") + d.expect("Forum !") + d.send("DES\r") + d.expect(" 0 Hardware\r\n 1 Software\r\n") + d.expect("Forum !") + d.send("POST SEC:1\r") + d.expect("To: ") + d.send("sysop\r") + d.expect("Subject: ") + d.send("Kickstart 1.3\r") + d.expect("Enter /EX to exit EDIT\r\n") + d.send("Where do I find it?\r/ex\r") + d.expect("Post action !") + d.send("S\r") + d.expect("Message # ") + d.expect(" Stored") + d.expect("Forum !") + d.send("EXIT\r") + d.expect("Thank you for visiting Amiga Tech Forum") + d.expect("Enter choice !") + d.send("OFF\r") + d.expect("Thank you for using CompuServe!") + + d = cisLogin(e, "72000,2", "sysoppass1") + d.send("GO AMIGATECH\r") + d.expect("Forum messages:") + d.expect("You have 1 message(s) waiting.") + d.expect("Forum !") + d.send("OPT;TYP N;SKI N;MOD COM\r") + d.expect("Options set") + d.expect("Forum !") + d.send("REA NEW\r") + d.expect("#: ") + d.expect(" S1/Software\r\n ") + d.expect("\r\nSb: Kickstart 1.3\r\nFm: alice 72000,1\r\nTo: sysop 72000,2\r\n\r\nWhere do I find it?\r\n") + d.expect("Forum !") + d.send("OPT;TYP P\r") + d.expect("Forum !") + d.send("REA ALL\r") + d.expect("Where do I find it?") + d.expect("Read action !") + d.send("RE\r") + d.expect("Enter /EX to exit EDIT") + d.send("In ROM.\r/post\r") + d.expect(" Stored") + d.expect("Read action !") + d.send("\r") + d.expect("Forum !") + d.send("MESS;SCA ALL;M\r") + d.expect("Sb: #Kickstart 1.3\r\n") + d.expect("-Kickstart 1.3\r\nFm: sysop 72000,2\r\nTo: alice 72000,1\r\n") + d.expect("Forum !") + d.send("SCA QUI ALL\r") + d.expect(" S1 Kickstart 1.3\r\n") + d.send("REA ALL FROM:sysop\r") + d.expect("In ROM.") + d.expect("Read action !") + d.send("P\r") // the parent + d.expect("Where do I find it?\r\n\r\n* Reply: ") + d.expect("Read action !") + d.send("STOP\r") + d.expect("Forum !") + // what TAPCIS sends (its scripts wait for "um !" and "es !") + for _, c := range []string{"OPT;TYP N;SKI N;MOD COM;INI;1;CHA;P", "MESS;SCA QUI NEW;M", + "REA NEW", "REA WAI", "MESS;SCA WAI", "REA THR NUM:U", "RS"} { + d.send(c + "\r") + d.expect(" !") + if b := string(d.before); !strings.HasSuffix(b, "Forum") && !strings.HasSuffix(b, "Messages") || + strings.Contains(b, "not a forum command") { + t.Errorf("%s: %s", c, d.before) + } + } + d.send("OFF\r") + d.expect("Exiting at ") + d.expect("Thank you for using CompuServe!") +}