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
|
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 {
if nm == nil {
return "No modes set"
}
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
}
|