From 1283bd35c7ea68b72337f2d26abee67b8db3b350 Mon Sep 17 00:00:00 2001 From: Jeremy Latt Date: Sat, 22 Feb 2014 11:40:32 -0800 Subject: [PATCH] encapsulate client replies in method --- irc/channel.go | 16 ++++++++-------- irc/client.go | 21 ++++++++++++--------- irc/reply.go | 2 +- irc/server.go | 8 ++++---- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/irc/channel.go b/irc/channel.go index 34d46ed6..f9b16138 100644 --- a/irc/channel.go +++ b/irc/channel.go @@ -111,7 +111,7 @@ func (channel *Channel) Join(client *Client, key string) { reply := RplJoin(client, channel) for member := range channel.members { - member.replies <- reply + member.Reply(reply) } channel.GetTopic(client) channel.Names(client) @@ -125,7 +125,7 @@ func (channel *Channel) Part(client *Client, message string) { reply := RplPart(client, channel, message) for member := range channel.members { - member.replies <- reply + member.Reply(reply) } channel.Quit(client) @@ -164,7 +164,7 @@ func (channel *Channel) SetTopic(client *Client, topic string) { reply := RplTopicMsg(client, channel) for member := range channel.members { - member.replies <- reply + member.Reply(reply) } } @@ -177,7 +177,7 @@ func (channel *Channel) PrivMsg(client *Client, message string) { if member == client { continue } - member.replies <- RplPrivMsg(client, channel, message) + member.Reply(RplPrivMsg(client, channel, message)) } } @@ -247,7 +247,7 @@ func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) { continue } - target := channel.server.clients[change.arg] + target := channel.server.clients.Get(change.arg) if target == nil { // TODO err reply continue @@ -272,7 +272,7 @@ func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) { if len(applied) > 0 { for member := range channel.members { - member.replies <- RplChannelMode(client, channel, applied) + member.Reply(RplChannelMode(client, channel, applied)) } } } @@ -286,7 +286,7 @@ func (channel *Channel) Notice(client *Client, message string) { if member == client { continue } - member.replies <- RplNotice(client, channel, message) + member.Reply(RplNotice(client, channel, message)) } } @@ -311,7 +311,7 @@ func (channel *Channel) Kick(client *Client, target *Client, comment string) { reply := RplKick(channel, client, target, comment) for member := range channel.members { - member.replies <- reply + member.Reply(reply) } channel.Quit(target) } diff --git a/irc/client.go b/irc/client.go index 07c161f3..811504da 100644 --- a/irc/client.go +++ b/irc/client.go @@ -128,10 +128,6 @@ func (client *Client) writeReplies() { } } client.socket.Close() - - for _ = range client.replies { - // discard - } } // @@ -175,7 +171,7 @@ func (client *Client) Touch() { } func (client *Client) Idle() { - client.replies <- RplPing(client) + client.Reply(RplPing(client)) if client.quitTimer == nil { client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.connectionTimeout) @@ -295,17 +291,24 @@ func (client *Client) ChangeNickname(nickname string) { client.nick = nickname client.server.clients.Add(client) for friend := range client.Friends() { - friend.replies <- reply + friend.Reply(reply) } } +func (client *Client) Reply(reply string) { + if client.hasQuit { + return + } + client.replies <- reply +} + func (client *Client) Quit(message string) { if client.hasQuit { return } - client.replies <- RplError("connection closed") - client.replies <- EOF + client.Reply(RplError("connection closed")) + client.Reply(EOF) client.hasQuit = true friends := client.Friends() @@ -315,7 +318,7 @@ func (client *Client) Quit(message string) { if len(friends) > 0 { reply := RplQuit(client, message) for friend := range friends { - friend.replies <- reply + friend.Reply(reply) } } } diff --git a/irc/reply.go b/irc/reply.go index 3d9a9d3a..d729aa61 100644 --- a/irc/reply.go +++ b/irc/reply.go @@ -27,7 +27,7 @@ func NewNumericReply(target *Client, code NumericCode, func (target *Client) NumericReply(code NumericCode, format string, args ...interface{}) { - target.replies <- NewNumericReply(target, code, format, args...) + target.Reply(NewNumericReply(target, code, format, args...)) } // diff --git a/irc/server.go b/irc/server.go index 2596426e..40cfcc2b 100644 --- a/irc/server.go +++ b/irc/server.go @@ -340,7 +340,7 @@ func (m *PassCommand) HandleServer(s *Server) { } func (m *PingCommand) HandleServer(s *Server) { - m.Client().replies <- RplPong(m.Client()) + m.Client().Reply(RplPong(m.Client())) } func (m *PongCommand) HandleServer(s *Server) { @@ -443,7 +443,7 @@ func (msg *PrivMsgCommand) HandleServer(server *Server) { client.ErrNoSuchNick(msg.target) return } - target.replies <- RplPrivMsg(client, target, msg.message) + target.Reply(RplPrivMsg(client, target, msg.message)) if target.flags[Away] { target.RplAway(client) } @@ -488,7 +488,7 @@ func (m *ModeCommand) HandleServer(s *Server) { // Who should get these replies? if len(changes) > 0 { - client.replies <- RplMode(client, target, changes) + client.Reply(RplMode(client, target, changes)) } } @@ -648,7 +648,7 @@ func (msg *NoticeCommand) HandleServer(server *Server) { client.ErrNoSuchNick(msg.target) return } - target.replies <- RplNotice(client, target, msg.message) + target.Reply(RplNotice(client, target, msg.message)) } func (msg *KickCommand) HandleServer(server *Server) {