From 7cd700b4ae7cf4d7b4f5a19581ee19d3a91abdea Mon Sep 17 00:00:00 2001 From: Matias Wilkman Date: Mon, 9 Oct 2023 20:31:50 +0300 Subject: [PATCH] Seen: show when the target is currently in the channel (#1559) --- plugins/Seen/plugin.py | 49 +++++++++++++++++++++++++++++++----------- plugins/Seen/test.py | 28 ++++++++++++++++++++---- 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/plugins/Seen/plugin.py b/plugins/Seen/plugin.py index cf789c63b..9435fe21a 100644 --- a/plugins/Seen/plugin.py +++ b/plugins/Seen/plugin.py @@ -195,9 +195,14 @@ class Seen(callbacks.Plugin): if len(results) == 1: (nick, info) = results[0] (when, said) = info - reply = format(_('%s was last seen in %s %s ago'), - nick, channel, - utils.timeElapsed(time.time()-when)) + if nick in irc.state.channels[channel].users: + reply = format(_('%s was last seen in %s %s ago, and is in the channel now'), + nick, channel, + utils.timeElapsed(time.time()-when)) + else: + reply = format(_('%s was last seen in %s %s ago'), + nick, channel, + utils.timeElapsed(time.time()-when)) if self.registryValue('showLastMessage', channel, irc.network): if minisix.PY2: said = said.decode('utf8') @@ -207,13 +212,20 @@ class Seen(callbacks.Plugin): L = [] for (nick, info) in results: (when, said) = info - L.append(format(_('%s (%s ago)'), nick, - utils.timeElapsed(time.time()-when))) + if nick in irc.state.channels[channel].users: + L.append(format(_('%s (%s ago, and is in the channel now)'), nick, + utils.timeElapsed(time.time()-when))) + else: + L.append(format(_('%s (%s ago)'), nick, + utils.timeElapsed(time.time()-when))) irc.reply(format(_('%s could be %L'), name, (L, _('or')))) else: irc.reply(format(_('I haven\'t seen anyone matching %s.'), name)) except KeyError: - irc.reply(format(_('I have not seen %s.'), name)) + if name in irc.state.channels[channel].users: + irc.reply(format(_("%s is in the channel right now."), name)) + else: + irc.reply(format(_('I have not seen %s.'), name)) def _checkChannelPresence(self, irc, channel, target, you): if channel not in irc.state.channels: @@ -277,8 +289,13 @@ class Seen(callbacks.Plugin): db = self.db try: (when, said) = db.seen(channel, '') - reply = format(_('Someone was last seen in %s %s ago'), - channel, utils.timeElapsed(time.time()-when)) + pattern = r'<(.*?)>' + match = re.search(pattern, said) + if not match: + irc.error(format(_('I couldn\'t parse the nick of the speaker of the last line.')), Raise=True) + nick = match.group(1) + reply = format(_('Last seen in %s was %s, %s ago'), + channel, nick, utils.timeElapsed(time.time()-when)) if self.registryValue('showLastMessage', channel, irc.network): reply = _('%s: %s') % (reply, said) irc.reply(reply) @@ -303,14 +320,22 @@ class Seen(callbacks.Plugin): db = self.db try: (when, said) = db.seen(channel, user.id) - reply = format(_('%s was last seen in %s %s ago'), - user.name, channel, - utils.timeElapsed(time.time()-when)) + if user.name in irc.state.channels[channel].users: + reply = format(_('%s was last seen in %s %s ago and is in the channel now'), + user.name, channel, + utils.timeElapsed(time.time()-when)) + else: + reply = format(_('%s was last seen in %s %s ago'), + user.name, channel, + utils.timeElapsed(time.time()-when)) if self.registryValue('showLastMessage', channel, irc.network): reply = _('%s: %s') % (reply, said) irc.reply(reply) except KeyError: - irc.reply(format(_('I have not seen %s.'), user.name)) + if user.name in irc.state.channels[channel].users: + irc.reply(format(_("%s is in the channel right now."), user.name)) + else: + irc.reply(format(_('I have not seen %s.'), user.name)) @internationalizeDocstring def user(self, irc, msg, args, channel, user): diff --git a/plugins/Seen/test.py b/plugins/Seen/test.py index 8a45b15b9..79be570e7 100644 --- a/plugins/Seen/test.py +++ b/plugins/Seen/test.py @@ -83,12 +83,10 @@ class ChannelDBTestCase(ChannelPluginTestCase): self.assertNotError('seen last') self.assertNotError('list') self.assertNotError('config plugins.Seen.minimumNonWildcard 2') - self.assertError('seen *') - self.assertNotError('seen %s' % self.nick) - m = self.assertNotError('seen %s' % self.nick.upper()) - self.assertIn(self.nick.upper(), m.args[1]) self.assertRegexp('seen user %s' % self.nick, '^%s was last seen' % self.nick) + self.assertError('seen *') + self.assertNotError('seen %s' % self.nick) self.assertNotError('config plugins.Seen.minimumNonWildcard 0') orig = conf.supybot.protocols.irc.strictRfc() try: @@ -101,6 +99,28 @@ class ChannelDBTestCase(ChannelPluginTestCase): finally: conf.supybot.protocols.irc.strictRfc.setValue(orig) + + def testSeenNickInChannel(self): + # Test case: 'seen' with a nick (user in channel) + self.irc.feedMsg(ircmsgs.join(self.channel, self.irc.nick, + prefix=self.prefix)) + self.assertRegexp('seen %s' % self.nick, 'is in the channel right now') + m = self.assertNotError('seen %s' % self.nick.upper()) + self.assertIn(self.nick.upper(), m.args[1]) + + def testSeenUserInChannel(self): + # Test case: 'seen' with a user (user in channel) + self.irc.feedMsg(ircmsgs.join(self.channel, self.irc.nick, + prefix=self.prefix)) + self.assertRegexp('seen user %s' % self.nick, 'is in the channel right now') + + def testSeenNickNotInChannel(self): + # Test case: 'seen' with a nick (user not in channel) + testnick = "user123" + self.irc.feedMsg(ircmsgs.join(self.channel, testnick, "user123!baz")) + self.irc.feedMsg(ircmsgs.part(self.channel, prefix="user123!baz")) + self.assertNotRegexp("seen %s" % testnick, "is in the channel right now") + def testSeenNoUser(self): self.irc.feedMsg(ircmsgs.join(self.channel, self.irc.nick, prefix=self.prefix))