From 35727fb2b85387b9fc2744e198cf7dc0e8561e67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken-H=C3=A5vard=20Lieng?= Date: Thu, 21 May 2020 05:24:26 +0200 Subject: [PATCH] Fix dm tab being opened on server messages --- pkg/irc/client.go | 4 +++ pkg/irc/message.go | 4 +++ server/irc_handler.go | 73 ++++++++++++++++++++++--------------------- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/pkg/irc/client.go b/pkg/irc/client.go index c5965d7e..b335a21b 100644 --- a/pkg/irc/client.go +++ b/pkg/irc/client.go @@ -75,6 +75,10 @@ func (c *Client) GetNick() string { return nick } +func (c *Client) Is(nick string) bool { + return c.EqualFold(nick, c.GetNick()) +} + func (c *Client) setNick(nick string) { c.lock.Lock() c.nick = nick diff --git a/pkg/irc/message.go b/pkg/irc/message.go index 850f29df..ed67e54c 100644 --- a/pkg/irc/message.go +++ b/pkg/irc/message.go @@ -19,6 +19,10 @@ func (m *Message) LastParam() string { return "" } +func (m *Message) IsFromServer() bool { + return m.Nick == "" || strings.Contains(m.Nick, ".") +} + func (m *Message) ToCTCP() *CTCP { return DecodeCTCP(m.LastParam()) } diff --git a/server/irc_handler.go b/server/irc_handler.go index 9a45a13a..d714c46b 100644 --- a/server/irc_handler.go +++ b/server/irc_handler.go @@ -117,7 +117,7 @@ func (i *ircHandler) nick(msg *irc.Message) { channelStore.RenameUser(msg.Nick, msg.LastParam(), i.client.Host) - if msg.LastParam() == i.client.GetNick() { + if i.client.Is(msg.LastParam()) { go i.state.user.SetNick(msg.LastParam(), i.client.Host) } } @@ -132,7 +132,7 @@ func (i *ircHandler) join(msg *irc.Message) { channel := msg.Params[0] channelStore.AddUser(msg.Nick, i.client.Host, channel) - if msg.Nick == i.client.GetNick() { + if i.client.Is(msg.Nick) { // In case no topic is set and there's a cached one that needs to be cleared i.client.Topic(channel) @@ -160,7 +160,7 @@ func (i *ircHandler) part(msg *irc.Message) { channelStore.RemoveUser(msg.Nick, i.client.Host, part.Channel) - if msg.Nick == i.client.GetNick() { + if i.client.Is(msg.Nick) { go i.state.user.RemoveChannel(i.client.Host, part.Channel) } } @@ -179,35 +179,6 @@ func (i *ircHandler) mode(msg *irc.Message) { } } -func (i *ircHandler) receiveDCCSend(pack *irc.DCCSend, msg *irc.Message) { - cfg := i.state.srv.Config() - - if cfg.DCC.Enabled { - if cfg.DCC.Autoget.Enabled { - file, err := os.OpenFile(storage.Path.DownloadedFile(i.state.user.Username, pack.File), os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - return - } - defer file.Close() - - irc.DownloadDCC(file, pack, i.dccProgress) - } else { - i.state.setPendingDCC(pack.File, pack) - - i.state.sendJSON("dcc_send", DCCSend{ - Server: i.client.Host, - From: msg.Nick, - Filename: pack.File, - URL: fmt.Sprintf("%s://%s/downloads/%s/%s", - i.state.String("scheme"), i.state.String("host"), i.state.user.Username, pack.File), - }) - - time.Sleep(150 * time.Second) - i.state.deletePendingDCC(pack.File) - } - } -} - func (i *ircHandler) message(msg *irc.Message) { if ctcp := msg.ToCTCP(); ctcp != nil { if ctcp.Command == "DCC" && strings.HasPrefix(ctcp.Params, "SEND") { @@ -228,9 +199,12 @@ func (i *ircHandler) message(msg *irc.Message) { } target := msg.Params[0] - if target == i.client.GetNick() { + if i.client.Is(target) { i.state.sendJSON("pm", message) - i.state.user.AddOpenDM(i.client.Host, message.From) + + if !msg.IsFromServer() { + i.state.user.AddOpenDM(i.client.Host, message.From) + } target = message.From } else { @@ -238,7 +212,7 @@ func (i *ircHandler) message(msg *irc.Message) { i.state.sendJSON("message", message) } - if target != "*" { + if target != "*" && !msg.IsFromServer() { go i.state.user.LogMessage(message.ID, i.client.Host, msg.Nick, target, msg.LastParam()) } @@ -432,6 +406,35 @@ func (i *ircHandler) error(msg *irc.Message) { }) } +func (i *ircHandler) receiveDCCSend(pack *irc.DCCSend, msg *irc.Message) { + cfg := i.state.srv.Config() + + if cfg.DCC.Enabled { + if cfg.DCC.Autoget.Enabled { + file, err := os.OpenFile(storage.Path.DownloadedFile(i.state.user.Username, pack.File), os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return + } + defer file.Close() + + irc.DownloadDCC(file, pack, i.dccProgress) + } else { + i.state.setPendingDCC(pack.File, pack) + + i.state.sendJSON("dcc_send", DCCSend{ + Server: i.client.Host, + From: msg.Nick, + Filename: pack.File, + URL: fmt.Sprintf("%s://%s/downloads/%s/%s", + i.state.String("scheme"), i.state.String("host"), i.state.user.Username, pack.File), + }) + + time.Sleep(150 * time.Second) + i.state.deletePendingDCC(pack.File) + } + } +} + func (i *ircHandler) initHandlers() { i.handlers = map[string]func(*irc.Message){ irc.Nick: i.nick,