Store nick in Message.Nick, keep full prefix in Message.Prefix

This commit is contained in:
Ken-Håvard Lieng 2015-06-01 01:48:07 +02:00
parent 65b3aac977
commit 5cf2822c34
2 changed files with 20 additions and 26 deletions

View File

@ -52,6 +52,7 @@ const (
type Message struct {
Prefix string
Nick string
Command string
Params []string
Trailing string
@ -246,8 +247,6 @@ func (i *IRC) recv() {
}
msg := parseMessage(line)
msg.Prefix = parseUser(msg.Prefix)
i.Messages <- msg
switch msg.Command {
@ -276,6 +275,7 @@ func parseMessage(line string) *Message {
if strings.HasPrefix(line, ":") {
cmdStart = strings.Index(line, " ") + 1
msg.Prefix = line[1 : cmdStart-1]
msg.Nick = parseNick(msg.Prefix)
}
if i := strings.Index(line, " :"); i > 0 {
@ -296,9 +296,9 @@ func parseMessage(line string) *Message {
return &msg
}
func parseUser(user string) string {
if i := strings.Index(user, "!"); i > 0 {
return user[:i]
func parseNick(prefix string) string {
if i := strings.Index(prefix, "!"); i > 0 {
return prefix[:i]
}
return user
return prefix
}

View File

@ -23,24 +23,22 @@ func handleMessages(irc *IRC, session *Session) {
case NICK:
session.sendJSON("nick", Nick{
Server: irc.Host,
Old: msg.Prefix,
Old: msg.Nick,
New: msg.Trailing,
})
channelStore.RenameUser(msg.Prefix, msg.Trailing, irc.Host)
channelStore.RenameUser(msg.Nick, msg.Trailing, irc.Host)
case JOIN:
user := msg.Prefix
session.sendJSON("join", Join{
Server: irc.Host,
User: user,
User: msg.Nick,
Channels: msg.Params,
})
channelStore.AddUser(user, irc.Host, msg.Params[0])
channelStore.AddUser(msg.Nick, irc.Host, msg.Params[0])
if user == irc.GetNick() {
if msg.Nick == irc.GetNick() {
session.user.AddChannel(storage.Channel{
Server: irc.Host,
Name: msg.Params[0],
@ -48,20 +46,18 @@ func handleMessages(irc *IRC, session *Session) {
}
case PART:
user := msg.Prefix
session.sendJSON("part", Part{
Join: Join{
Server: irc.Host,
User: user,
User: msg.Nick,
Channels: msg.Params,
},
Reason: msg.Trailing,
})
channelStore.RemoveUser(user, irc.Host, msg.Params[0])
channelStore.RemoveUser(msg.Nick, irc.Host, msg.Params[0])
if user == irc.GetNick() {
if msg.Nick == irc.GetNick() {
session.user.RemoveChannel(irc.Host, msg.Params[0])
}
@ -82,32 +78,30 @@ func handleMessages(irc *IRC, session *Session) {
if msg.Params[0] == irc.GetNick() {
session.sendJSON("pm", Chat{
Server: irc.Host,
From: msg.Prefix,
From: msg.Nick,
Message: msg.Trailing,
})
} else {
session.sendJSON("message", Chat{
Server: irc.Host,
From: msg.Prefix,
From: msg.Nick,
To: msg.Params[0],
Message: msg.Trailing,
})
}
if msg.Params[0] != "*" {
session.user.LogMessage(irc.Host, msg.Prefix, msg.Params[0], msg.Trailing)
session.user.LogMessage(irc.Host, msg.Nick, msg.Params[0], msg.Trailing)
}
case QUIT:
user := msg.Prefix
session.sendJSON("quit", Quit{
Server: irc.Host,
User: user,
User: msg.Nick,
Reason: msg.Trailing,
})
channelStore.RemoveUserAll(user, irc.Host)
channelStore.RemoveUserAll(msg.Nick, irc.Host)
case RPL_WELCOME,
RPL_YOURHOST,
@ -119,7 +113,7 @@ func handleMessages(irc *IRC, session *Session) {
RPL_LUSERME:
session.sendJSON("pm", Chat{
Server: irc.Host,
From: msg.Prefix,
From: msg.Nick,
Message: strings.Join(msg.Params[1:], " "),
})