~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to build/lib/gplugs/kickban.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Malcolm
  • Date: 2012-04-03 21:58:28 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20120403215828-6mik0tzug5na93la
Tags: 0.99.1-2
* Removes logfiles on purge (Closes: #668767)
* Reverted location of installed files back to /usr/lib/gozerbot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from gozerbot.tests import tests
 
2
from gozerbot.generic import getwho, rlog
 
3
from gozerbot.commands import cmnds
 
4
from gozerbot.examples import examples
 
5
from gozerbot.aliases import aliases
 
6
from gozerbot.callbacks import callbacks
 
7
from gozerbot.plughelp import plughelp
 
8
import Queue
 
9
import time
 
10
 
 
11
plughelp.add('kickban', 'channel ban list management')
 
12
 
 
13
bans      = {}
 
14
cachetime = 300
 
15
timeout   = 10 
 
16
 
 
17
def handle_367(bot, ievent):
 
18
    rlog(0, '367', str(ievent))
 
19
    channel = ievent.arguments[1].lower()
 
20
    if not bot.name in bans or not channel in bans[bot.name]:
 
21
        return # not requested by this plugin
 
22
    bans[bot.name][channel].append(ievent.txt.split()[0])
 
23
 
 
24
def handle_mode(bot, ievent):
 
25
    rlog(0, 'mode', str(ievent))
 
26
    # [18 Jan 2008 13:41:29] (mode) cmnd=MODE prefix=maze!wijnand@2833335b.cc9dd561.com.hmsk postfix=#eth0-test +b *!*@je.moeder.ook arguments=[u'#eth0-test', u'+b', u'*!*@je.moeder.ook'] nick=maze user=wijnand userhost=wijnand@2833335b.cc9dd561.com.hmsk channel=#eth0-test txt= command= args=[] rest= speed=5 options={}
 
27
 
 
28
def get_bans(bot, channel):
 
29
    # :ironforge.sorcery.net 367 basla #eth0 *!*@71174af5.e1d1a3cf.net.hmsk eth0!eth0@62.212.76.127 1200657224
 
30
    # :ironforge.sorcery.net 367 basla #eth0 *!*@6ca5f0a3.14055a38.89.123.imsk eth0!eth0@62.212.76.127 1200238584
 
31
    # :ironforge.sorcery.net 368 basla #eth0 :End of Channel Ban List
 
32
    channel = channel.lower()
 
33
    if not bot.name in bans:
 
34
        bans[bot.name] = {}
 
35
    bans[bot.name][channel] = []
 
36
    queue368 = Queue.Queue()
 
37
    bot.wait.register('368', channel, queue368)
 
38
    bot.sendraw('MODE %s +b' % (channel, ))
 
39
    # wait for End of Channel Ban List
 
40
    try:
 
41
        res = queue368.get(1, timeout)
 
42
    except Queue.Empty:
 
43
        return None
 
44
    return bans[bot.name][channel]
 
45
 
 
46
def get_bothost(bot):
 
47
    return getwho(bot, bot.nick).split('@')[-1].lower()
 
48
 
 
49
def handle_ban_list(bot, ievent):
 
50
    banslist = get_bans(bot, ievent.channel)
 
51
    if not banslist:
 
52
        ievent.reply('the ban list for %s is empty' % ievent.channel)
 
53
    else:
 
54
        ievent.reply('bans on %s: ' % ievent.channel, banslist, nr=True)
 
55
 
 
56
def handle_ban_remove(bot, ievent):
 
57
    channel = ievent.channel.lower()
 
58
    if len(ievent.args) != 1 or not ievent.args[0].isdigit():
 
59
        ievent.missing('<banlist index>')
 
60
        return
 
61
    if not bot.name in bans or not channel in bans[bot.name]:
 
62
        banslist = get_bans(bot, ievent.channel)
 
63
    else:
 
64
        banslist = bans[bot.name][channel]
 
65
        index = int(ievent.args[0])-1
 
66
        if len(banslist) <= index:
 
67
            ievent.reply('ban index out of range')
 
68
        else:
 
69
            unban = banslist[index]
 
70
            banslist.remove(unban)
 
71
            bot.sendraw('MODE %s -b %s' % (channel, unban))
 
72
            ievent.reply('unbanned %s' % (unban, ))
 
73
 
 
74
def handle_ban_add(bot, ievent):
 
75
    if not ievent.args:
 
76
        ievent.missing('<nick>')
 
77
        return
 
78
    if ievent.args[0].lower() == bot.nick.lower():
 
79
        ievent.reply('not going to ban myself')
 
80
        return
 
81
    userhost = getwho(bot, ievent.args[0])
 
82
    if userhost:
 
83
        host = userhost.split('@')[-1].lower()
 
84
        if host == get_bothost(bot):
 
85
            ievent.reply('not going to ban myself')
 
86
            return
 
87
        bot.sendraw('MODE %s +b *!*@%s' % (ievent.channel, host))
 
88
        ievent.reply('banned %s' % (host, ))
 
89
    else:
 
90
        ievent.reply('can not get userhost of %s' % ievent.args[0])
 
91
 
 
92
def handle_kickban_add(bot, ievent):
 
93
    if not ievent.args:
 
94
        ievent.missing('<nick> [<reason>]')
 
95
        return
 
96
    if ievent.args[0].lower() == bot.nick.lower():
 
97
        ievent.reply('not going to kickban myself')
 
98
        return
 
99
    userhost = getwho(bot, ievent.args[0])
 
100
    reason = len(ievent.args) > 1 and ' '.join(ievent.args[1:]) or 'Permban requested, bye'
 
101
    if userhost:
 
102
        host = userhost.split('@')[-1].lower()
 
103
        if host == get_bothost(bot):
 
104
            ievent.reply('not going to kickban myself')
 
105
            return
 
106
        bot.sendraw('MODE %s +b *!*@%s' % (ievent.channel, host))
 
107
        bot.sendraw('KICK %s %s :%s' % (ievent.channel, ievent.args[0], reason))
 
108
    else:
 
109
        ievent.reply('can not get userhost of %s' % ievent.args[0])
 
110
 
 
111
callbacks.add('367', handle_367)
 
112
callbacks.add('MODE', handle_mode)
 
113
aliases.data['ban'] = 'ban-add'
 
114
aliases.data['bans'] = 'ban-list'
 
115
aliases.data['unban'] = 'ban-remove'
 
116
aliases.data['kb'] = 'ban-kickban'
 
117
aliases.data['kickban'] = 'ban-kickban'
 
118
cmnds.add('ban-add', handle_ban_add, 'OPER')
 
119
examples.add('ban-add', 'adds a host to the ban list', 'ban-add *!*@lamers.are.us')
 
120
tests.add('ban-add *!*@lamer', 'added')
 
121
cmnds.add('ban-list', handle_ban_list, 'OPER', threaded=True)
 
122
tests.add('ban-list', 'lamer')
 
123
cmnds.add('ban-remove', handle_ban_remove, 'OPER', threaded=True)
 
124
examples.add('ban-remove', 'removes a host from the ban list', 'ban-remove 1')
 
125
tests.add('ban-remove', '*!*@lamer')
 
126
cmnds.add('ban-kickban', handle_kickban_add, 'OPER')
 
127
examples.add('ban-kickban', 'kickbans the given nick', 'kickban Lam0r Get out of here')
 
128
tests.add('ban-kickban', 'ban-kickban exec')