From 8608c72b164e0687bae04015d18b203a0b8ab6c5 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sat, 9 Jun 2018 23:12:37 -0700 Subject: [PATCH] automode: allow removing entries by entry numbers Closes #506. This isn't the most efficient implementation because Automode entry lists are actually unordered... So far we're relying on consistent sorting of entries between LISTACC and DELACC, and indrectly removing entries by comparing the entry list with remove_range's output. --- plugins/automode.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/plugins/automode.py b/plugins/automode.py index c77536a..3aaa628 100644 --- a/plugins/automode.py +++ b/plugins/automode.py @@ -236,9 +236,12 @@ def setacc(irc, source, args): modebot.add_cmd(setacc, aliases=('setaccess', 'set'), featured=True) def delacc(irc, source, args): - """ + """ - Removes the Automode entry for the given mask on the given channel, if one exists. + Removes the Automode entry for the given mask or range string, if they exist. + + Range strings are indices (entry numbers) or ranges of them joined together with commas: e.g. + "1", "2-10", "1,3,5-8". Entry numbers are shown by LISTACC. """ try: chanpair, mask = args @@ -259,7 +262,22 @@ def delacc(irc, source, args): log.info('(%s) %s removed modes for %s on %s', ircobj.name, irc.get_hostmask(source), mask, channel) reply(irc, "Done. Removed the Automode access entry for \x02%s\x02 in \x02%s\x02." % (mask, channel)) else: - error(irc, "No Automode access entry for \x02%s\x02 exists in \x02%s\x02." % (mask, channel)) + # Treat the mask as a range string. + try: + new_keys = utils.remove_range(mask, sorted(dbentry.keys())) + except ValueError: + error(irc, "No Automode access entry for \x02%s\x02 exists in \x02%s\x02." % (mask, channel)) + return + + # XXX: Automode entries are actually unordered: what we're actually doing is sorting the keys + # by name into a list, running remove_range on that, and removing the difference. + removed = [] + for mask_entry in dbentry.copy(): + if mask_entry not in new_keys: + del dbentry[mask_entry] + removed.append(mask_entry) + + irc.reply('Done. Removed %d entries on %s: %s' % (len(removed), channel, ', '.join(removed))) # Remove channels if no more entries are left. if not dbentry: @@ -290,7 +308,7 @@ def listacc(irc, source, args): # Iterate over all entries and print them. Do this in private to prevent channel # floods. reply(irc, "Showing Automode entries for \x02%s\x02:" % channel, private=True) - for entrynum, entry in enumerate(dbentry.items(), start=1): + for entrynum, entry in enumerate(sorted(dbentry.items()), start=1): mask, modes = entry reply(irc, "[%s] \x02%s\x02 has modes +\x02%s\x02" % (entrynum, mask, modes), private=True) reply(irc, "End of Automode entries list.", private=True)