Merge pull request #99 from prologic/master
Add realname support. Guard against race conditions on Disconnect
This commit is contained in:
commit
db5bd176f7
28
irc.go
28
irc.go
@ -388,6 +388,20 @@ func (irc *Connection) Connected() bool {
|
|||||||
// A disconnect sends all buffered messages (if possible),
|
// A disconnect sends all buffered messages (if possible),
|
||||||
// stops all goroutines and then closes the socket.
|
// stops all goroutines and then closes the socket.
|
||||||
func (irc *Connection) Disconnect() {
|
func (irc *Connection) Disconnect() {
|
||||||
|
irc.Lock()
|
||||||
|
defer irc.Unlock()
|
||||||
|
|
||||||
|
if irc.end != nil {
|
||||||
|
close(irc.end)
|
||||||
|
}
|
||||||
|
|
||||||
|
irc.end = nil
|
||||||
|
|
||||||
|
if irc.pwrite != nil {
|
||||||
|
close(irc.pwrite)
|
||||||
|
}
|
||||||
|
|
||||||
|
irc.Wait()
|
||||||
if irc.socket != nil {
|
if irc.socket != nil {
|
||||||
irc.socket.Close()
|
irc.socket.Close()
|
||||||
}
|
}
|
||||||
@ -468,8 +482,13 @@ func (irc *Connection) Connect(server string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
realname := irc.user
|
||||||
|
if irc.RealName != "" {
|
||||||
|
realname = irc.RealName
|
||||||
|
}
|
||||||
|
|
||||||
irc.pwrite <- fmt.Sprintf("NICK %s\r\n", irc.nick)
|
irc.pwrite <- fmt.Sprintf("NICK %s\r\n", irc.nick)
|
||||||
irc.pwrite <- fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", irc.user, irc.user)
|
irc.pwrite <- fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", irc.user, realname)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -541,6 +560,13 @@ func (irc *Connection) negotiateCaps() error {
|
|||||||
}
|
}
|
||||||
irc.pwrite <- fmt.Sprintf("CAP END\r\n")
|
irc.pwrite <- fmt.Sprintf("CAP END\r\n")
|
||||||
|
|
||||||
|
realname := irc.user
|
||||||
|
if irc.RealName != "" {
|
||||||
|
realname = irc.RealName
|
||||||
|
}
|
||||||
|
|
||||||
|
irc.pwrite <- fmt.Sprintf("NICK %s\r\n", irc.nick)
|
||||||
|
irc.pwrite <- fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", irc.user, realname)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,9 +158,6 @@ func (irc *Connection) RunCallbacks(event *Event) {
|
|||||||
func (irc *Connection) setupCallbacks() {
|
func (irc *Connection) setupCallbacks() {
|
||||||
irc.events = make(map[string]map[int]func(*Event))
|
irc.events = make(map[string]map[int]func(*Event))
|
||||||
|
|
||||||
//Handle error events.
|
|
||||||
irc.AddCallback("ERROR", func(e *Event) { irc.Disconnect() })
|
|
||||||
|
|
||||||
//Handle ping events
|
//Handle ping events
|
||||||
irc.AddCallback("PING", func(e *Event) { irc.SendRaw("PONG :" + e.Message()) })
|
irc.AddCallback("PING", func(e *Event) { irc.SendRaw("PONG :" + e.Message()) })
|
||||||
|
|
||||||
|
@ -32,6 +32,9 @@ type Connection struct {
|
|||||||
KeepAlive time.Duration
|
KeepAlive time.Duration
|
||||||
Server string
|
Server string
|
||||||
|
|
||||||
|
RealName string // The real name we want to display.
|
||||||
|
// If zero-value defaults to the user.
|
||||||
|
|
||||||
socket net.Conn
|
socket net.Conn
|
||||||
pwrite chan string
|
pwrite chan string
|
||||||
end chan struct{}
|
end chan struct{}
|
||||||
|
Loading…
Reference in New Issue
Block a user