Files
Claude 139dcfd153 IRC: Solanum's channel modes for registered rooms, INVITE and KNOCK
A registered room's ops set its modes with MODE (/mode in the BBS's
chat): n t s p m i z g T, the key (k) and limit (l), and the lists: bans
(b), quiets (q), exceptions (e) and invite exceptions (I), with masks
nick!how@nodeN and the extbans $a, $a:mask and $o (~ negates). c and C
are always on, as the rooms always were. MODE b/q/e/I with no mask lists
them (367, 728, 348, 346). Modes and lists are kept in room_reg and the
new room_lists table, and are the room's: a caller on the text BBS, AOL
or Yahoo is kept out or quiet as an IRC user is.

INVITE (an op, or anyone in a +g room) gets one past i, k and l for the
hour; KNOCK asks the ops of a room one is kept out of, once a minute,
but not in a +p room. ChanServ gains INVITE and GETKEY (the i flag, which
also gets past i, k and l), and INFO shows the modes. JOIN takes keys;
/join ROOM KEY, /invite and /knock come to the BBS's chat. ISUPPORT gives
CHANMODES=Ibeq,k,l,CcTgimnpstz, EXCEPTS, INVEX, EXTBAN, MAXLIST, KEYLEN
and KNOCK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRTXE7MNm3r3hHAmTG2
2026-11-11 04:20:31 +00:00

212 lines
5.8 KiB
Go

// Package ircd is an IRC server for the BBS's chat: its channels are the
// chat rooms, so IRC clients talk with callers on terminals, the web and
// the API. Callers log in with their BBS names and passwords (SASL PLAIN
// or PASS); their nicks are their names.
//
// The protocol's parts are ergo's (github.com/ergochat/ergo): messages
// are read and written by its irc-go library, and the numerics and
// ISUPPORT lists are taken from its source (see LICENSE.ergo).
package ircd
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/D-Modem/bbs/internal/bbs"
"github.com/D-Modem/bbs/internal/certs"
"github.com/D-Modem/bbs/internal/ircd/isupport"
"github.com/D-Modem/bbs/internal/sockets"
"github.com/D-Modem/bbs/internal/tcpserv"
)
// Settings are the [irc] section's.
type Settings struct {
Address string // plain IRC ("": none)
TLSAddress string // IRC over TLS ("": none)
CertFile string
KeyFile string
ServerName string // the server's name to clients (default: the host's name)
Network string // the network's name (default: the BBS's)
MaxClients int
MaxPerIP int
Service string // the services bot's nick (default BBSServ)
ChanServ string // the rooms' service's nick (default ChanServ)
// ChanServExpire: days a registered room may go unused before it is
// dropped (0: never)
ChanServExpire int
}
// Server serves IRC.
type Server struct {
B *bbs.BBS
S Settings
created time.Time
support *isupport.List
svc *service // BBSServ
cs *service // ChanServ
mu sync.Mutex
clients map[*client]bool
}
// New checks the settings and makes the server.
func New(b *bbs.BBS, s Settings) (*Server, error) {
if s.Address == "" && s.TLSAddress == "" {
return nil, errors.New("irc: no address to listen on")
}
if s.TLSAddress != "" {
if _, err := certs.New(s.CertFile, s.KeyFile); err != nil {
return nil, fmt.Errorf("irc: certificate: %w", err)
}
}
if s.ServerName == "" {
s.ServerName, _ = os.Hostname()
if s.ServerName == "" || !strings.Contains(s.ServerName, ".") {
s.ServerName = "irc.bbs"
}
}
if s.Network == "" {
s.Network = strings.Map(func(r rune) rune {
if r == ' ' {
return '-'
}
return r
}, b.Conf().Name)
}
if s.Service == "" {
s.Service = "BBSServ"
}
if !ValidNick(s.Service) {
return nil, fmt.Errorf("irc: service %q: a nick is letters, digits, - and _", s.Service)
}
if s.ChanServ == "" {
s.ChanServ = "ChanServ"
}
if !ValidNick(s.ChanServ) || fold(s.ChanServ) == fold(s.Service) {
return nil, fmt.Errorf("irc: chanserv %q: a nick is letters, digits, - and _, and not the other service's", s.ChanServ)
}
b.SetChanServ(s.ChanServ, time.Duration(s.ChanServExpire)*24*time.Hour)
srv := &Server{B: b, S: s, created: time.Now(), clients: map[*client]bool{}, svc: newBBSServ(s.Service), cs: newChanServ(s.ChanServ)}
srv.support = isupport.NewList()
for k, v := range map[string]string{
"CASEMAPPING": "ascii",
"CHANTYPES": "#",
"CHANNELLEN": "21",
"CHANLIMIT": "#:20",
"NICKLEN": strconv.Itoa(bbs.MaxNameLen),
"NETWORK": s.Network,
"PREFIX": "(ov)@+",
"CHANMODES": bbs.RoomListModes + ",k,l," + bbs.RoomFixedModes + bbs.RoomFlagModes,
"EXCEPTS": "e",
"INVEX": "I",
"EXTBAN": "$,ao",
"MAXLIST": bbs.RoomListModes + ":" + strconv.Itoa(bbs.MaxRoomList),
"KEYLEN": strconv.Itoa(bbs.MaxRoomKey),
"KNOCK": "",
"MODES": "4",
"TOPICLEN": "0",
"TARGMAX": "PRIVMSG:1,NOTICE:1,JOIN:,PART:,NAMES:1,WHOIS:1",
"LINELEN": "512",
"UTF8ONLY": "",
"SAFELIST": "",
} {
srv.support.Add(k, v)
}
if err := srv.support.RegenerateCachedReply(); err != nil {
return nil, err
}
return srv, nil
}
// Run listens until ctx is done.
func (s *Server) Run(ctx context.Context) error {
ts := &tcpserv.Server{Name: "irc", MaxClients: s.S.MaxClients, MaxPerIP: s.S.MaxPerIP, Log: s.B.Log,
Handle: s.handle,
Busy: func(c net.Conn) {
fmt.Fprintf(c, "ERROR :Too many connections; try again later\r\n")
},
Refuse: func(c net.Conn) bool { return s.B.Refuse("irc", c.RemoteAddr()) }}
var lns []net.Listener
if s.S.Address != "" {
ln, err := sockets.Listen("tcp", s.S.Address)
if err != nil {
return fmt.Errorf("irc: %w", err)
}
lns = append(lns, ln)
}
if s.S.TLSAddress != "" {
cfg, err := certs.Config(s.S.CertFile, s.S.KeyFile)
if err != nil {
return fmt.Errorf("irc: certificate: %w", err)
}
ln, err := sockets.Listen("tcp", s.S.TLSAddress)
if err == nil {
ln = tls.NewListener(ln, cfg)
}
if err != nil {
for _, l := range lns {
l.Close()
}
return fmt.Errorf("irc: %w", err)
}
lns = append(lns, ln)
}
errs := make(chan error, len(lns))
for _, ln := range lns {
go func() { errs <- ts.Serve(ctx, ln) }()
}
var err error
for range lns {
if e := <-errs; e != nil && err == nil {
err = e
}
}
return err
}
// Clients is how many are connected.
func (s *Server) Clients() int {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.clients)
}
// Nick is a BBS name as an IRC nick: what nicks may not have (spaces,
// dots) as underscores.
func Nick(name string) string { return bbs.Nick(name) }
// service is the services bot of a nick (nil: none).
func (s *Server) service(nick string) *service {
for _, svc := range []*service{s.svc, s.cs} {
if fold(nick) == fold(svc.Name) {
return svc
}
}
return nil
}
// numerics ergo's list lacks
const (
RPL_WHOISSPECIAL = "320"
RPL_LISTSTART = "321"
)
// ValidNick: a nick as Nick makes them (a letter first).
func ValidNick(nick string) bool {
if nick == "" || len(nick) > 30 || !(nick[0] >= 'a' && nick[0] <= 'z' || nick[0] >= 'A' && nick[0] <= 'Z') {
return false
}
return Nick(nick) == nick
}
// fold is a nick or channel as compared (CASEMAPPING=ascii).
func fold(s string) string { return strings.ToLower(s) }