2011-10-12 21:59:52 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2013-09-27 21:19:40 +00:00
|
|
|
"github.com/fluffle/goirc/logging"
|
2014-05-20 19:57:04 +00:00
|
|
|
|
2011-10-12 21:59:52 +00:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2014-12-31 13:17:11 +00:00
|
|
|
// A Channel is returned from the state tracker and contains
|
|
|
|
// a copy of the channel state at a particular time.
|
2011-10-12 21:59:52 +00:00
|
|
|
type Channel struct {
|
|
|
|
Name, Topic string
|
|
|
|
Modes *ChanMode
|
2014-12-31 13:17:11 +00:00
|
|
|
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
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A struct representing the modes of an IRC Channel
|
|
|
|
// (the ones we care about, at least).
|
2013-01-10 21:22:19 +00:00
|
|
|
// http://www.unrealircd.com/files/docs/unreal32docs.html#userchannelmodes
|
2011-10-12 21:59:52 +00:00
|
|
|
type ChanMode struct {
|
|
|
|
// MODE +p, +s, +t, +n, +m
|
|
|
|
Private, Secret, ProtectedTopic, NoExternalMsg, Moderated bool
|
|
|
|
|
|
|
|
// MODE +i, +O, +z
|
|
|
|
InviteOnly, OperOnly, SSLOnly bool
|
|
|
|
|
2013-01-10 21:22:19 +00:00
|
|
|
// MODE +r, +Z
|
|
|
|
Registered, AllSSL bool
|
|
|
|
|
2011-10-12 21:59:52 +00:00
|
|
|
// 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",
|
2013-01-10 21:22:19 +00:00
|
|
|
"Registered": "r",
|
|
|
|
"AllSSL": "Z",
|
2011-10-12 21:59:52 +00:00
|
|
|
"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
|
|
|
|
\******************************************************************************/
|
|
|
|
|
2014-12-31 13:17:11 +00:00
|
|
|
func newChannel(name string) *channel {
|
|
|
|
return &channel{
|
|
|
|
name: name,
|
|
|
|
modes: new(ChanMode),
|
|
|
|
nicks: make(map[*nick]*ChanPrivs),
|
|
|
|
lookup: make(map[string]*nick),
|
2011-10-13 21:48:04 +00:00
|
|
|
}
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:11 +00:00
|
|
|
// 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
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:11 +00:00
|
|
|
func (ch *channel) isOn(nk *nick) (*ChanPrivs, bool) {
|
|
|
|
cp, ok := ch.nicks[nk]
|
|
|
|
return cp.Copy(), ok
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
|
|
|
|
2011-10-19 23:10:33 +00:00
|
|
|
// Associates a Nick with a Channel
|
2014-12-31 13:17:11 +00:00
|
|
|
func (ch *channel) addNick(nk *nick, cp *ChanPrivs) {
|
2011-10-19 23:10:33 +00:00
|
|
|
if _, ok := ch.nicks[nk]; !ok {
|
|
|
|
ch.nicks[nk] = cp
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.lookup[nk.nick] = nk
|
2011-10-22 22:57:22 +00:00
|
|
|
} else {
|
2014-12-31 13:17:11 +00:00
|
|
|
logging.Warn("Channel.addNick(): %s already on %s.", nk.nick, ch.name)
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-19 23:10:33 +00:00
|
|
|
// Disassociates a Nick from a Channel.
|
2014-12-31 13:17:11 +00:00
|
|
|
func (ch *channel) delNick(nk *nick) {
|
2011-10-19 23:10:33 +00:00
|
|
|
if _, ok := ch.nicks[nk]; ok {
|
2011-11-13 13:32:53 +00:00
|
|
|
delete(ch.nicks, nk)
|
2014-12-31 13:17:11 +00:00
|
|
|
delete(ch.lookup, nk.nick)
|
2011-10-22 22:57:22 +00:00
|
|
|
} else {
|
2014-12-31 13:17:11 +00:00
|
|
|
logging.Warn("Channel.delNick(): %s not on %s.", nk.nick, ch.name)
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses mode strings for a channel.
|
2014-12-31 13:17:11 +00:00
|
|
|
func (ch *channel) parseModes(modes string, modeargs ...string) {
|
2011-10-12 21:59:52 +00:00
|
|
|
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':
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.InviteOnly = modeop
|
2011-10-12 21:59:52 +00:00
|
|
|
case 'm':
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.Moderated = modeop
|
2011-10-12 21:59:52 +00:00
|
|
|
case 'n':
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.NoExternalMsg = modeop
|
2011-10-12 21:59:52 +00:00
|
|
|
case 'p':
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.Private = modeop
|
2013-01-10 21:22:19 +00:00
|
|
|
case 'r':
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.Registered = modeop
|
2011-10-12 21:59:52 +00:00
|
|
|
case 's':
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.Secret = modeop
|
2011-10-12 21:59:52 +00:00
|
|
|
case 't':
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.ProtectedTopic = modeop
|
2011-10-12 21:59:52 +00:00
|
|
|
case 'z':
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.SSLOnly = modeop
|
2013-01-10 21:22:19 +00:00
|
|
|
case 'Z':
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.AllSSL = modeop
|
2011-10-12 21:59:52 +00:00
|
|
|
case 'O':
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.OperOnly = modeop
|
2011-10-12 21:59:52 +00:00
|
|
|
case 'k':
|
|
|
|
if modeop && len(modeargs) != 0 {
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.Key, modeargs = modeargs[0], modeargs[1:]
|
2011-10-12 21:59:52 +00:00
|
|
|
} else if !modeop {
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.Key = ""
|
2011-10-12 21:59:52 +00:00
|
|
|
} else {
|
2013-01-23 22:33:01 +00:00
|
|
|
logging.Warn("Channel.ParseModes(): not enough arguments to "+
|
2014-12-31 13:17:11 +00:00
|
|
|
"process MODE %s %s%c", ch.name, modestr, m)
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
|
|
|
case 'l':
|
|
|
|
if modeop && len(modeargs) != 0 {
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.Limit, _ = strconv.Atoi(modeargs[0])
|
2011-10-12 21:59:52 +00:00
|
|
|
modeargs = modeargs[1:]
|
|
|
|
} else if !modeop {
|
2014-12-31 13:17:11 +00:00
|
|
|
ch.modes.Limit = 0
|
2011-10-12 21:59:52 +00:00
|
|
|
} else {
|
2013-01-23 22:33:01 +00:00
|
|
|
logging.Warn("Channel.ParseModes(): not enough arguments to "+
|
2014-12-31 13:17:11 +00:00
|
|
|
"process MODE %s %s%c", ch.name, modestr, m)
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
|
|
|
case 'q', 'a', 'o', 'h', 'v':
|
|
|
|
if len(modeargs) != 0 {
|
2011-11-13 13:32:53 +00:00
|
|
|
if nk, ok := ch.lookup[modeargs[0]]; ok {
|
2011-10-19 23:10:33 +00:00
|
|
|
cp := ch.nicks[nk]
|
2011-10-12 21:59:52 +00:00
|
|
|
switch m {
|
|
|
|
case 'q':
|
2011-10-19 23:10:33 +00:00
|
|
|
cp.Owner = modeop
|
2011-10-12 21:59:52 +00:00
|
|
|
case 'a':
|
2011-10-19 23:10:33 +00:00
|
|
|
cp.Admin = modeop
|
2011-10-12 21:59:52 +00:00
|
|
|
case 'o':
|
2011-10-19 23:10:33 +00:00
|
|
|
cp.Op = modeop
|
2011-10-12 21:59:52 +00:00
|
|
|
case 'h':
|
2011-10-19 23:10:33 +00:00
|
|
|
cp.HalfOp = modeop
|
2011-10-12 21:59:52 +00:00
|
|
|
case 'v':
|
2011-10-19 23:10:33 +00:00
|
|
|
cp.Voice = modeop
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
|
|
|
modeargs = modeargs[1:]
|
|
|
|
} else {
|
2013-01-23 22:33:01 +00:00
|
|
|
logging.Warn("Channel.ParseModes(): untracked nick %s "+
|
2014-12-31 13:17:11 +00:00
|
|
|
"received MODE on channel %s", modeargs[0], ch.name)
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-01-23 22:33:01 +00:00
|
|
|
logging.Warn("Channel.ParseModes(): not enough arguments to "+
|
2014-12-31 13:17:11 +00:00
|
|
|
"process MODE %s %s%c", ch.name, modestr, m)
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
2011-10-22 22:57:22 +00:00
|
|
|
default:
|
2013-01-23 22:33:01 +00:00
|
|
|
logging.Info("Channel.ParseModes(): unknown mode char %c", m)
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:11 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2014-05-20 19:57:04 +00:00
|
|
|
|
2014-12-31 13:17:11 +00:00
|
|
|
// Duplicates a ChanMode struct.
|
|
|
|
func (cm *ChanMode) Copy() *ChanMode {
|
2018-06-16 22:09:53 +00:00
|
|
|
if cm == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-12-31 13:17:11 +00:00
|
|
|
c := *cm
|
|
|
|
return &c
|
|
|
|
}
|
2014-05-20 19:57:04 +00:00
|
|
|
|
2014-12-31 13:17:11 +00:00
|
|
|
// Test ChanMode equality.
|
|
|
|
func (cm *ChanMode) Equals(other *ChanMode) bool {
|
|
|
|
return reflect.DeepEqual(cm, other)
|
2013-01-06 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:11 +00:00
|
|
|
// Duplicates a ChanPrivs struct.
|
|
|
|
func (cp *ChanPrivs) Copy() *ChanPrivs {
|
2018-06-16 22:09:53 +00:00
|
|
|
if cp == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-12-31 13:17:11 +00:00
|
|
|
c := *cp
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test ChanPrivs equality.
|
|
|
|
func (cp *ChanPrivs) Equals(other *ChanPrivs) bool {
|
|
|
|
return reflect.DeepEqual(cp, other)
|
2013-01-06 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
2011-10-12 21:59:52 +00:00
|
|
|
// 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"
|
2014-12-31 13:17:11 +00:00
|
|
|
for nk, cp := range ch.Nicks {
|
|
|
|
str += "\t\t" + nk + ": " + cp.String() + "\n"
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:11 +00:00
|
|
|
func (ch *channel) String() string {
|
|
|
|
return ch.Channel().String()
|
|
|
|
}
|
|
|
|
|
2011-10-12 21:59:52 +00:00
|
|
|
// 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]
|
2014-12-31 13:17:11 +00:00
|
|
|
a = append(a, strconv.FormatInt(f.Int(), 10))
|
2011-10-12 21:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
2011-10-19 23:10:33 +00:00
|
|
|
func (cp *ChanPrivs) String() string {
|
2011-10-12 21:59:52 +00:00
|
|
|
str := "+"
|
2011-10-19 23:10:33 +00:00
|
|
|
v := reflect.Indirect(reflect.ValueOf(cp))
|
2011-10-12 21:59:52 +00:00
|
|
|
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
|
|
|
|
}
|