Compare commits
2 Commits
main
...
6551e03eee
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6551e03eee | ||
|
|
42157a7b23 |
120
README.md
120
README.md
@@ -27,6 +27,7 @@ web client as a convenience/reference implementation, but the API comes first.
|
|||||||
- [Federation](#federation-server-to-server)
|
- [Federation](#federation-server-to-server)
|
||||||
- [Storage](#storage)
|
- [Storage](#storage)
|
||||||
- [Configuration](#configuration)
|
- [Configuration](#configuration)
|
||||||
|
- [IRC Protocol Listener](#irc-protocol-listener)
|
||||||
- [Deployment](#deployment)
|
- [Deployment](#deployment)
|
||||||
- [Client Development Guide](#client-development-guide)
|
- [Client Development Guide](#client-development-guide)
|
||||||
- [Rate Limiting & Abuse Prevention](#rate-limiting--abuse-prevention)
|
- [Rate Limiting & Abuse Prevention](#rate-limiting--abuse-prevention)
|
||||||
@@ -1079,8 +1080,8 @@ the server to the client (never C2S) and use 3-digit string codes in the
|
|||||||
| `001` | RPL_WELCOME | After session creation | `{"command":"001","to":"alice","body":["Welcome to the network, alice"]}` |
|
| `001` | RPL_WELCOME | After session creation | `{"command":"001","to":"alice","body":["Welcome to the network, alice"]}` |
|
||||||
| `002` | RPL_YOURHOST | After session creation | `{"command":"002","to":"alice","body":["Your host is neoirc, running version 0.1"]}` |
|
| `002` | RPL_YOURHOST | After session creation | `{"command":"002","to":"alice","body":["Your host is neoirc, running version 0.1"]}` |
|
||||||
| `003` | RPL_CREATED | After session creation | `{"command":"003","to":"alice","body":["This server was created 2026-02-10"]}` |
|
| `003` | RPL_CREATED | After session creation | `{"command":"003","to":"alice","body":["This server was created 2026-02-10"]}` |
|
||||||
| `004` | RPL_MYINFO | After session creation | `{"command":"004","to":"alice","params":["neoirc","0.1","","ikmnostl"]}` |
|
| `004` | RPL_MYINFO | After session creation | `{"command":"004","to":"alice","params":["neoirc","0.1","","mnst"]}` |
|
||||||
| `005` | RPL_ISUPPORT | After session creation | `{"command":"005","to":"alice","params":["CHANTYPES=#","NICKLEN=32","PREFIX=(ov)@+","CHANMODES=b,k,Hl,imnst","NETWORK=neoirc"],"body":["are supported by this server"]}` |
|
| `005` | RPL_ISUPPORT | After session creation | `{"command":"005","to":"alice","params":["CHANTYPES=#","NICKLEN=32","PREFIX=(ov)@+","CHANMODES=,,H,mnst","NETWORK=neoirc"],"body":["are supported by this server"]}` |
|
||||||
| `221` | RPL_UMODEIS | In response to user MODE query | `{"command":"221","to":"alice","body":["+"]}` |
|
| `221` | RPL_UMODEIS | In response to user MODE query | `{"command":"221","to":"alice","body":["+"]}` |
|
||||||
| `251` | RPL_LUSERCLIENT | On connect or LUSERS command | `{"command":"251","to":"alice","body":["There are 5 users and 0 invisible on 1 servers"]}` |
|
| `251` | RPL_LUSERCLIENT | On connect or LUSERS command | `{"command":"251","to":"alice","body":["There are 5 users and 0 invisible on 1 servers"]}` |
|
||||||
| `252` | RPL_LUSEROP | On connect or LUSERS command | `{"command":"252","to":"alice","params":["0"],"body":["operator(s) online"]}` |
|
| `252` | RPL_LUSEROP | On connect or LUSERS command | `{"command":"252","to":"alice","params":["0"],"body":["operator(s) online"]}` |
|
||||||
@@ -1128,15 +1129,12 @@ Inspired by IRC, simplified:
|
|||||||
|
|
||||||
| Mode | Name | Meaning | Status |
|
| Mode | Name | Meaning | Status |
|
||||||
|------|----------------|---------|--------|
|
|------|----------------|---------|--------|
|
||||||
| `+b` | Ban | Prevents matching hostmasks from joining or sending (parameter: `nick!user@host` mask with wildcards) | **Enforced** |
|
|
||||||
| `+i` | Invite-only | Only invited users can join; use `INVITE nick #channel` to invite | **Enforced** |
|
|
||||||
| `+k` | Channel key | Requires a password to join (parameter: key string) | **Enforced** |
|
|
||||||
| `+l` | User limit | Maximum number of members allowed in the channel (parameter: integer) | **Enforced** |
|
|
||||||
| `+m` | Moderated | Only voiced (`+v`) users and operators (`+o`) can send | **Enforced** |
|
| `+m` | Moderated | Only voiced (`+v`) users and operators (`+o`) can send | **Enforced** |
|
||||||
| `+n` | No external | Only channel members can send messages to the channel | **Enforced** |
|
|
||||||
| `+s` | Secret | Channel hidden from LIST and WHOIS for non-members | **Enforced** |
|
|
||||||
| `+t` | Topic lock | Only operators can change the topic (default: ON) | **Enforced** |
|
| `+t` | Topic lock | Only operators can change the topic (default: ON) | **Enforced** |
|
||||||
|
| `+n` | No external | Only channel members can send messages to the channel | **Enforced** |
|
||||||
| `+H` | Hashcash | Requires proof-of-work for PRIVMSG (parameter: bits, e.g. `+H 20`) | **Enforced** |
|
| `+H` | Hashcash | Requires proof-of-work for PRIVMSG (parameter: bits, e.g. `+H 20`) | **Enforced** |
|
||||||
|
| `+i` | Invite-only | Only invited users can join | Not yet enforced |
|
||||||
|
| `+s` | Secret | Channel hidden from LIST response | Not yet enforced |
|
||||||
|
|
||||||
**User channel modes (set per-user per-channel):**
|
**User channel modes (set per-user per-channel):**
|
||||||
|
|
||||||
@@ -1148,42 +1146,6 @@ Inspired by IRC, simplified:
|
|||||||
**Channel creator auto-op:** The first user to JOIN a channel (creating it)
|
**Channel creator auto-op:** The first user to JOIN a channel (creating it)
|
||||||
automatically receives `+o` operator status.
|
automatically receives `+o` operator status.
|
||||||
|
|
||||||
**Ban system (+b):** Operators can ban users by hostmask pattern with wildcard
|
|
||||||
matching (`*` and `?`). `MODE #channel +b` with no argument lists current bans.
|
|
||||||
Bans prevent both joining and sending messages.
|
|
||||||
|
|
||||||
```
|
|
||||||
MODE #channel +b *!*@*.example.com — ban all users from example.com
|
|
||||||
MODE #channel -b *!*@*.example.com — remove the ban
|
|
||||||
MODE #channel +b — list all bans (RPL_BANLIST 367/368)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Invite-only (+i):** When set, users must be invited by an operator before
|
|
||||||
joining. The `INVITE` command records an invite that is consumed on JOIN.
|
|
||||||
|
|
||||||
```
|
|
||||||
MODE #channel +i — set invite-only
|
|
||||||
INVITE nick #channel — invite a user (operator only on +i channels)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Channel key (+k):** Requires a password to join the channel.
|
|
||||||
|
|
||||||
```
|
|
||||||
MODE #channel +k secretpass — set a channel key
|
|
||||||
MODE #channel -k * — remove the key
|
|
||||||
JOIN #channel secretpass — join with key
|
|
||||||
```
|
|
||||||
|
|
||||||
**User limit (+l):** Caps the number of members in the channel.
|
|
||||||
|
|
||||||
```
|
|
||||||
MODE #channel +l 50 — set limit to 50 members
|
|
||||||
MODE #channel -l — remove the limit
|
|
||||||
```
|
|
||||||
|
|
||||||
**Secret (+s):** Hides the channel from `LIST` for non-members and from
|
|
||||||
`WHOIS` channel lists when the querier is not in the same channel.
|
|
||||||
|
|
||||||
**KICK command:** Channel operators can remove users with `KICK #channel nick
|
**KICK command:** Channel operators can remove users with `KICK #channel nick
|
||||||
[:reason]`. The kicked user and all channel members receive the KICK message.
|
[:reason]`. The kicked user and all channel members receive the KICK message.
|
||||||
|
|
||||||
@@ -1192,7 +1154,7 @@ RPL_AWAY), and skips hashcash validation on +H channels (servers and services
|
|||||||
use NOTICE).
|
use NOTICE).
|
||||||
|
|
||||||
**ISUPPORT:** The server advertises `PREFIX=(ov)@+` and
|
**ISUPPORT:** The server advertises `PREFIX=(ov)@+` and
|
||||||
`CHANMODES=b,k,Hl,imnst` in RPL_ISUPPORT (005).
|
`CHANMODES=,,H,mnst` in RPL_ISUPPORT (005).
|
||||||
|
|
||||||
### Per-Channel Hashcash (Anti-Spam)
|
### Per-Channel Hashcash (Anti-Spam)
|
||||||
|
|
||||||
@@ -2266,6 +2228,7 @@ directory is also loaded automatically via
|
|||||||
| `NEOIRC_OPER_PASSWORD` | string | `""` | Server operator (o-line) password. Both name and password must be set to enable OPER. |
|
| `NEOIRC_OPER_PASSWORD` | string | `""` | Server operator (o-line) password. Both name and password must be set to enable OPER. |
|
||||||
| `LOGIN_RATE_LIMIT` | float | `1` | Allowed login attempts per second per IP address. |
|
| `LOGIN_RATE_LIMIT` | float | `1` | Allowed login attempts per second per IP address. |
|
||||||
| `LOGIN_RATE_BURST` | int | `5` | Maximum burst of login attempts per IP before rate limiting kicks in. |
|
| `LOGIN_RATE_BURST` | int | `5` | Maximum burst of login attempts per IP before rate limiting kicks in. |
|
||||||
|
| `IRC_LISTEN_ADDR` | string | `":6667"` | TCP address for the traditional IRC protocol listener. Set empty to disable. |
|
||||||
| `MAINTENANCE_MODE` | bool | `false` | Maintenance mode flag (reserved) |
|
| `MAINTENANCE_MODE` | bool | `false` | Maintenance mode flag (reserved) |
|
||||||
|
|
||||||
### Example `.env` file
|
### Example `.env` file
|
||||||
@@ -2282,6 +2245,71 @@ NEOIRC_HASHCASH_BITS=20
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## IRC Protocol Listener
|
||||||
|
|
||||||
|
neoirc includes an optional traditional IRC wire protocol listener (RFC
|
||||||
|
1459/2812) that allows standard IRC clients to connect directly. This enables
|
||||||
|
backward compatibility with existing IRC clients like irssi, weechat, hexchat,
|
||||||
|
and others.
|
||||||
|
|
||||||
|
### Enabling
|
||||||
|
|
||||||
|
Set the `IRC_LISTEN_ADDR` environment variable to a TCP address:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
IRC_LISTEN_ADDR=:6667
|
||||||
|
```
|
||||||
|
|
||||||
|
When unset or empty, the IRC listener is disabled and only the HTTP/JSON API is
|
||||||
|
available.
|
||||||
|
|
||||||
|
### Supported Commands
|
||||||
|
|
||||||
|
| Category | Commands |
|
||||||
|
|------------|------------------------------------------------------|
|
||||||
|
| Connection | `NICK`, `USER`, `PASS`, `QUIT`, `PING`/`PONG`, `CAP` |
|
||||||
|
| Channels | `JOIN`, `PART`, `MODE`, `TOPIC`, `NAMES`, `LIST`, `KICK`, `INVITE` |
|
||||||
|
| Messaging | `PRIVMSG`, `NOTICE` |
|
||||||
|
| Info | `WHO`, `WHOIS`, `LUSERS`, `MOTD`, `AWAY` |
|
||||||
|
| Operator | `OPER` (requires `NEOIRC_OPER_NAME` and `NEOIRC_OPER_PASSWORD`) |
|
||||||
|
|
||||||
|
### Protocol Details
|
||||||
|
|
||||||
|
- **Wire format**: CR-LF delimited lines, max 512 bytes per line
|
||||||
|
- **Connection registration**: Clients must send `NICK` and `USER` to register.
|
||||||
|
An optional `PASS` before registration sets the session password (minimum 8
|
||||||
|
characters).
|
||||||
|
- **CAP negotiation**: `CAP LS` and `CAP END` are silently handled for
|
||||||
|
compatibility with modern clients. No capabilities are advertised.
|
||||||
|
- **Channel prefixes**: Channels must start with `#`. If omitted, it's
|
||||||
|
automatically prepended.
|
||||||
|
- **First joiner**: The first user to join a channel is automatically granted
|
||||||
|
operator status (`@`).
|
||||||
|
- **Channel modes**: `+m` (moderated), `+t` (topic lock), `+o` (operator),
|
||||||
|
`+v` (voice)
|
||||||
|
|
||||||
|
### Bridge to HTTP API
|
||||||
|
|
||||||
|
Messages sent by IRC clients appear in channels visible to HTTP/JSON API
|
||||||
|
clients and vice versa. The IRC listener and HTTP API share the same database,
|
||||||
|
broker, and session infrastructure. A user connected via IRC and a user
|
||||||
|
connected via the HTTP API can communicate in the same channels seamlessly.
|
||||||
|
|
||||||
|
### Docker Usage
|
||||||
|
|
||||||
|
To expose the IRC port in Docker:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -d \
|
||||||
|
-p 8080:8080 \
|
||||||
|
-p 6667:6667 \
|
||||||
|
-e IRC_LISTEN_ADDR=:6667 \
|
||||||
|
-v neoirc-data:/var/lib/neoirc \
|
||||||
|
neoirc
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Deployment
|
## Deployment
|
||||||
|
|
||||||
### Docker (Recommended)
|
### Docker (Recommended)
|
||||||
@@ -2734,7 +2762,7 @@ guess is borne by the server (bcrypt), not the client.
|
|||||||
- [x] **Client output queue pruning** — delete old client output queue entries per `QUEUE_MAX_AGE`
|
- [x] **Client output queue pruning** — delete old client output queue entries per `QUEUE_MAX_AGE`
|
||||||
- [x] **Message rotation** — prune messages older than `MESSAGE_MAX_AGE`
|
- [x] **Message rotation** — prune messages older than `MESSAGE_MAX_AGE`
|
||||||
- [x] **Channel modes** — enforce `+m` (moderated), `+t` (topic lock), `+n` (no external)
|
- [x] **Channel modes** — enforce `+m` (moderated), `+t` (topic lock), `+n` (no external)
|
||||||
- [x] **Channel modes (tier 2)** — enforce `+i` (invite-only), `+s` (secret), `+b` (ban), `+k` (key), `+l` (limit)
|
- [ ] **Channel modes (tier 2)** — enforce `+i` (invite-only), `+s` (secret), `+b` (ban), `+k` (key), `+l` (limit)
|
||||||
- [x] **User channel modes** — `+o` (operator), `+v` (voice) with NAMES prefixes
|
- [x] **User channel modes** — `+o` (operator), `+v` (voice) with NAMES prefixes
|
||||||
- [x] **KICK command** — operator-only channel kick with broadcast
|
- [x] **KICK command** — operator-only channel kick with broadcast
|
||||||
- [x] **MODE command** — query and set channel/user modes
|
- [x] **MODE command** — query and set channel/user modes
|
||||||
|
|||||||
@@ -2,11 +2,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/broker"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/config"
|
"git.eeqj.de/sneak/neoirc/internal/config"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/db"
|
"git.eeqj.de/sneak/neoirc/internal/db"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/globals"
|
"git.eeqj.de/sneak/neoirc/internal/globals"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/handlers"
|
"git.eeqj.de/sneak/neoirc/internal/handlers"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/healthcheck"
|
"git.eeqj.de/sneak/neoirc/internal/healthcheck"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/ircserver"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/logger"
|
"git.eeqj.de/sneak/neoirc/internal/logger"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/middleware"
|
"git.eeqj.de/sneak/neoirc/internal/middleware"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/server"
|
"git.eeqj.de/sneak/neoirc/internal/server"
|
||||||
@@ -28,16 +30,22 @@ func main() {
|
|||||||
|
|
||||||
fx.New(
|
fx.New(
|
||||||
fx.Provide(
|
fx.Provide(
|
||||||
|
broker.New,
|
||||||
config.New,
|
config.New,
|
||||||
db.New,
|
db.New,
|
||||||
globals.New,
|
globals.New,
|
||||||
handlers.New,
|
handlers.New,
|
||||||
|
ircserver.New,
|
||||||
logger.New,
|
logger.New,
|
||||||
server.New,
|
server.New,
|
||||||
middleware.New,
|
middleware.New,
|
||||||
healthcheck.New,
|
healthcheck.New,
|
||||||
stats.New,
|
stats.New,
|
||||||
),
|
),
|
||||||
fx.Invoke(func(*server.Server) {}),
|
fx.Invoke(func(
|
||||||
|
_ *server.Server,
|
||||||
|
_ *ircserver.Server,
|
||||||
|
) {
|
||||||
|
}),
|
||||||
).Run()
|
).Run()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ type Config struct {
|
|||||||
OperPassword string
|
OperPassword string
|
||||||
LoginRateLimit float64
|
LoginRateLimit float64
|
||||||
LoginRateBurst int
|
LoginRateBurst int
|
||||||
|
IRCListenAddr string
|
||||||
params *Params
|
params *Params
|
||||||
log *slog.Logger
|
log *slog.Logger
|
||||||
}
|
}
|
||||||
@@ -86,6 +87,7 @@ func New(
|
|||||||
viper.SetDefault("NEOIRC_OPER_PASSWORD", "")
|
viper.SetDefault("NEOIRC_OPER_PASSWORD", "")
|
||||||
viper.SetDefault("LOGIN_RATE_LIMIT", "1")
|
viper.SetDefault("LOGIN_RATE_LIMIT", "1")
|
||||||
viper.SetDefault("LOGIN_RATE_BURST", "5")
|
viper.SetDefault("LOGIN_RATE_BURST", "5")
|
||||||
|
viper.SetDefault("IRC_LISTEN_ADDR", ":6667")
|
||||||
|
|
||||||
err := viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -116,6 +118,7 @@ func New(
|
|||||||
OperPassword: viper.GetString("NEOIRC_OPER_PASSWORD"),
|
OperPassword: viper.GetString("NEOIRC_OPER_PASSWORD"),
|
||||||
LoginRateLimit: viper.GetFloat64("LOGIN_RATE_LIMIT"),
|
LoginRateLimit: viper.GetFloat64("LOGIN_RATE_LIMIT"),
|
||||||
LoginRateBurst: viper.GetInt("LOGIN_RATE_BURST"),
|
LoginRateBurst: viper.GetInt("LOGIN_RATE_BURST"),
|
||||||
|
IRCListenAddr: viper.GetString("IRC_LISTEN_ADDR"),
|
||||||
log: log,
|
log: log,
|
||||||
params: ¶ms,
|
params: ¶ms,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.eeqj.de/sneak/neoirc/pkg/irc"
|
"git.eeqj.de/sneak/neoirc/pkg/irc"
|
||||||
@@ -1836,534 +1835,3 @@ func (database *Database) PruneSpentHashcash(
|
|||||||
|
|
||||||
return deleted, nil
|
return deleted, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Tier 2: Ban system (+b) ---
|
|
||||||
|
|
||||||
// BanInfo represents a channel ban entry.
|
|
||||||
type BanInfo struct {
|
|
||||||
Mask string
|
|
||||||
SetBy string
|
|
||||||
CreatedAt time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddChannelBan inserts a ban mask for a channel.
|
|
||||||
func (database *Database) AddChannelBan(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID int64,
|
|
||||||
mask, setBy string,
|
|
||||||
) error {
|
|
||||||
_, err := database.conn.ExecContext(ctx,
|
|
||||||
`INSERT OR IGNORE INTO channel_bans
|
|
||||||
(channel_id, mask, set_by, created_at)
|
|
||||||
VALUES (?, ?, ?, ?)`,
|
|
||||||
channelID, mask, setBy, time.Now())
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("add channel ban: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RemoveChannelBan removes a ban mask from a channel.
|
|
||||||
func (database *Database) RemoveChannelBan(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID int64,
|
|
||||||
mask string,
|
|
||||||
) error {
|
|
||||||
_, err := database.conn.ExecContext(ctx,
|
|
||||||
`DELETE FROM channel_bans
|
|
||||||
WHERE channel_id = ? AND mask = ?`,
|
|
||||||
channelID, mask)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("remove channel ban: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListChannelBans returns all bans for a channel.
|
|
||||||
//
|
|
||||||
//nolint:dupl // different query+type vs filtered variant
|
|
||||||
func (database *Database) ListChannelBans(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID int64,
|
|
||||||
) ([]BanInfo, error) {
|
|
||||||
rows, err := database.conn.QueryContext(ctx,
|
|
||||||
`SELECT mask, set_by, created_at
|
|
||||||
FROM channel_bans
|
|
||||||
WHERE channel_id = ?
|
|
||||||
ORDER BY created_at ASC`,
|
|
||||||
channelID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("list channel bans: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() { _ = rows.Close() }()
|
|
||||||
|
|
||||||
var bans []BanInfo
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
var ban BanInfo
|
|
||||||
|
|
||||||
if scanErr := rows.Scan(
|
|
||||||
&ban.Mask, &ban.SetBy, &ban.CreatedAt,
|
|
||||||
); scanErr != nil {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"scan channel ban: %w", scanErr,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
bans = append(bans, ban)
|
|
||||||
}
|
|
||||||
|
|
||||||
if rowErr := rows.Err(); rowErr != nil {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"iterate channel bans: %w", rowErr,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return bans, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsSessionBanned checks if a session's hostmask matches
|
|
||||||
// any ban in the channel. Returns true if banned.
|
|
||||||
func (database *Database) IsSessionBanned(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID, sessionID int64,
|
|
||||||
) (bool, error) {
|
|
||||||
// Get the session's hostmask parts.
|
|
||||||
var nick, username, hostname string
|
|
||||||
|
|
||||||
err := database.conn.QueryRowContext(ctx,
|
|
||||||
`SELECT nick, username, hostname
|
|
||||||
FROM sessions WHERE id = ?`,
|
|
||||||
sessionID,
|
|
||||||
).Scan(&nick, &username, &hostname)
|
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf(
|
|
||||||
"get session hostmask: %w", err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
hostmask := FormatHostmask(nick, username, hostname)
|
|
||||||
|
|
||||||
// Get all ban masks for the channel.
|
|
||||||
bans, banErr := database.ListChannelBans(ctx, channelID)
|
|
||||||
if banErr != nil {
|
|
||||||
return false, banErr
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, ban := range bans {
|
|
||||||
if MatchBanMask(ban.Mask, hostmask) {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// MatchBanMask checks if hostmask matches a ban pattern
|
|
||||||
// using IRC-style wildcard matching (* and ?).
|
|
||||||
func MatchBanMask(pattern, hostmask string) bool {
|
|
||||||
return wildcardMatch(
|
|
||||||
strings.ToLower(pattern),
|
|
||||||
strings.ToLower(hostmask),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// wildcardMatch implements simple glob-style matching
|
|
||||||
// with * (any sequence) and ? (any single character).
|
|
||||||
func wildcardMatch(pattern, str string) bool {
|
|
||||||
for len(pattern) > 0 {
|
|
||||||
switch pattern[0] {
|
|
||||||
case '*':
|
|
||||||
// Skip consecutive asterisks.
|
|
||||||
for len(pattern) > 0 && pattern[0] == '*' {
|
|
||||||
pattern = pattern[1:]
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(pattern) == 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i <= len(str); i++ {
|
|
||||||
if wildcardMatch(pattern, str[i:]) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
case '?':
|
|
||||||
if len(str) == 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
pattern = pattern[1:]
|
|
||||||
str = str[1:]
|
|
||||||
default:
|
|
||||||
if len(str) == 0 || pattern[0] != str[0] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
pattern = pattern[1:]
|
|
||||||
str = str[1:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return len(str) == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Tier 2: Invite-only (+i) ---
|
|
||||||
|
|
||||||
// IsChannelInviteOnly checks if a channel has +i mode.
|
|
||||||
func (database *Database) IsChannelInviteOnly(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID int64,
|
|
||||||
) (bool, error) {
|
|
||||||
var isInviteOnly int
|
|
||||||
|
|
||||||
err := database.conn.QueryRowContext(ctx,
|
|
||||||
`SELECT is_invite_only FROM channels
|
|
||||||
WHERE id = ?`,
|
|
||||||
channelID,
|
|
||||||
).Scan(&isInviteOnly)
|
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf(
|
|
||||||
"check invite only: %w", err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return isInviteOnly != 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetChannelInviteOnly sets or unsets +i mode.
|
|
||||||
func (database *Database) SetChannelInviteOnly(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID int64,
|
|
||||||
inviteOnly bool,
|
|
||||||
) error {
|
|
||||||
val := 0
|
|
||||||
if inviteOnly {
|
|
||||||
val = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := database.conn.ExecContext(ctx,
|
|
||||||
`UPDATE channels
|
|
||||||
SET is_invite_only = ?, updated_at = ?
|
|
||||||
WHERE id = ?`,
|
|
||||||
val, time.Now(), channelID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf(
|
|
||||||
"set invite only: %w", err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddChannelInvite records that a session has been
|
|
||||||
// invited to a channel.
|
|
||||||
func (database *Database) AddChannelInvite(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID, sessionID int64,
|
|
||||||
invitedBy string,
|
|
||||||
) error {
|
|
||||||
_, err := database.conn.ExecContext(ctx,
|
|
||||||
`INSERT OR IGNORE INTO channel_invites
|
|
||||||
(channel_id, session_id, invited_by, created_at)
|
|
||||||
VALUES (?, ?, ?, ?)`,
|
|
||||||
channelID, sessionID, invitedBy, time.Now())
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("add channel invite: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// HasChannelInvite checks if a session has been invited
|
|
||||||
// to a channel.
|
|
||||||
func (database *Database) HasChannelInvite(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID, sessionID int64,
|
|
||||||
) (bool, error) {
|
|
||||||
var count int
|
|
||||||
|
|
||||||
err := database.conn.QueryRowContext(ctx,
|
|
||||||
`SELECT COUNT(*) FROM channel_invites
|
|
||||||
WHERE channel_id = ? AND session_id = ?`,
|
|
||||||
channelID, sessionID,
|
|
||||||
).Scan(&count)
|
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf(
|
|
||||||
"check invite: %w", err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return count > 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClearChannelInvite removes a session's invite to a
|
|
||||||
// channel (called after successful JOIN).
|
|
||||||
func (database *Database) ClearChannelInvite(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID, sessionID int64,
|
|
||||||
) error {
|
|
||||||
_, err := database.conn.ExecContext(ctx,
|
|
||||||
`DELETE FROM channel_invites
|
|
||||||
WHERE channel_id = ? AND session_id = ?`,
|
|
||||||
channelID, sessionID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("clear invite: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Tier 2: Secret (+s) ---
|
|
||||||
|
|
||||||
// IsChannelSecret checks if a channel has +s mode.
|
|
||||||
func (database *Database) IsChannelSecret(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID int64,
|
|
||||||
) (bool, error) {
|
|
||||||
var isSecret int
|
|
||||||
|
|
||||||
err := database.conn.QueryRowContext(ctx,
|
|
||||||
`SELECT is_secret FROM channels
|
|
||||||
WHERE id = ?`,
|
|
||||||
channelID,
|
|
||||||
).Scan(&isSecret)
|
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf(
|
|
||||||
"check secret: %w", err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return isSecret != 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetChannelSecret sets or unsets +s mode.
|
|
||||||
func (database *Database) SetChannelSecret(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID int64,
|
|
||||||
secret bool,
|
|
||||||
) error {
|
|
||||||
val := 0
|
|
||||||
if secret {
|
|
||||||
val = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := database.conn.ExecContext(ctx,
|
|
||||||
`UPDATE channels
|
|
||||||
SET is_secret = ?, updated_at = ?
|
|
||||||
WHERE id = ?`,
|
|
||||||
val, time.Now(), channelID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("set secret: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListAllChannelsWithCountsFiltered returns all channels
|
|
||||||
// with member counts, excluding secret channels that
|
|
||||||
// the given session is not a member of.
|
|
||||||
//
|
|
||||||
//nolint:dupl // different query+type vs ListChannelBans
|
|
||||||
func (database *Database) ListAllChannelsWithCountsFiltered(
|
|
||||||
ctx context.Context,
|
|
||||||
sessionID int64,
|
|
||||||
) ([]ChannelInfoFull, error) {
|
|
||||||
rows, err := database.conn.QueryContext(ctx,
|
|
||||||
`SELECT c.name, COUNT(cm.id) AS member_count,
|
|
||||||
c.topic
|
|
||||||
FROM channels c
|
|
||||||
LEFT JOIN channel_members cm
|
|
||||||
ON cm.channel_id = c.id
|
|
||||||
WHERE c.is_secret = 0
|
|
||||||
OR c.id IN (
|
|
||||||
SELECT channel_id FROM channel_members
|
|
||||||
WHERE session_id = ?
|
|
||||||
)
|
|
||||||
GROUP BY c.id
|
|
||||||
ORDER BY c.name ASC`,
|
|
||||||
sessionID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"list channels filtered: %w", err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() { _ = rows.Close() }()
|
|
||||||
|
|
||||||
var channels []ChannelInfoFull
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
var chanInfo ChannelInfoFull
|
|
||||||
|
|
||||||
if scanErr := rows.Scan(
|
|
||||||
&chanInfo.Name,
|
|
||||||
&chanInfo.MemberCount,
|
|
||||||
&chanInfo.Topic,
|
|
||||||
); scanErr != nil {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"scan channel: %w", scanErr,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
channels = append(channels, chanInfo)
|
|
||||||
}
|
|
||||||
|
|
||||||
if rowErr := rows.Err(); rowErr != nil {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"iterate channels: %w", rowErr,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return channels, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetSessionChannelsFiltered returns channels a session
|
|
||||||
// belongs to, optionally excluding secret channels for
|
|
||||||
// WHOIS (when the querier is not in the same channel).
|
|
||||||
// If querierID == targetID, returns all channels.
|
|
||||||
func (database *Database) GetSessionChannelsFiltered(
|
|
||||||
ctx context.Context,
|
|
||||||
targetSID, querierSID int64,
|
|
||||||
) ([]ChannelInfo, error) {
|
|
||||||
// If querying yourself, return all channels.
|
|
||||||
if targetSID == querierSID {
|
|
||||||
return database.GetSessionChannels(ctx, targetSID)
|
|
||||||
}
|
|
||||||
|
|
||||||
rows, err := database.conn.QueryContext(ctx,
|
|
||||||
`SELECT c.id, c.name, c.topic
|
|
||||||
FROM channels c
|
|
||||||
JOIN channel_members cm
|
|
||||||
ON cm.channel_id = c.id
|
|
||||||
WHERE cm.session_id = ?
|
|
||||||
AND (c.is_secret = 0
|
|
||||||
OR c.id IN (
|
|
||||||
SELECT channel_id FROM channel_members
|
|
||||||
WHERE session_id = ?
|
|
||||||
))
|
|
||||||
ORDER BY c.name ASC`,
|
|
||||||
targetSID, querierSID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"get session channels filtered: %w", err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() { _ = rows.Close() }()
|
|
||||||
|
|
||||||
var channels []ChannelInfo
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
var chanInfo ChannelInfo
|
|
||||||
|
|
||||||
if scanErr := rows.Scan(
|
|
||||||
&chanInfo.ID,
|
|
||||||
&chanInfo.Name,
|
|
||||||
&chanInfo.Topic,
|
|
||||||
); scanErr != nil {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"scan channel: %w", scanErr,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
channels = append(channels, chanInfo)
|
|
||||||
}
|
|
||||||
|
|
||||||
if rowErr := rows.Err(); rowErr != nil {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"iterate channels: %w", rowErr,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return channels, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Tier 2: Channel Key (+k) ---
|
|
||||||
|
|
||||||
// GetChannelKey returns the key for a channel (empty
|
|
||||||
// string means no key set).
|
|
||||||
func (database *Database) GetChannelKey(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID int64,
|
|
||||||
) (string, error) {
|
|
||||||
var key string
|
|
||||||
|
|
||||||
err := database.conn.QueryRowContext(ctx,
|
|
||||||
`SELECT channel_key FROM channels
|
|
||||||
WHERE id = ?`,
|
|
||||||
channelID,
|
|
||||||
).Scan(&key)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("get channel key: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return key, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetChannelKey sets or clears the key for a channel.
|
|
||||||
func (database *Database) SetChannelKey(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID int64,
|
|
||||||
key string,
|
|
||||||
) error {
|
|
||||||
_, err := database.conn.ExecContext(ctx,
|
|
||||||
`UPDATE channels
|
|
||||||
SET channel_key = ?, updated_at = ?
|
|
||||||
WHERE id = ?`,
|
|
||||||
key, time.Now(), channelID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("set channel key: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Tier 2: User Limit (+l) ---
|
|
||||||
|
|
||||||
// GetChannelUserLimit returns the user limit for a
|
|
||||||
// channel (0 means no limit).
|
|
||||||
func (database *Database) GetChannelUserLimit(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID int64,
|
|
||||||
) (int, error) {
|
|
||||||
var limit int
|
|
||||||
|
|
||||||
err := database.conn.QueryRowContext(ctx,
|
|
||||||
`SELECT user_limit FROM channels
|
|
||||||
WHERE id = ?`,
|
|
||||||
channelID,
|
|
||||||
).Scan(&limit)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf(
|
|
||||||
"get channel user limit: %w", err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return limit, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetChannelUserLimit sets the user limit for a channel.
|
|
||||||
func (database *Database) SetChannelUserLimit(
|
|
||||||
ctx context.Context,
|
|
||||||
channelID int64,
|
|
||||||
limit int,
|
|
||||||
) error {
|
|
||||||
_, err := database.conn.ExecContext(ctx,
|
|
||||||
`UPDATE channels
|
|
||||||
SET user_limit = ?, updated_at = ?
|
|
||||||
WHERE id = ?`,
|
|
||||||
limit, time.Now(), channelID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf(
|
|
||||||
"set channel user limit: %w", err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1017,474 +1017,3 @@ func TestGetOperCount(t *testing.T) {
|
|||||||
t.Fatalf("expected 1 oper, got %d", count)
|
t.Fatalf("expected 1 oper, got %d", count)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Tier 2 Tests ---
|
|
||||||
|
|
||||||
func TestWildcardMatch(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
pattern string
|
|
||||||
input string
|
|
||||||
match bool
|
|
||||||
}{
|
|
||||||
{"*!*@*", "nick!user@host", true},
|
|
||||||
{"*!*@*.example.com", "nick!user@foo.example.com", true},
|
|
||||||
{"*!*@*.example.com", "nick!user@other.net", false},
|
|
||||||
{"badnick!*@*", "badnick!user@host", true},
|
|
||||||
{"badnick!*@*", "goodnick!user@host", false},
|
|
||||||
{"nick!user@host", "nick!user@host", true},
|
|
||||||
{"nick!user@host", "nick!user@other", false},
|
|
||||||
{"*", "anything", true},
|
|
||||||
{"?ick!*@*", "nick!user@host", true},
|
|
||||||
{"?ick!*@*", "nn!user@host", false},
|
|
||||||
// Case-insensitive.
|
|
||||||
{"Nick!*@*", "nick!user@host", true},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range tests {
|
|
||||||
result := db.MatchBanMask(tc.pattern, tc.input)
|
|
||||||
if result != tc.match {
|
|
||||||
t.Errorf(
|
|
||||||
"MatchBanMask(%q, %q) = %v, want %v",
|
|
||||||
tc.pattern, tc.input, result, tc.match,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestChannelBanCRUD(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
database := setupTestDB(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
|
|
||||||
chID, err := database.GetOrCreateChannel(ctx, "#test")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// No bans initially.
|
|
||||||
bans, err := database.ListChannelBans(ctx, chID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(bans) != 0 {
|
|
||||||
t.Fatalf("expected 0 bans, got %d", len(bans))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add a ban.
|
|
||||||
err = database.AddChannelBan(
|
|
||||||
ctx, chID, "*!*@evil.com", "op",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
bans, err = database.ListChannelBans(ctx, chID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(bans) != 1 {
|
|
||||||
t.Fatalf("expected 1 ban, got %d", len(bans))
|
|
||||||
}
|
|
||||||
|
|
||||||
if bans[0].Mask != "*!*@evil.com" {
|
|
||||||
t.Fatalf("wrong mask: %s", bans[0].Mask)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Duplicate add is ignored (OR IGNORE).
|
|
||||||
err = database.AddChannelBan(
|
|
||||||
ctx, chID, "*!*@evil.com", "op2",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
bans, _ = database.ListChannelBans(ctx, chID)
|
|
||||||
if len(bans) != 1 {
|
|
||||||
t.Fatalf("expected 1 ban after dup, got %d", len(bans))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove ban.
|
|
||||||
err = database.RemoveChannelBan(
|
|
||||||
ctx, chID, "*!*@evil.com",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
bans, _ = database.ListChannelBans(ctx, chID)
|
|
||||||
if len(bans) != 0 {
|
|
||||||
t.Fatalf("expected 0 bans after remove, got %d", len(bans))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsSessionBanned(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
database := setupTestDB(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
|
|
||||||
sid, _, _, err := database.CreateSession(
|
|
||||||
ctx, "victim", "victim", "evil.com", "",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
chID, err := database.GetOrCreateChannel(ctx, "#bantest")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not banned initially.
|
|
||||||
banned, err := database.IsSessionBanned(ctx, chID, sid)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if banned {
|
|
||||||
t.Fatal("should not be banned initially")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add ban matching the hostmask.
|
|
||||||
err = database.AddChannelBan(
|
|
||||||
ctx, chID, "*!*@evil.com", "op",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
banned, err = database.IsSessionBanned(ctx, chID, sid)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !banned {
|
|
||||||
t.Fatal("should be banned")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestChannelInviteOnly(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
database := setupTestDB(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
|
|
||||||
chID, err := database.GetOrCreateChannel(ctx, "#invite")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default: not invite-only.
|
|
||||||
isIO, err := database.IsChannelInviteOnly(ctx, chID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if isIO {
|
|
||||||
t.Fatal("should not be invite-only by default")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set invite-only.
|
|
||||||
err = database.SetChannelInviteOnly(ctx, chID, true)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
isIO, _ = database.IsChannelInviteOnly(ctx, chID)
|
|
||||||
if !isIO {
|
|
||||||
t.Fatal("should be invite-only")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unset.
|
|
||||||
err = database.SetChannelInviteOnly(ctx, chID, false)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
isIO, _ = database.IsChannelInviteOnly(ctx, chID)
|
|
||||||
if isIO {
|
|
||||||
t.Fatal("should not be invite-only")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestChannelInviteCRUD(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
database := setupTestDB(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
|
|
||||||
sid, _, _, err := database.CreateSession(
|
|
||||||
ctx, "invited", "", "", "",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
chID, err := database.GetOrCreateChannel(ctx, "#inv")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// No invite initially.
|
|
||||||
has, err := database.HasChannelInvite(ctx, chID, sid)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if has {
|
|
||||||
t.Fatal("should not have invite")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add invite.
|
|
||||||
err = database.AddChannelInvite(ctx, chID, sid, "op")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
has, _ = database.HasChannelInvite(ctx, chID, sid)
|
|
||||||
if !has {
|
|
||||||
t.Fatal("should have invite")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear invite.
|
|
||||||
err = database.ClearChannelInvite(ctx, chID, sid)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
has, _ = database.HasChannelInvite(ctx, chID, sid)
|
|
||||||
if has {
|
|
||||||
t.Fatal("invite should be cleared")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestChannelSecret(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
database := setupTestDB(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
|
|
||||||
chID, err := database.GetOrCreateChannel(ctx, "#secret")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default: not secret.
|
|
||||||
isSec, err := database.IsChannelSecret(ctx, chID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if isSec {
|
|
||||||
t.Fatal("should not be secret by default")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = database.SetChannelSecret(ctx, chID, true)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
isSec, _ = database.IsChannelSecret(ctx, chID)
|
|
||||||
if !isSec {
|
|
||||||
t.Fatal("should be secret")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// createTestSession is a helper to create a session and
|
|
||||||
// return only the session ID.
|
|
||||||
func createTestSession(
|
|
||||||
t *testing.T,
|
|
||||||
database *db.Database,
|
|
||||||
nick string,
|
|
||||||
) int64 {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
sid, _, _, err := database.CreateSession(
|
|
||||||
t.Context(), nick, "", "", "",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("create session %s: %v", nick, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return sid
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSecretChannelFiltering(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
database := setupTestDB(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
|
|
||||||
// Create two sessions.
|
|
||||||
sid1 := createTestSession(t, database, "member")
|
|
||||||
sid2 := createTestSession(t, database, "outsider")
|
|
||||||
|
|
||||||
// Create a secret channel.
|
|
||||||
chID, _ := database.GetOrCreateChannel(ctx, "#secret")
|
|
||||||
_ = database.SetChannelSecret(ctx, chID, true)
|
|
||||||
_ = database.JoinChannel(ctx, chID, sid1)
|
|
||||||
|
|
||||||
// Create a non-secret channel.
|
|
||||||
chID2, _ := database.GetOrCreateChannel(ctx, "#public")
|
|
||||||
_ = database.JoinChannel(ctx, chID2, sid1)
|
|
||||||
|
|
||||||
// Member should see both.
|
|
||||||
list, err := database.ListAllChannelsWithCountsFiltered(
|
|
||||||
ctx, sid1,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(list) != 2 {
|
|
||||||
t.Fatalf("member should see 2 channels, got %d", len(list))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Outsider should only see public.
|
|
||||||
list, _ = database.ListAllChannelsWithCountsFiltered(
|
|
||||||
ctx, sid2,
|
|
||||||
)
|
|
||||||
if len(list) != 1 {
|
|
||||||
t.Fatalf("outsider should see 1 channel, got %d", len(list))
|
|
||||||
}
|
|
||||||
|
|
||||||
if list[0].Name != "#public" {
|
|
||||||
t.Fatalf("outsider should see #public, got %s", list[0].Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWhoisChannelFiltering(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
database := setupTestDB(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
|
|
||||||
sid1 := createTestSession(t, database, "target")
|
|
||||||
sid2 := createTestSession(t, database, "querier")
|
|
||||||
|
|
||||||
// Create secret channel, target joins it.
|
|
||||||
chID, _ := database.GetOrCreateChannel(ctx, "#hidden")
|
|
||||||
_ = database.SetChannelSecret(ctx, chID, true)
|
|
||||||
_ = database.JoinChannel(ctx, chID, sid1)
|
|
||||||
|
|
||||||
// Querier (non-member) should not see the channel.
|
|
||||||
channels, err := database.GetSessionChannelsFiltered(
|
|
||||||
ctx, sid1, sid2,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(channels) != 0 {
|
|
||||||
t.Fatalf(
|
|
||||||
"querier should see 0 channels, got %d",
|
|
||||||
len(channels),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Target querying self should see it.
|
|
||||||
channels, _ = database.GetSessionChannelsFiltered(
|
|
||||||
ctx, sid1, sid1,
|
|
||||||
)
|
|
||||||
if len(channels) != 1 {
|
|
||||||
t.Fatalf(
|
|
||||||
"self-query should see 1 channel, got %d",
|
|
||||||
len(channels),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint:dupl // structurally similar to TestChannelUserLimit
|
|
||||||
func TestChannelKey(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
database := setupTestDB(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
|
|
||||||
chID, err := database.GetOrCreateChannel(ctx, "#keyed")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default: no key.
|
|
||||||
key, err := database.GetChannelKey(ctx, chID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if key != "" {
|
|
||||||
t.Fatalf("expected empty key, got %q", key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set key.
|
|
||||||
err = database.SetChannelKey(ctx, chID, "secret123")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
key, _ = database.GetChannelKey(ctx, chID)
|
|
||||||
if key != "secret123" {
|
|
||||||
t.Fatalf("expected secret123, got %q", key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear key.
|
|
||||||
err = database.SetChannelKey(ctx, chID, "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
key, _ = database.GetChannelKey(ctx, chID)
|
|
||||||
if key != "" {
|
|
||||||
t.Fatalf("expected empty key, got %q", key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint:dupl // structurally similar to TestChannelKey
|
|
||||||
func TestChannelUserLimit(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
database := setupTestDB(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
|
|
||||||
chID, err := database.GetOrCreateChannel(ctx, "#limited")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default: no limit.
|
|
||||||
limit, err := database.GetChannelUserLimit(ctx, chID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if limit != 0 {
|
|
||||||
t.Fatalf("expected 0 limit, got %d", limit)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set limit.
|
|
||||||
err = database.SetChannelUserLimit(ctx, chID, 50)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
limit, _ = database.GetChannelUserLimit(ctx, chID)
|
|
||||||
if limit != 50 {
|
|
||||||
t.Fatalf("expected 50, got %d", limit)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear limit.
|
|
||||||
err = database.SetChannelUserLimit(ctx, chID, 0)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
limit, _ = database.GetChannelUserLimit(ctx, chID)
|
|
||||||
if limit != 0 {
|
|
||||||
t.Fatalf("expected 0, got %d", limit)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -42,36 +42,10 @@ CREATE TABLE IF NOT EXISTS channels (
|
|||||||
hashcash_bits INTEGER NOT NULL DEFAULT 0,
|
hashcash_bits INTEGER NOT NULL DEFAULT 0,
|
||||||
is_moderated INTEGER NOT NULL DEFAULT 0,
|
is_moderated INTEGER NOT NULL DEFAULT 0,
|
||||||
is_topic_locked INTEGER NOT NULL DEFAULT 1,
|
is_topic_locked INTEGER NOT NULL DEFAULT 1,
|
||||||
is_invite_only INTEGER NOT NULL DEFAULT 0,
|
|
||||||
is_secret INTEGER NOT NULL DEFAULT 0,
|
|
||||||
channel_key TEXT NOT NULL DEFAULT '',
|
|
||||||
user_limit INTEGER NOT NULL DEFAULT 0,
|
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Channel bans
|
|
||||||
CREATE TABLE IF NOT EXISTS channel_bans (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
channel_id INTEGER NOT NULL REFERENCES channels(id) ON DELETE CASCADE,
|
|
||||||
mask TEXT NOT NULL,
|
|
||||||
set_by TEXT NOT NULL,
|
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
UNIQUE(channel_id, mask)
|
|
||||||
);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_channel_bans_channel ON channel_bans(channel_id);
|
|
||||||
|
|
||||||
-- Channel invites (in-memory would be simpler but DB survives restarts)
|
|
||||||
CREATE TABLE IF NOT EXISTS channel_invites (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
channel_id INTEGER NOT NULL REFERENCES channels(id) ON DELETE CASCADE,
|
|
||||||
session_id INTEGER NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
|
|
||||||
invited_by TEXT NOT NULL DEFAULT '',
|
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
UNIQUE(channel_id, session_id)
|
|
||||||
);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_channel_invites_channel ON channel_invites(channel_id);
|
|
||||||
|
|
||||||
-- Channel members
|
-- Channel members
|
||||||
CREATE TABLE IF NOT EXISTS channel_members (
|
CREATE TABLE IF NOT EXISTS channel_members (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
|||||||
25
internal/db/testing.go
Normal file
25
internal/db/testing.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"log/slog"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewTestDatabaseFromConn creates a Database wrapping an
|
||||||
|
// existing *sql.DB connection. Intended for integration
|
||||||
|
// tests in other packages.
|
||||||
|
func NewTestDatabaseFromConn(conn *sql.DB) *Database {
|
||||||
|
return &Database{ //nolint:exhaustruct
|
||||||
|
conn: conn,
|
||||||
|
log: slog.Default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunMigrations applies all schema migrations. Exposed
|
||||||
|
// for integration tests in other packages.
|
||||||
|
func (database *Database) RunMigrations(
|
||||||
|
ctx context.Context,
|
||||||
|
) error {
|
||||||
|
return database.runMigrations(ctx)
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -18,6 +18,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/broker"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/config"
|
"git.eeqj.de/sneak/neoirc/internal/config"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/db"
|
"git.eeqj.de/sneak/neoirc/internal/db"
|
||||||
"git.eeqj.de/sneak/neoirc/internal/globals"
|
"git.eeqj.de/sneak/neoirc/internal/globals"
|
||||||
@@ -213,6 +214,7 @@ func newTestHandlers(
|
|||||||
Database: database,
|
Database: database,
|
||||||
Healthcheck: hcheck,
|
Healthcheck: hcheck,
|
||||||
Stats: tracker,
|
Stats: tracker,
|
||||||
|
Broker: broker.New(),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("test handlers: %w", err)
|
return nil, fmt.Errorf("test handlers: %w", err)
|
||||||
@@ -4378,486 +4380,3 @@ func TestKickDefaultReason(t *testing.T) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Tier 2 Handler Tests ---
|
|
||||||
|
|
||||||
const (
|
|
||||||
inviteCmd = "INVITE"
|
|
||||||
joinedStatus = "joined"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestBanAddRemoveList verifies +b add, list, and -b
|
|
||||||
// remove via MODE commands.
|
|
||||||
func TestBanAddRemoveList(t *testing.T) {
|
|
||||||
tserver := newTestServer(t)
|
|
||||||
opToken := tserver.createSession("banop")
|
|
||||||
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#bans",
|
|
||||||
})
|
|
||||||
|
|
||||||
// Add a ban.
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: modeCmd,
|
|
||||||
toKey: "#bans",
|
|
||||||
bodyKey: []string{"+b", "*!*@evil.com"},
|
|
||||||
})
|
|
||||||
|
|
||||||
_, lastID := tserver.pollMessages(opToken, 0)
|
|
||||||
|
|
||||||
// List bans (+b with no argument).
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: modeCmd,
|
|
||||||
toKey: "#bans",
|
|
||||||
bodyKey: []string{"+b"},
|
|
||||||
})
|
|
||||||
|
|
||||||
msgs, _ := tserver.pollMessages(opToken, lastID)
|
|
||||||
|
|
||||||
// Should have RPL_BANLIST (367).
|
|
||||||
banMsg := findNumericWithParams(msgs, "367")
|
|
||||||
if banMsg == nil {
|
|
||||||
t.Fatalf("expected 367 RPL_BANLIST, got %v", msgs)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Should have RPL_ENDOFBANLIST (368).
|
|
||||||
if !findNumeric(msgs, "368") {
|
|
||||||
t.Fatal("expected 368 RPL_ENDOFBANLIST")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove the ban.
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: modeCmd,
|
|
||||||
toKey: "#bans",
|
|
||||||
bodyKey: []string{"-b", "*!*@evil.com"},
|
|
||||||
})
|
|
||||||
|
|
||||||
_, lastID = tserver.pollMessages(opToken, lastID)
|
|
||||||
|
|
||||||
// List again — should be empty (just end-of-list).
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: modeCmd,
|
|
||||||
toKey: "#bans",
|
|
||||||
bodyKey: []string{"+b"},
|
|
||||||
})
|
|
||||||
|
|
||||||
msgs, _ = tserver.pollMessages(opToken, lastID)
|
|
||||||
banMsg = findNumericWithParams(msgs, "367")
|
|
||||||
if banMsg != nil {
|
|
||||||
t.Fatal("expected no 367 after ban removal")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !findNumeric(msgs, "368") {
|
|
||||||
t.Fatal("expected 368 RPL_ENDOFBANLIST")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBanBlocksJoin verifies that a banned user cannot
|
|
||||||
// join a channel.
|
|
||||||
func TestBanBlocksJoin(t *testing.T) {
|
|
||||||
tserver := newTestServer(t)
|
|
||||||
opToken := tserver.createSession("banop2")
|
|
||||||
userToken := tserver.createSession("banned2")
|
|
||||||
|
|
||||||
// Op creates channel and sets a ban.
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#banjoin",
|
|
||||||
})
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: modeCmd,
|
|
||||||
toKey: "#banjoin",
|
|
||||||
bodyKey: []string{"+b", "banned2!*@*"},
|
|
||||||
})
|
|
||||||
|
|
||||||
// Banned user tries to join.
|
|
||||||
_, lastID := tserver.pollMessages(userToken, 0)
|
|
||||||
tserver.sendCommand(userToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#banjoin",
|
|
||||||
})
|
|
||||||
|
|
||||||
msgs, _ := tserver.pollMessages(userToken, lastID)
|
|
||||||
|
|
||||||
// Should get ERR_BANNEDFROMCHAN (474).
|
|
||||||
if !findNumeric(msgs, "474") {
|
|
||||||
t.Fatalf("expected 474 ERR_BANNEDFROMCHAN, got %v", msgs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBanBlocksPrivmsg verifies that a banned user who
|
|
||||||
// is already in a channel cannot send messages.
|
|
||||||
func TestBanBlocksPrivmsg(t *testing.T) {
|
|
||||||
tserver := newTestServer(t)
|
|
||||||
opToken := tserver.createSession("banmsgop")
|
|
||||||
userToken := tserver.createSession("banmsgusr")
|
|
||||||
|
|
||||||
// Both join.
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#banmsg",
|
|
||||||
})
|
|
||||||
tserver.sendCommand(userToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#banmsg",
|
|
||||||
})
|
|
||||||
|
|
||||||
// Op bans the user.
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: modeCmd,
|
|
||||||
toKey: "#banmsg",
|
|
||||||
bodyKey: []string{"+b", "banmsgusr!*@*"},
|
|
||||||
})
|
|
||||||
|
|
||||||
// User tries to send a message.
|
|
||||||
_, lastID := tserver.pollMessages(userToken, 0)
|
|
||||||
tserver.sendCommand(userToken, map[string]any{
|
|
||||||
commandKey: privmsgCmd,
|
|
||||||
toKey: "#banmsg",
|
|
||||||
bodyKey: []string{"hello"},
|
|
||||||
})
|
|
||||||
|
|
||||||
msgs, _ := tserver.pollMessages(userToken, lastID)
|
|
||||||
|
|
||||||
// Should get ERR_CANNOTSENDTOCHAN (404).
|
|
||||||
if !findNumeric(msgs, "404") {
|
|
||||||
t.Fatalf("expected 404 ERR_CANNOTSENDTOCHAN, got %v", msgs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestInviteOnlyJoin verifies +i behavior: join rejected
|
|
||||||
// without invite, accepted with invite.
|
|
||||||
func TestInviteOnlyJoin(t *testing.T) {
|
|
||||||
tserver := newTestServer(t)
|
|
||||||
opToken := tserver.createSession("invop")
|
|
||||||
userToken := tserver.createSession("invusr")
|
|
||||||
|
|
||||||
// Op creates channel and sets +i.
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#invonly",
|
|
||||||
})
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: modeCmd,
|
|
||||||
toKey: "#invonly",
|
|
||||||
bodyKey: []string{"+i"},
|
|
||||||
})
|
|
||||||
|
|
||||||
// User tries to join without invite.
|
|
||||||
_, lastID := tserver.pollMessages(userToken, 0)
|
|
||||||
tserver.sendCommand(userToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#invonly",
|
|
||||||
})
|
|
||||||
|
|
||||||
msgs, _ := tserver.pollMessages(userToken, lastID)
|
|
||||||
|
|
||||||
if !findNumeric(msgs, "473") {
|
|
||||||
t.Fatalf(
|
|
||||||
"expected 473 ERR_INVITEONLYCHAN, got %v",
|
|
||||||
msgs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Op invites user.
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: inviteCmd,
|
|
||||||
bodyKey: []string{"invusr", "#invonly"},
|
|
||||||
})
|
|
||||||
|
|
||||||
// User tries again — should succeed with invite.
|
|
||||||
_, result := tserver.sendCommand(userToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#invonly",
|
|
||||||
})
|
|
||||||
|
|
||||||
if result[statusKey] != joinedStatus {
|
|
||||||
t.Fatalf(
|
|
||||||
"expected join to succeed with invite, got %v",
|
|
||||||
result,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestSecretChannelHiddenFromList verifies +s hides a
|
|
||||||
// channel from LIST for non-members.
|
|
||||||
func TestSecretChannelHiddenFromList(t *testing.T) {
|
|
||||||
tserver := newTestServer(t)
|
|
||||||
opToken := tserver.createSession("secop")
|
|
||||||
outsiderToken := tserver.createSession("secout")
|
|
||||||
|
|
||||||
// Op creates secret channel.
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#secret",
|
|
||||||
})
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: modeCmd,
|
|
||||||
toKey: "#secret",
|
|
||||||
bodyKey: []string{"+s"},
|
|
||||||
})
|
|
||||||
|
|
||||||
// Outsider does LIST.
|
|
||||||
_, lastID := tserver.pollMessages(outsiderToken, 0)
|
|
||||||
tserver.sendCommand(outsiderToken, map[string]any{
|
|
||||||
commandKey: "LIST",
|
|
||||||
})
|
|
||||||
|
|
||||||
msgs, _ := tserver.pollMessages(outsiderToken, lastID)
|
|
||||||
|
|
||||||
// Should NOT see #secret in any 322 (RPL_LIST).
|
|
||||||
for _, msg := range msgs {
|
|
||||||
code, ok := msg["code"].(float64)
|
|
||||||
if !ok || int(code) != 322 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
params := getNumericParams(msg)
|
|
||||||
for _, p := range params {
|
|
||||||
if p == "#secret" {
|
|
||||||
t.Fatal("outsider should not see #secret in LIST")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Member does LIST — should see it.
|
|
||||||
_, lastID = tserver.pollMessages(opToken, 0)
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: "LIST",
|
|
||||||
})
|
|
||||||
|
|
||||||
msgs, _ = tserver.pollMessages(opToken, lastID)
|
|
||||||
|
|
||||||
found := false
|
|
||||||
|
|
||||||
for _, msg := range msgs {
|
|
||||||
code, ok := msg["code"].(float64)
|
|
||||||
if !ok || int(code) != 322 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
params := getNumericParams(msg)
|
|
||||||
for _, p := range params {
|
|
||||||
if p == "#secret" {
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found {
|
|
||||||
t.Fatal("member should see #secret in LIST")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestChannelKeyJoin verifies +k behavior: wrong/missing
|
|
||||||
// key is rejected, correct key allows join.
|
|
||||||
func TestChannelKeyJoin(t *testing.T) {
|
|
||||||
tserver := newTestServer(t)
|
|
||||||
opToken := tserver.createSession("keyop")
|
|
||||||
userToken := tserver.createSession("keyusr")
|
|
||||||
|
|
||||||
// Op creates keyed channel.
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#keyed",
|
|
||||||
})
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: modeCmd,
|
|
||||||
toKey: "#keyed",
|
|
||||||
bodyKey: []string{"+k", "mykey"},
|
|
||||||
})
|
|
||||||
|
|
||||||
// User tries without key.
|
|
||||||
_, lastID := tserver.pollMessages(userToken, 0)
|
|
||||||
tserver.sendCommand(userToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#keyed",
|
|
||||||
})
|
|
||||||
|
|
||||||
msgs, _ := tserver.pollMessages(userToken, lastID)
|
|
||||||
|
|
||||||
if !findNumeric(msgs, "475") {
|
|
||||||
t.Fatalf(
|
|
||||||
"expected 475 ERR_BADCHANNELKEY, got %v",
|
|
||||||
msgs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// User tries with wrong key.
|
|
||||||
tserver.sendCommand(userToken, map[string]any{
|
|
||||||
commandKey: joinCmd,
|
|
||||||
toKey: "#keyed",
|
|
||||||
bodyKey: []string{"wrongkey"},
|
|
||||||
})
|
|
||||||
|
|
||||||
msgs, _ = tserver.pollMessages(userToken, lastID)
|
|
||||||
if !findNumeric(msgs, "475") {
|
|
||||||
t.Fatalf(
|
|
||||||
"expected 475 ERR_BADCHANNELKEY for wrong key, got %v",
|
|
||||||
msgs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// User tries with correct key.
|
|
||||||
_, result := tserver.sendCommand(userToken, map[string]any{
|
|
||||||
commandKey: joinCmd,
|
|
||||||
toKey: "#keyed",
|
|
||||||
bodyKey: []string{"mykey"},
|
|
||||||
})
|
|
||||||
|
|
||||||
if result[statusKey] != joinedStatus {
|
|
||||||
t.Fatalf(
|
|
||||||
"expected join to succeed with correct key, got %v",
|
|
||||||
result,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestUserLimitEnforcement verifies +l behavior: blocks
|
|
||||||
// join when at capacity.
|
|
||||||
func TestUserLimitEnforcement(t *testing.T) {
|
|
||||||
tserver := newTestServer(t)
|
|
||||||
opToken := tserver.createSession("limop")
|
|
||||||
user1Token := tserver.createSession("limusr1")
|
|
||||||
user2Token := tserver.createSession("limusr2")
|
|
||||||
|
|
||||||
// Op creates channel with limit 2.
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#limited",
|
|
||||||
})
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: modeCmd,
|
|
||||||
toKey: "#limited",
|
|
||||||
bodyKey: []string{"+l", "2"},
|
|
||||||
})
|
|
||||||
|
|
||||||
// User1 joins — should succeed (2 members now: op + user1).
|
|
||||||
_, result := tserver.sendCommand(user1Token, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#limited",
|
|
||||||
})
|
|
||||||
if result[statusKey] != joinedStatus {
|
|
||||||
t.Fatalf("user1 should join, got %v", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
// User2 tries to join — should fail (at limit: 2/2).
|
|
||||||
_, lastID := tserver.pollMessages(user2Token, 0)
|
|
||||||
tserver.sendCommand(user2Token, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#limited",
|
|
||||||
})
|
|
||||||
|
|
||||||
msgs, _ := tserver.pollMessages(user2Token, lastID)
|
|
||||||
|
|
||||||
if !findNumeric(msgs, "471") {
|
|
||||||
t.Fatalf(
|
|
||||||
"expected 471 ERR_CHANNELISFULL, got %v",
|
|
||||||
msgs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestModeStringIncludesNewModes verifies that querying
|
|
||||||
// channel mode returns the new modes (+i, +s, +k, +l).
|
|
||||||
func TestModeStringIncludesNewModes(t *testing.T) {
|
|
||||||
tserver := newTestServer(t)
|
|
||||||
opToken := tserver.createSession("modestrop")
|
|
||||||
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#modestr",
|
|
||||||
})
|
|
||||||
|
|
||||||
// Set all tier 2 modes.
|
|
||||||
for _, modeChange := range [][]string{
|
|
||||||
{"+i"}, {"+s"}, {"+k", "pw"}, {"+l", "50"},
|
|
||||||
} {
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: modeCmd,
|
|
||||||
toKey: "#modestr",
|
|
||||||
bodyKey: modeChange,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
_, lastID := tserver.pollMessages(opToken, 0)
|
|
||||||
|
|
||||||
// Query mode.
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: modeCmd, toKey: "#modestr",
|
|
||||||
})
|
|
||||||
|
|
||||||
msgs, _ := tserver.pollMessages(opToken, lastID)
|
|
||||||
modeMsg := findNumericWithParams(msgs, "324")
|
|
||||||
|
|
||||||
if modeMsg == nil {
|
|
||||||
t.Fatal("expected 324 RPL_CHANNELMODEIS")
|
|
||||||
}
|
|
||||||
|
|
||||||
params := getNumericParams(modeMsg)
|
|
||||||
if len(params) < 2 {
|
|
||||||
t.Fatalf("too few params in 324: %v", params)
|
|
||||||
}
|
|
||||||
|
|
||||||
modeString := params[1]
|
|
||||||
|
|
||||||
for _, c := range []string{"i", "s", "k", "l"} {
|
|
||||||
if !strings.Contains(modeString, c) {
|
|
||||||
t.Fatalf(
|
|
||||||
"mode string %q missing %q",
|
|
||||||
modeString, c,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestISUPPORT verifies the 005 numeric includes the
|
|
||||||
// updated CHANMODES string.
|
|
||||||
func TestISUPPORT(t *testing.T) {
|
|
||||||
tserver := newTestServer(t)
|
|
||||||
token := tserver.createSession("isupport")
|
|
||||||
|
|
||||||
msgs, _ := tserver.pollMessages(token, 0)
|
|
||||||
|
|
||||||
isupp := findNumericWithParams(msgs, "005")
|
|
||||||
if isupp == nil {
|
|
||||||
t.Fatal("expected 005 RPL_ISUPPORT")
|
|
||||||
}
|
|
||||||
|
|
||||||
body, _ := isupp["body"].(string)
|
|
||||||
params := getNumericParams(isupp)
|
|
||||||
|
|
||||||
combined := body + " " + strings.Join(params, " ")
|
|
||||||
|
|
||||||
if !strings.Contains(combined, "CHANMODES=b,k,Hl,imnst") {
|
|
||||||
t.Fatalf(
|
|
||||||
"ISUPPORT missing updated CHANMODES, got body=%q params=%v",
|
|
||||||
body, params,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestNonOpCannotSetModes verifies non-operators
|
|
||||||
// cannot set +i, +s, +k, +l, +b.
|
|
||||||
func TestNonOpCannotSetModes(t *testing.T) {
|
|
||||||
tserver := newTestServer(t)
|
|
||||||
opToken := tserver.createSession("modeopx")
|
|
||||||
userToken := tserver.createSession("modeusrx")
|
|
||||||
|
|
||||||
tserver.sendCommand(opToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#noperm",
|
|
||||||
})
|
|
||||||
tserver.sendCommand(userToken, map[string]any{
|
|
||||||
commandKey: joinCmd, toKey: "#noperm",
|
|
||||||
})
|
|
||||||
|
|
||||||
modes := [][]string{
|
|
||||||
{"+i"}, {"+s"}, {"+k", "key"}, {"+l", "10"},
|
|
||||||
{"+b", "bad!*@*"},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, modeChange := range modes {
|
|
||||||
_, lastID := tserver.pollMessages(userToken, 0)
|
|
||||||
tserver.sendCommand(userToken, map[string]any{
|
|
||||||
commandKey: modeCmd,
|
|
||||||
toKey: "#noperm",
|
|
||||||
bodyKey: modeChange,
|
|
||||||
})
|
|
||||||
|
|
||||||
msgs, _ := tserver.pollMessages(userToken, lastID)
|
|
||||||
|
|
||||||
// Should get 482 ERR_CHANOPRIVSNEEDED.
|
|
||||||
if !findNumeric(msgs, "482") {
|
|
||||||
t.Fatalf(
|
|
||||||
"expected 482 for %v, got %v",
|
|
||||||
modeChange, msgs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ type Params struct {
|
|||||||
Database *db.Database
|
Database *db.Database
|
||||||
Healthcheck *healthcheck.Healthcheck
|
Healthcheck *healthcheck.Healthcheck
|
||||||
Stats *stats.Tracker
|
Stats *stats.Tracker
|
||||||
|
Broker *broker.Broker
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultIdleTimeout = 30 * 24 * time.Hour
|
const defaultIdleTimeout = 30 * 24 * time.Hour
|
||||||
@@ -79,7 +80,7 @@ func New(
|
|||||||
params: ¶ms,
|
params: ¶ms,
|
||||||
log: params.Logger.Get(),
|
log: params.Logger.Get(),
|
||||||
hc: params.Healthcheck,
|
hc: params.Healthcheck,
|
||||||
broker: broker.New(),
|
broker: params.Broker,
|
||||||
hashcashVal: hashcash.NewValidator(resource),
|
hashcashVal: hashcash.NewValidator(resource),
|
||||||
channelHashcash: hashcash.NewChannelValidator(),
|
channelHashcash: hashcash.NewChannelValidator(),
|
||||||
loginLimiter: ratelimit.New(loginRate, loginBurst),
|
loginLimiter: ratelimit.New(loginRate, loginBurst),
|
||||||
|
|||||||
1562
internal/ircserver/commands.go
Normal file
1562
internal/ircserver/commands.go
Normal file
File diff suppressed because it is too large
Load Diff
551
internal/ircserver/conn.go
Normal file
551
internal/ircserver/conn.go
Normal file
@@ -0,0 +1,551 @@
|
|||||||
|
package ircserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/broker"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/config"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/db"
|
||||||
|
"git.eeqj.de/sneak/neoirc/pkg/irc"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxLineLen = 512
|
||||||
|
readTimeout = 5 * time.Minute
|
||||||
|
writeTimeout = 30 * time.Second
|
||||||
|
dnsTimeout = 3 * time.Second
|
||||||
|
pollInterval = 100 * time.Millisecond
|
||||||
|
pingInterval = 90 * time.Second
|
||||||
|
pongDeadline = 30 * time.Second
|
||||||
|
maxNickLen = 32
|
||||||
|
minPasswordLen = 8
|
||||||
|
)
|
||||||
|
|
||||||
|
// Conn represents a single IRC client TCP connection.
|
||||||
|
type Conn struct {
|
||||||
|
conn net.Conn
|
||||||
|
log *slog.Logger
|
||||||
|
database *db.Database
|
||||||
|
brk *broker.Broker
|
||||||
|
cfg *config.Config
|
||||||
|
serverSfx string
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
nick string
|
||||||
|
username string
|
||||||
|
realname string
|
||||||
|
hostname string
|
||||||
|
remoteIP string
|
||||||
|
sessionID int64
|
||||||
|
clientID int64
|
||||||
|
|
||||||
|
registered bool
|
||||||
|
gotNick bool
|
||||||
|
gotUser bool
|
||||||
|
passWord string
|
||||||
|
|
||||||
|
lastQueueID int64
|
||||||
|
closed bool
|
||||||
|
cancel context.CancelFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConn(
|
||||||
|
ctx context.Context,
|
||||||
|
tcpConn net.Conn,
|
||||||
|
log *slog.Logger,
|
||||||
|
database *db.Database,
|
||||||
|
brk *broker.Broker,
|
||||||
|
cfg *config.Config,
|
||||||
|
) *Conn {
|
||||||
|
host, _, _ := net.SplitHostPort(tcpConn.RemoteAddr().String())
|
||||||
|
|
||||||
|
srvName := cfg.ServerName
|
||||||
|
if srvName == "" {
|
||||||
|
srvName = "neoirc"
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Conn{ //nolint:exhaustruct // zero-value defaults
|
||||||
|
conn: tcpConn,
|
||||||
|
log: log,
|
||||||
|
database: database,
|
||||||
|
brk: brk,
|
||||||
|
cfg: cfg,
|
||||||
|
serverSfx: srvName,
|
||||||
|
remoteIP: host,
|
||||||
|
hostname: resolveHost(ctx, host),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// resolveHost does a reverse DNS lookup, returning the IP
|
||||||
|
// on failure.
|
||||||
|
func resolveHost(ctx context.Context, addr string) string {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, dnsTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
resolver := &net.Resolver{} //nolint:exhaustruct
|
||||||
|
|
||||||
|
names, err := resolver.LookupAddr(ctx, addr)
|
||||||
|
if err != nil || len(names) == 0 {
|
||||||
|
return addr
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimSuffix(names[0], ".")
|
||||||
|
}
|
||||||
|
|
||||||
|
// serve is the main loop for a single IRC client connection.
|
||||||
|
func (c *Conn) serve(ctx context.Context) {
|
||||||
|
ctx, c.cancel = context.WithCancel(ctx)
|
||||||
|
defer c.cleanup(ctx)
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(c.conn)
|
||||||
|
scanner.Buffer(make([]byte, maxLineLen), maxLineLen)
|
||||||
|
|
||||||
|
for {
|
||||||
|
_ = c.conn.SetReadDeadline(
|
||||||
|
time.Now().Add(readTimeout),
|
||||||
|
)
|
||||||
|
|
||||||
|
if !scanner.Scan() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
line := scanner.Text()
|
||||||
|
if line == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := ParseMessage(line)
|
||||||
|
if msg == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
c.handleMessage(ctx, msg)
|
||||||
|
|
||||||
|
if c.closed {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Conn) cleanup(ctx context.Context) {
|
||||||
|
c.mu.Lock()
|
||||||
|
wasRegistered := c.registered
|
||||||
|
sessID := c.sessionID
|
||||||
|
nick := c.nick
|
||||||
|
c.closed = true
|
||||||
|
c.mu.Unlock()
|
||||||
|
|
||||||
|
if wasRegistered && sessID > 0 {
|
||||||
|
c.broadcastQuit(ctx, nick, "Connection closed")
|
||||||
|
c.database.DeleteSession(ctx, sessID) //nolint:errcheck,gosec
|
||||||
|
}
|
||||||
|
|
||||||
|
c.conn.Close() //nolint:errcheck,gosec
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Conn) broadcastQuit(
|
||||||
|
ctx context.Context,
|
||||||
|
nick, reason string,
|
||||||
|
) {
|
||||||
|
channels, err := c.database.GetSessionChannels(
|
||||||
|
ctx, c.sessionID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
notified := make(map[int64]bool)
|
||||||
|
|
||||||
|
for _, ch := range channels {
|
||||||
|
chID, getErr := c.database.GetChannelByName(
|
||||||
|
ctx, ch.Name,
|
||||||
|
)
|
||||||
|
if getErr != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
memberIDs, memErr := c.database.GetChannelMemberIDs(
|
||||||
|
ctx, chID,
|
||||||
|
)
|
||||||
|
if memErr != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, mid := range memberIDs {
|
||||||
|
if mid == c.sessionID || notified[mid] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
notified[mid] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
body, _ := json.Marshal([]string{reason}) //nolint:errchkjson
|
||||||
|
|
||||||
|
for sid := range notified {
|
||||||
|
dbID, _, insErr := c.database.InsertMessage(
|
||||||
|
ctx, irc.CmdQuit, nick, "", nil, body, nil,
|
||||||
|
)
|
||||||
|
if insErr != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = c.database.EnqueueToSession(ctx, sid, dbID)
|
||||||
|
c.brk.Notify(sid)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Part from all channels so they get cleaned up.
|
||||||
|
for _, ch := range channels {
|
||||||
|
c.database.PartChannel(ctx, ch.ID, c.sessionID) //nolint:errcheck,gosec
|
||||||
|
c.database.DeleteChannelIfEmpty(ctx, ch.ID) //nolint:errcheck,gosec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// send writes a formatted IRC line to the connection.
|
||||||
|
func (c *Conn) send(line string) {
|
||||||
|
_ = c.conn.SetWriteDeadline(
|
||||||
|
time.Now().Add(writeTimeout),
|
||||||
|
)
|
||||||
|
|
||||||
|
_, _ = fmt.Fprintf(c.conn, "%s\r\n", line)
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendNumeric sends a numeric reply from the server.
|
||||||
|
func (c *Conn) sendNumeric(
|
||||||
|
code irc.IRCMessageType,
|
||||||
|
params ...string,
|
||||||
|
) {
|
||||||
|
nick := c.nick
|
||||||
|
if nick == "" {
|
||||||
|
nick = "*"
|
||||||
|
}
|
||||||
|
|
||||||
|
allParams := make([]string, 0, 1+len(params))
|
||||||
|
allParams = append(allParams, nick)
|
||||||
|
allParams = append(allParams, params...)
|
||||||
|
|
||||||
|
c.send(FormatMessage(
|
||||||
|
c.serverSfx, code.Code(), allParams...,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendFromServer sends a message from the server.
|
||||||
|
func (c *Conn) sendFromServer(
|
||||||
|
command string, params ...string,
|
||||||
|
) {
|
||||||
|
c.send(FormatMessage(c.serverSfx, command, params...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// hostmask returns the client's full hostmask
|
||||||
|
// (nick!user@host).
|
||||||
|
func (c *Conn) hostmask() string {
|
||||||
|
user := c.username
|
||||||
|
if user == "" {
|
||||||
|
user = c.nick
|
||||||
|
}
|
||||||
|
|
||||||
|
host := c.hostname
|
||||||
|
if host == "" {
|
||||||
|
host = c.remoteIP
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.nick + "!" + user + "@" + host
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleMessage dispatches a parsed IRC message.
|
||||||
|
//
|
||||||
|
//nolint:cyclop // dispatch table is inherently branchy
|
||||||
|
func (c *Conn) handleMessage(
|
||||||
|
ctx context.Context,
|
||||||
|
msg *Message,
|
||||||
|
) {
|
||||||
|
// Before registration, only NICK, USER, PASS, PING,
|
||||||
|
// QUIT, and CAP are accepted.
|
||||||
|
if !c.registered {
|
||||||
|
c.handlePreRegistration(ctx, msg)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch msg.Command {
|
||||||
|
case irc.CmdPing:
|
||||||
|
c.handlePing(msg)
|
||||||
|
case "PONG":
|
||||||
|
// Silently accept.
|
||||||
|
case irc.CmdNick:
|
||||||
|
c.handleNick(ctx, msg)
|
||||||
|
case irc.CmdPrivmsg, irc.CmdNotice:
|
||||||
|
c.handlePrivmsg(ctx, msg)
|
||||||
|
case irc.CmdJoin:
|
||||||
|
c.handleJoin(ctx, msg)
|
||||||
|
case irc.CmdPart:
|
||||||
|
c.handlePart(ctx, msg)
|
||||||
|
case irc.CmdQuit:
|
||||||
|
c.handleQuit(msg)
|
||||||
|
case irc.CmdTopic:
|
||||||
|
c.handleTopic(ctx, msg)
|
||||||
|
case irc.CmdMode:
|
||||||
|
c.handleMode(ctx, msg)
|
||||||
|
case irc.CmdNames:
|
||||||
|
c.handleNames(ctx, msg)
|
||||||
|
case irc.CmdList:
|
||||||
|
c.handleList(ctx)
|
||||||
|
case irc.CmdWhois:
|
||||||
|
c.handleWhois(ctx, msg)
|
||||||
|
case irc.CmdWho:
|
||||||
|
c.handleWho(ctx, msg)
|
||||||
|
case irc.CmdLusers:
|
||||||
|
c.handleLusers(ctx)
|
||||||
|
case irc.CmdMotd:
|
||||||
|
c.deliverMOTD()
|
||||||
|
case irc.CmdOper:
|
||||||
|
c.handleOper(ctx, msg)
|
||||||
|
case irc.CmdAway:
|
||||||
|
c.handleAway(ctx, msg)
|
||||||
|
case irc.CmdKick:
|
||||||
|
c.handleKick(ctx, msg)
|
||||||
|
case irc.CmdPass:
|
||||||
|
c.handlePassPostReg(ctx, msg)
|
||||||
|
case "INVITE":
|
||||||
|
c.handleInvite(ctx, msg)
|
||||||
|
case "CAP":
|
||||||
|
c.handleCAP(msg)
|
||||||
|
case "USERHOST":
|
||||||
|
c.handleUserhost(ctx, msg)
|
||||||
|
default:
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.ErrUnknownCommand,
|
||||||
|
msg.Command, "Unknown command",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handlePreRegistration handles messages before the
|
||||||
|
// connection is registered (NICK+USER received).
|
||||||
|
func (c *Conn) handlePreRegistration(
|
||||||
|
ctx context.Context,
|
||||||
|
msg *Message,
|
||||||
|
) {
|
||||||
|
switch msg.Command {
|
||||||
|
case irc.CmdPass:
|
||||||
|
if len(msg.Params) < 1 {
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.ErrNeedMoreParams,
|
||||||
|
"PASS", "Not enough parameters",
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.passWord = msg.Params[0]
|
||||||
|
case irc.CmdNick:
|
||||||
|
if len(msg.Params) < 1 {
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.ErrNoNicknameGiven,
|
||||||
|
"No nickname given",
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.nick = msg.Params[0]
|
||||||
|
if len(c.nick) > maxNickLen {
|
||||||
|
c.nick = c.nick[:maxNickLen]
|
||||||
|
}
|
||||||
|
|
||||||
|
c.gotNick = true
|
||||||
|
case irc.CmdUser:
|
||||||
|
if len(msg.Params) < 4 { //nolint:mnd
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.ErrNeedMoreParams,
|
||||||
|
"USER", "Not enough parameters",
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.username = msg.Params[0]
|
||||||
|
c.realname = msg.Params[3]
|
||||||
|
c.gotUser = true
|
||||||
|
case irc.CmdPing:
|
||||||
|
c.handlePing(msg)
|
||||||
|
|
||||||
|
return
|
||||||
|
case irc.CmdQuit:
|
||||||
|
c.handleQuit(msg)
|
||||||
|
|
||||||
|
return
|
||||||
|
case "CAP":
|
||||||
|
c.handleCAP(msg)
|
||||||
|
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.ErrNotRegistered,
|
||||||
|
"You have not registered",
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to complete registration once we have both
|
||||||
|
// NICK and USER.
|
||||||
|
if c.gotNick && c.gotUser {
|
||||||
|
c.completeRegistration(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// completeRegistration creates a session and sends the
|
||||||
|
// welcome burst.
|
||||||
|
func (c *Conn) completeRegistration(ctx context.Context) {
|
||||||
|
// Check if nick is valid.
|
||||||
|
if c.nick == "" {
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.ErrNoNicknameGiven, "No nickname given",
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create session in DB.
|
||||||
|
sessionID, clientID, _, err := c.database.CreateSession(
|
||||||
|
ctx, c.nick, c.username, c.hostname, c.remoteIP,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
if strings.Contains(err.Error(), "UNIQUE constraint") ||
|
||||||
|
strings.Contains(err.Error(), "nick") {
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.ErrNicknameInUse,
|
||||||
|
c.nick, "Nickname is already in use",
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.log.Error(
|
||||||
|
"failed to create session", "error", err,
|
||||||
|
)
|
||||||
|
c.send("ERROR :Internal server error")
|
||||||
|
c.closed = true
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.mu.Lock()
|
||||||
|
c.sessionID = sessionID
|
||||||
|
c.clientID = clientID
|
||||||
|
c.registered = true
|
||||||
|
c.mu.Unlock()
|
||||||
|
|
||||||
|
// If PASS was provided before registration, set the
|
||||||
|
// session password.
|
||||||
|
if c.passWord != "" && len(c.passWord) >= minPasswordLen {
|
||||||
|
c.setPassword(ctx, c.passWord)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send welcome burst.
|
||||||
|
c.deliverWelcome()
|
||||||
|
c.deliverLusers(ctx)
|
||||||
|
c.deliverMOTD()
|
||||||
|
|
||||||
|
// Start the message relay goroutine.
|
||||||
|
go c.relayMessages(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverWelcome sends 001-005 welcome numerics.
|
||||||
|
func (c *Conn) deliverWelcome() {
|
||||||
|
c.sendNumeric(irc.RplWelcome, fmt.Sprintf(
|
||||||
|
"Welcome to the %s Network, %s",
|
||||||
|
c.serverSfx, c.hostmask(),
|
||||||
|
))
|
||||||
|
c.sendNumeric(irc.RplYourHost, fmt.Sprintf(
|
||||||
|
"Your host is %s, running version neoirc",
|
||||||
|
c.serverSfx,
|
||||||
|
))
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.RplCreated,
|
||||||
|
"This server was created recently",
|
||||||
|
)
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.RplMyInfo,
|
||||||
|
c.serverSfx, "neoirc", "", "mnst",
|
||||||
|
)
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.RplIsupport,
|
||||||
|
"CHANTYPES=#",
|
||||||
|
"NICKLEN=32",
|
||||||
|
"PREFIX=(ov)@+",
|
||||||
|
"CHANMODES=,,H,mnst",
|
||||||
|
"NETWORK="+c.serverSfx,
|
||||||
|
"are supported by this server",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverLusers sends 251/252/254/255 server statistics.
|
||||||
|
func (c *Conn) deliverLusers(ctx context.Context) {
|
||||||
|
users, _ := c.database.GetUserCount(ctx)
|
||||||
|
opers, _ := c.database.GetOperCount(ctx)
|
||||||
|
channels, _ := c.database.GetChannelCount(ctx)
|
||||||
|
|
||||||
|
c.sendNumeric(irc.RplLuserClient, fmt.Sprintf(
|
||||||
|
"There are %d users and 0 invisible on 1 servers",
|
||||||
|
users,
|
||||||
|
))
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.RplLuserOp,
|
||||||
|
strconv.FormatInt(opers, 10),
|
||||||
|
"operator(s) online",
|
||||||
|
)
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.RplLuserChannels,
|
||||||
|
strconv.FormatInt(channels, 10),
|
||||||
|
"channels formed",
|
||||||
|
)
|
||||||
|
c.sendNumeric(irc.RplLuserMe, fmt.Sprintf(
|
||||||
|
"I have %d clients and 1 servers", users,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverMOTD sends 375/372/376 MOTD lines.
|
||||||
|
func (c *Conn) deliverMOTD() {
|
||||||
|
motd := c.cfg.MOTD
|
||||||
|
if motd == "" {
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.ErrNoMotd, "MOTD File is missing",
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.sendNumeric(irc.RplMotdStart, fmt.Sprintf(
|
||||||
|
"- %s Message of the Day -", c.serverSfx,
|
||||||
|
))
|
||||||
|
|
||||||
|
for _, line := range strings.Split(motd, "\n") {
|
||||||
|
c.sendNumeric(irc.RplMotd, "- "+line)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.sendNumeric(
|
||||||
|
irc.RplEndOfMotd, "End of /MOTD command",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// setPassword sets a bcrypt password on the session.
|
||||||
|
func (c *Conn) setPassword(ctx context.Context, pw string) {
|
||||||
|
// Use the database's auth module to hash and store.
|
||||||
|
err := c.database.SetPassword(ctx, c.sessionID, pw)
|
||||||
|
if err != nil {
|
||||||
|
c.log.Error(
|
||||||
|
"failed to set password", "error", err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
43
internal/ircserver/export_test.go
Normal file
43
internal/ircserver/export_test.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package ircserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log/slog"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/broker"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/config"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/db"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewTestServer creates a Server suitable for testing.
|
||||||
|
// The caller must call Stop() when finished.
|
||||||
|
func NewTestServer(
|
||||||
|
log *slog.Logger,
|
||||||
|
cfg *config.Config,
|
||||||
|
database *db.Database,
|
||||||
|
brk *broker.Broker,
|
||||||
|
) *Server {
|
||||||
|
return &Server{ //nolint:exhaustruct
|
||||||
|
log: log,
|
||||||
|
cfg: cfg,
|
||||||
|
database: database,
|
||||||
|
brk: brk,
|
||||||
|
conns: make(map[*Conn]struct{}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start exposes the unexported start method for tests.
|
||||||
|
func (s *Server) Start(addr string) error {
|
||||||
|
return s.start(context.Background(), addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop exposes the unexported stop method for tests.
|
||||||
|
func (s *Server) Stop() {
|
||||||
|
s.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Listener returns the server's net.Listener for tests.
|
||||||
|
func (s *Server) Listener() net.Listener {
|
||||||
|
return s.listener
|
||||||
|
}
|
||||||
123
internal/ircserver/parser.go
Normal file
123
internal/ircserver/parser.go
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
// Package ircserver implements a traditional IRC wire protocol
|
||||||
|
// listener (RFC 1459/2812) that bridges to the neoirc HTTP/JSON
|
||||||
|
// server internals.
|
||||||
|
package ircserver
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
// Message represents a parsed IRC wire protocol message.
|
||||||
|
type Message struct {
|
||||||
|
// Prefix is the optional :prefix at the start (may be
|
||||||
|
// empty for client-to-server messages).
|
||||||
|
Prefix string
|
||||||
|
// Command is the IRC command (e.g., "PRIVMSG", "NICK").
|
||||||
|
Command string
|
||||||
|
// Params holds the positional parameters, including the
|
||||||
|
// trailing parameter (which was preceded by ':' on the
|
||||||
|
// wire).
|
||||||
|
Params []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseMessage parses a single IRC wire protocol line
|
||||||
|
// (without the trailing CR-LF) into a Message.
|
||||||
|
// Returns nil if the line is empty.
|
||||||
|
//
|
||||||
|
// IRC message format (RFC 1459 §2.3.1):
|
||||||
|
//
|
||||||
|
// [":" prefix SPACE] command { SPACE param } [SPACE ":" trailing]
|
||||||
|
func ParseMessage(line string) *Message {
|
||||||
|
if line == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := &Message{} //nolint:exhaustruct // fields set below
|
||||||
|
|
||||||
|
// Extract prefix if present.
|
||||||
|
if line[0] == ':' {
|
||||||
|
idx := strings.IndexByte(line, ' ')
|
||||||
|
if idx < 0 {
|
||||||
|
// Only a prefix, no command — invalid.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.Prefix = line[1:idx]
|
||||||
|
line = line[idx+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip leading spaces.
|
||||||
|
line = strings.TrimLeft(line, " ")
|
||||||
|
if line == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract command.
|
||||||
|
idx := strings.IndexByte(line, ' ')
|
||||||
|
if idx < 0 {
|
||||||
|
msg.Command = strings.ToUpper(line)
|
||||||
|
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.Command = strings.ToUpper(line[:idx])
|
||||||
|
line = line[idx+1:]
|
||||||
|
|
||||||
|
// Extract parameters.
|
||||||
|
for line != "" {
|
||||||
|
line = strings.TrimLeft(line, " ")
|
||||||
|
if line == "" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trailing parameter (everything after ':').
|
||||||
|
if line[0] == ':' {
|
||||||
|
msg.Params = append(msg.Params, line[1:])
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
idx = strings.IndexByte(line, ' ')
|
||||||
|
if idx < 0 {
|
||||||
|
msg.Params = append(msg.Params, line)
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.Params = append(msg.Params, line[:idx])
|
||||||
|
line = line[idx+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormatMessage formats an IRC message into wire protocol
|
||||||
|
// format (without the trailing CR-LF).
|
||||||
|
func FormatMessage(
|
||||||
|
prefix, command string,
|
||||||
|
params ...string,
|
||||||
|
) string {
|
||||||
|
var buf strings.Builder
|
||||||
|
|
||||||
|
if prefix != "" {
|
||||||
|
buf.WriteByte(':')
|
||||||
|
buf.WriteString(prefix)
|
||||||
|
buf.WriteByte(' ')
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.WriteString(command)
|
||||||
|
|
||||||
|
for i, param := range params {
|
||||||
|
buf.WriteByte(' ')
|
||||||
|
|
||||||
|
isLast := i == len(params)-1
|
||||||
|
needsColon := strings.Contains(param, " ") ||
|
||||||
|
param == "" || param[0] == ':'
|
||||||
|
|
||||||
|
if isLast && needsColon {
|
||||||
|
buf.WriteByte(':')
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.WriteString(param)
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
328
internal/ircserver/parser_test.go
Normal file
328
internal/ircserver/parser_test.go
Normal file
@@ -0,0 +1,328 @@
|
|||||||
|
package ircserver_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/ircserver"
|
||||||
|
)
|
||||||
|
|
||||||
|
//nolint:funlen // table-driven test
|
||||||
|
func TestParseMessage(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
want *ircserver.Message
|
||||||
|
wantNil bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "empty",
|
||||||
|
input: "",
|
||||||
|
want: nil,
|
||||||
|
wantNil: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "simple command",
|
||||||
|
input: "PING",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "PING",
|
||||||
|
Params: nil,
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "command with one param",
|
||||||
|
input: "NICK alice",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "NICK",
|
||||||
|
Params: []string{"alice"},
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "command case insensitive",
|
||||||
|
input: "nick Alice",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "NICK",
|
||||||
|
Params: []string{"Alice"},
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "privmsg with trailing",
|
||||||
|
input: "PRIVMSG #general :hello world",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "PRIVMSG",
|
||||||
|
Params: []string{"#general", "hello world"},
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with prefix",
|
||||||
|
input: ":server.example.com 001 alice :Welcome to IRC",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "server.example.com",
|
||||||
|
Command: "001",
|
||||||
|
Params: []string{"alice", "Welcome to IRC"},
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "user command",
|
||||||
|
input: "USER alice 0 * :Alice Smith",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "USER",
|
||||||
|
Params: []string{
|
||||||
|
"alice", "0", "*", "Alice Smith",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "join channel",
|
||||||
|
input: "JOIN #general",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "JOIN",
|
||||||
|
Params: []string{"#general"},
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "quit with trailing",
|
||||||
|
input: "QUIT :leaving now",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "QUIT",
|
||||||
|
Params: []string{"leaving now"},
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "quit without reason",
|
||||||
|
input: "QUIT",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "QUIT",
|
||||||
|
Params: nil,
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "mode query",
|
||||||
|
input: "MODE #general",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "MODE",
|
||||||
|
Params: []string{"#general"},
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "kick with reason",
|
||||||
|
input: "KICK #general bob :misbehaving",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "KICK",
|
||||||
|
Params: []string{
|
||||||
|
"#general", "bob", "misbehaving",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty trailing",
|
||||||
|
input: "PRIVMSG #general :",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "PRIVMSG",
|
||||||
|
Params: []string{"#general", ""},
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "pass command",
|
||||||
|
input: "PASS mysecret",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "PASS",
|
||||||
|
Params: []string{"mysecret"},
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ping with server",
|
||||||
|
input: "PING :irc.example.com",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "PING",
|
||||||
|
Params: []string{"irc.example.com"},
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "topic with trailing spaces",
|
||||||
|
input: "TOPIC #general :Welcome to the channel!",
|
||||||
|
want: &ircserver.Message{
|
||||||
|
Prefix: "",
|
||||||
|
Command: "TOPIC",
|
||||||
|
Params: []string{
|
||||||
|
"#general",
|
||||||
|
"Welcome to the channel!",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantNil: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range tests {
|
||||||
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
got := ircserver.ParseMessage(testCase.input)
|
||||||
|
if testCase.wantNil {
|
||||||
|
if got != nil {
|
||||||
|
t.Fatalf("expected nil, got %+v", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if got == nil {
|
||||||
|
t.Fatal("expected non-nil message")
|
||||||
|
}
|
||||||
|
|
||||||
|
if got.Prefix != testCase.want.Prefix {
|
||||||
|
t.Errorf(
|
||||||
|
"prefix: got %q, want %q",
|
||||||
|
got.Prefix, testCase.want.Prefix,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if got.Command != testCase.want.Command {
|
||||||
|
t.Errorf(
|
||||||
|
"command: got %q, want %q",
|
||||||
|
got.Command, testCase.want.Command,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(got.Params) != len(testCase.want.Params) {
|
||||||
|
t.Fatalf(
|
||||||
|
"params length: got %d, want %d (%v vs %v)",
|
||||||
|
len(got.Params),
|
||||||
|
len(testCase.want.Params),
|
||||||
|
got.Params,
|
||||||
|
testCase.want.Params,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, p := range got.Params {
|
||||||
|
if p != testCase.want.Params[i] {
|
||||||
|
t.Errorf(
|
||||||
|
"param[%d]: got %q, want %q",
|
||||||
|
i, p, testCase.want.Params[i],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormatMessage(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
prefix string
|
||||||
|
command string
|
||||||
|
params []string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "simple command",
|
||||||
|
prefix: "",
|
||||||
|
command: "PING",
|
||||||
|
params: nil,
|
||||||
|
want: "PING",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with prefix",
|
||||||
|
prefix: "server",
|
||||||
|
command: "PONG",
|
||||||
|
params: []string{"server"},
|
||||||
|
want: ":server PONG server",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "privmsg with trailing",
|
||||||
|
prefix: "alice!alice@host",
|
||||||
|
command: "PRIVMSG",
|
||||||
|
params: []string{"#general", "hello world"},
|
||||||
|
want: ":alice!alice@host PRIVMSG #general :hello world",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "numeric reply",
|
||||||
|
prefix: "server",
|
||||||
|
command: "001",
|
||||||
|
params: []string{"alice", "Welcome to IRC"},
|
||||||
|
want: ":server 001 alice :Welcome to IRC",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty trailing",
|
||||||
|
prefix: "server",
|
||||||
|
command: "PRIVMSG",
|
||||||
|
params: []string{"#chan", ""},
|
||||||
|
want: ":server PRIVMSG #chan :",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range tests {
|
||||||
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
got := ircserver.FormatMessage(
|
||||||
|
testCase.prefix, testCase.command, testCase.params...,
|
||||||
|
)
|
||||||
|
if got != testCase.want {
|
||||||
|
t.Errorf("got %q, want %q", got, testCase.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseFormatRoundTrip(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
// Round-trip only works for lines where the last
|
||||||
|
// parameter either contains a space (gets ':' prefix
|
||||||
|
// on format) or is a non-trailing single token.
|
||||||
|
lines := []string{
|
||||||
|
"PING",
|
||||||
|
"NICK alice",
|
||||||
|
"PRIVMSG #general :hello world",
|
||||||
|
"JOIN #general",
|
||||||
|
"MODE #general",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
msg := ircserver.ParseMessage(line)
|
||||||
|
if msg == nil {
|
||||||
|
t.Fatalf("failed to parse: %q", line)
|
||||||
|
}
|
||||||
|
|
||||||
|
formatted := ircserver.FormatMessage(
|
||||||
|
msg.Prefix, msg.Command, msg.Params...,
|
||||||
|
)
|
||||||
|
if formatted != line {
|
||||||
|
t.Errorf(
|
||||||
|
"round-trip failed: input %q, got %q",
|
||||||
|
line, formatted,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
319
internal/ircserver/relay.go
Normal file
319
internal/ircserver/relay.go
Normal file
@@ -0,0 +1,319 @@
|
|||||||
|
package ircserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/db"
|
||||||
|
"git.eeqj.de/sneak/neoirc/pkg/irc"
|
||||||
|
)
|
||||||
|
|
||||||
|
// relayMessages polls the client output queue and delivers
|
||||||
|
// IRC-formatted messages to the TCP connection. It runs
|
||||||
|
// in a goroutine for the lifetime of the connection.
|
||||||
|
func (c *Conn) relayMessages(ctx context.Context) {
|
||||||
|
// Use a ticker as a fallback; primary wakeup is via
|
||||||
|
// broker notification.
|
||||||
|
ticker := time.NewTicker(pollInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
// Drain any available messages.
|
||||||
|
delivered := c.drainQueue(ctx)
|
||||||
|
|
||||||
|
if delivered {
|
||||||
|
// Tight loop while there are messages.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for notification or timeout.
|
||||||
|
waitCh := c.brk.Wait(c.sessionID)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-waitCh:
|
||||||
|
// New message notification — loop back.
|
||||||
|
case <-ticker.C:
|
||||||
|
// Periodic check.
|
||||||
|
case <-ctx.Done():
|
||||||
|
c.brk.Remove(c.sessionID, waitCh)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const relayPollLimit = 100
|
||||||
|
|
||||||
|
// drainQueue polls the output queue and delivers all
|
||||||
|
// pending messages. Returns true if at least one message
|
||||||
|
// was delivered.
|
||||||
|
func (c *Conn) drainQueue(ctx context.Context) bool {
|
||||||
|
msgs, lastID, err := c.database.PollMessages(
|
||||||
|
ctx, c.clientID, c.lastQueueID, relayPollLimit,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(msgs) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range msgs {
|
||||||
|
c.deliverIRCMessage(ctx, &msgs[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
if lastID > c.lastQueueID {
|
||||||
|
c.lastQueueID = lastID
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverIRCMessage converts a db.IRCMessage to wire
|
||||||
|
// protocol and sends it.
|
||||||
|
//
|
||||||
|
//nolint:cyclop // dispatch table
|
||||||
|
func (c *Conn) deliverIRCMessage(
|
||||||
|
_ context.Context,
|
||||||
|
msg *db.IRCMessage,
|
||||||
|
) {
|
||||||
|
command := msg.Command
|
||||||
|
|
||||||
|
// Decode body as []string for the trailing text.
|
||||||
|
var bodyLines []string
|
||||||
|
|
||||||
|
if msg.Body != nil {
|
||||||
|
_ = json.Unmarshal(msg.Body, &bodyLines)
|
||||||
|
}
|
||||||
|
|
||||||
|
text := ""
|
||||||
|
if len(bodyLines) > 0 {
|
||||||
|
text = bodyLines[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Route by command type.
|
||||||
|
switch {
|
||||||
|
case isNumeric(command):
|
||||||
|
c.deliverNumeric(msg, text)
|
||||||
|
case command == irc.CmdPrivmsg || command == irc.CmdNotice:
|
||||||
|
c.deliverTextMessage(msg, command, text)
|
||||||
|
case command == irc.CmdJoin:
|
||||||
|
c.deliverJoin(msg)
|
||||||
|
case command == irc.CmdPart:
|
||||||
|
c.deliverPart(msg, text)
|
||||||
|
case command == irc.CmdNick:
|
||||||
|
c.deliverNickChange(msg, text)
|
||||||
|
case command == irc.CmdQuit:
|
||||||
|
c.deliverQuitMsg(msg, text)
|
||||||
|
case command == irc.CmdTopic:
|
||||||
|
c.deliverTopicChange(msg, text)
|
||||||
|
case command == irc.CmdKick:
|
||||||
|
c.deliverKickMsg(msg, text)
|
||||||
|
case command == "INVITE":
|
||||||
|
c.deliverInviteMsg(msg, text)
|
||||||
|
case command == irc.CmdMode:
|
||||||
|
c.deliverMode(msg, text)
|
||||||
|
case command == irc.CmdPing:
|
||||||
|
// Server-originated PING — reply with PONG.
|
||||||
|
c.sendFromServer("PING", c.serverSfx)
|
||||||
|
default:
|
||||||
|
// Unknown command — deliver as server notice.
|
||||||
|
if text != "" {
|
||||||
|
c.sendFromServer("NOTICE", c.nick, text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// isNumeric returns true if the command is a 3-digit
|
||||||
|
// numeric code.
|
||||||
|
func isNumeric(cmd string) bool {
|
||||||
|
return len(cmd) == 3 &&
|
||||||
|
cmd[0] >= '0' && cmd[0] <= '9' &&
|
||||||
|
cmd[1] >= '0' && cmd[1] <= '9' &&
|
||||||
|
cmd[2] >= '0' && cmd[2] <= '9'
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverNumeric sends a numeric reply.
|
||||||
|
func (c *Conn) deliverNumeric(
|
||||||
|
msg *db.IRCMessage,
|
||||||
|
text string,
|
||||||
|
) {
|
||||||
|
from := msg.From
|
||||||
|
if from == "" {
|
||||||
|
from = c.serverSfx
|
||||||
|
}
|
||||||
|
|
||||||
|
var params []string
|
||||||
|
|
||||||
|
if msg.Params != nil {
|
||||||
|
_ = json.Unmarshal(msg.Params, ¶ms)
|
||||||
|
}
|
||||||
|
|
||||||
|
allParams := make([]string, 0, 1+len(params)+1)
|
||||||
|
allParams = append(allParams, c.nick)
|
||||||
|
allParams = append(allParams, params...)
|
||||||
|
|
||||||
|
if text != "" {
|
||||||
|
allParams = append(allParams, text)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.send(FormatMessage(from, msg.Command, allParams...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverTextMessage sends PRIVMSG or NOTICE.
|
||||||
|
func (c *Conn) deliverTextMessage(
|
||||||
|
msg *db.IRCMessage,
|
||||||
|
command, text string,
|
||||||
|
) {
|
||||||
|
from := msg.From
|
||||||
|
target := msg.To
|
||||||
|
|
||||||
|
// Don't echo our own messages back.
|
||||||
|
if strings.EqualFold(from, c.nick) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
prefix := from
|
||||||
|
if !strings.Contains(prefix, "!") {
|
||||||
|
prefix = from + "!" + from + "@*"
|
||||||
|
}
|
||||||
|
|
||||||
|
c.send(FormatMessage(prefix, command, target, text))
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverJoin sends a JOIN notification.
|
||||||
|
func (c *Conn) deliverJoin(msg *db.IRCMessage) {
|
||||||
|
// Don't echo our own JOINs (we already sent them
|
||||||
|
// during joinChannel).
|
||||||
|
if strings.EqualFold(msg.From, c.nick) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
prefix := msg.From + "!" + msg.From + "@*"
|
||||||
|
channel := msg.To
|
||||||
|
|
||||||
|
c.send(FormatMessage(prefix, "JOIN", channel))
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverPart sends a PART notification.
|
||||||
|
func (c *Conn) deliverPart(msg *db.IRCMessage, text string) {
|
||||||
|
if strings.EqualFold(msg.From, c.nick) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
prefix := msg.From + "!" + msg.From + "@*"
|
||||||
|
channel := msg.To
|
||||||
|
|
||||||
|
if text != "" {
|
||||||
|
c.send(FormatMessage(
|
||||||
|
prefix, "PART", channel, text,
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
c.send(FormatMessage(prefix, "PART", channel))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverNickChange sends a NICK change notification.
|
||||||
|
func (c *Conn) deliverNickChange(
|
||||||
|
msg *db.IRCMessage,
|
||||||
|
newNick string,
|
||||||
|
) {
|
||||||
|
if strings.EqualFold(msg.From, c.nick) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
prefix := msg.From + "!" + msg.From + "@*"
|
||||||
|
|
||||||
|
c.send(FormatMessage(prefix, "NICK", newNick))
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverQuitMsg sends a QUIT notification.
|
||||||
|
func (c *Conn) deliverQuitMsg(
|
||||||
|
msg *db.IRCMessage,
|
||||||
|
text string,
|
||||||
|
) {
|
||||||
|
if strings.EqualFold(msg.From, c.nick) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
prefix := msg.From + "!" + msg.From + "@*"
|
||||||
|
|
||||||
|
if text != "" {
|
||||||
|
c.send(FormatMessage(
|
||||||
|
prefix, "QUIT", "Quit: "+text,
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
c.send(FormatMessage(prefix, "QUIT", "Quit"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverTopicChange sends a TOPIC change notification.
|
||||||
|
func (c *Conn) deliverTopicChange(
|
||||||
|
msg *db.IRCMessage,
|
||||||
|
text string,
|
||||||
|
) {
|
||||||
|
prefix := msg.From + "!" + msg.From + "@*"
|
||||||
|
channel := msg.To
|
||||||
|
|
||||||
|
c.send(FormatMessage(prefix, "TOPIC", channel, text))
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverKickMsg sends a KICK notification.
|
||||||
|
func (c *Conn) deliverKickMsg(
|
||||||
|
msg *db.IRCMessage,
|
||||||
|
text string,
|
||||||
|
) {
|
||||||
|
prefix := msg.From + "!" + msg.From + "@*"
|
||||||
|
channel := msg.To
|
||||||
|
|
||||||
|
var params []string
|
||||||
|
|
||||||
|
if msg.Params != nil {
|
||||||
|
_ = json.Unmarshal(msg.Params, ¶ms)
|
||||||
|
}
|
||||||
|
|
||||||
|
kickTarget := ""
|
||||||
|
if len(params) > 0 {
|
||||||
|
kickTarget = params[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
if kickTarget != "" {
|
||||||
|
c.send(FormatMessage(
|
||||||
|
prefix, "KICK", channel, kickTarget, text,
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
c.send(FormatMessage(
|
||||||
|
prefix, "KICK", channel, "?", text,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverInviteMsg sends an INVITE notification.
|
||||||
|
func (c *Conn) deliverInviteMsg(
|
||||||
|
_ *db.IRCMessage,
|
||||||
|
text string,
|
||||||
|
) {
|
||||||
|
c.sendFromServer("NOTICE", c.nick, text)
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverMode sends a MODE change notification.
|
||||||
|
func (c *Conn) deliverMode(
|
||||||
|
msg *db.IRCMessage,
|
||||||
|
text string,
|
||||||
|
) {
|
||||||
|
prefix := msg.From + "!" + msg.From + "@*"
|
||||||
|
target := msg.To
|
||||||
|
|
||||||
|
if text != "" {
|
||||||
|
c.send(FormatMessage(prefix, "MODE", target, text))
|
||||||
|
}
|
||||||
|
}
|
||||||
153
internal/ircserver/server.go
Normal file
153
internal/ircserver/server.go
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
package ircserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/broker"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/config"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/db"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/logger"
|
||||||
|
"go.uber.org/fx"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Params defines the dependencies for creating an IRC
|
||||||
|
// Server.
|
||||||
|
type Params struct {
|
||||||
|
fx.In
|
||||||
|
|
||||||
|
Logger *logger.Logger
|
||||||
|
Config *config.Config
|
||||||
|
Database *db.Database
|
||||||
|
Broker *broker.Broker
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server is the TCP IRC protocol server.
|
||||||
|
type Server struct {
|
||||||
|
log *slog.Logger
|
||||||
|
cfg *config.Config
|
||||||
|
database *db.Database
|
||||||
|
brk *broker.Broker
|
||||||
|
listener net.Listener
|
||||||
|
mu sync.Mutex
|
||||||
|
conns map[*Conn]struct{}
|
||||||
|
cancel context.CancelFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new IRC Server and registers its lifecycle
|
||||||
|
// hooks. The listener is only started if IRC_LISTEN_ADDR
|
||||||
|
// is configured; otherwise the server is inert.
|
||||||
|
func New(
|
||||||
|
lifecycle fx.Lifecycle,
|
||||||
|
params Params,
|
||||||
|
) *Server {
|
||||||
|
srv := &Server{
|
||||||
|
log: params.Logger.Get(),
|
||||||
|
cfg: params.Config,
|
||||||
|
database: params.Database,
|
||||||
|
brk: params.Broker,
|
||||||
|
conns: make(map[*Conn]struct{}),
|
||||||
|
listener: nil,
|
||||||
|
cancel: nil,
|
||||||
|
mu: sync.Mutex{},
|
||||||
|
}
|
||||||
|
|
||||||
|
listenAddr := params.Config.IRCListenAddr
|
||||||
|
if listenAddr == "" {
|
||||||
|
return srv
|
||||||
|
}
|
||||||
|
|
||||||
|
lifecycle.Append(fx.Hook{
|
||||||
|
OnStart: func(ctx context.Context) error {
|
||||||
|
return srv.start(ctx, listenAddr)
|
||||||
|
},
|
||||||
|
OnStop: func(_ context.Context) error {
|
||||||
|
srv.stop()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
return srv
|
||||||
|
}
|
||||||
|
|
||||||
|
// start begins listening for TCP connections.
|
||||||
|
//
|
||||||
|
//nolint:contextcheck // long-lived server ctx, not the short Fx one
|
||||||
|
func (s *Server) start(_ context.Context, addr string) error {
|
||||||
|
ln, err := net.Listen("tcp", addr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("irc listen: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.listener = ln
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
s.cancel = cancel
|
||||||
|
|
||||||
|
s.log.Info(
|
||||||
|
"irc server listening", "addr", addr,
|
||||||
|
)
|
||||||
|
|
||||||
|
go s.acceptLoop(ctx)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// stop shuts down the listener and all connections.
|
||||||
|
func (s *Server) stop() {
|
||||||
|
if s.cancel != nil {
|
||||||
|
s.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.listener != nil {
|
||||||
|
s.listener.Close() //nolint:errcheck,gosec
|
||||||
|
}
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
|
for c := range s.conns {
|
||||||
|
c.conn.Close() //nolint:errcheck,gosec
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// acceptLoop accepts new connections.
|
||||||
|
func (s *Server) acceptLoop(ctx context.Context) {
|
||||||
|
for {
|
||||||
|
tcpConn, err := s.listener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
s.log.Error(
|
||||||
|
"irc accept error", "error", err,
|
||||||
|
)
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
client := newConn(
|
||||||
|
ctx, tcpConn, s.log,
|
||||||
|
s.database, s.brk, s.cfg,
|
||||||
|
)
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
|
s.conns[client] = struct{}{}
|
||||||
|
s.mu.Unlock()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
s.mu.Lock()
|
||||||
|
delete(s.conns, client)
|
||||||
|
s.mu.Unlock()
|
||||||
|
}()
|
||||||
|
|
||||||
|
client.serve(ctx)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
625
internal/ircserver/server_test.go
Normal file
625
internal/ircserver/server_test.go
Normal file
@@ -0,0 +1,625 @@
|
|||||||
|
package ircserver_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/broker"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/config"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/db"
|
||||||
|
"git.eeqj.de/sneak/neoirc/internal/ircserver"
|
||||||
|
|
||||||
|
_ "modernc.org/sqlite"
|
||||||
|
)
|
||||||
|
|
||||||
|
const testTimeout = 5 * time.Second
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
db.SetBcryptCost(4)
|
||||||
|
|
||||||
|
os.Exit(m.Run())
|
||||||
|
}
|
||||||
|
|
||||||
|
// testEnv holds the shared test infrastructure.
|
||||||
|
type testEnv struct {
|
||||||
|
database *db.Database
|
||||||
|
brk *broker.Broker
|
||||||
|
cfg *config.Config
|
||||||
|
srv *ircserver.Server
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTestEnv(t *testing.T) *testEnv {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
dsn := fmt.Sprintf(
|
||||||
|
"file:%s?mode=memory&cache=shared&_journal_mode=WAL",
|
||||||
|
t.Name(),
|
||||||
|
)
|
||||||
|
|
||||||
|
conn, err := sql.Open("sqlite", dsn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open db: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.SetMaxOpenConns(1)
|
||||||
|
|
||||||
|
_, err = conn.ExecContext(
|
||||||
|
t.Context(), "PRAGMA foreign_keys = ON",
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("pragma: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
database := db.NewTestDatabaseFromConn(conn)
|
||||||
|
|
||||||
|
err = database.RunMigrations(t.Context())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("migrate: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
brk := broker.New()
|
||||||
|
|
||||||
|
cfg := &config.Config{ //nolint:exhaustruct
|
||||||
|
ServerName: "test.irc",
|
||||||
|
MOTD: "Welcome to test IRC",
|
||||||
|
}
|
||||||
|
|
||||||
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("listen: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
addr := listener.Addr().String()
|
||||||
|
|
||||||
|
err = listener.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("close listener: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log := slog.New(slog.NewTextHandler(
|
||||||
|
os.Stderr,
|
||||||
|
&slog.HandlerOptions{Level: slog.LevelError}, //nolint:exhaustruct
|
||||||
|
))
|
||||||
|
|
||||||
|
srv := ircserver.NewTestServer(log, cfg, database, brk)
|
||||||
|
|
||||||
|
err = srv.Start(addr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("start irc server: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Cleanup(func() {
|
||||||
|
srv.Stop()
|
||||||
|
|
||||||
|
err := conn.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Logf("close db: %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return &testEnv{
|
||||||
|
database: database,
|
||||||
|
brk: brk,
|
||||||
|
cfg: cfg,
|
||||||
|
srv: srv,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// dial connects to the test server.
|
||||||
|
func (env *testEnv) dial(t *testing.T) *testClient {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
conn, err := net.DialTimeout(
|
||||||
|
"tcp",
|
||||||
|
env.srv.Listener().Addr().String(),
|
||||||
|
testTimeout,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("dial: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Cleanup(func() {
|
||||||
|
err := conn.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Logf("close conn: %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return &testClient{
|
||||||
|
t: t,
|
||||||
|
conn: conn,
|
||||||
|
scanner: bufio.NewScanner(conn),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// testClient wraps a raw TCP connection with helpers.
|
||||||
|
type testClient struct {
|
||||||
|
t *testing.T
|
||||||
|
conn net.Conn
|
||||||
|
scanner *bufio.Scanner
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tc *testClient) send(line string) {
|
||||||
|
tc.t.Helper()
|
||||||
|
|
||||||
|
_ = tc.conn.SetWriteDeadline(
|
||||||
|
time.Now().Add(testTimeout),
|
||||||
|
)
|
||||||
|
|
||||||
|
_, err := fmt.Fprintf(tc.conn, "%s\r\n", line)
|
||||||
|
if err != nil {
|
||||||
|
tc.t.Fatalf("send: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tc *testClient) readLine() string {
|
||||||
|
tc.t.Helper()
|
||||||
|
|
||||||
|
_ = tc.conn.SetReadDeadline(
|
||||||
|
time.Now().Add(testTimeout),
|
||||||
|
)
|
||||||
|
|
||||||
|
if !tc.scanner.Scan() {
|
||||||
|
err := tc.scanner.Err()
|
||||||
|
if err != nil {
|
||||||
|
tc.t.Fatalf("read: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tc.t.Fatal("connection closed unexpectedly")
|
||||||
|
}
|
||||||
|
|
||||||
|
return tc.scanner.Text()
|
||||||
|
}
|
||||||
|
|
||||||
|
// readUntil reads lines until one matches the predicate.
|
||||||
|
func (tc *testClient) readUntil(
|
||||||
|
pred func(string) bool,
|
||||||
|
) []string {
|
||||||
|
tc.t.Helper()
|
||||||
|
|
||||||
|
var lines []string
|
||||||
|
|
||||||
|
for {
|
||||||
|
line := tc.readLine()
|
||||||
|
lines = append(lines, line)
|
||||||
|
|
||||||
|
if pred(line) {
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// register sends NICK + USER and reads through the welcome
|
||||||
|
// burst.
|
||||||
|
func (tc *testClient) register(nick string) []string {
|
||||||
|
tc.t.Helper()
|
||||||
|
|
||||||
|
tc.send("NICK " + nick)
|
||||||
|
tc.send("USER " + nick + " 0 * :Test User")
|
||||||
|
|
||||||
|
return tc.readUntil(func(line string) bool {
|
||||||
|
return strings.Contains(line, " 376 ") ||
|
||||||
|
strings.Contains(line, " 422 ")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// assertContains checks that at least one line matches the
|
||||||
|
// given substring.
|
||||||
|
func assertContains(
|
||||||
|
t *testing.T,
|
||||||
|
lines []string,
|
||||||
|
substr, description string,
|
||||||
|
) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
if strings.Contains(line, substr) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Errorf("did not find %q in output: %s", substr, description)
|
||||||
|
}
|
||||||
|
|
||||||
|
// joinAndDrain joins a channel and reads until
|
||||||
|
// RPL_ENDOFNAMES.
|
||||||
|
func (tc *testClient) joinAndDrain(channel string) {
|
||||||
|
tc.t.Helper()
|
||||||
|
|
||||||
|
tc.send("JOIN " + channel)
|
||||||
|
|
||||||
|
tc.readUntil(func(line string) bool {
|
||||||
|
return strings.Contains(line, " 366 ")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendAndExpect sends a command and reads until a line
|
||||||
|
// containing the expected substring is found.
|
||||||
|
func (tc *testClient) sendAndExpect(
|
||||||
|
cmd, expect string,
|
||||||
|
) []string {
|
||||||
|
tc.t.Helper()
|
||||||
|
|
||||||
|
tc.send(cmd)
|
||||||
|
|
||||||
|
return tc.readUntil(func(line string) bool {
|
||||||
|
return strings.Contains(line, expect)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRegistration(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
lines := client.register("alice")
|
||||||
|
assertContains(t, lines, " 001 ", "RPL_WELCOME")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWelcomeContainsNick(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
lines := client.register("bob")
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
if strings.Contains(line, " 001 ") &&
|
||||||
|
!strings.Contains(line, "bob") {
|
||||||
|
t.Errorf("001 should contain nick: %s", line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPingPong(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("pingtest")
|
||||||
|
lines := client.sendAndExpect("PING :hello", "PONG")
|
||||||
|
assertContains(t, lines, "PONG", "PONG response")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJoinChannel(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("joiner")
|
||||||
|
client.send("JOIN #test")
|
||||||
|
|
||||||
|
lines := client.readUntil(func(line string) bool {
|
||||||
|
return strings.Contains(line, " 366 ")
|
||||||
|
})
|
||||||
|
|
||||||
|
assertContains(t, lines, "JOIN", "JOIN echo")
|
||||||
|
assertContains(t, lines, " 366 ", "RPL_ENDOFNAMES")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrivmsgBetweenClients(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
|
||||||
|
alice := env.dial(t)
|
||||||
|
alice.register("alice_pm")
|
||||||
|
|
||||||
|
bob := env.dial(t)
|
||||||
|
bob.register("bob_pm")
|
||||||
|
|
||||||
|
alice.joinAndDrain("#chat")
|
||||||
|
bob.joinAndDrain("#chat")
|
||||||
|
|
||||||
|
alice.send("PRIVMSG #chat :hello bob!")
|
||||||
|
lines := bob.sendAndExpect("PING :sync", "hello bob!")
|
||||||
|
assertContains(t, lines, "hello bob!", "channel PRIVMSG")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNickChange(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("oldnick")
|
||||||
|
lines := client.sendAndExpect("NICK newnick", "newnick")
|
||||||
|
assertContains(t, lines, "NICK", "NICK change echo")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDuplicateNick(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
|
||||||
|
first := env.dial(t)
|
||||||
|
first.register("taken")
|
||||||
|
|
||||||
|
second := env.dial(t)
|
||||||
|
second.send("NICK taken")
|
||||||
|
second.send("USER taken 0 * :Test")
|
||||||
|
|
||||||
|
lines := second.readUntil(func(line string) bool {
|
||||||
|
return strings.Contains(line, " 433 ")
|
||||||
|
})
|
||||||
|
|
||||||
|
assertContains(t, lines, " 433 ", "ERR_NICKNAMEINUSE")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListChannels(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("lister")
|
||||||
|
client.joinAndDrain("#listtest")
|
||||||
|
lines := client.sendAndExpect("LIST", " 323 ")
|
||||||
|
assertContains(t, lines, " 323 ", "RPL_LISTEND") //nolint:misspell // IRC term
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWhois(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("whoistest")
|
||||||
|
lines := client.sendAndExpect(
|
||||||
|
"WHOIS whoistest", " 318 ",
|
||||||
|
)
|
||||||
|
|
||||||
|
assertContains(t, lines, " 311 ", "RPL_WHOISUSER")
|
||||||
|
assertContains(t, lines, " 318 ", "RPL_ENDOFWHOIS")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuit(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("quitter")
|
||||||
|
lines := client.sendAndExpect(
|
||||||
|
"QUIT :goodbye", "ERROR",
|
||||||
|
)
|
||||||
|
|
||||||
|
assertContains(t, lines, "goodbye", "QUIT reason")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTopicSetAndGet(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("topicuser")
|
||||||
|
client.joinAndDrain("#topictest")
|
||||||
|
lines := client.sendAndExpect(
|
||||||
|
"TOPIC #topictest :New topic here",
|
||||||
|
"New topic here",
|
||||||
|
)
|
||||||
|
|
||||||
|
assertContains(
|
||||||
|
t, lines, "New topic here", "TOPIC echo",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnknownCommand(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("unknowncmd")
|
||||||
|
lines := client.sendAndExpect("FOOBAR", " 421 ")
|
||||||
|
assertContains(t, lines, " 421 ", "ERR_UNKNOWNCOMMAND")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDirectMessage(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
|
||||||
|
sender := env.dial(t)
|
||||||
|
sender.register("dmsender")
|
||||||
|
|
||||||
|
receiver := env.dial(t)
|
||||||
|
receiver.register("dmreceiver")
|
||||||
|
|
||||||
|
// Give relay goroutines time to start.
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
|
sender.send("PRIVMSG dmreceiver :hello privately")
|
||||||
|
|
||||||
|
lines := receiver.readUntil(func(line string) bool {
|
||||||
|
return strings.Contains(line, "hello privately")
|
||||||
|
})
|
||||||
|
|
||||||
|
assertContains(
|
||||||
|
t, lines, "hello privately", "direct PRIVMSG",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCAPNegotiation(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.send("CAP LS 302")
|
||||||
|
|
||||||
|
line := client.readLine()
|
||||||
|
if !strings.Contains(line, "CAP") {
|
||||||
|
t.Errorf("expected CAP response, got: %s", line)
|
||||||
|
}
|
||||||
|
|
||||||
|
client.send("CAP END")
|
||||||
|
lines := client.register("capuser")
|
||||||
|
assertContains(t, lines, " 001 ", "registration after CAP")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPartChannel(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("parter")
|
||||||
|
client.joinAndDrain("#parttest")
|
||||||
|
lines := client.sendAndExpect(
|
||||||
|
"PART #parttest :leaving", "PART",
|
||||||
|
)
|
||||||
|
|
||||||
|
assertContains(t, lines, "#parttest", "PART echo")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestModeQuery(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("modeuser")
|
||||||
|
client.joinAndDrain("#modetest")
|
||||||
|
lines := client.sendAndExpect(
|
||||||
|
"MODE #modetest", " 324 ",
|
||||||
|
)
|
||||||
|
|
||||||
|
assertContains(
|
||||||
|
t, lines, " 324 ", "RPL_CHANNELMODEIS",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWhoChannel(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("whouser")
|
||||||
|
client.joinAndDrain("#whotest")
|
||||||
|
lines := client.sendAndExpect("WHO #whotest", " 315 ")
|
||||||
|
assertContains(t, lines, " 352 ", "RPL_WHOREPLY")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLusers(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("luseruser")
|
||||||
|
lines := client.sendAndExpect("LUSERS", " 255 ")
|
||||||
|
assertContains(t, lines, " 251 ", "RPL_LUSERCLIENT")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMotd(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("motduser")
|
||||||
|
lines := client.sendAndExpect("MOTD", " 376 ")
|
||||||
|
assertContains(t, lines, " 376 ", "RPL_ENDOFMOTD")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAwaySetAndClear(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("awayuser")
|
||||||
|
|
||||||
|
setLines := client.sendAndExpect(
|
||||||
|
"AWAY :brb lunch", " 306 ",
|
||||||
|
)
|
||||||
|
assertContains(t, setLines, " 306 ", "RPL_NOWAWAY")
|
||||||
|
|
||||||
|
clearLines := client.sendAndExpect("AWAY", " 305 ")
|
||||||
|
assertContains(t, clearLines, " 305 ", "RPL_UNAWAY")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandlePassPostRegistration(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("passuser")
|
||||||
|
lines := client.sendAndExpect(
|
||||||
|
"PASS :mypassword123", "Password set",
|
||||||
|
)
|
||||||
|
|
||||||
|
assertContains(
|
||||||
|
t, lines, "Password set", "password confirmation",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPreRegistrationNotRegistered(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.send("PRIVMSG #test :hello")
|
||||||
|
|
||||||
|
line := client.readLine()
|
||||||
|
if !strings.Contains(line, " 451 ") {
|
||||||
|
t.Errorf(
|
||||||
|
"expected ERR_NOTREGISTERED (451), got: %s",
|
||||||
|
line,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNamesNonExistentChannel(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newTestEnv(t)
|
||||||
|
client := env.dial(t)
|
||||||
|
|
||||||
|
client.register("namesuser")
|
||||||
|
lines := client.sendAndExpect(
|
||||||
|
"NAMES #doesnotexist", " 366 ",
|
||||||
|
)
|
||||||
|
|
||||||
|
assertContains(
|
||||||
|
t, lines, " 366 ",
|
||||||
|
"RPL_ENDOFNAMES for non-existent channel",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkParseMessage(b *testing.B) {
|
||||||
|
line := ":nick!user@host PRIVMSG #channel :Hello, world!"
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
for range b.N {
|
||||||
|
_ = ircserver.ParseMessage(line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkFormatMessage(b *testing.B) {
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
for range b.N {
|
||||||
|
_ = ircserver.FormatMessage(
|
||||||
|
"nick!user@host", "PRIVMSG",
|
||||||
|
"#channel", "Hello, world!",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,6 +21,7 @@ const (
|
|||||||
CmdPrivmsg = "PRIVMSG"
|
CmdPrivmsg = "PRIVMSG"
|
||||||
CmdQuit = "QUIT"
|
CmdQuit = "QUIT"
|
||||||
CmdTopic = "TOPIC"
|
CmdTopic = "TOPIC"
|
||||||
|
CmdUser = "USER"
|
||||||
CmdWho = "WHO"
|
CmdWho = "WHO"
|
||||||
CmdWhois = "WHOIS"
|
CmdWhois = "WHOIS"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user