|
1 | 1 | import re |
2 | | -import random |
| 2 | +from collections import defaultdict |
3 | 3 |
|
4 | | -from sqlalchemy import Table, Column, String, PrimaryKeyConstraint |
| 4 | +from sqlalchemy import Table, Column, String, PrimaryKeyConstraint, select |
5 | 5 |
|
6 | | -from cloudbot.event import EventType |
7 | 6 | from cloudbot import hook |
| 7 | +from cloudbot.event import EventType |
8 | 8 | from cloudbot.util import database |
9 | 9 |
|
10 | 10 | table = Table( |
|
16 | 16 | PrimaryKeyConstraint('word', 'chan') |
17 | 17 | ) |
18 | 18 |
|
| 19 | +badcache = defaultdict(list) |
| 20 | + |
19 | 21 |
|
20 | 22 | @hook.on_start() |
21 | 23 | @hook.command("loadbad", permissions=["badwords"], autohelp=False) |
22 | 24 | def load_bad(db): |
23 | 25 | """- Should run on start of bot to load the existing words into the regex""" |
24 | | - global badword_re, blacklist, black_re |
25 | | - words = db.execute("select word from badwords").fetchall() |
26 | | - out = "" |
27 | | - for word in words: |
28 | | - out = out + "{}|".format(word[0]) |
29 | | - blacklist = out[:-1] |
30 | | - black_re = '(\s|^|[^\w\s])({0})(\s|$|[^\w\s])'.format(blacklist) |
31 | | - badwords_re = re.compile(black_re, re.IGNORECASE) |
| 26 | + global badword_re |
| 27 | + badcache.clear() |
| 28 | + words = [] |
| 29 | + for chan, word in db.execute(select([table.c.chan, table.c.word])): |
| 30 | + badcache[chan.casefold()].append(word) |
| 31 | + words.append(word) |
| 32 | + |
| 33 | + badword_re = re.compile( |
| 34 | + r'(\s|^|[^\w\s])({0})(\s|$|[^\w\s])'.format('|'.join(words)), re.IGNORECASE |
| 35 | + ) |
32 | 36 |
|
33 | 37 |
|
34 | 38 | @hook.command("addbad", permissions=["badwords"]) |
35 | | -def add_bad(text, nick, db, conn): |
| 39 | +def add_bad(text, nick, db): |
36 | 40 | """<word> <channel> - adds a bad word to the auto kick list must specify a channel with each word""" |
37 | | - global blacklist, black_re, blacklist |
38 | | - word = text.split(' ')[0].lower() |
39 | | - channel = text.split(' ')[1].lower() |
| 41 | + splt = text.lower().split(None, 1) |
| 42 | + word, channel = splt |
40 | 43 | if not channel.startswith('#'): |
41 | 44 | return "Please specify a valid channel name after the bad word." |
| 45 | + |
42 | 46 | word = re.escape(word) |
43 | | - wordlist = list_bad(channel, db, conn) |
| 47 | + wordlist = list_bad(channel) |
44 | 48 | if word in wordlist: |
45 | 49 | return "{} is already added to the bad word list for {}".format( |
46 | | - word, |
47 | | - channel) |
48 | | - else: |
49 | | - if len( |
50 | | - db.execute( |
51 | | - "select word from badwords where chan = :chan", { |
52 | | - "chan": channel}).fetchall()) < 10: |
53 | | - db.execute( |
54 | | - "insert into badwords ( word, nick, chan ) values ( :word, :nick, :chan)", { |
55 | | - "word": word, "nick": nick, "chan": channel}) |
56 | | - db.commit() |
57 | | - load_bad(db, conn) |
58 | | - wordlist = list_bad(channel, db, conn) |
59 | | - return "Current badwords: {}".format(wordlist) |
60 | | - else: |
61 | | - return "There are too many words listed for channel {}. Please remove a word using .rmbad before adding anymore. For a list of bad words use .listbad".format( |
62 | | - channel) |
| 50 | + word, channel |
| 51 | + ) |
| 52 | + |
| 53 | + if len(badcache[channel]) >= 10: |
| 54 | + return "There are too many words listed for channel {}. Please remove a word using .rmbad before adding anymore. For a list of bad words use .listbad".format( |
| 55 | + channel |
| 56 | + ) |
| 57 | + |
| 58 | + db.execute(table.insert().values(word=word, nick=nick, chan=channel)) |
| 59 | + db.commit() |
| 60 | + load_bad(db) |
| 61 | + wordlist = list_bad(channel) |
| 62 | + return "Current badwords: {}".format(wordlist) |
63 | 63 |
|
64 | 64 |
|
65 | 65 | @hook.command("rmbad", "delbad", permissions=["badwords"]) |
66 | | -def del_bad(text, db, conn): |
| 66 | +def del_bad(text, db): |
67 | 67 | """<word> <channel> - removes the specified word from the specified channels bad word list""" |
68 | | - global blacklist, black_re, blacklist |
69 | | - word = text.split(' ')[0].lower() |
70 | | - if not (text.split(' ')[1] or text.split(' ')[1]('#')): |
71 | | - return "please specify a valid channel name" |
72 | | - channel = text.split(' ')[1].lower() |
73 | | - db.execute( |
74 | | - "delete from badwords where word = :word and chan = :chan", { |
75 | | - "word": word, "chan": channel}) |
| 68 | + splt = text.lower().split(None, 1) |
| 69 | + word, channel = splt |
| 70 | + if not channel.startswith('#'): |
| 71 | + return "Please specify a valid channel name after the bad word." |
| 72 | + |
| 73 | + db.execute(table.delete().where(table.c.word == word).where(table.c.chan == channel)) |
76 | 74 | db.commit() |
77 | | - newlist = list_bad(channel, db, conn) |
78 | | - load_bad(db, conn) |
| 75 | + newlist = list_bad(channel) |
| 76 | + load_bad(db) |
79 | 77 | return "Removing {} new bad word list for {} is: {}".format( |
80 | | - word, |
81 | | - channel, |
82 | | - newlist) |
| 78 | + word, channel, newlist |
| 79 | + ) |
83 | 80 |
|
84 | 81 |
|
85 | 82 | @hook.command("listbad", permissions=["badwords"]) |
86 | | -def list_bad(text, db): |
| 83 | +def list_bad(text): |
87 | 84 | """<channel> - Returns a list of bad words specify a channel to see words for a particular channel""" |
88 | 85 | text = text.split(' ')[0].lower() |
89 | | - out = "" |
90 | 86 | if not text.startswith('#'): |
91 | 87 | return "Please specify a valid channel name" |
92 | | - words = db.execute( |
93 | | - "select word from badwords where chan = :chan", { |
94 | | - "chan": text}).fetchall() |
95 | | - for word in words: |
96 | | - out = out + "{}|".format(word[0]) |
97 | | - return out[:-1] |
| 88 | + |
| 89 | + return '|'.join(badcache[text]) |
98 | 90 |
|
99 | 91 |
|
100 | 92 | @hook.event([EventType.message, EventType.action], singlethread=True) |
101 | | -def test_badwords(event, db, conn, message): |
102 | | - match = re.search(black_re, event.content, re.IGNORECASE) |
103 | | - if match: |
104 | | - # Check to see if the match is for this channel |
105 | | - word = match.group().lower().strip() |
106 | | - check = db.execute( |
107 | | - "select word, nick from badwords where word = :word and chan = :chan", { |
108 | | - "word": word, "chan": event.chan}).fetchone() |
109 | | - if (check) or ((word == "fap") and (event.chan == "#conversations")): |
110 | | - out = "KICK {} {} :that fucking word is so damn offensive".format( |
111 | | - event.chan, |
112 | | - event.nick) |
113 | | - message( |
114 | | - "{}, congratulations you've won!".format( |
115 | | - event.nick), |
116 | | - event.chan) |
117 | | - conn.send(out) |
118 | | - else: |
119 | | - pass |
| 93 | +def test_badwords(conn, message, chan, content, nick): |
| 94 | + match = badword_re.match(content) |
| 95 | + if not match: |
| 96 | + return |
| 97 | + |
| 98 | + # Check to see if the match is for this channel |
| 99 | + word = match.group().lower().strip() |
| 100 | + if word in badcache[chan]: |
| 101 | + conn.cmd("KICK", chan, nick, "that fucking word is so damn offensive") |
| 102 | + message("{}, congratulations you've won!".format(nick)) |
0 commit comments