From dc58a7079463c12cd0901fe6d5d4df7d26df4bc1 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 25 Feb 2022 21:05:10 +0100 Subject: [PATCH] msgstore_fs: fix direct message targets When fetching messages via draft/chathistory from a conversation with another user, soju would send the following: :sender PRIVMSG sender :hey instead of :sender PRIVMSG recipient :hey because the file-system message store format doesn't contain the original PRIVMSG target. Fix this by doing some guesswork. --- msgstore_fs.go | 19 ++++++++++++++----- user.go | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/msgstore_fs.go b/msgstore_fs.go index ebed557..b7bd850 100644 --- a/msgstore_fs.go +++ b/msgstore_fs.go @@ -80,6 +80,7 @@ type fsMessageStoreFile struct { // https://github.com/znc/znc/blob/master/modules/log.cpp type fsMessageStore struct { root string + user *User // Write-only files used by Append files map[string]*fsMessageStoreFile // indexed by entity @@ -88,9 +89,10 @@ type fsMessageStore struct { var _ messageStore = (*fsMessageStore)(nil) var _ chatHistoryMessageStore = (*fsMessageStore)(nil) -func newFSMessageStore(root, username string) *fsMessageStore { +func newFSMessageStore(root string, user *User) *fsMessageStore { return &fsMessageStore{ - root: filepath.Join(root, escapeFilename(username)), + root: filepath.Join(root, escapeFilename(user.Username)), + user: user, files: make(map[string]*fsMessageStoreFile), } } @@ -250,7 +252,7 @@ func formatMessage(msg *irc.Message) string { } } -func parseMessage(line, entity string, ref time.Time, events bool) (*irc.Message, time.Time, error) { +func (ms *fsMessageStore) parseMessage(line string, network *Network, entity string, ref time.Time, events bool) (*irc.Message, time.Time, error) { var hour, minute, second int _, err := fmt.Sscanf(line, "[%02d:%02d:%02d] ", &hour, &minute, &second) if err != nil { @@ -372,6 +374,13 @@ func parseMessage(line, entity string, ref time.Time, events bool) (*irc.Message } prefix = &irc.Prefix{Name: sender} + if entity == sender { + // This is a direct message from a user to us. We don't store own + // our nickname in the logs, so grab it from the network settings. + // Not very accurate since this may not match our nick at the time + // the message was received, but we can't do a lot better. + entity = GetNick(ms.user, network) + } params = []string{entity, text} } @@ -413,7 +422,7 @@ func (ms *fsMessageStore) parseMessagesBefore(network *Network, entity string, r } for sc.Scan() { - msg, t, err := parseMessage(sc.Text(), entity, ref, events) + msg, t, err := ms.parseMessage(sc.Text(), network, entity, ref, events) if err != nil { return nil, err } else if msg == nil || !t.After(end) { @@ -459,7 +468,7 @@ func (ms *fsMessageStore) parseMessagesAfter(network *Network, entity string, re var history []*irc.Message sc := bufio.NewScanner(f) for sc.Scan() && len(history) < limit { - msg, t, err := parseMessage(sc.Text(), entity, ref, events) + msg, t, err := ms.parseMessage(sc.Text(), network, entity, ref, events) if err != nil { return nil, err } else if msg == nil || !t.After(ref) { diff --git a/user.go b/user.go index d31483b..0364ff3 100644 --- a/user.go +++ b/user.go @@ -451,7 +451,7 @@ func newUser(srv *Server, record *User) *user { var msgStore messageStore if logPath := srv.Config().LogPath; logPath != "" { - msgStore = newFSMessageStore(logPath, record.Username) + msgStore = newFSMessageStore(logPath, record) } else { msgStore = newMemoryMessageStore() }