3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00

DRY mode apply code

ParseUserModeChanges and ParseChannelModeChanges already validate
that the modes are modifiable, so there's no need to repeat it here.
This commit is contained in:
Shivaram Lingamneni 2020-10-01 19:52:50 -04:00
parent b426ba628a
commit c279b2d14c
2 changed files with 16 additions and 16 deletions

View File

@ -33,8 +33,7 @@ func ApplyUserModeChanges(client *Client, changes modes.ModeChanges, force bool,
applied := make(modes.ModeChanges, 0) applied := make(modes.ModeChanges, 0)
for _, change := range changes { for _, change := range changes {
switch change.Mode { if change.Mode != modes.ServerNotice {
case modes.Bot, modes.Invisible, modes.WallOps, modes.UserRoleplaying, modes.Operator, modes.LocalOperator, modes.RegisteredOnly, modes.UserNoCTCP:
switch change.Op { switch change.Op {
case modes.Add: case modes.Add:
if (change.Mode == modes.Operator || change.Mode == modes.LocalOperator) && !(force && oper != nil) { if (change.Mode == modes.Operator || change.Mode == modes.LocalOperator) && !(force && oper != nil) {
@ -73,8 +72,8 @@ func ApplyUserModeChanges(client *Client, changes modes.ModeChanges, force bool,
} }
} }
} }
} else {
case modes.ServerNotice: // server notices are weird
if !client.HasMode(modes.Operator) { if !client.HasMode(modes.Operator) {
continue continue
} }
@ -98,8 +97,6 @@ func ApplyUserModeChanges(client *Client, changes modes.ModeChanges, force bool,
applied = append(applied, change) applied = append(applied, change)
} }
} }
// can't do anything to TLS mode
} }
if len(applied) != 0 { if len(applied) != 0 {
@ -271,15 +268,6 @@ func (channel *Channel) ApplyChannelModeChanges(client *Client, isSamode bool, c
applied = append(applied, change) applied = append(applied, change)
} }
case modes.InviteOnly, modes.Moderated, modes.NoOutside, modes.OpOnlyTopic, modes.RegisteredOnly, modes.Secret, modes.ChanRoleplaying, modes.NoCTCP, modes.RegisteredOnlySpeak:
if change.Op == modes.List {
continue
}
if channel.flags.SetMode(change.Mode, change.Op == modes.Add) {
applied = append(applied, change)
}
case modes.ChannelFounder, modes.ChannelAdmin, modes.ChannelOperator, modes.Halfop, modes.Voice: case modes.ChannelFounder, modes.ChannelAdmin, modes.ChannelOperator, modes.Halfop, modes.Voice:
if change.Op == modes.List { if change.Op == modes.List {
continue continue
@ -295,6 +283,16 @@ func (channel *Channel) ApplyChannelModeChanges(client *Client, isSamode bool, c
if success { if success {
applied = append(applied, change) applied = append(applied, change)
} }
default:
// all channel modes with no args, e.g., InviteOnly, Secret
if change.Op == modes.List {
continue
}
if channel.flags.SetMode(change.Mode, change.Op == modes.Add) {
applied = append(applied, change)
}
} }
} }

View File

@ -430,8 +430,10 @@ func (a ByCodepoint) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByCodepoint) Less(i, j int) bool { return a[i] < a[j] } func (a ByCodepoint) Less(i, j int) bool { return a[i] < a[j] }
func RplMyInfo() (param1, param2, param3 string) { func RplMyInfo() (param1, param2, param3 string) {
userModes := make(Modes, len(SupportedUserModes)) userModes := make(Modes, len(SupportedUserModes), len(SupportedUserModes)+1)
copy(userModes, SupportedUserModes) copy(userModes, SupportedUserModes)
// TLS is not in SupportedUserModes because it can't be modified
userModes = append(userModes, TLS)
sort.Sort(ByCodepoint(userModes)) sort.Sort(ByCodepoint(userModes))
channelModes := make(Modes, len(SupportedChannelModes)+len(ChannelUserModes)) channelModes := make(Modes, len(SupportedChannelModes)+len(ChannelUserModes))