From cc3b1e3a82644205599cdd5b9a410d5fa049c513 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken-H=C3=A5vard=20Lieng?= Date: Sat, 17 Dec 2016 00:11:44 +0100 Subject: [PATCH] Add Message.LastParam() --- irc/conn.go | 2 +- irc/message.go | 4 ++++ irc/message_test.go | 4 ++++ server/irc_handler.go | 26 +++++++++++++------------- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/irc/conn.go b/irc/conn.go index 30a16426..bf08ffc0 100644 --- a/irc/conn.go +++ b/irc/conn.go @@ -179,7 +179,7 @@ func (c *Client) recv() { switch msg.Command { case Ping: - go c.write("PONG :" + msg.Params[len(msg.Params)-1]) + go c.write("PONG :" + msg.LastParam()) case Join: if msg.Nick == c.GetNick() { diff --git a/irc/message.go b/irc/message.go index dfec47de..1b9b231f 100644 --- a/irc/message.go +++ b/irc/message.go @@ -11,6 +11,10 @@ type Message struct { Params []string } +func (m *Message) LastParam() string { + return m.Params[len(m.Params)-1] +} + func parseMessage(line string) *Message { line = strings.Trim(line, "\r\n") msg := Message{} diff --git a/irc/message_test.go b/irc/message_test.go index 9ea7b94f..67390bd9 100644 --- a/irc/message_test.go +++ b/irc/message_test.go @@ -72,6 +72,10 @@ func TestParseMessage(t *testing.T) { } } +func TestLastParam(t *testing.T) { + assert.Equal(t, "some message", parseMessage(":user CMD #chan :some message\r\n").LastParam()) +} + func TestBadMessagePanic(t *testing.T) { parseMessage(":user\r\n") parseMessage(":\r\n") diff --git a/server/irc_handler.go b/server/irc_handler.go index e9ad7539..9f031498 100644 --- a/server/irc_handler.go +++ b/server/irc_handler.go @@ -59,11 +59,11 @@ func (i *ircHandler) nick(msg *irc.Message) { i.session.sendJSON("nick", Nick{ Server: i.client.Host, Old: msg.Nick, - New: msg.Params[len(msg.Params)-1], + New: msg.LastParam(), Channels: channelStore.FindUserChannels(msg.Nick, i.client.Host), }) - channelStore.RenameUser(msg.Nick, msg.Params[len(msg.Params)-1], i.client.Host) + channelStore.RenameUser(msg.Nick, msg.LastParam(), i.client.Host) } func (i *ircHandler) join(msg *irc.Message) { @@ -90,7 +90,7 @@ func (i *ircHandler) part(msg *irc.Message) { User: msg.Nick, Channels: msg.Params[:len(msg.Params)-1], }, - Reason: msg.Params[len(msg.Params)-1], + Reason: msg.LastParam(), }) channelStore.RemoveUser(msg.Nick, i.client.Host, msg.Params[0]) @@ -118,7 +118,7 @@ func (i *ircHandler) message(msg *irc.Message) { message := Chat{ Server: i.client.Host, From: msg.Nick, - Message: msg.Params[len(msg.Params)-1], + Message: msg.LastParam(), } if msg.Params[0] == i.client.GetNick() { @@ -129,7 +129,7 @@ func (i *ircHandler) message(msg *irc.Message) { } if msg.Params[0] != "*" { - go i.session.user.LogMessage(i.client.Host, msg.Nick, msg.Params[0], msg.Params[len(msg.Params)-1]) + go i.session.user.LogMessage(i.client.Host, msg.Nick, msg.Params[0], msg.LastParam()) } } @@ -137,7 +137,7 @@ func (i *ircHandler) quit(msg *irc.Message) { i.session.sendJSON("quit", Quit{ Server: i.client.Host, User: msg.Nick, - Reason: msg.Params[len(msg.Params)-1], + Reason: msg.LastParam(), Channels: channelStore.FindUserChannels(msg.Nick, i.client.Host), }) @@ -164,7 +164,7 @@ func (i *ircHandler) whoisServer(msg *irc.Message) { } func (i *ircHandler) whoisChannels(msg *irc.Message) { - i.whois.Channels = append(i.whois.Channels, strings.Split(strings.TrimRight(msg.Params[len(msg.Params)-1], " "), " ")...) + i.whois.Channels = append(i.whois.Channels, strings.Split(strings.TrimRight(msg.LastParam(), " "), " ")...) } func (i *ircHandler) whoisEnd(msg *irc.Message) { @@ -176,14 +176,14 @@ func (i *ircHandler) topic(msg *irc.Message) { i.session.sendJSON("topic", Topic{ Server: i.client.Host, Channel: msg.Params[1], - Topic: msg.Params[len(msg.Params)-1], + Topic: msg.LastParam(), }) - channelStore.SetTopic(msg.Params[len(msg.Params)-1], i.client.Host, msg.Params[1]) + channelStore.SetTopic(msg.LastParam(), i.client.Host, msg.Params[1]) } func (i *ircHandler) names(msg *irc.Message) { - users := strings.Split(strings.TrimSuffix(msg.Params[len(msg.Params)-1], " "), " ") + users := strings.Split(strings.TrimSuffix(msg.LastParam(), " "), " ") userBuffer := i.userBuffers[msg.Params[2]] i.userBuffers[msg.Params[2]] = append(userBuffer, users...) } @@ -204,11 +204,11 @@ func (i *ircHandler) namesEnd(msg *irc.Message) { func (i *ircHandler) motdStart(msg *irc.Message) { i.motdBuffer.Server = i.client.Host - i.motdBuffer.Title = msg.Params[len(msg.Params)-1] + i.motdBuffer.Title = msg.LastParam() } func (i *ircHandler) motd(msg *irc.Message) { - i.motdBuffer.Content = append(i.motdBuffer.Content, msg.Params[len(msg.Params)-1]) + i.motdBuffer.Content = append(i.motdBuffer.Content, msg.LastParam()) } func (i *ircHandler) motdEnd(msg *irc.Message) { @@ -270,5 +270,5 @@ func isChannel(s string) bool { } func printMessage(msg *irc.Message, i *irc.Client) { - log.Println(i.GetNick()+":", msg.Prefix, msg.Command, msg.Params, msg.Params[len(msg.Params)-1]) + log.Println(i.GetNick()+":", msg.Prefix, msg.Command, msg.Params, msg.LastParam()) }