-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBanList.py
323 lines (253 loc) · 11.9 KB
/
BanList.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import sqlite3
import re
from cStringIO import StringIO
ALLOWEDCHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}'
ALLOWEDCHARS_IDENT = ALLOWEDCHARS+"~"
ALLOWEDCHARS_HOST = ALLOWEDCHARS + ":."
class InvalidCharacterUsed(Exception):
def __init__(self, string, char, pos):
self.string = string
self.char = char
self.pos = pos
def __str__(self):
hex_char = hex(ord(self.char))
return "String contains invalid character {0} on position {1}".format(hex_char, self.pos)
class NoSuchBanGroup(Exception):
def __init__(self, group_name):
self.group = group_name
def __str__(self):
return "No such ban group exists: '{0}'".format(self.group)
class BanList:
def __init__(self, filename):
self.ESCAPESTRING = "/"
self.ESCAPE = "[]"
self.NOT_ESCAPE = "*?!^"
self.conn = sqlite3.connect(filename)
self.cursor = self.conn.cursor()
# Create table for bans
self.cursor.execute("""
CREATE TABLE IF NOT EXISTS Banlist(groupName TEXT, pattern TEXT,
ban_reason TEXT,
timestamp INTEGER, banlength INTEGER
)
""")
# Create table for the names of the ban groups.
# This will be used to check if a group exists
# when checking if a user is banned in that group.
self.cursor.execute("""
CREATE TABLE IF NOT EXISTS Bangroups(groupName TEXT)
""")
self.defineGroup("Global")
# You need to define a group name if you want
# to have your own ban groups.
# This should prevent accidents in which an user
# is banned in a group that doesn't exist.
def defineGroup(self, groupName):
doesExist = self.__groupExists__(groupName)
if not doesExist:
self.cursor.execute("""
INSERT INTO Bangroups(groupName)
VALUES (?)
""", (groupName, ) )
self.conn.commit()
# True means that a new group has been defined.
return True
# False means that no new group has been defined, i.e.
# the group already exists.
return False
def banUser(self, user, ident="*", host="*", groupName="Global",
ban_reason="None",
timestamp=(-1), banlength=(-1)):
banstring = self.__assembleBanstring__(user, ident, host).lower()
if not self.__groupExists__(groupName):
raise NoSuchBanGroup(groupName)
if not self.__banExists__(groupName, banstring):
self.__ban__(banstring, groupName, ban_reason, timestamp, banlength)
# The operation was successful, we banned the pattern.
return True
else:
# We did not ban the pattern because it was already banned.
return False
def unbanUser(self, user, ident="*", host="*",
groupName="Global"):
banstring = self.__assembleBanstring__(user, ident, host).lower()
if not self.__groupExists__(groupName):
raise NoSuchBanGroup(groupName)
if self.__banExists__(groupName, banstring):
self.__unban__(banstring, groupName)
# The operation was successful, the pattern was unbanned.
return True
else:
# We did not unban the pattern because it was never banned in the first place.
return False
def clearBanlist_all(self):
self.cursor.execute("""
DELETE FROM Banlist
""")
self.conn.commit()
def clearBanlist_group(self, groupName):
self.cursor.execute("""
DELETE FROM Banlist
WHERE groupName = ?
""", (groupName, ) )
self.conn.commit()
def getBans(self, groupName=None, matchingString=None):
if groupName is None:
if matchingString is None:
self.cursor.execute("""
SELECT * FROM Banlist
""")
else:
self.cursor.execute("""
SELECT * FROM Banlist
WHERE ? GLOB pattern
""", (matchingString.lower(), ))
return self.cursor.fetchall()
else:
if self.__groupExists__(groupName):
if matchingString is None:
self.cursor.execute("""
SELECT * FROM Banlist
WHERE groupName = ?
""", (groupName, ))
else:
self.cursor.execute("""
SELECT * FROM Banlist
WHERE groupName = ? AND ? GLOB pattern
""", (groupName, matchingString.lower()))
return self.cursor.fetchall()
else:
raise NoSuchBanGroup(groupName)
def checkBan(self, user, ident, host,
groupName="Global"):
if not self.__groupExists__(groupName):
raise NoSuchBanGroup(groupName)
else:
banstring = u"{0}!{1}@{2}".format(user, ident, host).lower()
self.cursor.execute("""
SELECT * FROM Banlist
WHERE groupName = ? AND ? GLOB pattern
""", (groupName, banstring))#, self.ESCAPESTRING))
result = self.cursor.fetchone()
if result != None:
return True, result
else:
return False, None
def getGroups(self):
self.cursor.execute("""
SELECT groupName FROM Bangroups
""")
groupTuples = self.cursor.fetchall()
return [groupTuple[0] for groupTuple in groupTuples]
def raw_ban(self, banstring, groupName, ban_reason, timestamp=(-1), banlength=(-1)):
self.__ban__(banstring, groupName, ban_reason, timestamp, banlength)
def raw_unban(self, banstring, groupName):
self.__unban__(banstring, groupName)
# We do the reverse of what __createString_forSQL__ is doing.
# The result is a string which should be correct for using the
# banUser and unbanUser methods, and the ban/unban commands.
def unescape_banstring(self, banstring):
finstring = StringIO()
length = len(banstring)
string_iter = enumerate(banstring)
for pos, char in string_iter:
charsLeft = length - pos - 1
if char == "[" and charsLeft >= 3:
nextchar = banstring[pos+1]
closedBracket = banstring[pos+2]
if closedBracket == "]":
finstring.write(nextchar)
string_iter.next()
string_iter.next()
continue
if char in self.ESCAPE:
finstring.write(self.ESCAPESTRING+char)
continue
finstring.write(char)
return finstring.getvalue()
def __regex_return_unescaped__(self, match):
pass
def __ban__(self, banstring, groupName="Global", ban_reason="None", timestamp=(-1), banlength=(-1)):
self.cursor.execute("""
INSERT INTO Banlist(groupName, pattern, ban_reason, timestamp, banlength)
VALUES (?, ?, ?, ?, ?)
""", (groupName, banstring, ban_reason, timestamp, banlength))
self.conn.commit()
def __unban__(self, banstring, groupName = "Global"):
self.cursor.execute("""
DELETE FROM Banlist
WHERE groupName = ? AND pattern = ?
""", (groupName, banstring))
self.conn.commit()
def __banExists__(self, groupName, banstring):
self.cursor.execute("""
SELECT 1 FROM Banlist
WHERE groupName = ? AND pattern = ?
""", (groupName, banstring) )
result = self.cursor.fetchone()
print result, type(result)
if result != None and result[0] == 1:
return True
else:
return False
def __groupExists__(self, groupName):
self.cursor.execute("""
SELECT 1 FROM Bangroups
WHERE groupName = ?
""", (groupName, ) )
result = self.cursor.fetchone()
print result, type(result)
if result != None and result[0] == 1:
return True
else:
return False
def __stringIsValid__(self, string):
for char in string:
if char not in ALLOWEDCHARS:
return False
return True
def __assembleBanstring__(self, user, ident, host):
escapedUser = self.__createString_forSQL__(user)
escapedIdent = self.__createString_forSQL__(ident, ident = True)
escapedHost = self.__createString_forSQL__(host, hostname = True)
banstring = u"{0}!{1}@{2}".format(escapedUser, escapedIdent, escapedHost)
return banstring
# The createString_forSQL function takes a string and
# formats it according to specific rules.
# It also prevents characters that aren't in
# the ALLOWEDCHARS constant to be used so that
# characters not allowed in specific IRC arguments
# (nickname, ident, host) appear in the string.
#
# It is not very specific and is only useful for
# very simple filtering so that unicode characters
# or special characters aren't used.
def __createString_forSQL__(self, string, hostname = False, ident = False):
newString = StringIO()
# Both flags should not be set at once.
assert not ( (hostname == True) and (ident == True) )
for pos, char in enumerate(string):
# We try reverse-escaping:
# - escaped chars will be written as literals
# - non-escaped chars included in the escape string will be escaped
# pos == 0 is an exception because characters at this
# position cannot be escaped in any way that makes sense.
if char == self.ESCAPESTRING:
continue
if char in self.NOT_ESCAPE:
newString.write(char)
elif pos > 0 and string[pos-1] == self.ESCAPESTRING and char in self.ESCAPE:
newString.write(char)
elif char in self.ESCAPE:
#newString.write(self.ESCAPESTRING+char)
newString.write("["+char+"]")
else:
if (
(not hostname and not ident and char not in ALLOWEDCHARS)
or (hostname and char not in ALLOWEDCHARS_HOST)
or (ident and char not in ALLOWEDCHARS_IDENT)
):
raise InvalidCharacterUsed(string, char, pos)
else:
newString.write(char)
return newString.getvalue()