Files
Claude b81f07e558 ymsg: Y!M 6 to 8's login, and file transfer
The challenge login of Yahoo! Messenger 6 to 8 (auth type 1) is served
for YMSG 10 to 15. The BBS writes each challenge so that the client's
search lands on depth 0, table 0: the transform then runs zero times and
the server needs none of Yahoo's tables. old_login = yes offers these
clients the Y!M 5 login instead.

Files go between Yahoo users through the web half: Y!M 7 and later by
the relay (FILETRANSFER_15 to _ACC_15, then /relay POST, HEAD and GET),
Y!M 5 and 6 by /notifyft, with a /ft/ link sent to the receiver. Files
are held in a temporary file until fetched, at most max_file_mb (16)
and an hour. Offers to users not on Yahoo are declined with a pointer
to the file areas.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRRfVskEUYFs9gMFCLD
2026-11-10 16:22:47 +00:00

289 lines
7.6 KiB
Go

// Package ymsg is a Yahoo! Messenger server (YMSG) for the BBS's chat,
// beside its IRC, AIM and MSN servers: Yahoo! Messenger clients sign in
// with BBS names and passwords; their buddy list is the user's friends
// list and their ignore list the blocked list; an IM is a private message
// (a PRIVMSG to an IRC user, a line on a caller's terminal, an IM on AIM
// or MSN), kept for one away; a Yahoo chat room is a BBS chat room, and a
// conference a room of its own.
//
// It speaks YMSG 9 to 16. Clients before YMSG 16 (Y!M 5.x, and those
// that take the login the server offers) sign in by the challenge login
// whose digests the store keeps (auth type 0); YMSG 16 clients (Y!M 9 and
// later) send the password over HTTPS to "login.yahoo.com", which the web
// half of the server answers (http.go), and then prove the token it gave.
package ymsg
import (
"context"
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"math/big"
"net"
"strings"
"sync"
"time"
"github.com/D-Modem/bbs/internal/bbs"
"github.com/D-Modem/bbs/internal/sockets"
"github.com/D-Modem/bbs/internal/store"
"github.com/D-Modem/bbs/internal/tcpserv"
)
// timeouts
const (
loginTimeout = time.Minute
idleTimeout = 15 * time.Minute // clients PING or KEEPALIVE every minute or so
writeTimeout = 30 * time.Second
tokenLife = 5 * time.Minute // from pwtoken_get to the sign-in
queueLen = 512
)
// Settings are the [ymsg] section's.
type Settings struct {
Address string // the pager, where to listen (":5050")
// HTTPAddress and HTTPSAddress serve the web half: the capacity
// lookup, the room list and (HTTPS) the YMSG 16 login ("": none)
HTTPAddress string
HTTPSAddress string
CertFile string
KeyFile string
// Host is the address the capacity lookup gives clients for the pager
// ("": the address each one asked on)
Host string
MaxClients int
MaxPerIP int
// System is the Yahoo ID the BBS's notices come from
System string
// OldLogin offers Y!M 6 to 8 (YMSG 10 to 15) the Y!M 5 challenge
// login (auth type 0) instead of their own (auth type 1)
OldLogin bool
// MaxFileSize is the largest file users may send each other (0: no
// file transfer). It needs the web half on port 80.
MaxFileSize int64
}
// Server serves YMSG.
type Server struct {
B *bbs.BBS
S Settings
mu sync.Mutex
sessions map[*session]bool
byNode map[int]*session
tokens map[string]*token // pwtoken_get's, by token
cookies map[string]*token // pwtoken_login's, by T cookie
xfers map[string]*xfer // files on their way, by token
}
// token is a YMSG 16 login in progress: the user whose password was
// checked, and what pwtoken_login gave.
type token struct {
u *store.User
crumb string
y, t string
expires time.Time
}
// New checks the settings and makes the server.
func New(b *bbs.BBS, s Settings) (*Server, error) {
if s.Address == "" {
return nil, errors.New("ymsg: no address to listen on")
}
if s.System == "" {
s.System = "bbs"
}
if s.MaxClients == 0 {
s.MaxClients = 256
}
return &Server{B: b, S: s, sessions: map[*session]bool{}, byNode: map[int]*session{},
tokens: map[string]*token{}, cookies: map[string]*token{}, xfers: map[string]*xfer{}}, nil
}
// Run listens until ctx is done: the pager, and the web half if set.
func (s *Server) Run(ctx context.Context) error {
ln, err := sockets.Listen("tcp", s.S.Address)
if err != nil {
return fmt.Errorf("ymsg: %w", err)
}
errs := make(chan error, 3)
if s.S.HTTPAddress != "" || s.S.HTTPSAddress != "" {
go func() { errs <- s.serveWeb(ctx) }()
}
go func() { errs <- s.Serve(ctx, ln) }()
return <-errs
}
// Serve answers the pager on ln until ctx is done.
func (s *Server) Serve(ctx context.Context, ln net.Listener) error {
s.B.Store.SetKeepYMSG(true)
stop := s.B.WatchPresence(s.presence)
defer stop()
stopFriends := s.B.WatchFriends(s.friendsChanged)
defer stopFriends()
ts := &tcpserv.Server{Name: "ymsg", MaxClients: s.S.MaxClients, MaxPerIP: s.S.MaxPerIP, Log: s.B.Log,
Handle: s.handle,
Refuse: func(c net.Conn) bool { return s.B.Refuse("ymsg", c.RemoteAddr()) }}
return ts.Serve(ctx, ln)
}
// Clients is how many are signed in.
func (s *Server) Clients() int {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.sessions)
}
// idOf is a user's Yahoo ID: their name in lower case, without spaces.
func idOf(u *store.User) string { return idOfName(u.Name) }
// idOfName is the Yahoo ID for a BBS name.
func idOfName(name string) string { return strings.ToLower(strings.ReplaceAll(name, " ", "")) }
// systemID is the ID the BBS's notices come from.
func (s *Server) systemID() string { return idOfName(s.S.System) }
// resolve is the account a Yahoo ID names (nil: none): a name with or
// without its spaces, in any case, without any "@yahoo.com".
func (s *Server) resolve(id string) *store.User {
id = strings.TrimSpace(id)
if i := strings.IndexByte(id, '@'); i >= 0 {
id = id[:i]
}
if id == "" {
return nil
}
u, err := s.B.Store.UserByScreenName(id)
if err != nil {
return nil
}
return u
}
// may: the user may use the chat.
func (s *Server) may(u *store.User) error {
if !s.B.LevelOf(u).Chat {
return bbs.ErrNotAllowed
}
return nil
}
func (s *Server) register(ss *session) {
s.mu.Lock()
s.sessions[ss] = true
s.byNode[ss.n.Num] = ss
s.mu.Unlock()
}
func (s *Server) unregister(ss *session) {
s.mu.Lock()
delete(s.sessions, ss)
if s.byNode[ss.n.Num] == ss {
delete(s.byNode, ss.n.Num)
}
s.mu.Unlock()
}
// all are the sessions signed in.
func (s *Server) all() []*session {
s.mu.Lock()
defer s.mu.Unlock()
out := make([]*session, 0, len(s.sessions))
for ss := range s.sessions {
out = append(out, ss)
}
return out
}
// sessionsOf are a user's sessions.
func (s *Server) sessionsOf(id int64) []*session {
var out []*session
for _, ss := range s.all() {
if ss.u.ID == id {
out = append(out, ss)
}
}
return out
}
// onNode is the session on a node (nil: the node is another kind).
func (s *Server) onNode(num int) *session {
s.mu.Lock()
defer s.mu.Unlock()
return s.byNode[num]
}
// nodesOf are the nodes a user is on.
func (s *Server) nodesOf(name string) []bbs.NodeInfo {
var out []bbs.NodeInfo
for _, n := range s.B.Nodes() {
if n.User != "" && strings.EqualFold(n.User, name) {
out = append(out, n)
}
}
return out
}
// presence is told of a BBS user coming online or going.
func (s *Server) presence(name string, online bool) {
if len(s.all()) == 0 {
return
}
go func() {
u, err := s.B.Store.UserByName(name)
if err != nil {
return
}
s.changed(u)
}()
}
// changed has every session told the user's state anew (where it
// differs from what it was told).
func (s *Server) changed(u *store.User) {
for _, x := range s.all() {
x.do(func() { x.buddyChanged(u) })
}
}
// friendsChanged is told that a user's friends or blocked list changed:
// their own sessions sync their lists; the others what they see of them.
func (s *Server) friendsChanged(user int64) {
go func() {
u, err := s.B.Store.UserByID(user)
if err != nil {
return
}
for _, x := range s.all() {
if x.u.ID == user {
x.do(x.syncLists)
} else {
x.do(func() { x.buddyChanged(u) })
}
}
}()
}
// randomString is n random letters and digits.
func randomString(n int) string {
const digits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, n)
for i := range b {
x, _ := rand.Int(rand.Reader, big.NewInt(int64(len(digits))))
b[i] = digits[x.Int64()]
}
return string(b)
}
// randomSession is a session id (never 0).
func randomSession() uint32 {
var b [4]byte
for {
rand.Read(b[:])
if v := binary.BigEndian.Uint32(b[:]) & 0x7fffffff; v != 0 {
return v
}
}
}