Skip to content

Instantly share code, notes, and snippets.

@vestel
Last active August 29, 2015 13:57
Show Gist options
  • Save vestel/9516306 to your computer and use it in GitHub Desktop.
Save vestel/9516306 to your computer and use it in GitHub Desktop.
tag.py
#!/sevabot
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import logging
import re
import Skype4Py
from sevabot.bot.stateful import StatefulSkypeHandler
from sevabot.utils import ensure_unicode
logger = logging.getLogger('Tag')
# Set to debug only during dev
logger.setLevel(logging.INFO)
logger.debug('Tag module level load import')
HELP_TEXT = """ Description
Commands:
!tag help: Show this help text
!tag start <topic> : Starts blog-mode for <topic>
!tag end <topic>: Exit blog-mode for <topic>
!tag clear: Clean all tags
"""
class TagHandler(StatefulSkypeHandler):
WRAP = "[%s]"
REGEXP = "[%s\]\s"
def __init__(self):
""" Use `init` method to initialize a handler. """
logger.debug('Tag handler constructed')
def init(self, sevabot):
"""
Set-up our state. This is called every time module is (re)loaded.
:param skype: Handle to Skype4Py instance
"""
logger.debug('Tag handler init')
self.skype = sevabot.getSkype()
self.calls = {}
self.commands = {'help': self.help, 'start': self.start_tag, 'end': self.end_tag,
'clean': self.clean, 'add': self.start_tag}
def handle_message(self, msg, status):
""" Override this method to customize a handler. """
body = ensure_unicode(msg.Body)
logger.debug('Tag handler got: {}'.format(body))
# If the separators are not specified, runs of consecutive
# whitespace are regarded as a single separator
words = body.split()
if not len(words):
return False
if words[0] != '!tag':
return False
args = words[1:]
for name, cmd in self.commands.items():
if name == args[0]:
cmd(msg, status, args)
return True
return False
def shutdown(self):
""" Called when the module is reloaded. """
logger.debug('Tag handler shutdown')
def help(self, msg, status, args):
""" Show help message to the sender. """
msg.Chat.SendMessage(HELP_TEXT)
def start_tag(self, msg, status, args):
""" Start a conference call of the chat if any call is not active. """
chat = msg.Chat
chat_name = chat.Name
old_topic = chat.Topic
__tag = "_".join(args[1:])
if not re.search(self.REGEXP % __tag, old_topic):
new_topic = "%s %s" % (self.WRAP % __tag, old_topic)
chat.SendMessage("/topic %s" % new_topic)
# Should set flag and start logging to external-file all msgs
def end_tag(self, msg, status, args):
""" End tagging a chat """
chat = msg.Chat
__tag = "_".join(args[1:])
new_topic = re.sub(self.REGEXP % __tag, "", chat.Topic)
chat.SendMessage("/topic %s" % new_topic)
# Should unset tag flag and save file for future reference
def clean(self, msg, status, args):
chat = msg.Chat
new_topic = re.sub(self.REGEXP % ".*", "", chat.Topic, re.S, re.DOTALL)
chat.SendMessage("/topic %s" % new_topic)
# Should unset all flags and save all files for future reference
# Export the instance to Sevabot
sevabot_handler = TagHandler()
__all__ = ['sevabot_handler']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment