Files
bbs/internal/api/forum.go
T
Claude 32529543d6 Image boards on the terminals, with their pictures
The text BBS and 3270s have the image boards (B on the main menu, or a
menu's boards): the boards, threads by bump order, posts read as
messages with who posted, the thread, and the posts that quote them.
Callers post as the web's posters do (names, tripcodes, sage, staff's
capcodes), through the same checks: posting is now Submit, shared by
the web's form and bbs.ImageBoards. A thread's image comes by the
terminal's transfer protocol.

Pictures (view.Picture, on messages and panels) are shown as well as
the terminal can: kitty's graphics protocol (kitty, Ghostty), sixel,
iTerm2's inline images (iTerm2, mintty), or coloured half blocks in 256
colours on other UTF-8 terminals; described in words elsewhere and on
3270s. V shows the image whole; pictures on/off is on the profile. The
web terminal has xterm.js's image addon, so sixel there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
2026-10-14 02:16:21 +00:00

91 lines
2.8 KiB
Go

package api
import (
"errors"
"net/http"
"time"
"github.com/Neo-Desktop/bbs-sec/internal/forum"
"github.com/Neo-Desktop/bbs-sec/internal/imageboard"
"github.com/Neo-Desktop/bbs-sec/internal/store"
)
// The web forum's logins are the API's sessions: a browser's cookie holds
// a session token, as a program's Authorization header does.
type forumSessions struct{ s *Server }
func (f forumSessions) Login(r *http.Request, name, password string) (string, *store.User, error) {
s := f.s
u, err := s.B.Authenticate("forum", "ip:"+hostOf(r), name, password)
if err != nil {
return "", nil, err
}
scopes := "read post mail chat files"
secret, hash := NewToken()
if _, err := s.B.Store.AddToken(&store.Token{UserID: u.ID, Name: "forum login from " + hostOf(r), Hash: hash,
Scopes: scopes, Session: true, Expires: time.Now().Add(s.S.SessionTTL)}); err != nil {
return "", nil, errors.New("the system is in trouble; try again later")
}
s.B.Store.LoggedIn(u.ID)
s.B.Stats.Logins.Add(1)
s.B.Log.Printf("forum: %s signed in from %s", u.Name, hostOf(r))
return secret, u, nil
}
func (f forumSessions) User(secret string) *store.User {
w, err := f.s.byToken(secret)
if err != nil || w == nil || !w.token.Session {
return nil // an API key is no browser's cookie
}
return w.user
}
func (f forumSessions) Logout(secret string) {
if w, err := f.s.byToken(secret); err == nil && w != nil && w.token.Session {
f.s.B.Store.RevokeToken(w.token.ID, w.user.ID)
}
}
// forum is the web forum's handler, its requests counted against the
// caller's address as the API's are.
func (s *Server) forum() (http.Handler, error) {
f := &forum.Server{B: s.B, Sessions: forumSessions{s}, Guests: s.S.Guests, Secure: s.S.SecureCookies,
Terminal: s.terminalOn(), TTL: s.S.SessionTTL, Theme: s.S.Theme}
h, err := f.Handler()
if err != nil {
return nil, err
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !s.allow("ip:" + hostOf(r)) {
w.Header().Set("Retry-After", "1")
http.Error(w, "too many requests: slow down", http.StatusTooManyRequests)
return
}
h.ServeHTTP(w, r)
}), nil
}
// imageboard is the image boards' handler, its pages counted as the
// forum's are (not their images: a thread can have a hundred); their
// logins are the forum's sessions.
func (s *Server) imageboard() (http.Handler, error) {
ib := s.Chan
if ib == nil {
ib = &imageboard.Server{B: s.B, S: s.S.ChanSettings, Secure: s.S.SecureCookies, TTL: s.S.SessionTTL}
}
ib.Sessions = forumSessions{s}
h, err := ib.Handler()
if err != nil {
return nil, err
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !imageboard.IsAsset(r) && !s.allow("ip:"+hostOf(r)) {
w.Header().Set("Retry-After", "1")
http.Error(w, "too many requests: slow down", http.StatusTooManyRequests)
return
}
h.ServeHTTP(w, r)
}), nil
}