2010-11-21 19:53:14 +00:00
|
|
|
package client
|
2009-12-17 17:22:31 +00:00
|
|
|
|
|
|
|
// Here you'll find the Channel and Nick structs
|
|
|
|
// as well as the internal state maintenance code for the handlers
|
|
|
|
|
|
|
|
import (
|
2009-12-17 21:30:18 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2010-11-04 01:22:49 +00:00
|
|
|
"strconv"
|
2009-12-17 17:22:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A struct representing an IRC channel
|
|
|
|
type Channel struct {
|
2009-12-17 21:30:18 +00:00
|
|
|
Name, Topic string
|
|
|
|
Modes *ChanMode
|
2009-12-18 22:39:22 +00:00
|
|
|
Nicks map[*Nick]*ChanPrivs
|
|
|
|
conn *Conn
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A struct representing an IRC nick
|
|
|
|
type Nick struct {
|
2009-12-17 21:30:18 +00:00
|
|
|
Nick, Ident, Host, Name string
|
|
|
|
Modes *NickMode
|
|
|
|
Channels map[*Channel]*ChanPrivs
|
|
|
|
conn *Conn
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A struct representing the modes of an IRC Channel
|
2009-12-18 22:39:22 +00:00
|
|
|
// (the ones we care about, at least).
|
|
|
|
//
|
|
|
|
// See the MODE handler in setupEvents() for details of how this is maintained.
|
2009-12-17 17:22:31 +00:00
|
|
|
type ChanMode struct {
|
|
|
|
// MODE +p, +s, +t, +n, +m
|
2009-12-17 21:30:18 +00:00
|
|
|
Private, Secret, ProtectedTopic, NoExternalMsg, Moderated bool
|
2009-12-18 22:39:22 +00:00
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// MODE +i, +O, +z
|
2009-12-17 21:30:18 +00:00
|
|
|
InviteOnly, OperOnly, SSLOnly bool
|
2009-12-18 22:39:22 +00:00
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// MODE +k
|
2009-12-17 21:30:18 +00:00
|
|
|
Key string
|
2009-12-18 22:39:22 +00:00
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// MODE +l
|
2009-12-17 21:30:18 +00:00
|
|
|
Limit int
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A struct representing the modes of an IRC Nick (User Modes)
|
|
|
|
// (again, only the ones we care about)
|
2009-12-18 22:39:22 +00:00
|
|
|
//
|
2009-12-17 17:22:31 +00:00
|
|
|
// This is only really useful for conn.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 +i, +o, +w, +x, +z
|
2009-12-17 21:30:18 +00:00
|
|
|
Invisible, Oper, WallOps, HiddenHost, SSL bool
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A struct representing the modes a Nick can have on a Channel
|
|
|
|
type ChanPrivs struct {
|
|
|
|
// MODE +q, +a, +o, +h, +v
|
2009-12-17 21:30:18 +00:00
|
|
|
Owner, Admin, Op, HalfOp, Voice bool
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* Conn methods to create/look up nicks/channels
|
|
|
|
\******************************************************************************/
|
2009-12-18 22:39:22 +00:00
|
|
|
|
|
|
|
// Creates a new *irc.Nick, initialises it, and stores it in *irc.Conn so it
|
|
|
|
// can be properly tracked for state management purposes.
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) NewNick(nick, ident, name, host string) *Nick {
|
2009-12-17 21:30:18 +00:00
|
|
|
n := &Nick{Nick: nick, Ident: ident, Name: name, Host: host, conn: conn}
|
|
|
|
n.initialise()
|
|
|
|
conn.nicks[n.Nick] = n
|
|
|
|
return n
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Returns an *irc.Nick for the nick n, if we're tracking it.
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) GetNick(n string) *Nick {
|
|
|
|
if nick, ok := conn.nicks[n]; ok {
|
|
|
|
return nick
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Creates a new *irc.Channel, initialises it, and stores it in *irc.Conn so it
|
|
|
|
// can be properly tracked for state management purposes.
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) NewChannel(c string) *Channel {
|
2009-12-17 21:30:18 +00:00
|
|
|
ch := &Channel{Name: c, conn: conn}
|
|
|
|
ch.initialise()
|
|
|
|
conn.chans[ch.Name] = ch
|
2009-12-17 17:22:31 +00:00
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Returns an *irc.Channel for the channel c, if we're tracking it.
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) GetChannel(c string) *Channel {
|
|
|
|
if ch, ok := conn.chans[c]; ok {
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2010-11-04 01:22:49 +00:00
|
|
|
// Parses mode strings for a channel
|
|
|
|
func (conn *Conn) ParseChannelModes(ch *Channel, 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 's':
|
|
|
|
ch.Modes.Secret = modeop
|
|
|
|
case 't':
|
|
|
|
ch.Modes.ProtectedTopic = modeop
|
|
|
|
case 'z':
|
|
|
|
ch.Modes.SSLOnly = modeop
|
|
|
|
case 'O':
|
|
|
|
ch.Modes.OperOnly = modeop
|
|
|
|
case 'k':
|
|
|
|
if len(modeargs) != 0 {
|
|
|
|
ch.Modes.Key, modeargs = modeargs[0], modeargs[1:len(modeargs)]
|
|
|
|
} else {
|
|
|
|
conn.error("irc.ParseChanModes(): buh? not enough arguments to process MODE %s %s%s", ch.Name, modestr, m)
|
|
|
|
}
|
|
|
|
case 'l':
|
|
|
|
if len(modeargs) != 0 {
|
|
|
|
ch.Modes.Limit, _ = strconv.Atoi(modeargs[0])
|
|
|
|
modeargs = modeargs[1:len(modeargs)]
|
|
|
|
} else {
|
|
|
|
conn.error("irc.ParseChanModes(): buh? not enough arguments to process MODE %s %s%s", ch.Name, modestr, m)
|
|
|
|
}
|
|
|
|
case 'q', 'a', 'o', 'h', 'v':
|
|
|
|
if len(modeargs) != 0 {
|
|
|
|
n := conn.GetNick(modeargs[0])
|
|
|
|
if p, ok := ch.Nicks[n]; ok && n != nil {
|
|
|
|
switch m {
|
|
|
|
case 'q':
|
|
|
|
p.Owner = modeop
|
|
|
|
case 'a':
|
|
|
|
p.Admin = modeop
|
|
|
|
case 'o':
|
|
|
|
p.Op = modeop
|
|
|
|
case 'h':
|
|
|
|
p.HalfOp = modeop
|
|
|
|
case 'v':
|
|
|
|
p.Voice = modeop
|
|
|
|
}
|
|
|
|
modeargs = modeargs[1:len(modeargs)]
|
|
|
|
} else {
|
|
|
|
conn.error("irc.ParseChanModes(): MODE %s %s%s %s: buh? state tracking failure.", ch.Name, modestr, m, modeargs[0])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
conn.error("irc.ParseChanModes(): buh? not enough arguments to process MODE %s %s%s", ch.Name, modestr, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse mode strings for a nick
|
|
|
|
func (conn *Conn) ParseNickModes(n *Nick, 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 'i':
|
|
|
|
n.Modes.Invisible = modeop
|
|
|
|
case 'o':
|
|
|
|
n.Modes.Oper = modeop
|
|
|
|
case 'w':
|
|
|
|
n.Modes.WallOps = modeop
|
|
|
|
case 'x':
|
|
|
|
n.Modes.HiddenHost = modeop
|
|
|
|
case 'z':
|
|
|
|
n.Modes.SSL = modeop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
/******************************************************************************\
|
|
|
|
* Channel methods for state management
|
|
|
|
\******************************************************************************/
|
2009-12-18 22:39:22 +00:00
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
func (ch *Channel) initialise() {
|
2009-12-17 21:30:18 +00:00
|
|
|
ch.Modes = new(ChanMode)
|
|
|
|
ch.Nicks = make(map[*Nick]*ChanPrivs)
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Associates an *irc.Nick with an *irc.Channel using a shared *irc.ChanPrivs
|
2009-12-17 17:22:31 +00:00
|
|
|
func (ch *Channel) AddNick(n *Nick) {
|
|
|
|
if _, ok := ch.Nicks[n]; !ok {
|
2009-12-17 21:30:18 +00:00
|
|
|
ch.Nicks[n] = new(ChanPrivs)
|
|
|
|
n.Channels[ch] = ch.Nicks[n]
|
2009-12-17 17:22:31 +00:00
|
|
|
} else {
|
2009-12-17 21:30:18 +00:00
|
|
|
ch.conn.error("irc.Channel.AddNick() warning: trying to add already-present nick %s to channel %s", n.Nick, ch.Name)
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Disassociates an *irc.Nick from an *irc.Channel. Will call ch.Delete() if
|
|
|
|
// the *irc.Nick being removed is the connection's nick. Will also call
|
|
|
|
// n.DelChannel(ch) to remove the association from the perspective of *irc.Nick.
|
2009-12-17 17:22:31 +00:00
|
|
|
func (ch *Channel) DelNick(n *Nick) {
|
|
|
|
if _, ok := ch.Nicks[n]; ok {
|
|
|
|
if n == n.conn.Me {
|
|
|
|
// we're leaving the channel, so remove all state we have about it
|
2009-12-17 21:30:18 +00:00
|
|
|
ch.Delete()
|
2009-12-17 17:22:31 +00:00
|
|
|
} else {
|
2009-12-17 21:30:18 +00:00
|
|
|
ch.Nicks[n] = nil, false
|
|
|
|
n.DelChannel(ch)
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
} // no else here ...
|
|
|
|
// we call Channel.DelNick() and Nick.DelChan() from each other to ensure
|
|
|
|
// consistency, and this would mean spewing an error message every delete
|
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Stops the channel from being tracked by state tracking handlers. Also calls
|
|
|
|
// n.DelChannel(ch) for all nicks that are associated with the channel.
|
2009-12-17 17:22:31 +00:00
|
|
|
func (ch *Channel) Delete() {
|
|
|
|
for n, _ := range ch.Nicks {
|
2009-12-17 21:30:18 +00:00
|
|
|
n.DelChannel(ch)
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
ch.conn.chans[ch.Name] = nil, false
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* Nick methods for state management
|
|
|
|
\******************************************************************************/
|
|
|
|
func (n *Nick) initialise() {
|
2009-12-17 21:30:18 +00:00
|
|
|
n.Modes = new(NickMode)
|
|
|
|
n.Channels = make(map[*Channel]*ChanPrivs)
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Associates an *irc.Channel with an *irc.Nick using a shared *irc.ChanPrivs
|
|
|
|
//
|
|
|
|
// Very slightly different to irc.Channel.AddNick() in that it tests for a
|
|
|
|
// pre-existing association within the *irc.Nick object rather than the
|
|
|
|
// *irc.Channel object before associating the two.
|
2009-12-17 17:22:31 +00:00
|
|
|
func (n *Nick) AddChannel(ch *Channel) {
|
|
|
|
if _, ok := n.Channels[ch]; !ok {
|
2009-12-17 21:30:18 +00:00
|
|
|
ch.Nicks[n] = new(ChanPrivs)
|
|
|
|
n.Channels[ch] = ch.Nicks[n]
|
2009-12-17 17:22:31 +00:00
|
|
|
} else {
|
2009-12-17 21:30:18 +00:00
|
|
|
n.conn.error("irc.Nick.AddChannel() warning: trying to add already-present channel %s to nick %s", ch.Name, n.Nick)
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Disassociates an *irc.Channel from an *irc.Nick. Will call n.Delete() if
|
|
|
|
// the *irc.Nick is no longer on any channels we are tracking. Will also call
|
|
|
|
// ch.DelNick(n) to remove the association from the perspective of *irc.Channel.
|
2009-12-17 17:22:31 +00:00
|
|
|
func (n *Nick) DelChannel(ch *Channel) {
|
|
|
|
if _, ok := n.Channels[ch]; ok {
|
2009-12-17 21:30:18 +00:00
|
|
|
n.Channels[ch] = nil, false
|
|
|
|
ch.DelNick(n)
|
2009-12-17 17:22:31 +00:00
|
|
|
if len(n.Channels) == 0 {
|
|
|
|
// nick is no longer in any channels we inhabit, stop tracking it
|
2009-12-17 21:30:18 +00:00
|
|
|
n.Delete()
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Signals to the tracking code that the *irc.Nick object should be tracked
|
|
|
|
// under a "neu" nick rather than the old one.
|
2009-12-17 17:22:31 +00:00
|
|
|
func (n *Nick) ReNick(neu string) {
|
2009-12-17 21:30:18 +00:00
|
|
|
n.conn.nicks[n.Nick] = nil, false
|
|
|
|
n.Nick = neu
|
|
|
|
n.conn.nicks[n.Nick] = n
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Stops the nick from being tracked by state tracking handlers. Also calls
|
|
|
|
// ch.DelNick(n) for all nicks that are associated with the channel.
|
2009-12-17 17:22:31 +00:00
|
|
|
func (n *Nick) Delete() {
|
|
|
|
// we don't ever want to remove *our* nick from conn.nicks...
|
|
|
|
if n != n.conn.Me {
|
|
|
|
for ch, _ := range n.Channels {
|
2009-12-17 21:30:18 +00:00
|
|
|
ch.DelNick(n)
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
n.conn.nicks[n.Nick] = nil, false
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* String() methods for all structs in this file for ease of debugging.
|
|
|
|
\******************************************************************************/
|
2009-12-18 22:39:22 +00:00
|
|
|
|
|
|
|
// Map *irc.ChanMode fields to IRC mode characters
|
2009-12-17 17:22:31 +00:00
|
|
|
var ChanModeToString = map[string]string{
|
2010-11-04 00:25:46 +00:00
|
|
|
"Private": "p",
|
|
|
|
"Secret": "s",
|
2009-12-17 21:30:18 +00:00
|
|
|
"ProtectedTopic": "t",
|
2010-11-04 00:25:46 +00:00
|
|
|
"NoExternalMsg": "n",
|
|
|
|
"Moderated": "m",
|
|
|
|
"InviteOnly": "i",
|
|
|
|
"OperOnly": "O",
|
|
|
|
"SSLOnly": "z",
|
|
|
|
"Key": "k",
|
|
|
|
"Limit": "l",
|
2009-12-17 21:30:18 +00:00
|
|
|
}
|
2009-12-18 22:39:22 +00:00
|
|
|
|
|
|
|
// Map *irc.NickMode fields to IRC mode characters
|
2009-12-17 17:22:31 +00:00
|
|
|
var NickModeToString = map[string]string{
|
2010-11-04 00:25:46 +00:00
|
|
|
"Invisible": "i",
|
|
|
|
"Oper": "o",
|
|
|
|
"WallOps": "w",
|
2009-12-17 21:30:18 +00:00
|
|
|
"HiddenHost": "x",
|
2010-11-04 00:25:46 +00:00
|
|
|
"SSL": "z",
|
2009-12-17 21:30:18 +00:00
|
|
|
}
|
2009-12-18 22:39:22 +00:00
|
|
|
|
|
|
|
// Map *irc.ChanPrivs fields to IRC mode characters
|
2009-12-17 17:22:31 +00:00
|
|
|
var ChanPrivToString = map[string]string{
|
2010-11-04 00:25:46 +00:00
|
|
|
"Owner": "q",
|
|
|
|
"Admin": "a",
|
|
|
|
"Op": "o",
|
2009-12-17 21:30:18 +00:00
|
|
|
"HalfOp": "h",
|
2010-11-04 00:25:46 +00:00
|
|
|
"Voice": "v",
|
2009-12-17 21:30:18 +00:00
|
|
|
}
|
2009-12-18 22:39:22 +00:00
|
|
|
|
|
|
|
// Map *irc.ChanPrivs fields to the symbols used to represent these modes
|
|
|
|
// in NAMES and WHOIS responses
|
2009-12-17 17:22:31 +00:00
|
|
|
var ChanPrivToModeChar = map[string]byte{
|
2010-11-04 00:25:46 +00:00
|
|
|
"Owner": '~',
|
|
|
|
"Admin": '&',
|
|
|
|
"Op": '@',
|
2009-12-17 21:30:18 +00:00
|
|
|
"HalfOp": '%',
|
2010-11-04 00:25:46 +00:00
|
|
|
"Voice": '+',
|
2009-12-17 21:30:18 +00:00
|
|
|
}
|
2009-12-18 22:39:22 +00:00
|
|
|
|
|
|
|
// Reverse mappings of the above datastructures
|
2009-12-17 21:30:18 +00:00
|
|
|
var StringToChanMode, StringToNickMode, StringToChanPriv map[string]string
|
|
|
|
var ModeCharToChanPriv map[byte]string
|
2009-12-17 17:22:31 +00:00
|
|
|
|
|
|
|
// Init function to fill in reverse mappings for *toString constants etc.
|
|
|
|
func init() {
|
2009-12-17 21:30:18 +00:00
|
|
|
StringToChanMode = make(map[string]string)
|
|
|
|
for k, v := range ChanModeToString {
|
|
|
|
StringToChanMode[v] = k
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
StringToNickMode = make(map[string]string)
|
|
|
|
for k, v := range NickModeToString {
|
|
|
|
StringToNickMode[v] = k
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
StringToChanPriv = make(map[string]string)
|
|
|
|
for k, v := range ChanPrivToString {
|
|
|
|
StringToChanPriv[v] = k
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
ModeCharToChanPriv = make(map[byte]string)
|
|
|
|
for k, v := range ChanPrivToModeChar {
|
|
|
|
ModeCharToChanPriv[v] = k
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +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
|
|
|
|
// ...
|
2009-12-17 17:22:31 +00:00
|
|
|
func (ch *Channel) String() string {
|
2009-12-17 21:30:18 +00:00
|
|
|
str := "Channel: " + ch.Name + "\n\t"
|
|
|
|
str += "Topic: " + ch.Topic + "\n\t"
|
|
|
|
str += "Modes: " + ch.Modes.String() + "\n\t"
|
|
|
|
str += "Nicks: \n"
|
2009-12-17 17:22:31 +00:00
|
|
|
for n, p := range ch.Nicks {
|
2009-12-17 21:30:18 +00:00
|
|
|
str += "\t\t" + n.Nick + ": " + p.String() + "\n"
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
return str
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// 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
|
|
|
|
// ...
|
2009-12-17 17:22:31 +00:00
|
|
|
func (n *Nick) String() string {
|
2009-12-17 21:30:18 +00:00
|
|
|
str := "Nick: " + n.Nick + "\n\t"
|
|
|
|
str += "Hostmask: " + n.Ident + "@" + n.Host + "\n\t"
|
|
|
|
str += "Real Name: " + n.Name + "\n\t"
|
|
|
|
str += "Modes: " + n.Modes.String() + "\n\t"
|
|
|
|
str += "Channels: \n"
|
2009-12-17 17:22:31 +00:00
|
|
|
for ch, p := range n.Channels {
|
2009-12-17 21:30:18 +00:00
|
|
|
str += "\t\t" + ch.Name + ": " + p.String() + "\n"
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
return str
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Returns a string representing the channel modes. Looks like:
|
|
|
|
// +npk key
|
2009-12-17 17:22:31 +00:00
|
|
|
func (cm *ChanMode) String() string {
|
2009-12-17 21:30:18 +00:00
|
|
|
str := "+"
|
|
|
|
a := make([]string, 2)
|
2011-07-18 08:14:58 +00:00
|
|
|
v := reflect.Indirect(reflect.ValueOf(cm))
|
|
|
|
t := v.Type()
|
2009-12-17 17:22:31 +00:00
|
|
|
for i := 0; i < v.NumField(); i++ {
|
2011-07-18 08:14:58 +00:00
|
|
|
switch f := v.Field(i); f.Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
if f.Bool() {
|
2009-12-17 21:30:18 +00:00
|
|
|
str += ChanModeToString[t.Field(i).Name]
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2011-07-18 08:14:58 +00:00
|
|
|
case reflect.String:
|
|
|
|
if f.String() != "" {
|
2009-12-17 21:30:18 +00:00
|
|
|
str += ChanModeToString[t.Field(i).Name]
|
2011-07-18 08:14:58 +00:00
|
|
|
a[0] = f.String()
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2011-07-18 08:14:58 +00:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
if f.Int() != 0 {
|
2009-12-17 21:30:18 +00:00
|
|
|
str += ChanModeToString[t.Field(i).Name]
|
2011-07-18 08:14:58 +00:00
|
|
|
a[1] = fmt.Sprintf("%d", f.Int())
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, s := range a {
|
|
|
|
if s != "" {
|
2009-12-17 21:30:18 +00:00
|
|
|
str += " " + s
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if str == "+" {
|
2009-12-17 21:30:18 +00:00
|
|
|
str = "No modes set"
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
return str
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Returns a string representing the nick modes. Looks like:
|
|
|
|
// +iwx
|
2009-12-17 17:22:31 +00:00
|
|
|
func (nm *NickMode) String() string {
|
2009-12-17 21:30:18 +00:00
|
|
|
str := "+"
|
2011-07-18 08:14:58 +00:00
|
|
|
v := reflect.Indirect(reflect.ValueOf(nm))
|
|
|
|
t := v.Type()
|
2009-12-17 17:22:31 +00:00
|
|
|
for i := 0; i < v.NumField(); i++ {
|
2011-07-18 08:14:58 +00:00
|
|
|
switch f := v.Field(i); f.Kind() {
|
2009-12-17 17:22:31 +00:00
|
|
|
// only bools here at the mo!
|
2011-07-18 08:14:58 +00:00
|
|
|
case reflect.Bool:
|
|
|
|
if f.Bool() {
|
2009-12-17 21:30:18 +00:00
|
|
|
str += NickModeToString[t.Field(i).Name]
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if str == "+" {
|
2009-12-17 21:30:18 +00:00
|
|
|
str = "No modes set"
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
return str
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Returns a string representing the channel privileges. Looks like:
|
|
|
|
// +o
|
2009-12-17 17:22:31 +00:00
|
|
|
func (p *ChanPrivs) String() string {
|
2009-12-17 21:30:18 +00:00
|
|
|
str := "+"
|
2011-07-18 08:14:58 +00:00
|
|
|
v := reflect.Indirect(reflect.ValueOf(p))
|
|
|
|
t := v.Type()
|
2009-12-17 17:22:31 +00:00
|
|
|
for i := 0; i < v.NumField(); i++ {
|
2011-07-18 08:14:58 +00:00
|
|
|
switch f := v.Field(i); f.Kind() {
|
2009-12-17 17:22:31 +00:00
|
|
|
// only bools here at the mo too!
|
2011-07-18 08:14:58 +00:00
|
|
|
case reflect.Bool:
|
|
|
|
if f.Bool() {
|
2009-12-17 21:30:18 +00:00
|
|
|
str += ChanPrivToString[t.Field(i).Name]
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if str == "+" {
|
2009-12-17 21:30:18 +00:00
|
|
|
str = "No modes set"
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
return str
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|