From a0eed1d687b324aa2ad70325b4ecd628b13f3c98 Mon Sep 17 00:00:00 2001 From: Jeremy Latt Date: Sat, 8 Feb 2014 17:53:06 -0800 Subject: [PATCH] properly implement join/part --- irc/channel.go | 25 ++++++++++++------------- irc/commands.go | 7 +++++++ irc/reply.go | 4 ++-- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/irc/channel.go b/irc/channel.go index fa958b30..1cf3db39 100644 --- a/irc/channel.go +++ b/irc/channel.go @@ -109,7 +109,8 @@ func (channel *Channel) String() string { func (channel *Channel) Join(client *Client) { channel.members[client] = true client.channels[channel] = true - reply := RplJoin(channel, client) + reply := RplJoin(client, channel) + client.replies <- reply channel.replies <- reply channel.GetTopic(client) channel.GetUsers(client) @@ -134,24 +135,22 @@ func (m *JoinCommand) HandleChannel(channel *Channel) { } func (m *PartCommand) HandleChannel(channel *Channel) { - c := m.Client() + client := m.Client() - if !channel.HasMember(c) { - c.replies <- ErrNotOnChannel(channel) + if !channel.HasMember(client) { + client.replies <- ErrNotOnChannel(channel) return } - msg := m.message - if msg == "" { - msg = c.Nick() - } + reply := RplPart(client, channel, m.Message()) + client.replies <- reply + channel.replies <- reply - channel.replies <- RplPart(channel, c, msg) + delete(channel.members, client) + delete(client.channels, channel) - delete(channel.members, c) - delete(c.channels, channel) - - if channel.IsEmpty() { // TODO persistent channels + // TODO persistent channels + if channel.IsEmpty() { channel.server.DeleteChannel(channel) } } diff --git a/irc/commands.go b/irc/commands.go index 5366fbfd..adea373c 100644 --- a/irc/commands.go +++ b/irc/commands.go @@ -301,6 +301,13 @@ type PartCommand struct { message string } +func (cmd *PartCommand) Message() string { + if cmd.message == "" { + return cmd.Source().Nick() + } + return cmd.message +} + func (cmd *PartCommand) String() string { return fmt.Sprintf("PART(channels=%s, message=%s)", cmd.channels, cmd.message) } diff --git a/irc/reply.go b/irc/reply.go index 90fa4112..ec1335ba 100644 --- a/irc/reply.go +++ b/irc/reply.go @@ -145,11 +145,11 @@ func RplNick(source Identifier, newNick string) Reply { return NewStringReply(source, RPL_NICK, newNick) } -func RplJoin(channel *Channel, client *Client) Reply { +func RplJoin(client *Client, channel *Channel) Reply { return NewStringReply(client, RPL_JOIN, channel.name) } -func RplPart(channel *Channel, client *Client, message string) Reply { +func RplPart(client *Client, channel *Channel, message string) Reply { return NewStringReply(client, RPL_PART, "%s :%s", channel.name, message) }