diff --git a/csbot/plugins/whois.py b/csbot/plugins/whois.py index d782acde..6849eb39 100644 --- a/csbot/plugins/whois.py +++ b/csbot/plugins/whois.py @@ -14,11 +14,23 @@ def whois_lookup(self, nick, channel, db=None): """Performs a whois lookup for a nick""" db = db or self.whoisdb - for ident in (self.identify_user(nick, channel), # lookup channel specific first - self.identify_user(nick)): # default fallback - user = db.find_one(ident) - if user: - return user['data'] + for d in (self.whois_lookup_channel(nick, channel), + self.whois_lookup_global(nick)): + if d: + return d + + def whois_lookup_channel(self, nick, channel): + return self._lookup(self.identify_user(nick, channel)) + + def whois_lookup_global(self, nick): + return self._lookup(self.identify_user(nick)) + + def _lookup(self, ident): + user = self.whoisdb.find_one(ident) + if user: + return user['data'] + + return None def whois_set(self, nick, whois_str, channel=None, db=None): db = db or self.whoisdb @@ -48,27 +60,64 @@ def whois(self, e): else: e.reply('{}: {}'.format(nick_, str(res))) + @Plugin.command('whois.setlocal', help=('whois.setlocal [whois_text]: sets the whois text' + ' for the user, but only for the current channel')) + def setlocal(self, e): + """Allow a user to associate data with themselves for this channel.""" + self.whois_set(nick(e['user']), e['data'], channel=e['channel']) - @Plugin.command('whois.setdefault', help=('whois.setdefault [default_whois]: sets the default' - ' whois text for the user, used when no channel-specific' - ' one is set')) + set_help = ('whois.setdefault [default_whois]: sets the default' + ' whois text for the user, used when no channel-specific' + ' one is set') + @Plugin.command('whois.setdefault', help=set_help) def setdefault(self, e): self.whois_set(nick(e['user']), e['data'], channel=None) - @Plugin.command('whois.set') + @Plugin.command('whois.set', help='sets the whois text for this channel') def set(self, e): - """Allow a user to associate data with themselves for this channel.""" - self.whois_set(nick(e['user']), e['data'], channel=e['channel']) - + nick_ = nick(e['user']) + channel = e['channel'] + old_whois = self.whois_lookup_global(nick_) + local_whois = self.whois_lookup_channel(nick_, channel) + + if local_whois: + self.setlocal(e) + fmt = 'set new local whois for {} (was: "{}"). use whois.setdefault to set for all channels' + self.bot.reply(nick_, fmt.format(channel, str(local_whois))) + elif old_whois: + self.setlocal(e) + fmt = 'set new local whois for {}. use whois.setdefault to set for all channels' + self.bot.reply(nick_, fmt.format(channel)) + else: + self.setdefault(e) + self.bot.reply(nick_, 'set default whois for all channels') - @Plugin.command('whois.unset') - def unset(self, e): + @Plugin.command('whois.unsetlocal', help=('whois.unsetlocal: unsets the local whois text for the user' + ' but only for this channel')) + def unsetlocal(self, e): self.whois_unset(nick(e['user']), channel=e['channel']) - @Plugin.command('whois.unsetdefault') + unset_help = ('whois.unsetdefault: unsets the default whois text for the user.' + ' local, channel-specific, whois text is unaffected') + @Plugin.command('whois.unsetdefault', help=unset_help) def unsetdefault(self, e): self.whois_unset(nick(e['user'])) + @Plugin.command('whois.unset', help='unsets the currently set whois for this channel') + def unset(self, e): + nick_ = nick(e['user']) + old_local_whois = self.whois_lookup_channel(nick_, e['channel']) + old_global_whois = self.whois_lookup_global(nick_) + + if old_local_whois: + self.unsetlocal(e) + fmt = 'unset local whois for {} (was: "{}"), use whois.unsetdefault to unset the global default' + self.bot.reply(nick_, fmt.format(e['channel'], str(old_local_whois))) + elif old_global_whois: + self.unsetdefault(e) + fmt = 'unset default whois for all channels (was: {}). use whois.setdefault to re-set it' + self.bot.reply(nick_, fmt.format(str(old_global_whois))) + def identify_user(self, nick, channel=None): """Identify a user: by account if authed, if not, by nick. Produces a dict suitable for throwing at mongo.""" diff --git a/csbot/test/test_plugin_whois.py b/csbot/test/test_plugin_whois.py index b15615f5..bd6dcb48 100644 --- a/csbot/test/test_plugin_whois.py +++ b/csbot/test/test_plugin_whois.py @@ -101,22 +101,22 @@ def test_whois_setdefault_unset(self): @failsafe @run_client def test_client_reply_whois_after_set(self): - yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.set test1') + yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.setlocal test1') yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois') self.assert_sent('NOTICE {} :{}'.format('#First', 'Nick: test1')) @failsafe @run_client def test_client_reply_whois_different_channel(self): - yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.set test1') + yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.setlocal test1') yield from self._recv_privmsg('Nick!~user@host', '#Second', '!whois') self.assert_sent('NOTICE {} :{}'.format('#Second', 'No data for Nick')) @failsafe @run_client def test_client_reply_whois_multiple_users_channels(self): - yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.set test1') - yield from self._recv_privmsg('Nick!~user@host', '#Second', '!whois.set test2') + yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.setlocal test1') + yield from self._recv_privmsg('Nick!~user@host', '#Second', '!whois.setlocal test2') yield from self._recv_privmsg('Other!~other@otherhost', '#First', '!whois Nick') self.assert_sent('NOTICE {} :{}'.format('#First', 'Nick: test1')) @@ -127,10 +127,25 @@ def test_client_reply_whois_multiple_users_channels(self): @failsafe @run_client def test_client_reply_whois_self(self): - yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.set test1') + yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.setlocal test1') yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois') self.assert_sent('NOTICE {} :{}'.format('#First', 'Nick: test1')) + @failsafe + @run_client + def test_set_alias(self): + yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.set test1') + yield from self._recv_privmsg('Nick!~user@host', '#Second', '!whois') + self.assert_sent('NOTICE {} :{}'.format('#Second', 'Nick: test1')) + + @failsafe + @run_client + def test_unset_alias(self): + yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.set test1') + yield from self._recv_privmsg('Nick!~user@host', '#Second', '!whois.unset') + yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois') + self.assert_sent('NOTICE {} :{}'.format('#First', 'No data for Nick')) + @failsafe @run_client def test_client_reply_whois_setdefault_then_set_channel(self): @@ -141,7 +156,7 @@ def test_client_reply_whois_setdefault_then_set_channel(self): yield from self._recv_privmsg('Other!~other@otherhost', '#Third', '!whois Nick') self.assert_sent('NOTICE {} :{}'.format('#Third', 'Nick: test data')) - yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.set test first') + yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.setlocal test first') yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois') self.assert_sent('NOTICE {} :{}'.format('#First', 'Nick: test first')) @@ -155,11 +170,11 @@ def test_client_reply_whois_setdefault_then_unset_channel(self): yield from self._recv_privmsg('Nick!~user@host', '#Second', '!whois') self.assert_sent('NOTICE {} :{}'.format('#Second', 'Nick: test data')) - yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.set test first') + yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.setlocal test first') yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois') self.assert_sent('NOTICE {} :{}'.format('#First', 'Nick: test first')) - yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.unset') + yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.unsetlocal') yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois') self.assert_sent('NOTICE {} :{}'.format('#First', 'Nick: test data')) @@ -174,7 +189,7 @@ def test_client_reply_whois_setdefault_then_unsetdefault(self): yield from self._recv_privmsg('Other!~other@otherhost', '#Third', '!whois Nick') self.assert_sent('NOTICE {} :{}'.format('#Third', 'Nick: test data')) - yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.unsetdefault') + yield from self._recv_privmsg('Nick!~user@host', '#First', '!whois.unset') yield from self._recv_privmsg('Nick!~user@host', '#Second', '!whois') self.assert_sent('NOTICE {} :{}'.format('#Second', 'No data for Nick'))