Skip to content

Commit c348317

Browse files
authored
Merge pull request CloudBotIRC#153 from linuxdaemon/gonzobot+fix-badwords
Clean up and fix errors in badwords.py
2 parents 5bcb39c + f1a9fc8 commit c348317

1 file changed

Lines changed: 56 additions & 72 deletions

File tree

plugins/badwords.py

Lines changed: 56 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
2+
from collections import defaultdict
23

3-
from sqlalchemy import Table, Column, String, PrimaryKeyConstraint
4+
from sqlalchemy import Table, Column, String, PrimaryKeyConstraint, select
45

56
from cloudbot import hook
67
from cloudbot.event import EventType
@@ -15,104 +16,87 @@
1516
PrimaryKeyConstraint('word', 'chan')
1617
)
1718

19+
badcache = defaultdict(list)
20+
1821

1922
@hook.on_start()
2023
@hook.command("loadbad", permissions=["badwords"], autohelp=False)
2124
def load_bad(db):
2225
"""- Should run on start of bot to load the existing words into the regex"""
23-
global badword_re, blacklist, black_re
24-
words = db.execute("select word from badwords").fetchall()
25-
out = ""
26-
for word in words:
27-
out += "{}|".format(word[0])
28-
blacklist = out[:-1]
29-
black_re = '(\s|^|[^\w\s])({0})(\s|$|[^\w\s])'.format(blacklist)
30-
badword_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+
)
3136

3237

3338
@hook.command("addbad", permissions=["badwords"])
34-
def add_bad(text, nick, db, conn):
39+
def add_bad(text, nick, db):
3540
"""<word> <channel> - adds a bad word to the auto kick list must specify a channel with each word"""
36-
global blacklist, black_re, blacklist
37-
word = text.split(' ')[0].lower()
38-
channel = text.split(' ')[1].lower()
41+
splt = text.lower().split(None, 1)
42+
word, channel = splt
3943
if not channel.startswith('#'):
4044
return "Please specify a valid channel name after the bad word."
45+
4146
word = re.escape(word)
42-
wordlist = list_bad(channel, db, conn)
47+
wordlist = list_bad(channel)
4348
if word in wordlist:
4449
return "{} is already added to the bad word list for {}".format(
45-
word,
46-
channel)
47-
else:
48-
if len(
49-
db.execute(
50-
"select word from badwords where chan = :chan", {
51-
"chan": channel}).fetchall()) < 10:
52-
db.execute(
53-
"insert into badwords ( word, nick, chan ) values ( :word, :nick, :chan)", {
54-
"word": word, "nick": nick, "chan": channel})
55-
db.commit()
56-
load_bad(db, conn)
57-
wordlist = list_bad(channel, db, conn)
58-
return "Current badwords: {}".format(wordlist)
59-
else:
60-
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(
61-
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)
6263

6364

6465
@hook.command("rmbad", "delbad", permissions=["badwords"])
65-
def del_bad(text, db, conn):
66+
def del_bad(text, db):
6667
"""<word> <channel> - removes the specified word from the specified channels bad word list"""
67-
global blacklist, black_re, blacklist
68-
word = text.split(' ')[0].lower()
69-
if not (text.split(' ')[1] or text.split(' ')[1]('#')):
70-
return "please specify a valid channel name"
71-
channel = text.split(' ')[1].lower()
72-
db.execute(
73-
"delete from badwords where word = :word and chan = :chan", {
74-
"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))
7574
db.commit()
76-
newlist = list_bad(channel, db, conn)
77-
load_bad(db, conn)
75+
newlist = list_bad(channel)
76+
load_bad(db)
7877
return "Removing {} new bad word list for {} is: {}".format(
79-
word,
80-
channel,
81-
newlist)
78+
word, channel, newlist
79+
)
8280

8381

8482
@hook.command("listbad", permissions=["badwords"])
85-
def list_bad(text, db):
83+
def list_bad(text):
8684
"""<channel> - Returns a list of bad words specify a channel to see words for a particular channel"""
8785
text = text.split(' ')[0].lower()
88-
out = ""
8986
if not text.startswith('#'):
9087
return "Please specify a valid channel name"
91-
words = db.execute(
92-
"select word from badwords where chan = :chan", {
93-
"chan": text}).fetchall()
94-
for word in words:
95-
out += "{}|".format(word[0])
96-
return out[:-1]
88+
89+
return '|'.join(badcache[text])
9790

9891

9992
@hook.event([EventType.message, EventType.action], singlethread=True)
100-
def test_badwords(event, db, conn, message):
101-
match = re.search(black_re, event.content, re.IGNORECASE)
102-
if match:
103-
# Check to see if the match is for this channel
104-
word = match.group().lower().strip()
105-
check = db.execute(
106-
"select word, nick from badwords where word = :word and chan = :chan", {
107-
"word": word, "chan": event.chan}).fetchone()
108-
if (check) or ((word == "fap") and (event.chan == "#conversations")):
109-
out = "KICK {} {} :that fucking word is so damn offensive".format(
110-
event.chan,
111-
event.nick)
112-
message(
113-
"{}, congratulations you've won!".format(
114-
event.nick),
115-
event.chan)
116-
conn.send(out)
117-
else:
118-
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

Comments
 (0)