Merge pull request #4 from DanielOaks/fix-trailing

Remove Trailing param
This commit is contained in:
Ken-Håvard Lieng 2016-12-16 22:07:26 +01:00 committed by GitHub
commit 29fad58252
5 changed files with 69 additions and 67 deletions

View File

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

View File

@ -9,7 +9,6 @@ type Message struct {
Nick string
Command string
Params []string
Trailing string
}
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 {
cmdEnd = i
msg.Trailing = line[i+2:]
trailing = line[i+2:]
usesTrailing = true
}
cmd := strings.Split(line[cmdStart:cmdEnd], " ")
@ -42,8 +45,8 @@ func parseMessage(line string) *Message {
msg.Params = cmd[1:]
}
if msg.Trailing != "" {
msg.Params = append(msg.Params, msg.Trailing)
if usesTrailing {
msg.Params = append(msg.Params, trailing)
}
return &msg

View File

@ -18,7 +18,6 @@ func TestParseMessage(t *testing.T) {
Nick: "user",
Command: "CMD",
Params: []string{"#chan", "some message"},
Trailing: "some message",
},
}, {
":nick!user@host.com CMD a b\r\n",
@ -32,7 +31,7 @@ func TestParseMessage(t *testing.T) {
"CMD a b :\r\n",
&Message{
Command: "CMD",
Params: []string{"a", "b"},
Params: []string{"a", "b", ""},
},
}, {
"CMD a b\r\n",
@ -50,7 +49,6 @@ func TestParseMessage(t *testing.T) {
&Message{
Command: "CMD",
Params: []string{"tests and stuff"},
Trailing: "tests and stuff",
},
}, {
":nick@host.com CMD\r\n",

View File

@ -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.Trailing,
New: msg.Params[len(msg.Params)-1],
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) {
@ -84,13 +84,20 @@ func (i *ircHandler) join(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{
Join: Join{
Server: i.client.Host,
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])
@ -118,7 +125,7 @@ func (i *ircHandler) message(msg *irc.Message) {
message := Chat{
Server: i.client.Host,
From: msg.Nick,
Message: msg.Trailing,
Message: msg.Params[len(msg.Params)-1],
}
if msg.Params[0] == i.client.GetNick() {
@ -129,7 +136,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.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{
Server: i.client.Host,
User: msg.Nick,
Reason: msg.Trailing,
Reason: msg.Params[len(msg.Params)-1],
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) {
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) {
@ -176,14 +183,14 @@ func (i *ircHandler) topic(msg *irc.Message) {
i.session.sendJSON("topic", Topic{
Server: i.client.Host,
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) {
users := strings.Split(strings.TrimSuffix(msg.Trailing, " "), " ")
users := strings.Split(strings.TrimSuffix(msg.Params[len(msg.Params)-1], " "), " ")
userBuffer := i.userBuffers[msg.Params[2]]
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) {
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) {
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) {
@ -270,5 +277,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.Trailing)
log.Println(i.GetNick()+":", msg.Prefix, msg.Command, msg.Params, msg.Params[len(msg.Params)-1])
}

View File

@ -53,7 +53,7 @@ func TestHandleIRCNick(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.Nick,
Nick: "old",
Trailing: "new",
Params: []string{"new"},
})
checkResponse(t, "nick", Nick{
@ -81,8 +81,7 @@ func TestHandleIRCPart(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.Part,
Nick: "parting",
Params: []string{"#chan"},
Trailing: "the reason",
Params: []string{"#chan", "the reason"},
})
checkResponse(t, "part", Part{
@ -114,8 +113,7 @@ func TestHandleIRCMessage(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.Privmsg,
Nick: "nick",
Params: []string{"#chan"},
Trailing: "the message",
Params: []string{"#chan", "the message"},
})
checkResponse(t, "message", Chat{
@ -128,8 +126,7 @@ func TestHandleIRCMessage(t *testing.T) {
res = dispatchMessage(&irc.Message{
Command: irc.Privmsg,
Nick: "someone",
Params: []string{"nick"},
Trailing: "the message",
Params: []string{"nick", "the message"},
})
checkResponse(t, "pm", Chat{
@ -143,7 +140,7 @@ func TestHandleIRCQuit(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.Quit,
Nick: "nick",
Trailing: "the reason",
Params: []string{"the reason"},
})
checkResponse(t, "quit", Quit{
@ -183,7 +180,7 @@ func TestHandleIRCWhois(t *testing.T) {
})
i.dispatchMessage(&irc.Message{
Command: irc.ReplyWhoisChannels,
Trailing: "#chan #chan1",
Params: []string{"#chan #chan1"},
})
i.dispatchMessage(&irc.Message{Command: irc.ReplyEndOfWhois})
@ -200,8 +197,7 @@ func TestHandleIRCWhois(t *testing.T) {
func TestHandleIRCTopic(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.ReplyTopic,
Params: []string{"target", "#chan"},
Trailing: "the topic",
Params: []string{"target", "#chan", "the topic"},
})
checkResponse(t, "topic", Topic{
@ -219,13 +215,11 @@ func TestHandleIRCNames(t *testing.T) {
i.dispatchMessage(&irc.Message{
Command: irc.ReplyNamReply,
Params: []string{"", "", "#chan"},
Trailing: "a b c",
Params: []string{"", "", "#chan", "a b c"},
})
i.dispatchMessage(&irc.Message{
Command: irc.ReplyNamReply,
Params: []string{"", "", "#chan"},
Trailing: "d",
Params: []string{"", "", "#chan", "d"},
})
i.dispatchMessage(&irc.Message{
Command: irc.ReplyEndOfNames,
@ -247,15 +241,15 @@ func TestHandleIRCMotd(t *testing.T) {
i.dispatchMessage(&irc.Message{
Command: irc.ReplyMotdStart,
Trailing: "motd title",
Params: []string{"motd title"},
})
i.dispatchMessage(&irc.Message{
Command: irc.ReplyMotd,
Trailing: "line 1",
Params: []string{"line 1"},
})
i.dispatchMessage(&irc.Message{
Command: irc.ReplyMotd,
Trailing: "line 2",
Params: []string{"line 2"},
})
i.dispatchMessage(&irc.Message{Command: irc.ReplyEndOfMotd})