-
Notifications
You must be signed in to change notification settings - Fork 180
/
modes.go
338 lines (300 loc) · 10.6 KB
/
modes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2014-2015 Edmund Huber
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
"fmt"
"strconv"
"strings"
"github.com/oragono/oragono/irc/modes"
"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,
}
)
// 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)
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) {
continue
}
if client.SetMode(change.Mode, true) {
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:
if client.SetMode(change.Mode, false) {
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.ServerNotice:
if !client.HasMode(modes.Operator) {
continue
}
var masks []sno.Mask
if change.Op == modes.Add || change.Op == modes.Remove {
var newArg string
for _, char := range change.Arg {
mask := sno.Mask(char)
if sno.ValidMasks[mask] {
masks = append(masks, mask)
newArg += string(char)
}
}
change.Arg = newArg
}
if change.Op == modes.Add {
client.server.snomasks.AddMasks(client, masks...)
applied = append(applied, change)
} else if change.Op == modes.Remove {
client.server.snomasks.RemoveMasks(client, masks...)
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)
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
}
// ApplyChannelModeChanges applies a given set of mode changes.
func (channel *Channel) ApplyChannelModeChanges(client *Client, isSamode bool, changes modes.ModeChanges, rb *ResponseBuffer) (applied modes.ModeChanges) {
// so we only output one warning for each list type when full
listFullWarned := make(map[modes.Mode]bool)
var alreadySentPrivError bool
maskOpCount := 0
chname := channel.Name()
details := client.Details()
hasPrivs := func(change modes.ModeChange) bool {
if isSamode {
return true
}
switch change.Mode {
case modes.ChannelFounder, modes.ChannelAdmin, modes.ChannelOperator, modes.Halfop, modes.Voice:
// List on these modes is a no-op anyway
if change.Op == modes.List {
return true
}
cfarg, _ := CasefoldName(change.Arg)
isSelfChange := cfarg == details.nickCasefolded
if change.Op == modes.Remove && isSelfChange {
// "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.InviteMask, modes.ExceptMask:
// listing these requires privileges
return channel.ClientIsAtLeast(client, modes.ChannelOperator)
default:
// #163: allow unprivileged users to list ban masks, and any other modes
return change.Op == modes.List || channel.ClientIsAtLeast(client, modes.ChannelOperator)
}
}
for _, change := range changes {
if !hasPrivs(change) {
if !alreadySentPrivError {
alreadySentPrivError = true
rb.Add(nil, client.server.name, ERR_CHANOPRIVSNEEDED, details.nick, channel.name, client.t("You're not a channel operator"))
}
continue
}
switch change.Mode {
case modes.BanMask, modes.ExceptMask, modes.InviteMask:
maskOpCount += 1
if change.Op == modes.List {
channel.ShowMaskList(client, change.Mode, rb)
continue
}
mask := change.Arg
switch change.Op {
case modes.Add:
if channel.lists[change.Mode].Length() >= client.server.Config().Limits.ChanListModes {
if !listFullWarned[change.Mode] {
rb.Add(nil, client.server.name, ERR_BANLISTFULL, details.nick, chname, change.Mode.String(), client.t("Channel list is full"))
listFullWarned[change.Mode] = true
}
continue
}
maskAdded, err := channel.lists[change.Mode].Add(mask, details.nickMask, details.accountName)
if maskAdded != "" {
appliedChange := change
appliedChange.Arg = maskAdded
applied = append(applied, appliedChange)
} else if err != nil {
rb.Add(nil, client.server.name, ERR_INVALIDMODEPARAM, details.nick, mask, fmt.Sprintf(client.t("Invalid mode %s parameter: %s"), string(change.Mode), mask))
} else {
rb.Add(nil, client.server.name, ERR_LISTMODEALREADYSET, chname, mask, string(change.Mode), fmt.Sprintf(client.t("Channel %s list already contains %s"), chname, mask))
}
case modes.Remove:
maskRemoved, err := channel.lists[change.Mode].Remove(mask)
if maskRemoved != "" {
appliedChange := change
appliedChange.Arg = maskRemoved
applied = append(applied, appliedChange)
} else if err != nil {
rb.Add(nil, client.server.name, ERR_INVALIDMODEPARAM, details.nick, mask, fmt.Sprintf(client.t("Invalid mode %s parameter: %s"), string(change.Mode), mask))
} else {
rb.Add(nil, client.server.name, ERR_LISTMODENOTSET, chname, mask, string(change.Mode), fmt.Sprintf(client.t("Channel %s list does not contain %s"), chname, mask))
}
}
case modes.UserLimit:
switch change.Op {
case modes.Add:
val, err := strconv.Atoi(change.Arg)
if err == nil {
channel.setUserLimit(val)
applied = append(applied, change)
}
case modes.Remove:
channel.setUserLimit(0)
applied = append(applied, change)
}
case modes.Key:
switch change.Op {
case modes.Add:
channel.setKey(change.Arg)
applied = append(applied, change)
case modes.Remove:
channel.setKey("")
applied = append(applied, change)
}
case modes.InviteOnly, modes.Moderated, modes.NoOutside, modes.OpOnlyTopic, modes.RegisteredOnly, modes.Secret, modes.ChanRoleplaying:
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:
if change.Op == modes.List {
continue
}
nick := change.Arg
if nick == "" {
rb.Add(nil, client.server.name, ERR_NEEDMOREPARAMS, client.Nick(), "MODE", client.t("Not enough parameters"))
continue
}
change := channel.applyModeToMember(client, change.Mode, change.Op, nick, rb)
if change != nil {
applied = append(applied, *change)
}
}
}
// #649: don't send 324 RPL_CHANNELMODEIS if we were only working with mask lists
if len(applied) == 0 && !alreadySentPrivError && (maskOpCount == 0 || maskOpCount < len(changes)) {
args := append([]string{details.nick, chname}, channel.modeStrings(client)...)
rb.Add(nil, client.server.name, RPL_CHANNELMODEIS, args...)
rb.Add(nil, client.server.name, RPL_CREATIONTIME, details.nick, chname, strconv.FormatInt(channel.createdTime.Unix(), 10))
}
return applied
}
// 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) {
changed := false
defer func() {
if changed {
channel.MarkDirty(IncludeLists)
}
}()
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)) {
hasPrivs = true
} else if channelUserModeHasPrivsOver(clientMode, targetModeNow) && channelUserModeHasPrivsOver(clientMode, targetModeAfter) {
hasPrivs = true
} else if change.Op == modes.Remove && account == change.Arg {
// you can always de-op yourself
hasPrivs = true
}
if !hasPrivs {
return nil, errInsufficientPrivs
}
switch change.Op {
case modes.Add:
if targetModeNow != targetModeAfter {
channel.accountToUMode[change.Arg] = change.Mode
changed = true
return []modes.ModeChange{change}, nil
}
return nil, nil
case modes.Remove:
if targetModeNow == change.Mode {
delete(channel.accountToUMode, change.Arg)
changed = true
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
}
}