feat: implement Tier 2 channel modes (+b/+i/+s/+k/+l) (#92)
Some checks failed
check / check (push) Failing after 1m31s
Some checks failed
check / check (push) Failing after 1m31s
## Summary Implements the second tier of IRC channel features as described in [#86](#86). ## Features ### 1. Ban System (+b) - `channel_bans` table with mask, set_by, created_at - Add/remove/list bans via MODE +b/-b - Wildcard matching (`*!*@*.example.com`, `badnick!*@*`, etc.) - Ban enforcement on both JOIN and PRIVMSG - RPL_BANLIST (367) / RPL_ENDOFBANLIST (368) for ban listing ### 2. Invite-Only (+i) - `is_invite_only` column on channels table - INVITE command: operators can invite users - `channel_invites` table tracks pending invites - Invites consumed on successful JOIN - ERR_INVITEONLYCHAN (473) for uninvited JOIN attempts ### 3. Secret (+s) - `is_secret` column on channels table - Secret channels hidden from LIST for non-members - Secret channels hidden from WHOIS channel list for non-members ### 4. Channel Key (+k) - `channel_key` column on channels table - MODE +k sets key, MODE -k clears it - Key required on JOIN (`JOIN #channel key`) - ERR_BADCHANNELKEY (475) for wrong/missing key ### 5. User Limit (+l) - `user_limit` column on channels table (0 = no limit) - MODE +l sets limit, MODE -l removes it - ERR_CHANNELISFULL (471) when limit reached ## ISUPPORT Changes - CHANMODES updated to `b,k,Hl,imnst` - RPL_MYINFO modes updated to `ikmnostl` ## Tests ### Database-level tests: - Wildcard matching (10 patterns) - Ban CRUD operations - Session ban checking - Invite-only flag toggle - Invite CRUD + clearing - Secret channel filtering (LIST and WHOIS) - Channel key set/get/clear - User limit set/get/clear ### Handler-level tests: - Ban add/remove/list via MODE - Ban blocks JOIN - Ban blocks PRIVMSG - Invite-only JOIN rejection + INVITE acceptance - Secret channel hidden from LIST - Channel key required on JOIN - User limit enforcement - Mode string includes new modes - ISUPPORT updated CHANMODES - Non-operators cannot set any Tier 2 modes ## Schema Changes - Added `is_invite_only`, `is_secret`, `channel_key`, `user_limit` to `channels` table - Added `channel_bans` table - Added `channel_invites` table - All changes in `001_initial.sql` (pre-1.0.0 repo) closes #86 Co-authored-by: user <user@Mac.lan guest wan> Reviewed-on: #92 Co-authored-by: clawbot <clawbot@noreply.example.org> Co-committed-by: clawbot <clawbot@noreply.example.org>
This commit was merged in pull request #92.
This commit is contained in:
@@ -1017,3 +1017,474 @@ func TestGetOperCount(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user