From 656226dc22874dcb7ad22082584e5a5c4963b054 Mon Sep 17 00:00:00 2001 From: James Mills Date: Thu, 9 Nov 2017 00:52:12 -0800 Subject: [PATCH 1/2] Add realname support. Guard against race conditions on Disconnect --- irc.go | 28 +++++++++++++++++++++++++++- irc_struct.go | 7 +++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/irc.go b/irc.go index 4634390..0d9e83a 100644 --- a/irc.go +++ b/irc.go @@ -388,6 +388,20 @@ func (irc *Connection) Connected() bool { // A disconnect sends all buffered messages (if possible), // stops all goroutines and then closes the socket. 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 { irc.socket.Close() } @@ -468,8 +482,13 @@ func (irc *Connection) Connect(server string) error { 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("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 } @@ -541,6 +560,13 @@ func (irc *Connection) negotiateCaps() error { } 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 } diff --git a/irc_struct.go b/irc_struct.go index 1161900..38eeca8 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -32,6 +32,9 @@ type Connection struct { KeepAlive time.Duration Server string + RealName string // The real name we want to display. + // If zero-value defaults to the user. + socket net.Conn pwrite chan string end chan struct{} @@ -43,8 +46,8 @@ type Connection struct { events map[string]map[int]func(*Event) eventsMutex sync.Mutex - QuitMessage string - lastMessage time.Time + QuitMessage string + lastMessage time.Time lastMessageMutex sync.Mutex VerboseCallbackHandler bool From 547dde5ba34a852acac20af51e599ff89650560e Mon Sep 17 00:00:00 2001 From: James Mills Date: Sun, 12 Nov 2017 13:45:29 -0800 Subject: [PATCH 2/2] Do not Disconnect() on ERROR events (e.g: KILL). --- irc_callback.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/irc_callback.go b/irc_callback.go index 0c98947..15e7191 100644 --- a/irc_callback.go +++ b/irc_callback.go @@ -158,9 +158,6 @@ func (irc *Connection) RunCallbacks(event *Event) { func (irc *Connection) setupCallbacks() { irc.events = make(map[string]map[int]func(*Event)) - //Handle error events. - irc.AddCallback("ERROR", func(e *Event) { irc.Disconnect() }) - //Handle ping events irc.AddCallback("PING", func(e *Event) { irc.SendRaw("PONG :" + e.Message()) })