File: topic.py

package info (click to toggle)
gozerbot 0.8.1-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,932 kB
  • ctags: 6,492
  • sloc: python: 32,538; sh: 48; makefile: 42; perl: 23
file content (136 lines) | stat: -rw-r--r-- 4,246 bytes parent folder | download | duplicates (2)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# plugs/topic.py
#
#

__copyright__ = 'this file is in the public domain'
__gendocfirst__ = ['topic-set', ]

from gozerbot.commands import cmnds
from gozerbot.examples import examples
from gozerbot.plughelp import plughelp
import time

plughelp.add('topic', 'manage the topic of a channel')

def checktopicmode(bot, ievent):
    """ callback for change in channel topic mode """
    chan = ievent.channel
    mode = bot.channels.get(chan, 'mode')
    if mode and 't' in mode:
        if chan not in bot.state['opchan']:
            ievent.reply("i'm not op on %s" % chan)
            return 0
    return 1

def handle_gettopic(bot, ievent):
    """ topic [<channel>] .. get topic """
    try:
        channel = ievent.args[0]
    except IndexError:
        channel = ievent.channel
    result = bot.gettopic(channel)
    if not result:
        ievent.reply("can't get topic data of channel %s" % channel)
    else:
        (what, who, when) = result
        ievent.reply('topic on %s is %s made by %s on %s' % \
(channel, what, who, time.ctime(when)))

cmnds.add('topic', handle_gettopic, 'USER')
examples.add('topic', 'topic [<channel>] .. get topic', '1) topic 2) topic \
#dunkbots')

def handle_topicset(bot, ievent):
    """ topic-set .. set the topic """
    if not bot.jabber and not checktopicmode(bot, ievent):
        return
    if not ievent.rest:
        ievent.missing('<what>')
        return
    bot.settopic(ievent.channel, ievent.rest)

cmnds.add('topic-set', handle_topicset, 'USER', allowqueue=False)
examples.add('topic-set', 'set channel topic', 'topic-set Yooo')

def handle_topicadd(bot, ievent):
    """ topic-add <txt> .. add topic item """
    if not bot.jabber and not checktopicmode(bot, ievent):
        return
    if not ievent.rest:
        ievent.missing("<what>")
        return
    result = bot.gettopic(ievent.channel)
    if not result:
        ievent.reply("can't get topic data")
        return
    what = result[0]
    what += " | %s" % ievent.rest
    bot.settopic(ievent.channel, what)

cmnds.add('topic-add', handle_topicadd, 'USER')
examples.add('topic-add', 'topic-add <txt> .. add a topic item', \
'topic-add mekker')

def handle_topicdel(bot, ievent):
    """ topic-del <topicnr> .. delete topic item """
    if not bot.jabber and not checktopicmode(bot, ievent):
        return
    try:
        topicnr = int(ievent.args[0])
    except (IndexError, ValueError):
        ievent.reply('i need a integer as argument')
        return
    if topicnr < 1:
        ievent.reply('topic items start at 1')
        return
    result = bot.gettopic(ievent.channel)
    if not result:
        ievent.reply("can't get topic data")
        return
    what = result[0].split(' | ')
    if topicnr > len(what):
        ievent.reply('there are only %s topic items' % len(what))
        return
    del what[topicnr-1]
    newtopic = ' | '.join(what)
    bot.settopic(ievent.channel, newtopic)

cmnds.add('topic-del', handle_topicdel, 'USER')
examples.add('topic-del', 'topic-del <topicnr> .. delete topic item', \
'topic-del 1')

def handle_topicmove(bot, ievent):
    """ topic-move <nrfrom> <nrto> .. move topic item """
    if not bot.jabber and not checktopicmode(bot, ievent):
        return
    try:
        (topicfrom, topicto) = ievent.args
    except ValueError:
        ievent.missing('<from> <to>')
        return
    try:
        topicfrom = int(topicfrom)
        topicto = int(topicto)
    except ValueError:
        ievent.reply('i need two integers as arguments')
        return
    if topicfrom < 1 or topicto < 1:
        ievent.reply('topic items start at 1')
        return
    topicdata = bot.gettopic(ievent.channel)
    if not topicdata:
        ievent.reply("can't get topic data")
        return
    splitted = topicdata[0].split(' | ')
    if topicfrom > len(splitted) or topicto > len(splitted):
        ievent.reply('max item is %s' % len(splitted))
        return
    tmp = splitted[topicfrom-1]
    del splitted[topicfrom-1]
    splitted.insert(topicto-1, tmp)
    newtopic = ' | '.join(splitted)
    bot.settopic(ievent.channel, newtopic)

cmnds.add('topic-move', handle_topicmove, 'USER')
examples.add('topic-move', 'topic-move <nrfrom> <nrto> .. move topic \
items', 'topic-move 3 1')