3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-23 19:22:41 +01:00
ergo/irc/modes.go

317 lines
9.0 KiB
Go
Raw Normal View History

// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2014-2015 Edmund Huber
2017-03-12 23:08:18 +01:00
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
"strconv"
"strings"
"github.com/oragono/oragono/irc/modes"
2017-06-14 20:00:53 +02:00
"github.com/oragono/oragono/irc/sno"
)
var (
// DefaultChannelModes are enabled on brand new channels when they're created.
// this can be overridden in the `channels` config, with the `default-modes` key
DefaultChannelModes = modes.Modes{
modes.NoOutside, modes.OpOnlyTopic,
}
)
2018-04-23 00:47:10 +02:00
// ApplyUserModeChanges applies the given changes, and returns the applied changes.
func ApplyUserModeChanges(client *Client, changes modes.ModeChanges, force bool) modes.ModeChanges {
applied := make(modes.ModeChanges, 0)
2017-03-12 23:08:18 +01:00
for _, change := range changes {
switch change.Mode {
case modes.Bot, modes.Invisible, modes.WallOps, modes.UserRoleplaying, modes.Operator, modes.LocalOperator, modes.RegisteredOnly:
switch change.Op {
case modes.Add:
if !force && (change.Mode == modes.Operator || change.Mode == modes.LocalOperator) {
2017-05-08 01:15:16 +02:00
continue
}
2018-04-24 02:03:26 +02:00
if client.SetMode(change.Mode, true) {
2018-04-23 00:47:10 +02:00
if change.Mode == modes.Invisible {
client.server.stats.ChangeInvisible(1)
} else if change.Mode == modes.Operator || change.Mode == modes.LocalOperator {
client.server.stats.ChangeOperators(1)
}
applied = append(applied, change)
}
case modes.Remove:
2018-04-24 02:03:26 +02:00
if client.SetMode(change.Mode, false) {
2018-04-23 00:47:10 +02:00
if change.Mode == modes.Invisible {
client.server.stats.ChangeInvisible(-1)
} else if change.Mode == modes.Operator || change.Mode == modes.LocalOperator {
client.server.stats.ChangeOperators(-1)
}
applied = append(applied, change)
}
2017-03-12 23:08:18 +01:00
}
case modes.ServerNotice:
2018-04-23 00:47:10 +02:00
if !client.HasMode(modes.Operator) {
2017-05-08 01:15:16 +02:00
continue
}
var masks []sno.Mask
if change.Op == modes.Add || change.Op == modes.Remove {
2018-04-16 05:20:37 +02:00
var newArg string
for _, char := range change.Arg {
2018-04-16 05:20:37 +02:00
mask := sno.Mask(char)
if sno.ValidMasks[mask] {
masks = append(masks, mask)
newArg += string(char)
}
2017-03-12 23:08:18 +01:00
}
2018-04-16 05:20:37 +02:00
change.Arg = newArg
2017-05-08 01:15:16 +02:00
}
if change.Op == modes.Add {
2017-05-08 01:15:16 +02:00
client.server.snomasks.AddMasks(client, masks...)
applied = append(applied, change)
} else if change.Op == modes.Remove {
2017-05-08 01:15:16 +02:00
client.server.snomasks.RemoveMasks(client, masks...)
2017-03-12 23:08:18 +01:00
applied = append(applied, change)
}
}
// can't do anything to TLS mode
}
// return the changes we could actually apply
return applied
}
// ParseDefaultChannelModes parses the `default-modes` line of the config
func ParseDefaultChannelModes(rawModes *string) modes.Modes {
if rawModes == nil {
// not present in config, fall back to compile-time default
return DefaultChannelModes
}
modeChangeStrings := strings.Fields(*rawModes)
2018-04-23 00:47:10 +02:00
modeChanges, _ := modes.ParseChannelModeChanges(modeChangeStrings...)
defaultChannelModes := make(modes.Modes, 0)
for _, modeChange := range modeChanges {
if modeChange.Op == modes.Add {
defaultChannelModes = append(defaultChannelModes, modeChange.Mode)
}
}
return defaultChannelModes
}
2017-03-24 03:23:21 +01:00
// ApplyChannelModeChanges applies a given set of mode changes.
2018-02-05 15:21:08 +01:00
func (channel *Channel) ApplyChannelModeChanges(client *Client, isSamode bool, changes modes.ModeChanges, rb *ResponseBuffer) modes.ModeChanges {
2017-03-24 03:23:21 +01:00
// so we only output one warning for each list type when full
listFullWarned := make(map[modes.Mode]bool)
2017-03-24 03:23:21 +01:00
var alreadySentPrivError bool
applied := make(modes.ModeChanges, 0)
2017-03-24 03:23:21 +01:00
isListOp := func(change modes.ModeChange) bool {
return (change.Op == modes.List) || (change.Arg == "")
2017-11-13 08:55:26 +01:00
}
hasPrivs := func(change modes.ModeChange) bool {
2017-11-13 08:55:26 +01:00
if isSamode {
return true
}
switch change.Mode {
case modes.ChannelFounder, modes.ChannelAdmin, modes.ChannelOperator, modes.Halfop, modes.Voice:
2018-04-23 00:47:10 +02:00
// List on these modes is a no-op anyway
if change.Op == modes.List {
2017-11-13 08:55:26 +01:00
return true
}
cfarg, _ := CasefoldName(change.Arg)
2018-04-23 00:47:10 +02:00
isSelfChange := cfarg == client.NickCasefolded()
if change.Op == modes.Remove && isSelfChange {
2017-11-13 08:55:26 +01:00
// "There is no restriction, however, on anyone `deopping' themselves"
// <https://tools.ietf.org/html/rfc2812#section-3.1.5>
return true
}
return channelUserModeHasPrivsOver(channel.HighestUserMode(client), change.Mode)
case modes.BanMask:
2017-11-13 08:55:26 +01:00
// #163: allow unprivileged users to list ban masks
return isListOp(change) || channel.ClientIsAtLeast(client, modes.ChannelOperator)
2017-11-13 08:55:26 +01:00
default:
return channel.ClientIsAtLeast(client, modes.ChannelOperator)
2017-11-13 08:55:26 +01:00
}
}
2017-03-24 03:23:21 +01:00
for _, change := range changes {
2017-11-13 08:55:26 +01:00
if !hasPrivs(change) {
2017-03-24 03:23:21 +01:00
if !alreadySentPrivError {
alreadySentPrivError = true
2019-03-19 08:35:49 +01:00
rb.Add(nil, client.server.name, ERR_CHANOPRIVSNEEDED, client.Nick(), channel.name, client.t("You're not a channel operator"))
}
2017-03-24 03:23:21 +01:00
continue
}
switch change.Mode {
case modes.BanMask, modes.ExceptMask, modes.InviteMask:
2017-11-13 08:55:26 +01:00
if isListOp(change) {
2018-02-05 15:21:08 +01:00
channel.ShowMaskList(client, change.Mode, rb)
2017-03-24 03:23:21 +01:00
continue
}
// confirm mask looks valid
mask, err := Casefold(change.Arg)
2017-03-24 03:23:21 +01:00
if err != nil {
continue
}
2016-06-22 13:35:26 +02:00
switch change.Op {
case modes.Add:
if channel.lists[change.Mode].Length() >= client.server.Limits().ChanListModes {
if !listFullWarned[change.Mode] {
2018-02-05 15:21:08 +01:00
rb.Add(nil, client.server.name, ERR_BANLISTFULL, client.Nick(), channel.Name(), change.Mode.String(), client.t("Channel list is full"))
listFullWarned[change.Mode] = true
2017-03-24 03:23:21 +01:00
}
continue
}
channel.lists[change.Mode].Add(mask)
2017-03-24 03:23:21 +01:00
applied = append(applied, change)
2016-10-23 16:50:18 +02:00
case modes.Remove:
channel.lists[change.Mode].Remove(mask)
2017-03-24 03:23:21 +01:00
applied = append(applied, change)
}
2016-06-22 13:35:26 +02:00
case modes.UserLimit:
switch change.Op {
case modes.Add:
2018-12-28 19:45:55 +01:00
val, err := strconv.Atoi(change.Arg)
2017-03-24 03:23:21 +01:00
if err == nil {
2017-10-23 01:50:16 +02:00
channel.setUserLimit(val)
2016-06-22 13:35:26 +02:00
applied = append(applied, change)
}
case modes.Remove:
2017-10-23 01:50:16 +02:00
channel.setUserLimit(0)
2017-03-24 03:23:21 +01:00
applied = append(applied, change)
}
2016-09-14 11:48:47 +02:00
case modes.Key:
switch change.Op {
case modes.Add:
channel.setKey(change.Arg)
2019-02-13 21:09:23 +01:00
applied = append(applied, change)
case modes.Remove:
2017-10-23 01:50:16 +02:00
channel.setKey("")
2019-02-13 21:09:23 +01:00
applied = append(applied, change)
2017-03-24 03:23:21 +01:00
}
2016-09-14 11:48:47 +02:00
case modes.InviteOnly, modes.Moderated, modes.NoOutside, modes.OpOnlyTopic, modes.RegisteredOnly, modes.Secret, modes.ChanRoleplaying:
if change.Op == modes.List {
2017-10-23 01:50:16 +02:00
continue
}
2016-09-14 11:48:47 +02:00
2018-04-24 02:03:26 +02:00
if channel.flags.SetMode(change.Mode, change.Op == modes.Add) {
2017-03-24 03:23:21 +01:00
applied = append(applied, change)
}
2016-06-22 13:35:26 +02:00
case modes.ChannelFounder, modes.ChannelAdmin, modes.ChannelOperator, modes.Halfop, modes.Voice:
if change.Op == modes.List {
2017-10-23 01:50:16 +02:00
continue
}
2016-06-22 13:35:26 +02:00
2018-04-23 00:47:10 +02:00
nick := change.Arg
if nick == "" {
2019-03-19 08:35:49 +01:00
rb.Add(nil, client.server.name, ERR_NEEDMOREPARAMS, client.Nick(), "MODE", client.t("Not enough parameters"))
2018-04-23 00:47:10 +02:00
return nil
}
change := channel.applyModeToMember(client, change.Mode, change.Op, nick, rb)
2017-03-24 03:23:21 +01:00
if change != nil {
applied = append(applied, *change)
2016-06-22 13:35:26 +02:00
}
}
}
2017-03-24 03:23:21 +01:00
return applied
}
2018-05-25 06:38:20 +02:00
// tests whether l > r, in the channel-user mode ordering (e.g., Halfop > Voice)
func umodeGreaterThan(l modes.Mode, r modes.Mode) bool {
for _, mode := range modes.ChannelUserModes {
if l == mode && r != mode {
return true
} else if r == mode {
return false
}
}
return false
}
// ProcessAccountToUmodeChange processes Add/Remove/List operations for channel persistent usermodes.
func (channel *Channel) ProcessAccountToUmodeChange(client *Client, change modes.ModeChange) (results []modes.ModeChange, err error) {
2019-04-07 08:12:14 +02:00
changed := false
defer func() {
if changed {
channel.MarkDirty(IncludeLists)
}
}()
2018-05-25 06:38:20 +02:00
account := client.Account()
isOperChange := client.HasRoleCapabs("chanreg")
channel.stateMutex.Lock()
defer channel.stateMutex.Unlock()
clientMode := channel.accountToUMode[account]
targetModeNow := channel.accountToUMode[change.Arg]
var targetModeAfter modes.Mode
if change.Op == modes.Add {
targetModeAfter = change.Mode
}
// operators and founders can do anything
hasPrivs := isOperChange || (account != "" && account == channel.registeredFounder)
// halfop and up can list, and do add/removes at levels <= their own
if change.Op == modes.List && (clientMode == modes.Halfop || umodeGreaterThan(clientMode, modes.Halfop)) {
2018-05-25 06:38:20 +02:00
hasPrivs = true
} else if channelUserModeHasPrivsOver(clientMode, targetModeNow) && channelUserModeHasPrivsOver(clientMode, targetModeAfter) {
2018-05-25 06:38:20 +02:00
hasPrivs = true
}
if !hasPrivs {
return nil, errInsufficientPrivs
}
switch change.Op {
case modes.Add:
if targetModeNow != targetModeAfter {
channel.accountToUMode[change.Arg] = change.Mode
2019-04-07 08:12:14 +02:00
changed = true
2018-05-25 06:38:20 +02:00
return []modes.ModeChange{change}, nil
}
return nil, nil
case modes.Remove:
if targetModeNow == change.Mode {
delete(channel.accountToUMode, change.Arg)
2019-04-07 08:12:14 +02:00
changed = true
2018-05-25 06:38:20 +02:00
return []modes.ModeChange{change}, nil
}
return nil, nil
case modes.List:
result := make([]modes.ModeChange, len(channel.accountToUMode))
pos := 0
for account, mode := range channel.accountToUMode {
result[pos] = modes.ModeChange{
Mode: mode,
Arg: account,
Op: modes.Add,
}
pos++
}
return result, nil
default:
// shouldn't happen
return nil, errInvalidCharacter
}
}