additional lock/unlocking issues
This commit is contained in:
parent
c78113dc51
commit
f0541cd3c6
@ -2,6 +2,9 @@ package girc
|
|||||||
|
|
||||||
// handleEvent runs the necessary callbacks for the incoming event
|
// handleEvent runs the necessary callbacks for the incoming event
|
||||||
func (c *Client) handleEvent(event *Event) {
|
func (c *Client) handleEvent(event *Event) {
|
||||||
|
// log the event
|
||||||
|
c.log.Print(event.String())
|
||||||
|
|
||||||
// wildcard callbacks first
|
// wildcard callbacks first
|
||||||
if callbacks, ok := c.callbacks[ALLEVENTS]; ok {
|
if callbacks, ok := c.callbacks[ALLEVENTS]; ok {
|
||||||
for i := 0; i < len(callbacks); i++ {
|
for i := 0; i < len(callbacks); i++ {
|
||||||
|
6
main.go
6
main.go
@ -215,11 +215,7 @@ func (c *Client) Wait() {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event := <-c.Events:
|
case event := <-c.Events:
|
||||||
// log the event
|
c.handleEvent(event)
|
||||||
c.log.Print(event.String())
|
|
||||||
|
|
||||||
// run in the background
|
|
||||||
go c.handleEvent(event)
|
|
||||||
case <-c.quitChan:
|
case <-c.quitChan:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
25
state.go
25
state.go
@ -44,43 +44,43 @@ func NewState() *State {
|
|||||||
|
|
||||||
// createChanIfNotExists creates the channel in state, if not already done
|
// createChanIfNotExists creates the channel in state, if not already done
|
||||||
func (s *State) createChanIfNotExists(channel string) {
|
func (s *State) createChanIfNotExists(channel string) {
|
||||||
s.m.Lock()
|
channel = strings.ToLower(channel)
|
||||||
defer s.m.Unlock()
|
|
||||||
|
|
||||||
// not a valid channel
|
// not a valid channel
|
||||||
if !IsValidChannel(channel) {
|
if !IsValidChannel(channel) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.m.Lock()
|
||||||
if _, ok := s.channels[channel]; !ok {
|
if _, ok := s.channels[channel]; !ok {
|
||||||
s.channels[channel] = &Channel{
|
s.channels[channel] = &Channel{
|
||||||
Name: strings.ToLower(channel),
|
Name: channel,
|
||||||
users: make(map[string]*User),
|
users: make(map[string]*User),
|
||||||
Joined: time.Now(),
|
Joined: time.Now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
s.m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// deleteChannel removes the channel from state, if not already done
|
// deleteChannel removes the channel from state, if not already done
|
||||||
func (s *State) deleteChannel(channel string) {
|
func (s *State) deleteChannel(channel string) {
|
||||||
s.m.Lock()
|
channel = strings.ToLower(channel)
|
||||||
defer s.m.Unlock()
|
|
||||||
|
|
||||||
s.createChanIfNotExists(channel)
|
s.createChanIfNotExists(channel)
|
||||||
|
|
||||||
|
s.m.Lock()
|
||||||
if _, ok := s.channels[channel]; ok {
|
if _, ok := s.channels[channel]; ok {
|
||||||
delete(s.channels, channel)
|
delete(s.channels, channel)
|
||||||
}
|
}
|
||||||
|
s.m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// createUserIfNotExists creates the channel and user in state,
|
// createUserIfNotExists creates the channel and user in state,
|
||||||
// if not already done
|
// if not already done
|
||||||
func (s *State) createUserIfNotExists(channel, nick, ident, host string) {
|
func (s *State) createUserIfNotExists(channel, nick, ident, host string) {
|
||||||
s.m.Lock()
|
channel = strings.ToLower(channel)
|
||||||
defer s.m.Unlock()
|
|
||||||
|
|
||||||
s.createChanIfNotExists(channel)
|
s.createChanIfNotExists(channel)
|
||||||
|
|
||||||
|
s.m.Lock()
|
||||||
if _, ok := s.channels[channel].users[nick]; !ok {
|
if _, ok := s.channels[channel].users[nick]; !ok {
|
||||||
s.channels[channel].users[nick] = &User{
|
s.channels[channel].users[nick] = &User{
|
||||||
Nick: nick,
|
Nick: nick,
|
||||||
@ -89,18 +89,19 @@ func (s *State) createUserIfNotExists(channel, nick, ident, host string) {
|
|||||||
FirstSeen: time.Now(),
|
FirstSeen: time.Now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
s.m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// deleteUser removes the user from channel state
|
// deleteUser removes the user from channel state
|
||||||
func (s *State) deleteUser(channel, nick string) {
|
func (s *State) deleteUser(channel, nick string) {
|
||||||
s.m.Lock()
|
channel = strings.ToLower(channel)
|
||||||
defer s.m.Unlock()
|
|
||||||
|
|
||||||
s.createChanIfNotExists(channel)
|
s.createChanIfNotExists(channel)
|
||||||
|
|
||||||
|
s.m.Lock()
|
||||||
if _, ok := s.channels[channel].users[nick]; ok {
|
if _, ok := s.channels[channel].users[nick]; ok {
|
||||||
delete(s.channels[channel].users, nick)
|
delete(s.channels[channel].users, nick)
|
||||||
}
|
}
|
||||||
|
s.m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// renameUser renames the user in state, in all locations where
|
// renameUser renames the user in state, in all locations where
|
||||||
|
Loading…
Reference in New Issue
Block a user