2016-06-15 13:50:56 +02:00
|
|
|
// 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>
|
2016-06-15 13:50:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2014-03-13 21:18:40 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2016-06-26 13:06:28 +02:00
|
|
|
"strconv"
|
2014-03-13 21:18:40 +01:00
|
|
|
"strings"
|
2016-06-20 14:53:45 +02:00
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
"github.com/oragono/oragono/irc/modes"
|
2017-06-14 20:00:53 +02:00
|
|
|
"github.com/oragono/oragono/irc/sno"
|
2014-03-13 21:18:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-06-19 22:53:16 +02:00
|
|
|
// DefaultChannelModes are enabled on brand new channels when they're created.
|
2017-09-06 23:34:38 +02:00
|
|
|
// this can be overridden in the `channels` config, with the `default-modes` key
|
2018-02-03 11:21:32 +01:00
|
|
|
DefaultChannelModes = modes.Modes{
|
|
|
|
modes.NoOutside, modes.OpOnlyTopic,
|
2016-04-14 01:35:36 +02:00
|
|
|
}
|
2014-03-13 21:18:40 +01:00
|
|
|
)
|
|
|
|
|
2017-03-24 03:23:21 +01:00
|
|
|
// applyUserModeChanges applies the given changes, and returns the applied changes.
|
2018-02-03 11:21:32 +01:00
|
|
|
func (client *Client) applyUserModeChanges(force bool, changes modes.ModeChanges) modes.ModeChanges {
|
|
|
|
applied := make(modes.ModeChanges, 0)
|
2017-03-12 23:08:18 +01:00
|
|
|
|
|
|
|
for _, change := range changes {
|
2018-02-03 11:21:32 +01:00
|
|
|
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-02-03 11:21:32 +01:00
|
|
|
if client.flags[change.Mode] {
|
2017-03-12 23:08:18 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-04-20 22:48:15 +02:00
|
|
|
|
|
|
|
if change.Mode == modes.Invisible {
|
|
|
|
client.server.stats.ChangeInvisible(1)
|
|
|
|
}
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
client.flags[change.Mode] = true
|
2017-03-12 23:08:18 +01:00
|
|
|
applied = append(applied, change)
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
case modes.Remove:
|
|
|
|
if !client.flags[change.Mode] {
|
2017-03-12 23:08:18 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-04-20 22:48:15 +02:00
|
|
|
|
|
|
|
if change.Mode == modes.Invisible {
|
|
|
|
client.server.stats.ChangeInvisible(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if change.Mode == modes.Operator || change.Mode == modes.LocalOperator {
|
|
|
|
client.server.stats.ChangeOperators(-1)
|
|
|
|
}
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
delete(client.flags, change.Mode)
|
2017-03-12 23:08:18 +01:00
|
|
|
applied = append(applied, change)
|
|
|
|
}
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
case modes.ServerNotice:
|
|
|
|
if !client.flags[modes.Operator] {
|
2017-05-08 01:15:16 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
var masks []sno.Mask
|
2018-02-03 11:21:32 +01:00
|
|
|
if change.Op == modes.Add || change.Op == modes.Remove {
|
2018-04-16 05:20:37 +02:00
|
|
|
var newArg string
|
2018-02-03 11:21:32 +01:00
|
|
|
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
|
|
|
}
|
2018-02-03 11:21:32 +01:00
|
|
|
if change.Op == modes.Add {
|
2017-05-08 01:15:16 +02:00
|
|
|
client.server.snomasks.AddMasks(client, masks...)
|
|
|
|
applied = append(applied, change)
|
2018-02-03 11:21:32 +01:00
|
|
|
} 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
|
2016-06-20 14:53:45 +02:00
|
|
|
}
|
|
|
|
|
2017-09-06 23:34:38 +02:00
|
|
|
// ParseDefaultChannelModes parses the `default-modes` line of the config
|
2018-02-03 11:21:32 +01:00
|
|
|
func ParseDefaultChannelModes(config *Config) modes.Modes {
|
2017-09-06 23:34:38 +02:00
|
|
|
if config.Channels.DefaultModes == nil {
|
|
|
|
// not present in config, fall back to compile-time default
|
|
|
|
return DefaultChannelModes
|
|
|
|
}
|
|
|
|
modeChangeStrings := strings.Split(strings.TrimSpace(*config.Channels.DefaultModes), " ")
|
|
|
|
modeChanges, _ := ParseChannelModeChanges(modeChangeStrings...)
|
2018-02-03 11:21:32 +01:00
|
|
|
defaultChannelModes := make(modes.Modes, 0)
|
2017-09-06 23:34:38 +02:00
|
|
|
for _, modeChange := range modeChanges {
|
2018-02-03 11:21:32 +01:00
|
|
|
if modeChange.Op == modes.Add {
|
|
|
|
defaultChannelModes = append(defaultChannelModes, modeChange.Mode)
|
2017-09-06 23:34:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultChannelModes
|
|
|
|
}
|
|
|
|
|
2017-03-24 03:23:21 +01:00
|
|
|
// ParseChannelModeChanges returns the valid changes, and the list of unknown chars.
|
2018-02-03 11:21:32 +01:00
|
|
|
func ParseChannelModeChanges(params ...string) (modes.ModeChanges, map[rune]bool) {
|
|
|
|
changes := make(modes.ModeChanges, 0)
|
2017-03-24 03:23:21 +01:00
|
|
|
unknown := make(map[rune]bool)
|
2016-06-22 13:35:26 +02:00
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
op := modes.List
|
2017-09-25 23:58:17 +02:00
|
|
|
|
2017-03-26 12:37:13 +02:00
|
|
|
if 0 < len(params) {
|
2017-03-24 03:23:21 +01:00
|
|
|
modeArg := params[0]
|
|
|
|
skipArgs := 1
|
|
|
|
|
2016-06-22 13:35:26 +02:00
|
|
|
for _, mode := range modeArg {
|
|
|
|
if mode == '-' || mode == '+' {
|
2018-02-03 11:21:32 +01:00
|
|
|
op = modes.ModeOp(mode)
|
2016-06-22 13:35:26 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-02-03 11:21:32 +01:00
|
|
|
change := modes.ModeChange{
|
|
|
|
Mode: modes.Mode(mode),
|
|
|
|
Op: op,
|
2016-06-22 13:35:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// put arg into modechange if needed
|
2018-02-03 11:21:32 +01:00
|
|
|
switch modes.Mode(mode) {
|
|
|
|
case modes.BanMask, modes.ExceptMask, modes.InviteMask:
|
2017-03-24 03:23:21 +01:00
|
|
|
if len(params) > skipArgs {
|
2018-02-03 11:21:32 +01:00
|
|
|
change.Arg = params[skipArgs]
|
2016-09-14 11:48:47 +02:00
|
|
|
skipArgs++
|
2016-06-22 13:35:26 +02:00
|
|
|
} else {
|
2018-02-03 11:21:32 +01:00
|
|
|
change.Op = modes.List
|
2016-06-22 13:35:26 +02:00
|
|
|
}
|
2018-02-03 11:21:32 +01:00
|
|
|
case modes.ChannelFounder, modes.ChannelAdmin, modes.ChannelOperator, modes.Halfop, modes.Voice:
|
2017-03-24 03:23:21 +01:00
|
|
|
if len(params) > skipArgs {
|
2018-02-03 11:21:32 +01:00
|
|
|
change.Arg = params[skipArgs]
|
2016-09-14 11:48:47 +02:00
|
|
|
skipArgs++
|
2016-06-22 13:35:26 +02:00
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
2018-02-03 11:21:32 +01:00
|
|
|
case modes.Key, modes.UserLimit:
|
2016-09-14 11:48:47 +02:00
|
|
|
// don't require value when removing
|
2018-02-03 11:21:32 +01:00
|
|
|
if change.Op == modes.Add {
|
2017-03-24 03:23:21 +01:00
|
|
|
if len(params) > skipArgs {
|
2018-02-03 11:21:32 +01:00
|
|
|
change.Arg = params[skipArgs]
|
2016-09-14 11:48:47 +02:00
|
|
|
skipArgs++
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 15:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var isKnown bool
|
2018-02-03 11:21:32 +01:00
|
|
|
for _, supportedMode := range modes.SupportedChannelModes {
|
2017-04-17 15:05:24 +02:00
|
|
|
if rune(supportedMode) == mode {
|
|
|
|
isKnown = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-02-03 11:21:32 +01:00
|
|
|
for _, supportedMode := range modes.ChannelPrivModes {
|
2017-06-29 16:59:51 +02:00
|
|
|
if rune(supportedMode) == mode {
|
|
|
|
isKnown = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-02-03 11:21:32 +01:00
|
|
|
if mode == rune(modes.Voice) {
|
2017-06-29 17:14:38 +02:00
|
|
|
isKnown = true
|
|
|
|
}
|
2017-04-17 15:05:24 +02:00
|
|
|
if !isKnown {
|
2017-03-24 03:23:21 +01:00
|
|
|
unknown[mode] = true
|
2017-05-08 01:15:16 +02:00
|
|
|
continue
|
2016-06-22 13:35:26 +02:00
|
|
|
}
|
|
|
|
|
2017-03-24 03:23:21 +01:00
|
|
|
changes = append(changes, change)
|
2016-06-22 13:35:26 +02:00
|
|
|
}
|
2017-03-24 03:23:21 +01:00
|
|
|
}
|
2016-06-22 13:35:26 +02:00
|
|
|
|
2017-03-24 03:23:21 +01:00
|
|
|
return changes, unknown
|
|
|
|
}
|
2016-10-23 16:50:18 +02:00
|
|
|
|
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
|
2018-02-03 11:21:32 +01:00
|
|
|
listFullWarned := make(map[modes.Mode]bool)
|
2016-11-03 07:59:57 +01:00
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
clientIsOp := channel.ClientIsAtLeast(client, modes.ChannelOperator)
|
2017-03-24 03:23:21 +01:00
|
|
|
var alreadySentPrivError bool
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
applied := make(modes.ModeChanges, 0)
|
2017-03-24 03:23:21 +01:00
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
isListOp := func(change modes.ModeChange) bool {
|
|
|
|
return (change.Op == modes.List) || (change.Arg == "")
|
2017-11-13 08:55:26 +01:00
|
|
|
}
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
hasPrivs := func(change modes.ModeChange) bool {
|
2017-11-13 08:55:26 +01:00
|
|
|
if isSamode {
|
|
|
|
return true
|
|
|
|
}
|
2018-02-03 11:21:32 +01:00
|
|
|
switch change.Mode {
|
|
|
|
case modes.ChannelFounder, modes.ChannelAdmin, modes.ChannelOperator, modes.Halfop, modes.Voice:
|
2017-11-13 08:55:26 +01:00
|
|
|
// Admins can't give other people Admin or remove it from others
|
2018-02-03 11:21:32 +01:00
|
|
|
if change.Mode == modes.ChannelAdmin {
|
2017-11-13 08:55:26 +01:00
|
|
|
return false
|
|
|
|
}
|
2018-02-03 11:21:32 +01:00
|
|
|
if change.Op == modes.List {
|
2017-11-13 08:55:26 +01:00
|
|
|
return true
|
|
|
|
}
|
2018-02-03 11:21:32 +01:00
|
|
|
cfarg, _ := CasefoldName(change.Arg)
|
|
|
|
if change.Op == modes.Remove && cfarg == client.nickCasefolded {
|
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
|
|
|
|
}
|
2018-02-03 11:21:32 +01:00
|
|
|
return channel.ClientIsAtLeast(client, change.Mode)
|
|
|
|
case modes.BanMask:
|
2017-11-13 08:55:26 +01:00
|
|
|
// #163: allow unprivileged users to list ban masks
|
|
|
|
return clientIsOp || isListOp(change)
|
|
|
|
default:
|
|
|
|
return clientIsOp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2018-01-28 01:51:46 +01:00
|
|
|
rb.Add(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.name, client.t("You're not a channel operator"))
|
2016-11-03 07:59:57 +01:00
|
|
|
}
|
2017-03-24 03:23:21 +01:00
|
|
|
continue
|
|
|
|
}
|
2016-11-03 07:59:57 +01:00
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
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
|
2018-02-03 11:21:32 +01:00
|
|
|
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
|
|
|
|
2018-02-03 11:21:32 +01: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"))
|
2018-02-03 11:21:32 +01:00
|
|
|
listFullWarned[change.Mode] = true
|
2017-03-24 03:23:21 +01:00
|
|
|
}
|
2016-10-11 15:51:46 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
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
|
|
|
|
2018-02-03 11:21:32 +01: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
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
case modes.UserLimit:
|
|
|
|
switch change.Op {
|
|
|
|
case modes.Add:
|
|
|
|
val, err := strconv.ParseUint(change.Arg, 10, 64)
|
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)
|
|
|
|
}
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
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
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
case modes.Key:
|
|
|
|
switch change.Op {
|
|
|
|
case modes.Add:
|
|
|
|
channel.setKey(change.Arg)
|
2016-09-14 11:48:47 +02:00
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
case modes.Remove:
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.setKey("")
|
2017-03-24 03:23:21 +01:00
|
|
|
}
|
|
|
|
applied = append(applied, change)
|
2016-09-14 11:48:47 +02:00
|
|
|
|
2018-02-03 11:21:32 +01: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-02-03 11:21:32 +01:00
|
|
|
already := channel.setMode(change.Mode, change.Op == modes.Add)
|
2017-10-23 01:50:16 +02:00
|
|
|
if !already {
|
2017-03-24 03:23:21 +01:00
|
|
|
applied = append(applied, change)
|
|
|
|
}
|
2016-06-22 13:35:26 +02:00
|
|
|
|
2018-02-03 11:21:32 +01: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-02-05 15:21:08 +01:00
|
|
|
change := channel.applyModeMemberNoMutex(client, change.Mode, change.Op, change.Arg, 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
|
|
|
|
}
|