Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Topic feature #43

Merged
merged 4 commits into from
Oct 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions csbot.example.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@
# This configuration is for the Example plugin
[example]
foo = bar

[topic]
separator = ::
users = Haegin Alan DinCahill hayashi
87 changes: 87 additions & 0 deletions csbot/plugins/topic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import requests
import urllib

from csbot.plugin import Plugin
from csbot.util import nick

class TopicException(Exception):
pass

class Topic(Plugin):

def setup(self):
super(Topic, self).setup()
self.topic = {}


@Plugin.hook('core.channel.topic')
def currentTopic(self, e):
self.topics[e["channel"]] = e['topic']

@Plugin.command('topic')
def topic(self, e):
"""Manipulate the topic. Possible commands: add append prepend remove change.
"""
try:
if not nick(e['user']) in self.config_get("users").split(" "):
raise TopicException(u"You do not have permission to execute that command")

command, payload = e['data'].split(" ", 1)
separator = self.config_get("separator")

splitted_topic = self.topics[e["reply_to"]].split(" "+separator+" ")

position = None

#map append alias to add to last position
if command == "append":
command = "add"
position = len(splitted_topic)

#map prepend alias to add to first position
if command == "prepend":
command = "add"
position = 0

#handle the add comand
if command == "add":
if position is None:
position, payload = payload.split(" ", 1)

if self.valid_position(position, splitted_topic):
raise TopicException(u"Invalid position number")

position = int(position)

if position == len(splitted_topic):
splitted_topic.append(payload)
else:
splitted_topic.insert(position, payload)

#handle the remove command
elif command == "remove":
position = payload

if self.valid_position(position, splitted_topic):
raise TopicException(u"Invalid position number")

position = int(position)

del splitted_topic[position]
#handle the change command
elif command == "change":
position, payload = payload.split(" ", 1)

if self.valid_position(position, splitted_topic):
raise TopicException(u"Invalid position number")

position = int(position)

splitted_topic[position] = payload

e.protocol.topic(e["reply_to"], (" "+separator+" ").join(splitted_topic))
except TopicException as exception:
e.protocol.msg(e["reply_to"], "Topic error: " + str(exception))

def valid_position(self, position, splitted_topic):
return not position.isdigit() or int(position) >= len(splitted_topic)