Skip to content

Commit

Permalink
[rss] Replaced the toggle command with enable/disable
Browse files Browse the repository at this point in the history
Probably anyone using this command will specifically want to enable or
disable a feed or feeds, rather than blindly set them to whatever they
were not.

Also fixed the regex in del and toggle to allow feed names with dashes.
  • Loading branch information
saltire committed Sep 12, 2013
1 parent c221c13 commit dc984cd
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ def _rss_del(self, bot, trigger, c):
"""
pattern = r"""
^\.rss\s+del
(?:\s+([&#+!][^\s,]+))? # channel (optional)
(?:\s+("[^"]+"|\w+))? # name (optional)
(?:\s+([&#+!][^\s,]+))? # channel (optional)
(?:\s+("[^"]+"|[\w-]+))? # name (optional)
"""
match = re.match(pattern, trigger.group(), re.IGNORECASE | re.VERBOSE)
if match is None or (not match.group(1) and not match.group(2)):
Expand All @@ -244,30 +244,43 @@ def _rss_del(self, bot, trigger, c):
return True


def _rss_toggle(self, bot, trigger, c):
"""Enable or disable a feed or feeds. Usage: .rss toggle [#channel] [Feed_Name]"""
def _rss_enable(self, bot, trigger, c):
"""Enable a feed or feeds. Usage: .rss enable [#channel] [Feed_Name]"""
return self._toggle(bot, trigger, c)


def _rss_disable(self, bot, trigger, c):
"""Disable a feed or feeds. Usage: .rss enable [#channel] [Feed_Name]"""
return self._toggle(bot, trigger, c)


def _toggle(self, bot, trigger, c):
"""Enable or disable a feed or feeds. Usage: .rss <enable|disable> [#channel] [Feed_Name]"""
pattern = r"""
^\.rss\s+toggle
(?:\s+([&#+!][^\s,]+))? # channel (optional)
(?:\s+("[^"]+"|\w+))? # name (optional)
^\.rss\s+(enable|disable) # command
(?:\s+([&#+!][^\s,]+))? # channel (optional)
(?:\s+("[^"]+"|[\w-]+))? # name (optional)
"""
match = re.match(pattern, trigger.group(), re.IGNORECASE | re.VERBOSE)
if match is None or (not match.group(1) and not match.group(2)):
bot.reply("Enable or disable a feed or feeds. Usage: .rss toggle [#channel] [Feed_Name]")
if match is None or (not match.group(2) and not match.group(3)):
bot.reply("{0} a feed or feeds. Usage: .rss toggle [#channel] [Feed_Name]".format(
trigger.group(3).capitalize()))
return

channel = match.group(1)
feed_name = match.group(2).strip('"') if match.group(2) else None
args = [arg for arg in (channel, feed_name) if arg]
command = trigger.group(3)
enabled = 1 if command == 'enable' else 0
channel = match.group(2)
feed_name = match.group(3).strip('"') if match.group(3) else None
args = [arg for arg in (enabled, channel, feed_name) if arg is not None]

c.execute(('UPDATE rss_feeds SET enabled = 1 - enabled WHERE '
c.execute(('UPDATE rss_feeds SET enabled = {0} WHERE '
+ ('channel = {0} AND ' if channel else '')
+ ('feed_name = {0}' if feed_name else '')
).rstrip(' AND ').format(self.sub), args)

if c.rowcount:
noun = 'feeds' if c.rowcount != 1 else 'feed'
bot.reply("Successfully toggled {0} {1}.".format(c.rowcount, noun))
bot.reply("Successfully {0}d {1} {2}.".format(command, c.rowcount, noun))
else:
bot.reply("No feeds matched the command.")

Expand Down

0 comments on commit dc984cd

Please sign in to comment.