govendor
This commit is contained in:
350
vendor/github.com/fluffle/goirc/state/channel.go
generated
vendored
Normal file
350
vendor/github.com/fluffle/goirc/state/channel.go
generated
vendored
Normal file
@@ -0,0 +1,350 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"github.com/fluffle/goirc/logging"
|
||||
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// A Channel is returned from the state tracker and contains
|
||||
// a copy of the channel state at a particular time.
|
||||
type Channel struct {
|
||||
Name, Topic string
|
||||
Modes *ChanMode
|
||||
Nicks map[string]*ChanPrivs
|
||||
}
|
||||
|
||||
// Internal bookkeeping struct for channels.
|
||||
type channel struct {
|
||||
name, topic string
|
||||
modes *ChanMode
|
||||
lookup map[string]*nick
|
||||
nicks map[*nick]*ChanPrivs
|
||||
}
|
||||
|
||||
// A struct representing the modes of an IRC Channel
|
||||
// (the ones we care about, at least).
|
||||
// http://www.unrealircd.com/files/docs/unreal32docs.html#userchannelmodes
|
||||
type ChanMode struct {
|
||||
// MODE +p, +s, +t, +n, +m
|
||||
Private, Secret, ProtectedTopic, NoExternalMsg, Moderated bool
|
||||
|
||||
// MODE +i, +O, +z
|
||||
InviteOnly, OperOnly, SSLOnly bool
|
||||
|
||||
// MODE +r, +Z
|
||||
Registered, AllSSL bool
|
||||
|
||||
// MODE +k
|
||||
Key string
|
||||
|
||||
// MODE +l
|
||||
Limit int
|
||||
}
|
||||
|
||||
// A struct representing the modes a Nick can have on a Channel
|
||||
type ChanPrivs struct {
|
||||
// MODE +q, +a, +o, +h, +v
|
||||
Owner, Admin, Op, HalfOp, Voice bool
|
||||
}
|
||||
|
||||
// Map ChanMode fields to IRC mode characters
|
||||
var StringToChanMode = map[string]string{}
|
||||
var ChanModeToString = map[string]string{
|
||||
"Private": "p",
|
||||
"Secret": "s",
|
||||
"ProtectedTopic": "t",
|
||||
"NoExternalMsg": "n",
|
||||
"Moderated": "m",
|
||||
"InviteOnly": "i",
|
||||
"OperOnly": "O",
|
||||
"SSLOnly": "z",
|
||||
"Registered": "r",
|
||||
"AllSSL": "Z",
|
||||
"Key": "k",
|
||||
"Limit": "l",
|
||||
}
|
||||
|
||||
// Map *irc.ChanPrivs fields to IRC mode characters
|
||||
var StringToChanPriv = map[string]string{}
|
||||
var ChanPrivToString = map[string]string{
|
||||
"Owner": "q",
|
||||
"Admin": "a",
|
||||
"Op": "o",
|
||||
"HalfOp": "h",
|
||||
"Voice": "v",
|
||||
}
|
||||
|
||||
// Map *irc.ChanPrivs fields to the symbols used to represent these modes
|
||||
// in NAMES and WHOIS responses
|
||||
var ModeCharToChanPriv = map[byte]string{}
|
||||
var ChanPrivToModeChar = map[string]byte{
|
||||
"Owner": '~',
|
||||
"Admin": '&',
|
||||
"Op": '@',
|
||||
"HalfOp": '%',
|
||||
"Voice": '+',
|
||||
}
|
||||
|
||||
// Init function to fill in reverse mappings for *toString constants.
|
||||
func init() {
|
||||
for k, v := range ChanModeToString {
|
||||
StringToChanMode[v] = k
|
||||
}
|
||||
for k, v := range ChanPrivToString {
|
||||
StringToChanPriv[v] = k
|
||||
}
|
||||
for k, v := range ChanPrivToModeChar {
|
||||
ModeCharToChanPriv[v] = k
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************\
|
||||
* Channel methods for state management
|
||||
\******************************************************************************/
|
||||
|
||||
func newChannel(name string) *channel {
|
||||
return &channel{
|
||||
name: name,
|
||||
modes: new(ChanMode),
|
||||
nicks: make(map[*nick]*ChanPrivs),
|
||||
lookup: make(map[string]*nick),
|
||||
}
|
||||
}
|
||||
|
||||
// Returns a copy of the internal tracker channel state at this time.
|
||||
// Relies on tracker-level locking for concurrent access.
|
||||
func (ch *channel) Channel() *Channel {
|
||||
c := &Channel{
|
||||
Name: ch.name,
|
||||
Topic: ch.topic,
|
||||
Modes: ch.modes.Copy(),
|
||||
Nicks: make(map[string]*ChanPrivs),
|
||||
}
|
||||
for n, cp := range ch.nicks {
|
||||
c.Nicks[n.nick] = cp.Copy()
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (ch *channel) isOn(nk *nick) (*ChanPrivs, bool) {
|
||||
cp, ok := ch.nicks[nk]
|
||||
return cp.Copy(), ok
|
||||
}
|
||||
|
||||
// Associates a Nick with a Channel
|
||||
func (ch *channel) addNick(nk *nick, cp *ChanPrivs) {
|
||||
if _, ok := ch.nicks[nk]; !ok {
|
||||
ch.nicks[nk] = cp
|
||||
ch.lookup[nk.nick] = nk
|
||||
} else {
|
||||
logging.Warn("Channel.addNick(): %s already on %s.", nk.nick, ch.name)
|
||||
}
|
||||
}
|
||||
|
||||
// Disassociates a Nick from a Channel.
|
||||
func (ch *channel) delNick(nk *nick) {
|
||||
if _, ok := ch.nicks[nk]; ok {
|
||||
delete(ch.nicks, nk)
|
||||
delete(ch.lookup, nk.nick)
|
||||
} else {
|
||||
logging.Warn("Channel.delNick(): %s not on %s.", nk.nick, ch.name)
|
||||
}
|
||||
}
|
||||
|
||||
// Parses mode strings for a channel.
|
||||
func (ch *channel) parseModes(modes string, modeargs ...string) {
|
||||
var modeop bool // true => add mode, false => remove mode
|
||||
var modestr string
|
||||
for i := 0; i < len(modes); i++ {
|
||||
switch m := modes[i]; m {
|
||||
case '+':
|
||||
modeop = true
|
||||
modestr = string(m)
|
||||
case '-':
|
||||
modeop = false
|
||||
modestr = string(m)
|
||||
case 'i':
|
||||
ch.modes.InviteOnly = modeop
|
||||
case 'm':
|
||||
ch.modes.Moderated = modeop
|
||||
case 'n':
|
||||
ch.modes.NoExternalMsg = modeop
|
||||
case 'p':
|
||||
ch.modes.Private = modeop
|
||||
case 'r':
|
||||
ch.modes.Registered = modeop
|
||||
case 's':
|
||||
ch.modes.Secret = modeop
|
||||
case 't':
|
||||
ch.modes.ProtectedTopic = modeop
|
||||
case 'z':
|
||||
ch.modes.SSLOnly = modeop
|
||||
case 'Z':
|
||||
ch.modes.AllSSL = modeop
|
||||
case 'O':
|
||||
ch.modes.OperOnly = modeop
|
||||
case 'k':
|
||||
if modeop && len(modeargs) != 0 {
|
||||
ch.modes.Key, modeargs = modeargs[0], modeargs[1:]
|
||||
} else if !modeop {
|
||||
ch.modes.Key = ""
|
||||
} else {
|
||||
logging.Warn("Channel.ParseModes(): not enough arguments to "+
|
||||
"process MODE %s %s%c", ch.name, modestr, m)
|
||||
}
|
||||
case 'l':
|
||||
if modeop && len(modeargs) != 0 {
|
||||
ch.modes.Limit, _ = strconv.Atoi(modeargs[0])
|
||||
modeargs = modeargs[1:]
|
||||
} else if !modeop {
|
||||
ch.modes.Limit = 0
|
||||
} else {
|
||||
logging.Warn("Channel.ParseModes(): not enough arguments to "+
|
||||
"process MODE %s %s%c", ch.name, modestr, m)
|
||||
}
|
||||
case 'q', 'a', 'o', 'h', 'v':
|
||||
if len(modeargs) != 0 {
|
||||
if nk, ok := ch.lookup[modeargs[0]]; ok {
|
||||
cp := ch.nicks[nk]
|
||||
switch m {
|
||||
case 'q':
|
||||
cp.Owner = modeop
|
||||
case 'a':
|
||||
cp.Admin = modeop
|
||||
case 'o':
|
||||
cp.Op = modeop
|
||||
case 'h':
|
||||
cp.HalfOp = modeop
|
||||
case 'v':
|
||||
cp.Voice = modeop
|
||||
}
|
||||
modeargs = modeargs[1:]
|
||||
} else {
|
||||
logging.Warn("Channel.ParseModes(): untracked nick %s "+
|
||||
"received MODE on channel %s", modeargs[0], ch.name)
|
||||
}
|
||||
} else {
|
||||
logging.Warn("Channel.ParseModes(): not enough arguments to "+
|
||||
"process MODE %s %s%c", ch.name, modestr, m)
|
||||
}
|
||||
default:
|
||||
logging.Info("Channel.ParseModes(): unknown mode char %c", m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if the Nick is associated with the Channel
|
||||
func (ch *Channel) IsOn(nk string) (*ChanPrivs, bool) {
|
||||
cp, ok := ch.Nicks[nk]
|
||||
return cp, ok
|
||||
}
|
||||
|
||||
// Test Channel equality.
|
||||
func (ch *Channel) Equals(other *Channel) bool {
|
||||
return reflect.DeepEqual(ch, other)
|
||||
}
|
||||
|
||||
// Duplicates a ChanMode struct.
|
||||
func (cm *ChanMode) Copy() *ChanMode {
|
||||
if cm == nil { return nil }
|
||||
c := *cm
|
||||
return &c
|
||||
}
|
||||
|
||||
// Test ChanMode equality.
|
||||
func (cm *ChanMode) Equals(other *ChanMode) bool {
|
||||
return reflect.DeepEqual(cm, other)
|
||||
}
|
||||
|
||||
// Duplicates a ChanPrivs struct.
|
||||
func (cp *ChanPrivs) Copy() *ChanPrivs {
|
||||
if cp == nil { return nil }
|
||||
c := *cp
|
||||
return &c
|
||||
}
|
||||
|
||||
// Test ChanPrivs equality.
|
||||
func (cp *ChanPrivs) Equals(other *ChanPrivs) bool {
|
||||
return reflect.DeepEqual(cp, other)
|
||||
}
|
||||
|
||||
// Returns a string representing the channel. Looks like:
|
||||
// Channel: <channel name> e.g. #moo
|
||||
// Topic: <channel topic> e.g. Discussing the merits of cows!
|
||||
// Mode: <channel modes> e.g. +nsti
|
||||
// Nicks:
|
||||
// <nick>: <privs> e.g. CowMaster: +o
|
||||
// ...
|
||||
func (ch *Channel) String() string {
|
||||
str := "Channel: " + ch.Name + "\n\t"
|
||||
str += "Topic: " + ch.Topic + "\n\t"
|
||||
str += "Modes: " + ch.Modes.String() + "\n\t"
|
||||
str += "Nicks: \n"
|
||||
for nk, cp := range ch.Nicks {
|
||||
str += "\t\t" + nk + ": " + cp.String() + "\n"
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
func (ch *channel) String() string {
|
||||
return ch.Channel().String()
|
||||
}
|
||||
|
||||
// Returns a string representing the channel modes. Looks like:
|
||||
// +npk key
|
||||
func (cm *ChanMode) String() string {
|
||||
str := "+"
|
||||
a := make([]string, 0)
|
||||
v := reflect.Indirect(reflect.ValueOf(cm))
|
||||
t := v.Type()
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
switch f := v.Field(i); f.Kind() {
|
||||
case reflect.Bool:
|
||||
if f.Bool() {
|
||||
str += ChanModeToString[t.Field(i).Name]
|
||||
}
|
||||
case reflect.String:
|
||||
if f.String() != "" {
|
||||
str += ChanModeToString[t.Field(i).Name]
|
||||
a = append(a, f.String())
|
||||
}
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
if f.Int() != 0 {
|
||||
str += ChanModeToString[t.Field(i).Name]
|
||||
a = append(a, strconv.FormatInt(f.Int(), 10))
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, s := range a {
|
||||
if s != "" {
|
||||
str += " " + s
|
||||
}
|
||||
}
|
||||
if str == "+" {
|
||||
str = "No modes set"
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// Returns a string representing the channel privileges. Looks like:
|
||||
// +o
|
||||
func (cp *ChanPrivs) String() string {
|
||||
str := "+"
|
||||
v := reflect.Indirect(reflect.ValueOf(cp))
|
||||
t := v.Type()
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
switch f := v.Field(i); f.Kind() {
|
||||
// only bools here at the mo too!
|
||||
case reflect.Bool:
|
||||
if f.Bool() {
|
||||
str += ChanPrivToString[t.Field(i).Name]
|
||||
}
|
||||
}
|
||||
}
|
||||
if str == "+" {
|
||||
str = "No modes set"
|
||||
}
|
||||
return str
|
||||
}
|
201
vendor/github.com/fluffle/goirc/state/mock_tracker.go
generated
vendored
Normal file
201
vendor/github.com/fluffle/goirc/state/mock_tracker.go
generated
vendored
Normal file
@@ -0,0 +1,201 @@
|
||||
// Automatically generated by MockGen. DO NOT EDIT!
|
||||
// Source: tracker.go
|
||||
|
||||
package state
|
||||
|
||||
import (
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
// Mock of Tracker interface
|
||||
type MockTracker struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *_MockTrackerRecorder
|
||||
}
|
||||
|
||||
// Recorder for MockTracker (not exported)
|
||||
type _MockTrackerRecorder struct {
|
||||
mock *MockTracker
|
||||
}
|
||||
|
||||
func NewMockTracker(ctrl *gomock.Controller) *MockTracker {
|
||||
mock := &MockTracker{ctrl: ctrl}
|
||||
mock.recorder = &_MockTrackerRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
func (_m *MockTracker) EXPECT() *_MockTrackerRecorder {
|
||||
return _m.recorder
|
||||
}
|
||||
|
||||
func (_m *MockTracker) NewNick(nick string) *Nick {
|
||||
ret := _m.ctrl.Call(_m, "NewNick", nick)
|
||||
ret0, _ := ret[0].(*Nick)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) NewNick(arg0 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "NewNick", arg0)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) GetNick(nick string) *Nick {
|
||||
ret := _m.ctrl.Call(_m, "GetNick", nick)
|
||||
ret0, _ := ret[0].(*Nick)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) GetNick(arg0 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetNick", arg0)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) ReNick(old string, neu string) *Nick {
|
||||
ret := _m.ctrl.Call(_m, "ReNick", old, neu)
|
||||
ret0, _ := ret[0].(*Nick)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) ReNick(arg0, arg1 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "ReNick", arg0, arg1)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) DelNick(nick string) *Nick {
|
||||
ret := _m.ctrl.Call(_m, "DelNick", nick)
|
||||
ret0, _ := ret[0].(*Nick)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) DelNick(arg0 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "DelNick", arg0)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) NickInfo(nick string, ident string, host string, name string) *Nick {
|
||||
ret := _m.ctrl.Call(_m, "NickInfo", nick, ident, host, name)
|
||||
ret0, _ := ret[0].(*Nick)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) NickInfo(arg0, arg1, arg2, arg3 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "NickInfo", arg0, arg1, arg2, arg3)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) NickModes(nick string, modestr string) *Nick {
|
||||
ret := _m.ctrl.Call(_m, "NickModes", nick, modestr)
|
||||
ret0, _ := ret[0].(*Nick)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) NickModes(arg0, arg1 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "NickModes", arg0, arg1)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) NewChannel(channel string) *Channel {
|
||||
ret := _m.ctrl.Call(_m, "NewChannel", channel)
|
||||
ret0, _ := ret[0].(*Channel)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) NewChannel(arg0 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "NewChannel", arg0)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) GetChannel(channel string) *Channel {
|
||||
ret := _m.ctrl.Call(_m, "GetChannel", channel)
|
||||
ret0, _ := ret[0].(*Channel)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) GetChannel(arg0 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetChannel", arg0)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) DelChannel(channel string) *Channel {
|
||||
ret := _m.ctrl.Call(_m, "DelChannel", channel)
|
||||
ret0, _ := ret[0].(*Channel)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) DelChannel(arg0 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "DelChannel", arg0)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) Topic(channel string, topic string) *Channel {
|
||||
ret := _m.ctrl.Call(_m, "Topic", channel, topic)
|
||||
ret0, _ := ret[0].(*Channel)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) Topic(arg0, arg1 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "Topic", arg0, arg1)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) ChannelModes(channel string, modestr string, modeargs ...string) *Channel {
|
||||
_s := []interface{}{channel, modestr}
|
||||
for _, _x := range modeargs {
|
||||
_s = append(_s, _x)
|
||||
}
|
||||
ret := _m.ctrl.Call(_m, "ChannelModes", _s...)
|
||||
ret0, _ := ret[0].(*Channel)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) ChannelModes(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call {
|
||||
_s := append([]interface{}{arg0, arg1}, arg2...)
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "ChannelModes", _s...)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) Me() *Nick {
|
||||
ret := _m.ctrl.Call(_m, "Me")
|
||||
ret0, _ := ret[0].(*Nick)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) Me() *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "Me")
|
||||
}
|
||||
|
||||
func (_m *MockTracker) IsOn(channel string, nick string) (*ChanPrivs, bool) {
|
||||
ret := _m.ctrl.Call(_m, "IsOn", channel, nick)
|
||||
ret0, _ := ret[0].(*ChanPrivs)
|
||||
ret1, _ := ret[1].(bool)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) IsOn(arg0, arg1 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "IsOn", arg0, arg1)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) Associate(channel string, nick string) *ChanPrivs {
|
||||
ret := _m.ctrl.Call(_m, "Associate", channel, nick)
|
||||
ret0, _ := ret[0].(*ChanPrivs)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) Associate(arg0, arg1 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "Associate", arg0, arg1)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) Dissociate(channel string, nick string) {
|
||||
_m.ctrl.Call(_m, "Dissociate", channel, nick)
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) Dissociate(arg0, arg1 interface{}) *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "Dissociate", arg0, arg1)
|
||||
}
|
||||
|
||||
func (_m *MockTracker) Wipe() {
|
||||
_m.ctrl.Call(_m, "Wipe")
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) Wipe() *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "Wipe")
|
||||
}
|
||||
|
||||
func (_m *MockTracker) String() string {
|
||||
ret := _m.ctrl.Call(_m, "String")
|
||||
ret0, _ := ret[0].(string)
|
||||
return ret0
|
||||
}
|
||||
|
||||
func (_mr *_MockTrackerRecorder) String() *gomock.Call {
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "String")
|
||||
}
|
200
vendor/github.com/fluffle/goirc/state/nick.go
generated
vendored
Normal file
200
vendor/github.com/fluffle/goirc/state/nick.go
generated
vendored
Normal file
@@ -0,0 +1,200 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"github.com/fluffle/goirc/logging"
|
||||
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// A Nick is returned from the state tracker and contains
|
||||
// a copy of the nick state at a particular time.
|
||||
type Nick struct {
|
||||
Nick, Ident, Host, Name string
|
||||
Modes *NickMode
|
||||
Channels map[string]*ChanPrivs
|
||||
}
|
||||
|
||||
// Internal bookkeeping struct for nicks.
|
||||
type nick struct {
|
||||
nick, ident, host, name string
|
||||
modes *NickMode
|
||||
lookup map[string]*channel
|
||||
chans map[*channel]*ChanPrivs
|
||||
}
|
||||
|
||||
// A struct representing the modes of an IRC Nick (User Modes)
|
||||
// (again, only the ones we care about)
|
||||
//
|
||||
// This is only really useful for me, as we can't see other people's modes
|
||||
// without IRC operator privileges (and even then only on some IRCd's).
|
||||
type NickMode struct {
|
||||
// MODE +B, +i, +o, +w, +x, +z
|
||||
Bot, Invisible, Oper, WallOps, HiddenHost, SSL bool
|
||||
}
|
||||
|
||||
// Map *irc.NickMode fields to IRC mode characters and vice versa
|
||||
var StringToNickMode = map[string]string{}
|
||||
var NickModeToString = map[string]string{
|
||||
"Bot": "B",
|
||||
"Invisible": "i",
|
||||
"Oper": "o",
|
||||
"WallOps": "w",
|
||||
"HiddenHost": "x",
|
||||
"SSL": "z",
|
||||
}
|
||||
|
||||
func init() {
|
||||
for k, v := range NickModeToString {
|
||||
StringToNickMode[v] = k
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************\
|
||||
* nick methods for state management
|
||||
\******************************************************************************/
|
||||
|
||||
func newNick(n string) *nick {
|
||||
return &nick{
|
||||
nick: n,
|
||||
modes: new(NickMode),
|
||||
chans: make(map[*channel]*ChanPrivs),
|
||||
lookup: make(map[string]*channel),
|
||||
}
|
||||
}
|
||||
|
||||
// Returns a copy of the internal tracker nick state at this time.
|
||||
// Relies on tracker-level locking for concurrent access.
|
||||
func (nk *nick) Nick() *Nick {
|
||||
n := &Nick{
|
||||
Nick: nk.nick,
|
||||
Ident: nk.ident,
|
||||
Host: nk.host,
|
||||
Name: nk.name,
|
||||
Modes: nk.modes.Copy(),
|
||||
Channels: make(map[string]*ChanPrivs),
|
||||
}
|
||||
for c, cp := range nk.chans {
|
||||
n.Channels[c.name] = cp.Copy()
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (nk *nick) isOn(ch *channel) (*ChanPrivs, bool) {
|
||||
cp, ok := nk.chans[ch]
|
||||
return cp.Copy(), ok
|
||||
}
|
||||
|
||||
// Associates a Channel with a Nick.
|
||||
func (nk *nick) addChannel(ch *channel, cp *ChanPrivs) {
|
||||
if _, ok := nk.chans[ch]; !ok {
|
||||
nk.chans[ch] = cp
|
||||
nk.lookup[ch.name] = ch
|
||||
} else {
|
||||
logging.Warn("Nick.addChannel(): %s already on %s.", nk.nick, ch.name)
|
||||
}
|
||||
}
|
||||
|
||||
// Disassociates a Channel from a Nick.
|
||||
func (nk *nick) delChannel(ch *channel) {
|
||||
if _, ok := nk.chans[ch]; ok {
|
||||
delete(nk.chans, ch)
|
||||
delete(nk.lookup, ch.name)
|
||||
} else {
|
||||
logging.Warn("Nick.delChannel(): %s not on %s.", nk.nick, ch.name)
|
||||
}
|
||||
}
|
||||
|
||||
// Parse mode strings for a Nick.
|
||||
func (nk *nick) parseModes(modes string) {
|
||||
var modeop bool // true => add mode, false => remove mode
|
||||
for i := 0; i < len(modes); i++ {
|
||||
switch m := modes[i]; m {
|
||||
case '+':
|
||||
modeop = true
|
||||
case '-':
|
||||
modeop = false
|
||||
case 'B':
|
||||
nk.modes.Bot = modeop
|
||||
case 'i':
|
||||
nk.modes.Invisible = modeop
|
||||
case 'o':
|
||||
nk.modes.Oper = modeop
|
||||
case 'w':
|
||||
nk.modes.WallOps = modeop
|
||||
case 'x':
|
||||
nk.modes.HiddenHost = modeop
|
||||
case 'z':
|
||||
nk.modes.SSL = modeop
|
||||
default:
|
||||
logging.Info("Nick.ParseModes(): unknown mode char %c", m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if the Nick is associated with the Channel.
|
||||
func (nk *Nick) IsOn(ch string) (*ChanPrivs, bool) {
|
||||
cp, ok := nk.Channels[ch]
|
||||
return cp, ok
|
||||
}
|
||||
|
||||
// Tests Nick equality.
|
||||
func (nk *Nick) Equals(other *Nick) bool {
|
||||
return reflect.DeepEqual(nk, other)
|
||||
}
|
||||
|
||||
// Duplicates a NickMode struct.
|
||||
func (nm *NickMode) Copy() *NickMode {
|
||||
if nm == nil { return nil }
|
||||
n := *nm
|
||||
return &n
|
||||
}
|
||||
|
||||
// Tests NickMode equality.
|
||||
func (nm *NickMode) Equals(other *NickMode) bool {
|
||||
return reflect.DeepEqual(nm, other)
|
||||
}
|
||||
|
||||
// Returns a string representing the nick. Looks like:
|
||||
// Nick: <nick name> e.g. CowMaster
|
||||
// Hostmask: <ident@host> e.g. moo@cows.org
|
||||
// Real Name: <real name> e.g. Steve "CowMaster" Bush
|
||||
// Modes: <nick modes> e.g. +z
|
||||
// Channels:
|
||||
// <channel>: <privs> e.g. #moo: +o
|
||||
// ...
|
||||
func (nk *Nick) String() string {
|
||||
str := "Nick: " + nk.Nick + "\n\t"
|
||||
str += "Hostmask: " + nk.Ident + "@" + nk.Host + "\n\t"
|
||||
str += "Real Name: " + nk.Name + "\n\t"
|
||||
str += "Modes: " + nk.Modes.String() + "\n\t"
|
||||
str += "Channels: \n"
|
||||
for ch, cp := range nk.Channels {
|
||||
str += "\t\t" + ch + ": " + cp.String() + "\n"
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
func (nk *nick) String() string {
|
||||
return nk.Nick().String()
|
||||
}
|
||||
|
||||
// Returns a string representing the nick modes. Looks like:
|
||||
// +iwx
|
||||
func (nm *NickMode) String() string {
|
||||
str := "+"
|
||||
v := reflect.Indirect(reflect.ValueOf(nm))
|
||||
t := v.Type()
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
switch f := v.Field(i); f.Kind() {
|
||||
// only bools here at the mo!
|
||||
case reflect.Bool:
|
||||
if f.Bool() {
|
||||
str += NickModeToString[t.Field(i).Name]
|
||||
}
|
||||
}
|
||||
}
|
||||
if str == "+" {
|
||||
str = "No modes set"
|
||||
}
|
||||
return str
|
||||
}
|
366
vendor/github.com/fluffle/goirc/state/tracker.go
generated
vendored
Normal file
366
vendor/github.com/fluffle/goirc/state/tracker.go
generated
vendored
Normal file
@@ -0,0 +1,366 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"github.com/fluffle/goirc/logging"
|
||||
|
||||
"sync"
|
||||
)
|
||||
|
||||
// The state manager interface
|
||||
type Tracker interface {
|
||||
// Nick methods
|
||||
NewNick(nick string) *Nick
|
||||
GetNick(nick string) *Nick
|
||||
ReNick(old, neu string) *Nick
|
||||
DelNick(nick string) *Nick
|
||||
NickInfo(nick, ident, host, name string) *Nick
|
||||
NickModes(nick, modestr string) *Nick
|
||||
// Channel methods
|
||||
NewChannel(channel string) *Channel
|
||||
GetChannel(channel string) *Channel
|
||||
DelChannel(channel string) *Channel
|
||||
Topic(channel, topic string) *Channel
|
||||
ChannelModes(channel, modestr string, modeargs ...string) *Channel
|
||||
// Information about ME!
|
||||
Me() *Nick
|
||||
// And the tracking operations
|
||||
IsOn(channel, nick string) (*ChanPrivs, bool)
|
||||
Associate(channel, nick string) *ChanPrivs
|
||||
Dissociate(channel, nick string)
|
||||
Wipe()
|
||||
// The state tracker can output a debugging string
|
||||
String() string
|
||||
}
|
||||
|
||||
// ... and a struct to implement it ...
|
||||
type stateTracker struct {
|
||||
// Map of channels we're on
|
||||
chans map[string]*channel
|
||||
// Map of nicks we know about
|
||||
nicks map[string]*nick
|
||||
|
||||
// We need to keep state on who we are :-)
|
||||
me *nick
|
||||
|
||||
// And we need to protect against data races *cough*.
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
var _ Tracker = (*stateTracker)(nil)
|
||||
|
||||
// ... and a constructor to make it ...
|
||||
func NewTracker(mynick string) *stateTracker {
|
||||
st := &stateTracker{
|
||||
chans: make(map[string]*channel),
|
||||
nicks: make(map[string]*nick),
|
||||
}
|
||||
st.me = newNick(mynick)
|
||||
st.nicks[mynick] = st.me
|
||||
return st
|
||||
}
|
||||
|
||||
// ... and a method to wipe the state clean.
|
||||
func (st *stateTracker) Wipe() {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
// Deleting all the channels implicitly deletes every nick but me.
|
||||
for _, ch := range st.chans {
|
||||
st.delChannel(ch)
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************\
|
||||
* tracker methods to create/look up nicks/channels
|
||||
\******************************************************************************/
|
||||
|
||||
// Creates a new nick, initialises it, and stores it so it
|
||||
// can be properly tracked for state management purposes.
|
||||
func (st *stateTracker) NewNick(n string) *Nick {
|
||||
if n == "" {
|
||||
logging.Warn("Tracker.NewNick(): Not tracking empty nick.")
|
||||
return nil
|
||||
}
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
if _, ok := st.nicks[n]; ok {
|
||||
logging.Warn("Tracker.NewNick(): %s already tracked.", n)
|
||||
return nil
|
||||
}
|
||||
st.nicks[n] = newNick(n)
|
||||
return st.nicks[n].Nick()
|
||||
}
|
||||
|
||||
// Returns a nick for the nick n, if we're tracking it.
|
||||
func (st *stateTracker) GetNick(n string) *Nick {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
if nk, ok := st.nicks[n]; ok {
|
||||
return nk.Nick()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Signals to the tracker that a nick should be tracked
|
||||
// under a "neu" nick rather than the old one.
|
||||
func (st *stateTracker) ReNick(old, neu string) *Nick {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
nk, ok := st.nicks[old]
|
||||
if !ok {
|
||||
logging.Warn("Tracker.ReNick(): %s not tracked.", old)
|
||||
return nil
|
||||
}
|
||||
if _, ok := st.nicks[neu]; ok {
|
||||
logging.Warn("Tracker.ReNick(): %s already exists.", neu)
|
||||
return nil
|
||||
}
|
||||
|
||||
nk.nick = neu
|
||||
delete(st.nicks, old)
|
||||
st.nicks[neu] = nk
|
||||
for ch, _ := range nk.chans {
|
||||
// We also need to update the lookup maps of all the channels
|
||||
// the nick is on, to keep things in sync.
|
||||
delete(ch.lookup, old)
|
||||
ch.lookup[neu] = nk
|
||||
}
|
||||
return nk.Nick()
|
||||
}
|
||||
|
||||
// Removes a nick from being tracked.
|
||||
func (st *stateTracker) DelNick(n string) *Nick {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
if nk, ok := st.nicks[n]; ok {
|
||||
if nk == st.me {
|
||||
logging.Warn("Tracker.DelNick(): won't delete myself.")
|
||||
return nil
|
||||
}
|
||||
st.delNick(nk)
|
||||
return nk.Nick()
|
||||
}
|
||||
logging.Warn("Tracker.DelNick(): %s not tracked.", n)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (st *stateTracker) delNick(nk *nick) {
|
||||
// st.mu lock held by DelNick, DelChannel or Wipe
|
||||
if nk == st.me {
|
||||
// Shouldn't get here => internal state tracking code is fubar.
|
||||
logging.Error("Tracker.DelNick(): TRYING TO DELETE ME :-(")
|
||||
return
|
||||
}
|
||||
delete(st.nicks, nk.nick)
|
||||
for ch, _ := range nk.chans {
|
||||
nk.delChannel(ch)
|
||||
ch.delNick(nk)
|
||||
if len(ch.nicks) == 0 {
|
||||
// Deleting a nick from tracking shouldn't empty any channels as
|
||||
// *we* should be on the channel with them to be tracking them.
|
||||
logging.Error("Tracker.delNick(): deleting nick %s emptied "+
|
||||
"channel %s, this shouldn't happen!", nk.nick, ch.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sets ident, host and "real" name for the nick.
|
||||
func (st *stateTracker) NickInfo(n, ident, host, name string) *Nick {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
nk, ok := st.nicks[n]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
nk.ident = ident
|
||||
nk.host = host
|
||||
nk.name = name
|
||||
return nk.Nick()
|
||||
}
|
||||
|
||||
// Sets user modes for the nick.
|
||||
func (st *stateTracker) NickModes(n, modes string) *Nick {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
nk, ok := st.nicks[n]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
nk.parseModes(modes)
|
||||
return nk.Nick()
|
||||
}
|
||||
|
||||
// Creates a new Channel, initialises it, and stores it so it
|
||||
// can be properly tracked for state management purposes.
|
||||
func (st *stateTracker) NewChannel(c string) *Channel {
|
||||
if c == "" {
|
||||
logging.Warn("Tracker.NewChannel(): Not tracking empty channel.")
|
||||
return nil
|
||||
}
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
if _, ok := st.chans[c]; ok {
|
||||
logging.Warn("Tracker.NewChannel(): %s already tracked.", c)
|
||||
return nil
|
||||
}
|
||||
st.chans[c] = newChannel(c)
|
||||
return st.chans[c].Channel()
|
||||
}
|
||||
|
||||
// Returns a Channel for the channel c, if we're tracking it.
|
||||
func (st *stateTracker) GetChannel(c string) *Channel {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
if ch, ok := st.chans[c]; ok {
|
||||
return ch.Channel()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Removes a Channel from being tracked.
|
||||
func (st *stateTracker) DelChannel(c string) *Channel {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
if ch, ok := st.chans[c]; ok {
|
||||
st.delChannel(ch)
|
||||
return ch.Channel()
|
||||
}
|
||||
logging.Warn("Tracker.DelChannel(): %s not tracked.", c)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (st *stateTracker) delChannel(ch *channel) {
|
||||
// st.mu lock held by DelChannel or Wipe
|
||||
delete(st.chans, ch.name)
|
||||
for nk, _ := range ch.nicks {
|
||||
ch.delNick(nk)
|
||||
nk.delChannel(ch)
|
||||
if len(nk.chans) == 0 && nk != st.me {
|
||||
// We're no longer in any channels with this nick.
|
||||
st.delNick(nk)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sets the topic of a channel.
|
||||
func (st *stateTracker) Topic(c, topic string) *Channel {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
ch, ok := st.chans[c]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
ch.topic = topic
|
||||
return ch.Channel()
|
||||
}
|
||||
|
||||
// Sets modes for a channel, including privileges like +o.
|
||||
func (st *stateTracker) ChannelModes(c, modes string, args ...string) *Channel {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
ch, ok := st.chans[c]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
ch.parseModes(modes, args...)
|
||||
return ch.Channel()
|
||||
}
|
||||
|
||||
// Returns the Nick the state tracker thinks is Me.
|
||||
func (st *stateTracker) Me() *Nick {
|
||||
return st.me.Nick()
|
||||
}
|
||||
|
||||
// Returns true if both the channel c and the nick n are tracked
|
||||
// and the nick is associated with the channel.
|
||||
func (st *stateTracker) IsOn(c, n string) (*ChanPrivs, bool) {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
nk, nok := st.nicks[n]
|
||||
ch, cok := st.chans[c]
|
||||
if nok && cok {
|
||||
return nk.isOn(ch)
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Associates an already known nick with an already known channel.
|
||||
func (st *stateTracker) Associate(c, n string) *ChanPrivs {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
nk, nok := st.nicks[n]
|
||||
ch, cok := st.chans[c]
|
||||
|
||||
if !cok {
|
||||
// As we can implicitly delete both nicks and channels from being
|
||||
// tracked by dissociating one from the other, we should verify that
|
||||
// we're not being passed an old Nick or Channel.
|
||||
logging.Error("Tracker.Associate(): channel %s not found in "+
|
||||
"internal state.", c)
|
||||
return nil
|
||||
} else if !nok {
|
||||
logging.Error("Tracker.Associate(): nick %s not found in "+
|
||||
"internal state.", n)
|
||||
return nil
|
||||
} else if _, ok := nk.isOn(ch); ok {
|
||||
logging.Warn("Tracker.Associate(): %s already on %s.",
|
||||
nk, ch)
|
||||
return nil
|
||||
}
|
||||
cp := new(ChanPrivs)
|
||||
ch.addNick(nk, cp)
|
||||
nk.addChannel(ch, cp)
|
||||
return cp.Copy()
|
||||
}
|
||||
|
||||
// Dissociates an already known nick from an already known channel.
|
||||
// Does some tidying up to stop tracking nicks we're no longer on
|
||||
// any common channels with, and channels we're no longer on.
|
||||
func (st *stateTracker) Dissociate(c, n string) {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
nk, nok := st.nicks[n]
|
||||
ch, cok := st.chans[c]
|
||||
|
||||
if !cok {
|
||||
// As we can implicitly delete both nicks and channels from being
|
||||
// tracked by dissociating one from the other, we should verify that
|
||||
// we're not being passed an old Nick or Channel.
|
||||
logging.Error("Tracker.Dissociate(): channel %s not found in "+
|
||||
"internal state.", c)
|
||||
} else if !nok {
|
||||
logging.Error("Tracker.Dissociate(): nick %s not found in "+
|
||||
"internal state.", n)
|
||||
} else if _, ok := nk.isOn(ch); !ok {
|
||||
logging.Warn("Tracker.Dissociate(): %s not on %s.",
|
||||
nk.nick, ch.name)
|
||||
} else if nk == st.me {
|
||||
// I'm leaving the channel for some reason, so it won't be tracked.
|
||||
st.delChannel(ch)
|
||||
} else {
|
||||
// Remove the nick from the channel and the channel from the nick.
|
||||
ch.delNick(nk)
|
||||
nk.delChannel(ch)
|
||||
if len(nk.chans) == 0 {
|
||||
// We're no longer in any channels with this nick.
|
||||
st.delNick(nk)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (st *stateTracker) String() string {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
str := "GoIRC Channels\n"
|
||||
str += "--------------\n\n"
|
||||
for _, ch := range st.chans {
|
||||
str += ch.String() + "\n"
|
||||
}
|
||||
str += "GoIRC NickNames\n"
|
||||
str += "---------------\n\n"
|
||||
for _, n := range st.nicks {
|
||||
if n != st.me {
|
||||
str += n.String() + "\n"
|
||||
}
|
||||
}
|
||||
return str
|
||||
}
|
Reference in New Issue
Block a user