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

View File

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