Add Message.LastParam()

This commit is contained in:
Ken-Håvard Lieng 2016-12-17 00:11:44 +01:00
parent 83056c5396
commit cc3b1e3a82
4 changed files with 22 additions and 14 deletions

View File

@ -179,7 +179,7 @@ func (c *Client) recv() {
switch msg.Command { switch msg.Command {
case Ping: case Ping:
go c.write("PONG :" + msg.Params[len(msg.Params)-1]) go c.write("PONG :" + msg.LastParam())
case Join: case Join:
if msg.Nick == c.GetNick() { if msg.Nick == c.GetNick() {

View File

@ -11,6 +11,10 @@ type Message struct {
Params []string Params []string
} }
func (m *Message) LastParam() string {
return m.Params[len(m.Params)-1]
}
func parseMessage(line string) *Message { func parseMessage(line string) *Message {
line = strings.Trim(line, "\r\n") line = strings.Trim(line, "\r\n")
msg := Message{} msg := Message{}

View File

@ -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) { func TestBadMessagePanic(t *testing.T) {
parseMessage(":user\r\n") parseMessage(":user\r\n")
parseMessage(":\r\n") parseMessage(":\r\n")

View File

@ -59,11 +59,11 @@ func (i *ircHandler) nick(msg *irc.Message) {
i.session.sendJSON("nick", Nick{ i.session.sendJSON("nick", Nick{
Server: i.client.Host, Server: i.client.Host,
Old: msg.Nick, Old: msg.Nick,
New: msg.Params[len(msg.Params)-1], New: msg.LastParam(),
Channels: channelStore.FindUserChannels(msg.Nick, i.client.Host), 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) { func (i *ircHandler) join(msg *irc.Message) {
@ -90,7 +90,7 @@ func (i *ircHandler) part(msg *irc.Message) {
User: msg.Nick, User: msg.Nick,
Channels: msg.Params[:len(msg.Params)-1], 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]) channelStore.RemoveUser(msg.Nick, i.client.Host, msg.Params[0])
@ -118,7 +118,7 @@ func (i *ircHandler) message(msg *irc.Message) {
message := Chat{ message := Chat{
Server: i.client.Host, Server: i.client.Host,
From: msg.Nick, From: msg.Nick,
Message: msg.Params[len(msg.Params)-1], Message: msg.LastParam(),
} }
if msg.Params[0] == i.client.GetNick() { if msg.Params[0] == i.client.GetNick() {
@ -129,7 +129,7 @@ func (i *ircHandler) message(msg *irc.Message) {
} }
if msg.Params[0] != "*" { 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{ i.session.sendJSON("quit", Quit{
Server: i.client.Host, Server: i.client.Host,
User: msg.Nick, User: msg.Nick,
Reason: msg.Params[len(msg.Params)-1], Reason: msg.LastParam(),
Channels: channelStore.FindUserChannels(msg.Nick, i.client.Host), 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) { 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) { func (i *ircHandler) whoisEnd(msg *irc.Message) {
@ -176,14 +176,14 @@ func (i *ircHandler) topic(msg *irc.Message) {
i.session.sendJSON("topic", Topic{ i.session.sendJSON("topic", Topic{
Server: i.client.Host, Server: i.client.Host,
Channel: msg.Params[1], 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) { 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]] userBuffer := i.userBuffers[msg.Params[2]]
i.userBuffers[msg.Params[2]] = append(userBuffer, users...) 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) { func (i *ircHandler) motdStart(msg *irc.Message) {
i.motdBuffer.Server = i.client.Host 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) { 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) { func (i *ircHandler) motdEnd(msg *irc.Message) {
@ -270,5 +270,5 @@ func isChannel(s string) bool {
} }
func printMessage(msg *irc.Message, i *irc.Client) { 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())
} }