forked from bigfoot547/bigfootbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ban.py
74 lines (63 loc) · 1.88 KB
/
ban.py
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
import bot
class Ban:
def __init__(self, target, mask):
self.target = target
self.mask = mask
def set_ban(self, client):
client.rawmsg("MODE", self.target, '+b', self.mask)
def remove_ban(self, client):
client.rawmsg("MODE", self.target, '-b', self.mask)
class BanManager:
def __init__(self, filename, client):
self.bans = []
self.current_ban_number = 0
self.filename = filename
self.client = client
try:
file = open(self.filename, 'r')
except FileNotFoundError:
print("[BanManager] Input file not found.")
return
string = file.read().split('\n')
for each in string:
tab = each.split(' ')
if len(tab) < 2:
break
self.bans.append(0)
self.bans[self.current_ban_number] = Ban(tab[0], tab[1])
#self.bans[self.current_ban_number].set_ban(self.client)
self.current_ban_number += 1
file.close()
def save_bans(self):
file = open(self.filename, 'w')
for each in self.bans:
file.write(each.target + ' ' + each.mask + '\n')
file.close()
return 0
def add_ban(self, target, hostmask):
if self.current_ban_number == len(self.bans):
self.bans.append(0)
#print("In function add_ban {} {}".format(len(self.bans), self.current_ban_number))
self.bans[self.current_ban_number] = Ban(target, hostmask)
#print("Ban object created")
self.bans[self.current_ban_number].set_ban(self.client)
#print("Ban set")
self.current_ban_number += 1
return self.current_ban_number-1
def remove_ban(self, number):
#print("In function remove_ban")
if number > len(self.bans)-1:
return 1
mask = self.bans[number]
#print("Mask = ", type(mask))
#print("bans = ", self.bans)
if len(self.bans) == 1:
#print("Only one ban. ", number)
mask.remove_ban(self.client)
self.current_ban_number = 0
self.bans = []
return 0
self.bans.pop(number)
mask.remove_ban(self.client)
self.current_ban_number -= 1
return 0