Remove Trailing param

This commit is contained in:
Daniel Oaks 2016-12-12 16:19:22 +10:00
parent 7af50d52b9
commit 0293a7f4a5
5 changed files with 69 additions and 67 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.Trailing) go c.write("PONG :" + msg.Params[len(msg.Params)-1])
case Join: case Join:
if msg.Nick == c.GetNick() { if msg.Nick == c.GetNick() {

View File

@ -5,11 +5,10 @@ import (
) )
type Message struct { type Message struct {
Prefix string Prefix string
Nick string Nick string
Command string Command string
Params []string Params []string
Trailing string
} }
func parseMessage(line string) *Message { func parseMessage(line string) *Message {
@ -31,9 +30,13 @@ func parseMessage(line string) *Message {
} }
} }
var usesTrailing bool
var trailing string
if i := strings.Index(line, " :"); i > 0 { if i := strings.Index(line, " :"); i > 0 {
cmdEnd = i cmdEnd = i
msg.Trailing = line[i+2:] trailing = line[i+2:]
usesTrailing = true
} }
cmd := strings.Split(line[cmdStart:cmdEnd], " ") cmd := strings.Split(line[cmdStart:cmdEnd], " ")
@ -42,8 +45,8 @@ func parseMessage(line string) *Message {
msg.Params = cmd[1:] msg.Params = cmd[1:]
} }
if msg.Trailing != "" { if usesTrailing {
msg.Params = append(msg.Params, msg.Trailing) msg.Params = append(msg.Params, trailing)
} }
return &msg return &msg

View File

@ -14,11 +14,10 @@ func TestParseMessage(t *testing.T) {
{ {
":user CMD #chan :some message\r\n", ":user CMD #chan :some message\r\n",
&Message{ &Message{
Prefix: "user", Prefix: "user",
Nick: "user", Nick: "user",
Command: "CMD", Command: "CMD",
Params: []string{"#chan", "some message"}, Params: []string{"#chan", "some message"},
Trailing: "some message",
}, },
}, { }, {
":nick!user@host.com CMD a b\r\n", ":nick!user@host.com CMD a b\r\n",
@ -32,7 +31,7 @@ func TestParseMessage(t *testing.T) {
"CMD a b :\r\n", "CMD a b :\r\n",
&Message{ &Message{
Command: "CMD", Command: "CMD",
Params: []string{"a", "b"}, Params: []string{"a", "b", ""},
}, },
}, { }, {
"CMD a b\r\n", "CMD a b\r\n",
@ -48,9 +47,8 @@ func TestParseMessage(t *testing.T) {
}, { }, {
"CMD :tests and stuff\r\n", "CMD :tests and stuff\r\n",
&Message{ &Message{
Command: "CMD", Command: "CMD",
Params: []string{"tests and stuff"}, Params: []string{"tests and stuff"},
Trailing: "tests and stuff",
}, },
}, { }, {
":nick@host.com CMD\r\n", ":nick@host.com CMD\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.Trailing, New: msg.Params[len(msg.Params)-1],
Channels: channelStore.FindUserChannels(msg.Nick, i.client.Host), Channels: channelStore.FindUserChannels(msg.Nick, i.client.Host),
}) })
channelStore.RenameUser(msg.Nick, msg.Trailing, i.client.Host) channelStore.RenameUser(msg.Nick, msg.Params[len(msg.Params)-1], i.client.Host)
} }
func (i *ircHandler) join(msg *irc.Message) { func (i *ircHandler) join(msg *irc.Message) {
@ -84,13 +84,20 @@ func (i *ircHandler) join(msg *irc.Message) {
} }
func (i *ircHandler) part(msg *irc.Message) { func (i *ircHandler) part(msg *irc.Message) {
var chans []string
for i, param := range msg.Params {
if i != len(msg.Params)-1 {
chans = append(chans, param)
}
}
i.session.sendJSON("part", Part{ i.session.sendJSON("part", Part{
Join: Join{ Join: Join{
Server: i.client.Host, Server: i.client.Host,
User: msg.Nick, User: msg.Nick,
Channels: msg.Params, Channels: chans,
}, },
Reason: msg.Trailing, Reason: msg.Params[len(msg.Params)-1],
}) })
channelStore.RemoveUser(msg.Nick, i.client.Host, msg.Params[0]) channelStore.RemoveUser(msg.Nick, i.client.Host, msg.Params[0])
@ -118,7 +125,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.Trailing, Message: msg.Params[len(msg.Params)-1],
} }
if msg.Params[0] == i.client.GetNick() { if msg.Params[0] == i.client.GetNick() {
@ -129,7 +136,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.Trailing) go i.session.user.LogMessage(i.client.Host, msg.Nick, msg.Params[0], msg.Params[len(msg.Params)-1])
} }
} }
@ -137,7 +144,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.Trailing, Reason: msg.Params[len(msg.Params)-1],
Channels: channelStore.FindUserChannels(msg.Nick, i.client.Host), Channels: channelStore.FindUserChannels(msg.Nick, i.client.Host),
}) })
@ -164,7 +171,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.Trailing, " "), " ")...) i.whois.Channels = append(i.whois.Channels, strings.Split(strings.TrimRight(msg.Params[len(msg.Params)-1], " "), " ")...)
} }
func (i *ircHandler) whoisEnd(msg *irc.Message) { func (i *ircHandler) whoisEnd(msg *irc.Message) {
@ -176,14 +183,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.Trailing, Topic: msg.Params[len(msg.Params)-1],
}) })
channelStore.SetTopic(msg.Trailing, i.client.Host, msg.Params[1]) channelStore.SetTopic(msg.Params[len(msg.Params)-1], 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.Trailing, " "), " ") users := strings.Split(strings.TrimSuffix(msg.Params[len(msg.Params)-1], " "), " ")
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 +211,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.Trailing i.motdBuffer.Title = msg.Params[len(msg.Params)-1]
} }
func (i *ircHandler) motd(msg *irc.Message) { func (i *ircHandler) motd(msg *irc.Message) {
i.motdBuffer.Content = append(i.motdBuffer.Content, msg.Trailing) i.motdBuffer.Content = append(i.motdBuffer.Content, msg.Params[len(msg.Params)-1])
} }
func (i *ircHandler) motdEnd(msg *irc.Message) { func (i *ircHandler) motdEnd(msg *irc.Message) {
@ -270,5 +277,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.Trailing) log.Println(i.GetNick()+":", msg.Prefix, msg.Command, msg.Params, msg.Params[len(msg.Params)-1])
} }

View File

@ -51,9 +51,9 @@ func checkResponse(t *testing.T, expectedType string, expectedData interface{},
func TestHandleIRCNick(t *testing.T) { func TestHandleIRCNick(t *testing.T) {
res := dispatchMessage(&irc.Message{ res := dispatchMessage(&irc.Message{
Command: irc.Nick, Command: irc.Nick,
Nick: "old", Nick: "old",
Trailing: "new", Params: []string{"new"},
}) })
checkResponse(t, "nick", Nick{ checkResponse(t, "nick", Nick{
@ -79,10 +79,9 @@ func TestHandleIRCJoin(t *testing.T) {
func TestHandleIRCPart(t *testing.T) { func TestHandleIRCPart(t *testing.T) {
res := dispatchMessage(&irc.Message{ res := dispatchMessage(&irc.Message{
Command: irc.Part, Command: irc.Part,
Nick: "parting", Nick: "parting",
Params: []string{"#chan"}, Params: []string{"#chan", "the reason"},
Trailing: "the reason",
}) })
checkResponse(t, "part", Part{ checkResponse(t, "part", Part{
@ -112,10 +111,9 @@ func TestHandleIRCMode(t *testing.T) {
func TestHandleIRCMessage(t *testing.T) { func TestHandleIRCMessage(t *testing.T) {
res := dispatchMessage(&irc.Message{ res := dispatchMessage(&irc.Message{
Command: irc.Privmsg, Command: irc.Privmsg,
Nick: "nick", Nick: "nick",
Params: []string{"#chan"}, Params: []string{"#chan", "the message"},
Trailing: "the message",
}) })
checkResponse(t, "message", Chat{ checkResponse(t, "message", Chat{
@ -126,10 +124,9 @@ func TestHandleIRCMessage(t *testing.T) {
}, res) }, res)
res = dispatchMessage(&irc.Message{ res = dispatchMessage(&irc.Message{
Command: irc.Privmsg, Command: irc.Privmsg,
Nick: "someone", Nick: "someone",
Params: []string{"nick"}, Params: []string{"nick", "the message"},
Trailing: "the message",
}) })
checkResponse(t, "pm", Chat{ checkResponse(t, "pm", Chat{
@ -141,9 +138,9 @@ func TestHandleIRCMessage(t *testing.T) {
func TestHandleIRCQuit(t *testing.T) { func TestHandleIRCQuit(t *testing.T) {
res := dispatchMessage(&irc.Message{ res := dispatchMessage(&irc.Message{
Command: irc.Quit, Command: irc.Quit,
Nick: "nick", Nick: "nick",
Trailing: "the reason", Params: []string{"the reason"},
}) })
checkResponse(t, "quit", Quit{ checkResponse(t, "quit", Quit{
@ -182,8 +179,8 @@ func TestHandleIRCWhois(t *testing.T) {
Params: []string{"", "", "srv.com"}, Params: []string{"", "", "srv.com"},
}) })
i.dispatchMessage(&irc.Message{ i.dispatchMessage(&irc.Message{
Command: irc.ReplyWhoisChannels, Command: irc.ReplyWhoisChannels,
Trailing: "#chan #chan1", Params: []string{"#chan #chan1"},
}) })
i.dispatchMessage(&irc.Message{Command: irc.ReplyEndOfWhois}) i.dispatchMessage(&irc.Message{Command: irc.ReplyEndOfWhois})
@ -199,9 +196,8 @@ func TestHandleIRCWhois(t *testing.T) {
func TestHandleIRCTopic(t *testing.T) { func TestHandleIRCTopic(t *testing.T) {
res := dispatchMessage(&irc.Message{ res := dispatchMessage(&irc.Message{
Command: irc.ReplyTopic, Command: irc.ReplyTopic,
Params: []string{"target", "#chan"}, Params: []string{"target", "#chan", "the topic"},
Trailing: "the topic",
}) })
checkResponse(t, "topic", Topic{ checkResponse(t, "topic", Topic{
@ -218,14 +214,12 @@ func TestHandleIRCNames(t *testing.T) {
i := newIRCHandler(c, s) i := newIRCHandler(c, s)
i.dispatchMessage(&irc.Message{ i.dispatchMessage(&irc.Message{
Command: irc.ReplyNamReply, Command: irc.ReplyNamReply,
Params: []string{"", "", "#chan"}, Params: []string{"", "", "#chan", "a b c"},
Trailing: "a b c",
}) })
i.dispatchMessage(&irc.Message{ i.dispatchMessage(&irc.Message{
Command: irc.ReplyNamReply, Command: irc.ReplyNamReply,
Params: []string{"", "", "#chan"}, Params: []string{"", "", "#chan", "d"},
Trailing: "d",
}) })
i.dispatchMessage(&irc.Message{ i.dispatchMessage(&irc.Message{
Command: irc.ReplyEndOfNames, Command: irc.ReplyEndOfNames,
@ -246,16 +240,16 @@ func TestHandleIRCMotd(t *testing.T) {
i := newIRCHandler(c, s) i := newIRCHandler(c, s)
i.dispatchMessage(&irc.Message{ i.dispatchMessage(&irc.Message{
Command: irc.ReplyMotdStart, Command: irc.ReplyMotdStart,
Trailing: "motd title", Params: []string{"motd title"},
}) })
i.dispatchMessage(&irc.Message{ i.dispatchMessage(&irc.Message{
Command: irc.ReplyMotd, Command: irc.ReplyMotd,
Trailing: "line 1", Params: []string{"line 1"},
}) })
i.dispatchMessage(&irc.Message{ i.dispatchMessage(&irc.Message{
Command: irc.ReplyMotd, Command: irc.ReplyMotd,
Trailing: "line 2", Params: []string{"line 2"},
}) })
i.dispatchMessage(&irc.Message{Command: irc.ReplyEndOfMotd}) i.dispatchMessage(&irc.Message{Command: irc.ReplyEndOfMotd})