From 5cf2822c346b737294737ed80e3853e3507958cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken-H=C3=A5vard=20Lieng?= Date: Mon, 1 Jun 2015 01:48:07 +0200 Subject: [PATCH] Store nick in Message.Nick, keep full prefix in Message.Prefix --- server/irc.go | 12 ++++++------ server/message_handler.go | 34 ++++++++++++++-------------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/server/irc.go b/server/irc.go index 95c5ec21..c5ffe7c9 100644 --- a/server/irc.go +++ b/server/irc.go @@ -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 } diff --git a/server/message_handler.go b/server/message_handler.go index dd20605f..d8a7fff8 100644 --- a/server/message_handler.go +++ b/server/message_handler.go @@ -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:], " "), })