irclib: Fix target computation

It mistakenly used the bot's nick as target when the message is in private,
so 'more' after a private message always answered the user did not send
a command before (because said command was attributed to the bot)
This commit is contained in:
Valentin Lorentz 2021-05-26 00:30:27 +02:00
parent 710d16f301
commit e59e0f6908
2 changed files with 42 additions and 4 deletions

View File

@ -32,6 +32,35 @@ import re
from supybot.test import *
class MiscTestCase(PluginTestCase):
plugins = ('Misc', 'Utilities')
def testMore(self):
newprefix = 'fooo!bar@baaaaaaaaaaaaaaz'
# just for the sale of filling irc.state.nicksToHostmasks:
self.irc.feedMsg(ircmsgs.IrcMsg(command='CHGHOST', args=(self.nick, 'baz'),
prefix=self.prefix))
self.irc.feedMsg(ircmsgs.IrcMsg(command='CHGHOST', args=('foo', 'baz'),
prefix='foo!bar@oldbaz'))
self.assertResponse('echo %s' % ('abc '*400),
'abc '*112 + ' \x02(3 more messages)\x02',
frm=newprefix)
m = self.assertResponse('more',
'abc '*112 + ' \x02(2 more messages)\x02',
frm=newprefix)
self.assertResponse('more',
'abc '*112 + ' \x02(1 more message)\x02',
frm=newprefix)
self.assertResponse('more',
' '.join(['abc']*(400-112*3)),
frm=newprefix)
self.assertResponse('more',
"Error: That's all, there is no more.",
frm=newprefix)
class MiscTestCase(ChannelPluginTestCase):
plugins = ('Misc', 'Utilities', 'Anonymous', 'Plugin',
'Channel', 'Dict', 'User', 'String')

View File

@ -511,7 +511,10 @@ class RichReplyMethods(object):
# change the state of this Irc object.
if to is not None:
self.to = self.to or to
target = self.private and self.to or self.msg.args[0]
if self.private or self.msg.channel is None:
target = self.msg.nick
else:
target = self.to or self.msg.args[0]
return target
def replies(self, L, prefixer=None, joiner=None,
@ -703,11 +706,17 @@ class ReplyIrcProxy(RichReplyMethods):
'Old code alert: there is no longer a "msg" argument to reply.'
kwargs.pop('noLengthCheck', None)
if 'target' not in kwargs:
target = kwargs.get('private', False) and kwargs.get('to', None) \
or msg.args[0]
# TODO: deduplicate this with _getTarget
# TODO: it looks like 'target' is never in kwargs.
# (an old version of this code crashed when 'target' was
# not given, but no one complained). Remove the conditional?
if kwargs.get('private', False) or msg.channel is None:
kwargs['target'] = msg.nick
else:
kwargs['target'] = kwargs.get('to', None) or msg.args[0]
if 'prefixNick' not in kwargs:
kwargs['prefixNick'] = self._defaultPrefixNick(msg)
self._sendReply(s, target=target, msg=msg, **kwargs)
self._sendReply(s, msg=msg, **kwargs)
def __getattr__(self, attr):
return getattr(self.irc, attr)