2015-08-02 02:46:58 +00:00
|
|
|
package irc
|
|
|
|
|
2015-08-08 00:37:41 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
)
|
2015-08-02 02:46:58 +00:00
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// UserMode - RFC 1459 Section 4.2.3.2 and RFC 2812 Section 3.1.5
|
2015-08-02 02:46:58 +00:00
|
|
|
type UserMode rune
|
|
|
|
|
|
|
|
const (
|
|
|
|
UserModeAway UserMode = 'a'
|
|
|
|
UserModeInvisible UserMode = 'i'
|
|
|
|
UserModeWallOps UserMode = 'w'
|
|
|
|
UserModeRestricted UserMode = 'r'
|
|
|
|
UserModeOperator UserMode = 'o'
|
|
|
|
UserModeLocalOperator UserMode = 'O'
|
|
|
|
UserModeServerNotice UserMode = 's' //obsolete
|
|
|
|
)
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// UserModes contains the supported User UserMode types
|
2015-08-02 02:46:58 +00:00
|
|
|
var UserModes = map[UserMode]interface{}{
|
|
|
|
UserModeAway: nil,
|
|
|
|
UserModeInvisible: nil,
|
|
|
|
UserModeWallOps: nil,
|
|
|
|
UserModeRestricted: nil,
|
|
|
|
UserModeOperator: nil,
|
|
|
|
UserModeLocalOperator: nil,
|
|
|
|
UserModeServerNotice: nil,
|
|
|
|
}
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// UserModeSet provides means for storing and checking UserModes
|
2015-08-02 02:46:58 +00:00
|
|
|
type UserModeSet struct {
|
|
|
|
userModes map[UserMode]interface{}
|
2015-08-05 15:33:06 +00:00
|
|
|
mutex sync.RWMutex
|
2015-08-02 02:46:58 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// NewUserModeSet creates and returns a new UserModeSet
|
|
|
|
func NewUserModeSet() *UserModeSet {
|
2015-08-02 02:46:58 +00:00
|
|
|
u := UserModeSet{}
|
|
|
|
u.userModes = map[UserMode]interface{}{}
|
2015-08-05 15:33:06 +00:00
|
|
|
return &u
|
2015-08-02 02:46:58 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// AddMode adds a mode to the UserModeSet
|
2015-08-02 02:46:58 +00:00
|
|
|
func (u *UserModeSet) AddMode(mode UserMode) {
|
|
|
|
u.mutex.Lock()
|
|
|
|
defer u.mutex.Unlock()
|
|
|
|
u.userModes[mode] = nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// RemoveMode removes a mode from the UserModeSet
|
2015-08-02 02:46:58 +00:00
|
|
|
func (u *UserModeSet) RemoveMode(mode UserMode) {
|
|
|
|
u.mutex.Lock()
|
|
|
|
defer u.mutex.Unlock()
|
|
|
|
delete(u.userModes, mode)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// HasMode detects if a UserMode is contained in the UserModeSet
|
|
|
|
func (u *UserModeSet) HasMode(mode UserMode) bool {
|
|
|
|
u.mutex.RLock()
|
|
|
|
defer u.mutex.RUnlock()
|
|
|
|
_, found := u.userModes[mode]
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
// String formats the UserModeString to be returned for MODE queries
|
2015-08-02 02:46:58 +00:00
|
|
|
func (u *UserModeSet) String() string {
|
|
|
|
s := ""
|
2015-08-05 15:33:06 +00:00
|
|
|
if len(u.userModes) > 0 {
|
|
|
|
s = "+"
|
|
|
|
}
|
|
|
|
for m := range u.userModes {
|
2015-08-02 02:46:58 +00:00
|
|
|
s += string(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// ModeModifier - RFC 1459 Section 4.2.3 and RFC 2812 Section 3.1.5
|
2015-08-02 02:46:58 +00:00
|
|
|
type ModeModifier rune
|
|
|
|
|
|
|
|
const (
|
|
|
|
ModeModifierAdd ModeModifier = '+'
|
|
|
|
ModeModifierRemove ModeModifier = '-'
|
|
|
|
)
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// ChannelMode - RFC 1459 Section 4.2.3.1 and RFC 2812 Section 3.2.3 and RFC 2811 Section 4
|
2015-08-02 02:46:58 +00:00
|
|
|
type ChannelMode rune
|
|
|
|
|
|
|
|
const (
|
|
|
|
ChannelModeCreator ChannelMode = 'O'
|
|
|
|
ChannelModeOperator ChannelMode = 'o'
|
|
|
|
ChannelModeVoice ChannelMode = 'v'
|
|
|
|
|
|
|
|
ChannelModeAnonymous ChannelMode = 'a'
|
|
|
|
ChannelModeInviteOnly ChannelMode = 'i'
|
|
|
|
ChannelModeModerated ChannelMode = 'm'
|
|
|
|
ChannelModeNoOutsideMessages ChannelMode = 'n'
|
|
|
|
ChannelModeQuiet ChannelMode = 'q'
|
|
|
|
ChannelModePrivate ChannelMode = 'p'
|
|
|
|
ChannelModeSecret ChannelMode = 's'
|
|
|
|
ChannelModeReOp ChannelMode = 'r'
|
|
|
|
ChannelModeTopic ChannelMode = 't'
|
|
|
|
|
|
|
|
ChannelModeKey ChannelMode = 'k'
|
|
|
|
ChannelModeLimit ChannelMode = 'l'
|
|
|
|
|
|
|
|
ChannelModeBan ChannelMode = 'b'
|
|
|
|
ChannelModeExceptionMask ChannelMode = 'e'
|
|
|
|
ChannelModeInvitationMask ChannelMode = 'I'
|
|
|
|
)
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// ChannelModeSet represents a set of active ChannelModes
|
2015-08-02 02:46:58 +00:00
|
|
|
type ChannelModeSet struct {
|
2015-08-05 15:33:06 +00:00
|
|
|
modes map[ChannelMode]interface{}
|
|
|
|
mutex sync.RWMutex
|
2015-08-02 02:46:58 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// NewChannelModeSet creates and returns a new ChannelModeSet
|
|
|
|
func NewChannelModeSet() *ChannelModeSet {
|
2015-08-02 02:46:58 +00:00
|
|
|
c := ChannelModeSet{}
|
2015-08-05 15:33:06 +00:00
|
|
|
c.modes = map[ChannelMode]interface{}{}
|
|
|
|
return &c
|
2015-08-02 02:46:58 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// AddMode adds a ChannelMode as active
|
2015-08-02 02:46:58 +00:00
|
|
|
func (c *ChannelModeSet) AddMode(mode ChannelMode) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2015-08-05 15:33:06 +00:00
|
|
|
c.modes[mode] = nil
|
2015-08-02 02:46:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-08 00:37:41 +00:00
|
|
|
// AddModeWithValue adds a ChannelMode as active with a value
|
|
|
|
func (c *Channel) AddModeWithValue(mode ChannelMode, value interface{}) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
c.modes[mode] = value
|
|
|
|
}
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// RemoveMode removes the given mode from the active set
|
2015-08-02 02:46:58 +00:00
|
|
|
func (c *ChannelModeSet) RemoveMode(mode ChannelMode) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2015-08-05 15:33:06 +00:00
|
|
|
delete(c.modes, mode)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasMode determines if the ChannelModeSet contains the given ChannelMode
|
|
|
|
func (c *ChannelModeSet) HasMode(mode ChannelMode) bool {
|
|
|
|
c.mutex.RLock()
|
|
|
|
defer c.mutex.RUnlock()
|
|
|
|
_, found := c.modes[mode]
|
|
|
|
return found
|
2015-08-02 02:46:58 +00:00
|
|
|
|
|
|
|
}
|
2015-08-05 15:33:06 +00:00
|
|
|
|
2015-08-08 00:37:41 +00:00
|
|
|
// GetMode determines if the ChannelModeSet contains the given ChannelMode
|
|
|
|
func (c *ChannelModeSet) GetMode(mode ChannelMode) interface{} {
|
|
|
|
c.mutex.RLock()
|
|
|
|
defer c.mutex.RUnlock()
|
|
|
|
return c.modes[mode]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
// String returns the ChannelModeSet formatted for the MODE queries
|
|
|
|
func (c *ChannelModeSet) String() string {
|
2015-08-08 00:37:41 +00:00
|
|
|
s := "+"
|
|
|
|
|
|
|
|
params := []interface{}{}
|
|
|
|
for m, param := range c.modes {
|
|
|
|
switch m {
|
|
|
|
case ChannelModeKey, ChannelModeLimit, ChannelModeModerated, ChannelModeAnonymous, ChannelModeInviteOnly, ChannelModePrivate, ChannelModeSecret, ChannelModeTopic:
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
2015-08-05 15:33:06 +00:00
|
|
|
s += string(m)
|
2015-08-08 00:37:41 +00:00
|
|
|
params = append(params, param)
|
|
|
|
}
|
|
|
|
for _, param := range params {
|
2015-08-08 21:56:04 +00:00
|
|
|
if param != nil {
|
|
|
|
s += fmt.Sprintf(" %v", param)
|
|
|
|
}
|
|
|
|
|
2015-08-05 15:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|