Skip to content

Commit

Permalink
Merge pull request #99 from prologic/master
Browse files Browse the repository at this point in the history
Add realname support. Guard against race conditions on Disconnect
  • Loading branch information
thoj authored Nov 13, 2017
2 parents ef65ae6 + 547dde5 commit db5bd17
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
28 changes: 27 additions & 1 deletion irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 0 additions & 3 deletions irc_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()) })

Expand Down
7 changes: 5 additions & 2 deletions irc_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
Expand Down

0 comments on commit db5bd17

Please sign in to comment.