From bd3ca4ae47f9007e594c5a78907432ebe3303580 Mon Sep 17 00:00:00 2001 From: Jeremy Latt Date: Mon, 17 Feb 2014 18:10:52 -0800 Subject: [PATCH] fix race in idle timeout; fix some string trimming; fix some replies --- irc/client.go | 8 +++++++- irc/reply.go | 6 +++--- irc/server.go | 9 +++++++++ irc/socket.go | 2 +- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/irc/client.go b/irc/client.go index ce3bb079..f96d6ff0 100644 --- a/irc/client.go +++ b/irc/client.go @@ -95,6 +95,10 @@ func (client *Client) Touch() { } } +type ClientIdle struct { + BaseCommand +} + func (client *Client) Idle() { if client.quitTimer == nil { client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.connectionTimeout) @@ -102,7 +106,9 @@ func (client *Client) Idle() { client.quitTimer.Reset(QUIT_TIMEOUT) } - client.Reply(RplPing(client.server, client)) + cmd := &ClientIdle{} + cmd.SetClient(client) + client.server.commands <- cmd } func (client *Client) connectionTimeout() { diff --git a/irc/reply.go b/irc/reply.go index 2f39291f..640106e4 100644 --- a/irc/reply.go +++ b/irc/reply.go @@ -192,17 +192,17 @@ func RplKick(channel *Channel, client *Client, target *Client, comment string) R func RplWelcome(source Identifier, client *Client) Reply { return NewNumericReply(source, RPL_WELCOME, - "Welcome to the Internet Relay Network %s", client.Id()) + ":Welcome to the Internet Relay Network %s", client.Id()) } func RplYourHost(server *Server) Reply { return NewNumericReply(server, RPL_YOURHOST, - "Your host is %s, running version %s", server.name, VERSION) + ":Your host is %s, running version %s", server.name, VERSION) } func RplCreated(server *Server) Reply { return NewNumericReply(server, RPL_CREATED, - "This server was created %s", server.ctime.Format(time.RFC1123)) + ":This server was created %s", server.ctime.Format(time.RFC1123)) } func RplMyInfo(server *Server) Reply { diff --git a/irc/server.go b/irc/server.go index 07ac0632..790d899c 100644 --- a/irc/server.go +++ b/irc/server.go @@ -9,6 +9,7 @@ import ( "log" "net" "os" + "strings" "time" ) @@ -171,6 +172,8 @@ func (s *Server) tryRegister(c *Client) { if c.HasNick() && c.HasUsername() { c.phase = Normal c.loginTimer.Stop() + c.AddFriend(c) + c.Reply(RplWelcome(s, c)) c.Reply(RplYourHost(s)) c.Reply(RplCreated(s)) @@ -199,6 +202,7 @@ func (server *Server) MOTD(client *Client) { if err != nil { break } + line = strings.TrimRight(line, "\r\n") if len(line) > 80 { for len(line) > 80 { @@ -613,3 +617,8 @@ func (msg *ListCommand) HandleServer(server *Server) { } client.Reply(RplListEnd(server)) } + +func (msg *ClientIdle) HandleServer(server *Server) { + client := msg.Client() + client.Reply(RplPing(server, client)) +} diff --git a/irc/socket.go b/irc/socket.go index 039c2792..de7ed770 100644 --- a/irc/socket.go +++ b/irc/socket.go @@ -59,7 +59,7 @@ func (socket *Socket) readLines() { break } - line = strings.TrimSpace(line) + line = strings.TrimRight(line, "\r\n") if len(line) == 0 { continue }