start working on making state accessible

This commit is contained in:
Liam Stanley 2017-02-14 07:33:05 -05:00
parent 9818cded91
commit 3ded5b433a
3 changed files with 91 additions and 0 deletions

View File

@ -435,6 +435,20 @@ func (c *Client) Channels() []string {
return channels
}
// Lookup looks up a given channel in state. If the channel doesn't exist,
// channel is nil.
func (c *Client) Lookup(name string) (channel *Channel) {
c.state.mu.Lock()
defer c.state.mu.Unlock()
channel = c.state.lookupChannel(name)
if channel == nil {
return nil
}
return channel.Copy()
}
// IsInChannel returns true if the client is in channel. Panics if tracking
// is disabled.
func (c *Client) IsInChannel(channel string) bool {

View File

@ -50,6 +50,21 @@ type CModes struct {
modes []CMode // the list of modes for this given state.
}
// Copy returns a deep copy of CModes.
func (c *CModes) Copy() (nc CModes) {
nc = CModes{}
nc = *c
nc.modes = make([]CMode, len(c.modes))
// Copy modes.
for i := 0; i < len(c.modes); i++ {
nc.modes[i] = c.modes[i]
}
return nc
}
// String returns a complete set of modes for this given state (change?). For
// example, "+a-b+cde some-arg".
func (c *CModes) String() string {

View File

@ -142,6 +142,68 @@ type Channel struct {
Modes CModes
}
// Copy returns a deep copy of a given channel.
func (c *Channel) Copy() *Channel {
nc := &Channel{}
*nc = *c
// Copy the users.
nc.users = make(map[string]*User)
for k, v := range c.users {
nc.users[k] = v
}
// And modes.
nc.Modes = c.Modes.Copy()
return nc
}
// Users returns a list of users in a given channel.
func (c *Channel) Users() []*User {
out := make([]*User, len(c.users))
var index int
for _, u := range c.users {
out[index] = u
index++
}
return out
}
// NickList returns a list of nicknames in a given channel.
func (c *Channel) NickList() []string {
out := make([]string, len(c.users))
var index int
for k, _ := range c.users {
out[index] = k
index++
}
return out
}
// Len returns the count of users in a given channel.
func (c *Channel) Len() int {
return len(c.users)
}
func (c *Channel) Lookup(nick string) (user *User, ok bool) {
for k, v := range c.users {
if strings.ToLower(k) == strings.ToLower(nick) {
// No need to have a copy, as if one has access to a channel,
// should already have a full copy.
return v, true
}
}
return nil, false
}
// Message returns an event which can be used to send a response to the channel.
func (c *Channel) Message(message string) *Event {
return &Event{Command: PRIVMSG, Params: []string{c.Name}, Trailing: message}