-
Notifications
You must be signed in to change notification settings - Fork 179
/
modes.go
412 lines (351 loc) · 8.83 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// 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 modes
import (
"strings"
"github.com/oragono/oragono/irc/utils"
)
var (
// SupportedUserModes are the user modes that we actually support (modifying).
SupportedUserModes = Modes{
Bot, Invisible, Operator, RegisteredOnly, ServerNotice, UserRoleplaying,
}
// SupportedChannelModes are the channel modes that we support.
SupportedChannelModes = Modes{
BanMask, ChanRoleplaying, ExceptMask, InviteMask, InviteOnly, Key,
Moderated, NoOutside, OpOnlyTopic, RegisteredOnly, Secret, UserLimit,
}
)
// ModeOp is an operation performed with modes
type ModeOp rune
func (op ModeOp) String() string {
return string(op)
}
const (
// Add is used when adding the given key.
Add ModeOp = '+'
// List is used when listing modes (for instance, listing the current bans on a channel).
List ModeOp = '='
// Remove is used when taking away the given key.
Remove ModeOp = '-'
)
// Mode represents a user/channel/server mode
type Mode rune
func (mode Mode) String() string {
return string(mode)
}
// ModeChange is a single mode changing
type ModeChange struct {
Mode Mode
Op ModeOp
Arg string
}
func (change *ModeChange) String() (str string) {
if (change.Op == Add) || (change.Op == Remove) {
str = change.Op.String()
}
str += change.Mode.String()
if change.Arg != "" {
str += " " + change.Arg
}
return
}
// ModeChanges are a collection of 'ModeChange's
type ModeChanges []ModeChange
func (changes ModeChanges) String() string {
if len(changes) == 0 {
return ""
}
op := changes[0].Op
str := changes[0].Op.String()
for _, change := range changes {
if change.Op != op {
op = change.Op
str += change.Op.String()
}
str += change.Mode.String()
}
for _, change := range changes {
if change.Arg == "" {
continue
}
str += " " + change.Arg
}
return str
}
// Modes is just a raw list of modes
type Modes []Mode
func (modes Modes) String() string {
strs := make([]string, len(modes))
for index, mode := range modes {
strs[index] = mode.String()
}
return strings.Join(strs, "")
}
// User Modes
const (
Bot Mode = 'B'
Invisible Mode = 'i'
LocalOperator Mode = 'O'
Operator Mode = 'o'
Restricted Mode = 'r'
RegisteredOnly Mode = 'R'
ServerNotice Mode = 's'
TLS Mode = 'Z'
UserRoleplaying Mode = 'E'
WallOps Mode = 'w'
)
// Channel Modes
const (
BanMask Mode = 'b' // arg
ChanRoleplaying Mode = 'E' // flag
ExceptMask Mode = 'e' // arg
InviteMask Mode = 'I' // arg
InviteOnly Mode = 'i' // flag
Key Mode = 'k' // flag arg
Moderated Mode = 'm' // flag
NoOutside Mode = 'n' // flag
OpOnlyTopic Mode = 't' // flag
// RegisteredOnly mode is reused here from umode definition
Secret Mode = 's' // flag
UserLimit Mode = 'l' // flag arg
)
var (
ChannelFounder Mode = 'q' // arg
ChannelAdmin Mode = 'a' // arg
ChannelOperator Mode = 'o' // arg
Halfop Mode = 'h' // arg
Voice Mode = 'v' // arg
// ChannelUserModes holds the list of all modes that can be applied to a user in a channel,
// including Voice, in descending order of precedence
ChannelUserModes = Modes{
ChannelFounder, ChannelAdmin, ChannelOperator, Halfop, Voice,
}
ChannelModePrefixes = map[Mode]string{
ChannelFounder: "~",
ChannelAdmin: "&",
ChannelOperator: "@",
Halfop: "%",
Voice: "+",
}
)
//
// channel membership prefixes
//
// SplitChannelMembershipPrefixes takes a target and returns the prefixes on it, then the name.
func SplitChannelMembershipPrefixes(target string) (prefixes string, name string) {
name = target
for i := 0; i < len(name); i++ {
switch name[i] {
case '~', '&', '@', '%', '+':
prefixes = target[:i+1]
name = target[i+1:]
default:
return
}
}
return
}
// GetLowestChannelModePrefix returns the lowest channel prefix mode out of the given prefixes.
func GetLowestChannelModePrefix(prefixes string) (lowest Mode) {
for i, mode := range ChannelUserModes {
if strings.Contains(prefixes, ChannelModePrefixes[mode]) {
lowest = ChannelUserModes[i]
}
}
return
}
//
// commands
//
// ParseUserModeChanges returns the valid changes, and the list of unknown chars.
func ParseUserModeChanges(params ...string) (ModeChanges, map[rune]bool) {
changes := make(ModeChanges, 0)
unknown := make(map[rune]bool)
op := List
if 0 < len(params) {
modeArg := params[0]
skipArgs := 1
for _, mode := range modeArg {
if mode == '-' || mode == '+' {
op = ModeOp(mode)
continue
}
change := ModeChange{
Mode: Mode(mode),
Op: op,
}
// put arg into modechange if needed
switch Mode(mode) {
case ServerNotice:
// always require arg
if len(params) > skipArgs {
change.Arg = params[skipArgs]
skipArgs++
} else {
continue
}
}
var isKnown bool
for _, supportedMode := range SupportedUserModes {
if rune(supportedMode) == mode {
isKnown = true
break
}
}
if !isKnown {
unknown[mode] = true
continue
}
changes = append(changes, change)
}
}
return changes, unknown
}
// ParseChannelModeChanges returns the valid changes, and the list of unknown chars.
func ParseChannelModeChanges(params ...string) (ModeChanges, map[rune]bool) {
changes := make(ModeChanges, 0)
unknown := make(map[rune]bool)
op := List
if 0 < len(params) {
modeArg := params[0]
skipArgs := 1
for _, mode := range modeArg {
if mode == '-' || mode == '+' {
op = ModeOp(mode)
continue
}
change := ModeChange{
Mode: Mode(mode),
Op: op,
}
// put arg into modechange if needed
switch Mode(mode) {
case BanMask, ExceptMask, InviteMask:
if len(params) > skipArgs {
change.Arg = params[skipArgs]
skipArgs++
} else {
change.Op = List
}
case ChannelFounder, ChannelAdmin, ChannelOperator, Halfop, Voice:
if len(params) > skipArgs {
change.Arg = params[skipArgs]
skipArgs++
} else {
continue
}
case Key, UserLimit:
// don't require value when removing
if change.Op == Add {
if len(params) > skipArgs {
change.Arg = params[skipArgs]
skipArgs++
} else {
continue
}
}
}
var isKnown bool
for _, supportedMode := range SupportedChannelModes {
if rune(supportedMode) == mode {
isKnown = true
break
}
}
for _, supportedMode := range ChannelUserModes {
if rune(supportedMode) == mode {
isKnown = true
break
}
}
if !isKnown {
unknown[mode] = true
continue
}
changes = append(changes, change)
}
}
return changes, unknown
}
// ModeSet holds a set of modes.
type ModeSet [2]uint32
// valid modes go from 65 ('A') to 122 ('z'), making at most 58 possible values;
// subtract 65 from the mode value and use that bit of the uint32 to represent it
const (
minMode = 65 // 'A'
maxMode = 122 // 'z'
)
// returns a pointer to a new ModeSet
func NewModeSet() *ModeSet {
var set ModeSet
return &set
}
// test whether `mode` is set
func (set *ModeSet) HasMode(mode Mode) bool {
if set == nil {
return false
}
return utils.BitsetGet(set[:], uint(mode)-minMode)
}
// set `mode` to be on or off, return whether the value actually changed
func (set *ModeSet) SetMode(mode Mode, on bool) (applied bool) {
return utils.BitsetSet(set[:], uint(mode)-minMode, on)
}
// copy the contents of another modeset on top of this one
func (set *ModeSet) Copy(other *ModeSet) {
utils.BitsetCopy(set[:], other[:])
}
// return the modes in the set as a slice
func (set *ModeSet) AllModes() (result []Mode) {
if set == nil {
return
}
var i Mode
for i = minMode; i <= maxMode; i++ {
if set.HasMode(i) {
result = append(result, i)
}
}
return
}
// String returns the modes in this set.
func (set *ModeSet) String() (result string) {
if set == nil {
return
}
var buf strings.Builder
for _, mode := range set.AllModes() {
buf.WriteRune(rune(mode))
}
return buf.String()
}
// Prefixes returns a list of prefixes for the given set of channel modes.
func (set *ModeSet) Prefixes(isMultiPrefix bool) (prefixes string) {
if set == nil {
return
}
// add prefixes in order from highest to lowest privs
for _, mode := range ChannelUserModes {
if set.HasMode(mode) {
prefixes += ChannelModePrefixes[mode]
}
}
if !isMultiPrefix && len(prefixes) > 1 {
prefixes = string(prefixes[0])
}
return prefixes
}
// HighestChannelUserMode returns the most privileged channel-user mode
// (e.g., ChannelFounder, Halfop, Voice) present in the ModeSet.
// If no such modes are present, or `set` is nil, returns the zero mode.
func (set *ModeSet) HighestChannelUserMode() (result Mode) {
for _, mode := range ChannelUserModes {
if set.HasMode(mode) {
return mode
}
}
return
}