mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
IRC forgets what is said; now every private message is kept, however it was sent (the BBS's chat, IRC, AIM, ICQ), in a new chats table. One sent to a user who is not on is kept for them and given them on their next login: on IRC as a PRIVMSG with its time (301 tells the sender), on AIM as an IM once the client is ready, on ICQ as its offline messages (15/03 0x41), on a terminal at its next prompt. /history in chat and BBSServ HISTORY read a conversation back. /msg in chat takes a user's name as well as a node's number. Each user has one friends list (friends: user, friend, group, alias), the same on every front end: /friends, /friend and /unfriend in chat, BBSServ FRIENDS on IRC, and the AIM/ICQ buddy list. A buddy must be a BBS user (others are refused, 13/0E 0x0A); OSCAR's item ids are kept apart in oscar_buddies, so the list itself stays protocol-neutral for other front ends. A friend added elsewhere comes to a connected AIM client at once (13/08, in its group or "Friends"). Signed-off-by: Claude <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRTAJGnEAiwGZm9ygpG
353 lines
8.8 KiB
Go
353 lines
8.8 KiB
Go
// Package oscar is an OSCAR server (the protocol of AIM and ICQ) for the
|
|
// BBS's chat, beside its IRC server: AIM and ICQ clients sign on with BBS
|
|
// names (or ICQ numbers made of the accounts') and passwords; their
|
|
// buddies are the BBS's users, online by any route; an IM is a private
|
|
// message (a PRIVMSG to an IRC user, a line on a caller's terminal), and
|
|
// a chat room is a BBS chat room, an IRC channel.
|
|
//
|
|
// One port (5190) serves the whole of it: logins (FLAP channel 1 and
|
|
// BUCP), the BOS connection and the services' (chat navigation, chat
|
|
// rooms), all told apart by their cookies. AOL's own clients (P3) came on
|
|
// 5190 too: a connection whose first byte is P3's is handed to Server.P3.
|
|
package oscar
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"crypto/rand"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
"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 (
|
|
sniffWait = time.Second // for a client's first byte, before the server's hello
|
|
loginTimeout = time.Minute // to log in, or to bring up a connection
|
|
idleTimeout = 10 * time.Minute // without a frame (clients keep alive each minute or so)
|
|
writeTimeout = 30 * time.Second
|
|
cookieLife = 5 * time.Minute // a cookie is used within this
|
|
queueLen = 512 // things waiting for a session's goroutine
|
|
)
|
|
|
|
// p3Sync is the first byte of an AOL client's P3 packet.
|
|
const p3Sync = 0x5a
|
|
|
|
// Settings are the [oscar] section's.
|
|
type Settings struct {
|
|
Address string // where to listen
|
|
// Host is where clients are sent to reconnect (the BOS and the
|
|
// services), host:port; "": the address each one came to
|
|
Host string
|
|
MaxClients int
|
|
MaxPerIP int
|
|
// UINBase: an account's ICQ number is UINBase plus its id
|
|
UINBase uint32
|
|
// System is the screen name the BBS's notices come from
|
|
System string
|
|
}
|
|
|
|
// Server serves OSCAR.
|
|
type Server struct {
|
|
B *bbs.BBS
|
|
S Settings
|
|
// P3, if set, takes a connection that began as an AOL client's (P3):
|
|
// its first bytes are still to be read from it
|
|
P3 func(net.Conn)
|
|
|
|
mu sync.Mutex
|
|
sessions map[*session]bool
|
|
byNode map[int]*session
|
|
cookies map[string]*cookie
|
|
bagMu sync.Mutex // placing friends in buddy lists
|
|
}
|
|
|
|
// New checks the settings and makes the server.
|
|
func New(b *bbs.BBS, s Settings) (*Server, error) {
|
|
if s.Address == "" {
|
|
return nil, errors.New("oscar: no address to listen on")
|
|
}
|
|
if s.Host != "" {
|
|
if _, _, err := net.SplitHostPort(s.Host); err != nil {
|
|
return nil, fmt.Errorf("oscar: host: %w", err)
|
|
}
|
|
}
|
|
if s.UINBase == 0 {
|
|
s.UINBase = 100000
|
|
}
|
|
if s.System == "" {
|
|
s.System = "OnlineHost"
|
|
}
|
|
if s.MaxClients == 0 {
|
|
s.MaxClients = 256
|
|
}
|
|
return &Server{B: b, S: s, sessions: map[*session]bool{}, byNode: map[int]*session{},
|
|
cookies: map[string]*cookie{}}, nil
|
|
}
|
|
|
|
// Run listens until ctx is done.
|
|
func (s *Server) Run(ctx context.Context) error {
|
|
ln, err := sockets.Listen("tcp", s.S.Address)
|
|
if err != nil {
|
|
return fmt.Errorf("oscar: %w", err)
|
|
}
|
|
return s.Serve(ctx, ln)
|
|
}
|
|
|
|
// Serve answers on ln until ctx is done.
|
|
func (s *Server) Serve(ctx context.Context, ln net.Listener) error {
|
|
s.B.Store.SetKeepOSCAR(true)
|
|
stop := s.B.WatchPresence(s.presence)
|
|
defer stop()
|
|
stopFriends := s.B.WatchFriends(s.friendsChanged)
|
|
defer stopFriends()
|
|
ts := &tcpserv.Server{Name: "oscar", 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("oscar", c.RemoteAddr()) }}
|
|
return ts.Serve(ctx, ln)
|
|
}
|
|
|
|
// Clients is how many sessions are signed on.
|
|
func (s *Server) Clients() int {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return len(s.sessions)
|
|
}
|
|
|
|
// peeked is a connection whose first bytes were read into r.
|
|
type peeked struct {
|
|
net.Conn
|
|
r *bufio.Reader
|
|
}
|
|
|
|
func (p *peeked) Read(b []byte) (int, error) { return p.r.Read(b) }
|
|
|
|
// handle serves a connection: P3 to Server.P3, or OSCAR: a login, a BOS
|
|
// connection, or a service's.
|
|
func (s *Server) handle(c net.Conn) {
|
|
host, _, _ := net.SplitHostPort(c.RemoteAddr().String())
|
|
r := bufio.NewReaderSize(c, 8192)
|
|
c.SetReadDeadline(time.Now().Add(sniffWait))
|
|
first, err := r.Peek(1)
|
|
var ne net.Error
|
|
switch {
|
|
case err == nil && first[0] == p3Sync:
|
|
if s.P3 == nil {
|
|
s.B.Log.Printf("oscar: %s: an AOL client (P3), which is not served here", host)
|
|
return
|
|
}
|
|
c.SetReadDeadline(time.Time{})
|
|
s.P3(&peeked{c, r})
|
|
return
|
|
case err == nil && first[0] != flapStart:
|
|
return // neither
|
|
case err != nil && !(errors.As(err, &ne) && ne.Timeout()):
|
|
return
|
|
}
|
|
// OSCAR: the server's hello first (the client may have sent its own)
|
|
c.SetDeadline(time.Now().Add(loginTimeout))
|
|
fc := newFlapConn(c, r)
|
|
if err := fc.hello(); err != nil {
|
|
return
|
|
}
|
|
f, err := fc.read()
|
|
for err == nil && f.ch == chanKeepAlive {
|
|
f, err = fc.read()
|
|
}
|
|
if err != nil || f.ch != chanSignon || len(f.data) < 4 {
|
|
return
|
|
}
|
|
t := parseTLVs(f.data[4:])
|
|
switch {
|
|
case t.has(0x0006):
|
|
s.connect(fc, host, t)
|
|
case t.has(0x0001):
|
|
s.channelLogin(fc, host, t)
|
|
default:
|
|
s.bucp(fc, host)
|
|
}
|
|
}
|
|
|
|
// cookie is a login's or a service's ticket to connect.
|
|
type cookie struct {
|
|
user *store.User
|
|
sn string // the screen name the client signed on as
|
|
icq bool
|
|
clientID string
|
|
service uint16 // 0 for BOS
|
|
room string // chat: the room
|
|
exchange uint16 // chat: its exchange
|
|
sess *session // a service's: the session it serves
|
|
expires time.Time
|
|
}
|
|
|
|
// newCookie keeps a cookie and gives its bytes.
|
|
func (s *Server) newCookie(k *cookie) []byte {
|
|
b := make([]byte, 32)
|
|
rand.Read(b)
|
|
k.expires = time.Now().Add(cookieLife)
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
for key, v := range s.cookies {
|
|
if time.Now().After(v.expires) {
|
|
delete(s.cookies, key)
|
|
}
|
|
}
|
|
s.cookies[string(b)] = k
|
|
return b
|
|
}
|
|
|
|
// takeCookie spends a cookie (nil: none such, or expired).
|
|
func (s *Server) takeCookie(b []byte) *cookie {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
k := s.cookies[string(b)]
|
|
delete(s.cookies, string(b))
|
|
if k == nil || time.Now().After(k.expires) {
|
|
return nil
|
|
}
|
|
return k
|
|
}
|
|
|
|
// reconnectHost is where a client on conn is sent for the BOS or a
|
|
// service.
|
|
func (s *Server) reconnectHost(c net.Conn) string {
|
|
if s.S.Host != "" {
|
|
return s.S.Host
|
|
}
|
|
return c.LocalAddr().String()
|
|
}
|
|
|
|
// connect is a connection with a cookie: a BOS session's, or a service's.
|
|
func (s *Server) connect(fc *flapConn, host string, t tlvs) {
|
|
v, _ := t.get(0x0006)
|
|
k := s.takeCookie(v)
|
|
if k == nil {
|
|
var o tlvs
|
|
o.addU16(0x0008, 0x0018) // reconnect, the cookie is stale
|
|
fc.signoff(o)
|
|
return
|
|
}
|
|
if k.service == 0 {
|
|
s.bos(fc, host, k)
|
|
return
|
|
}
|
|
if k.sess == nil || k.sess.closed() {
|
|
return
|
|
}
|
|
k.sess.service(fc, k)
|
|
}
|
|
|
|
// register keeps a session on (its node known).
|
|
func (s *Server) register(ss *session) {
|
|
s.mu.Lock()
|
|
s.sessions[ss] = true
|
|
s.byNode[ss.n.Num] = ss
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
// unregister forgets it.
|
|
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 on.
|
|
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 && ss.ready() {
|
|
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]
|
|
}
|
|
|
|
// presence is told of a BBS user coming online or going: each session
|
|
// watching them is told, in its own goroutine.
|
|
func (s *Server) presence(name string, online bool) {
|
|
ss := s.all()
|
|
if len(ss) == 0 {
|
|
return
|
|
}
|
|
go func() {
|
|
u, err := s.B.Store.UserByName(name)
|
|
if err != nil {
|
|
return
|
|
}
|
|
for _, x := range ss {
|
|
x.do(func() { x.buddyChanged(u) })
|
|
}
|
|
}()
|
|
}
|
|
|
|
// changed tells every session watching the user that their state is
|
|
// new (away, idle, signed on or off by AIM).
|
|
func (s *Server) changed(u *store.User) {
|
|
for _, x := range s.all() {
|
|
x.do(func() { x.buddyChanged(u) })
|
|
}
|
|
}
|
|
|
|
// resolve is the account a screen name is (an ICQ number, or a name).
|
|
func (s *Server) resolve(sn string) *store.User {
|
|
if isUIN(sn) {
|
|
if id := s.accountOfUIN(sn); id != 0 {
|
|
if u, err := s.B.Store.UserByID(id); err == nil {
|
|
return u
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
u, err := s.B.Store.UserByScreenName(sn)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return u
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// itoa is strconv's.
|
|
func itoa(n int) string { return strconv.Itoa(n) }
|