summaryrefslogtreecommitdiffstats
path: root/modules/topic.py
blob: bdc5143874e1a4d2593783c8c47cbd3d03ddbffd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-

from . import ModuleBase
import re

class Module(ModuleBase):
    def __init__(self, manager):
        ModuleBase.__init__(self, manager)

    def commands(self):
        return [
            ('!topic [s[how]]', 'Zeigt das aktuelle Topic mit IDs an'),
            ('!topic a[dd] <Topic>', 'Fügt einen Text zum Thema des Chatraums hinzu'),
            ('!topic d[el[ete]] <ID>', 'Entfernt das Topic mit der ID')
            ]

    def _reply_topic(self, handler, prefix):
        topic = handler.get_topic()
        if topic is None or topic == '':
            handler.reply('Es ist kein Topic gesetzt.')
            return
        topics = [s.strip() for s in topic.split('|')]
        idtopics = zip(range(len(topics)), topics)
        idtopicstrings = ["%i: %s" % t for t in idtopics]
        handler.reply(prefix + '\n\n' + '\n'.join(idtopicstrings))

    def groupchat(self, room, nick, text, handler):
        if not re.match(r'!topic(?:\s|\Z)', text):
            return

        if re.match(r'!topic(?:\s+(?:s(?:how)?)?\s*)?\Z', text):
            self._reply_topic(handler, 'Aktuelles Topic:')
            return

        oldtopic = handler.get_topic()

        if re.match(r'!topic\s+a(?:dd)?\s', text):
            topic = re.sub(r'!topic\s+a(?:dd)?\s+', '', text)
            if topic == '':
                return

            if oldtopic != '' and oldtopic is not None:
                topic += ' | ' + oldtopic

            handler.set_topic(topic)
            self._reply_topic(handler, 'Neues Topic:')
            return

        if re.match(r'!topic\s+d(?:el(?:ete)?)?\s', text):
            try:
                topicid = int(re.sub(r'!topic\s+d(?:el(?:ete)?)?\s+', '', text))
            except:
                return

            topics = [s.strip() for s in oldtopic.split('|')]
            if topicid < 0 or topicid >= len(topics):
                return

            del topics[topicid]

            handler.set_topic(' | '.join(topics))
            self._reply_topic(handler, 'Neues Topic:')