diff --git a/dr_botzo/twitter/ircplugin.py b/dr_botzo/twitter/ircplugin.py index 1df3dfd..6e97c94 100644 --- a/dr_botzo/twitter/ircplugin.py +++ b/dr_botzo/twitter/ircplugin.py @@ -1,6 +1,7 @@ """Access to Twitter through bot commands.""" import logging +import threading import time import twython @@ -49,6 +50,10 @@ class Twitter(Plugin): self.handle_auth, -20) self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!twitter\s+replyto\s+(\S+)\s+(.*)', self.handle_replyto, -20) + self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!twitter\s+mentionpoll\s+start', + self.handle_start_mentionpoll, -20) + self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!twitter\s+mentionpoll\s+stop', + self.handle_stop_mentionpoll, -20) # try getting the stored auth tokens and logging in try: @@ -80,11 +85,30 @@ class Twitter(Plugin): self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_gettoken) self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_auth) self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_replyto) + self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_start_mentionpoll) + self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_stop_mentionpoll) self.poll_mentions = False super(Twitter, self).stop() + def handle_start_mentionpoll(self, connection, event, match): + if not has_permission(event.source, 'twitter.manage_threads'): + return self.bot.reply(event, "You do not have permission to start/stop threads.") + + self.poll_mentions = True + t = threading.Thread(target=self.thread_watch_mentions) + t.daemon = True + t.start() + self.bot.reply(event, "Now polling for mentions.") + + def handle_stop_mentionpoll(self, connection, event, match): + if not has_permission(event.source, 'twitter.manage_threads'): + return self.bot.reply(event, "You do not have permission to start/stop threads.") + + self.poll_mentions = False + self.bot.reply(event, "No longer polling for mentions.") + def handle_getstatus(self, connection, event, match): """Get a status by tweet ID.""" diff --git a/dr_botzo/twitter/migrations/0006_add_manage_threads_permission.py b/dr_botzo/twitter/migrations/0006_add_manage_threads_permission.py new file mode 100644 index 0000000..d154df5 --- /dev/null +++ b/dr_botzo/twitter/migrations/0006_add_manage_threads_permission.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('twitter', '0005_replace_since_id_with_replies_specific_one'), + ] + + operations = [ + migrations.AlterModelOptions( + name='twitterclient', + options={'permissions': (('send_tweets', 'Can send tweets via IRC'), ('manage_threads', 'Can start/stop polling threads via IRC'))}, + ), + ] diff --git a/dr_botzo/twitter/models.py b/dr_botzo/twitter/models.py index ea73f98..0020c4b 100644 --- a/dr_botzo/twitter/models.py +++ b/dr_botzo/twitter/models.py @@ -24,4 +24,5 @@ class TwitterClient(models.Model): class Meta: permissions = ( ('send_tweets', "Can send tweets via IRC"), + ('manage_threads', "Can start/stop polling threads via IRC"), )