Fixed passive action bug

This commit is contained in:
aewens 2019-04-10 14:12:14 -04:00
parent 64d0208837
commit 579f7df5a5
7 changed files with 71 additions and 33 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@
settings.json settings.json
settings.demo.json settings.demo.json
settings.test.json settings.test.json
*.local
data/*.json data/*.json
logs/*.log* logs/*.log*
logs/*.log.* logs/*.log.*

View File

@ -51,7 +51,7 @@ actions = [
"callback": nomad "callback": nomad
}, },
{ {
"type": "response", "type": "passive",
"pattern": "/^[^!]*hm+/", "pattern": "/^[^!]*hm+/",
"callback": score_word("hmm", "hm+") "callback": score_word("hmm", "hm+")
}, },
@ -66,9 +66,9 @@ actions = [
"callback": wordscoreboard("hmm") "callback": wordscoreboard("hmm")
}, },
{ {
"type": "response", "type": "passive",
"pattern": "/^[^!]*oo+f/", "pattern": "/^[^!]*oo+f/",
"callback": score_word("oof", "o+f") "callback": score_word("oof", "oo+f")
}, },
{ {
"type": "response", "type": "response",

View File

@ -28,10 +28,12 @@ def pardon(self, name, source, response):
if name != author: if name != author:
return return
del self.bot.memories["users"][user]["blacklist"] user_memories = self.bot.memories["users"].get(user, dict())
if user_memories.get("blacklist", None) is not None:
del user_memories["blacklist"]
self.bot.thread(self.bot.save_memories) self.bot.thread(self.bot.save_memories)
confirmation = "{} has been pardoned".format(user) confirmation = "{} has been pardoned".format(user)
self.bot.send_message(source, confirmation) self.bot.send_message(source, confirmation)

View File

@ -78,4 +78,4 @@ def wordscoreboard(word):
response = "{} Score Leaderboard: {}".format(capitalize(word), leaders) response = "{} Score Leaderboard: {}".format(capitalize(word), leaders)
self.bot.send_message(source, response) self.bot.send_message(source, response)
return scoreboard return scoreboard

10
app.py
View File

@ -17,7 +17,7 @@ parser.add_argument(
) )
arguments = parser.parse_args() arguments = parser.parse_args()
bot = Bot("127.0.0.1", 6667) bot = Bot("localhost", 6667)
responses = Responses(bot) responses = Responses(bot)
tasks = Tasks(bot) tasks = Tasks(bot)
tasks.coroutines = coroutines tasks.coroutines = coroutines
@ -62,8 +62,8 @@ def handle_invite(channel, name):
bot.memories["users"][name]["invites"].append(channel) bot.memories["users"][name]["invites"].append(channel)
changed = True changed = True
if changed: #if changed:
bot.thread(bot.save_memories) # bot.thread(bot.save_memories)
def handle_kick(name, source): def handle_kick(name, source):
if source in bot.settings.get("extras", dict()).get("rejoin", list()): if source in bot.settings.get("extras", dict()).get("rejoin", list()):
@ -73,7 +73,7 @@ def handle_kick(name, source):
bot.memories["users"][name] = dict() bot.memories["users"][name] = dict()
bot.memories["users"][name]["kicker"] = True bot.memories["users"][name]["kicker"] = True
bot.thread(bot.save_memories) #bot.thread(bot.save_memories)
def handle_message(name, source, response): def handle_message(name, source, response):
responses.parse(name, source, response) responses.parse(name, source, response)
@ -81,7 +81,7 @@ def handle_message(name, source, response):
bot.logger.debug(":: {}".format(bot.memories)) bot.logger.debug(":: {}".format(bot.memories))
def handle_raw(message): def handle_raw(message):
if "KICK #chaos {}".format(bot.author) in message: if "KICK #chaos {} :".format(bot.author) in message:
bot.send("INVITE {} :#chaos".format(bot.author)) bot.send("INVITE {} :#chaos".format(bot.author))
def handle_crashed(): def handle_crashed():

View File

@ -60,7 +60,10 @@ class Bot:
self.logger.info(response) self.logger.info(response)
print("DEBUG: ", response) print("DEBUG: ", response)
self.ircsock.send(response.encode()) try:
self.ircsock.send(response.encode())
except BrokenPipeError:
self.stop()
def send_action(self, target, message, *args): def send_action(self, target, message, *args):
self.send_message(target, "\001ACTION {}\001".format(message), *args) self.send_message(target, "\001ACTION {}\001".format(message), *args)
@ -72,14 +75,18 @@ class Bot:
message = "" message = ""
magic_string = "End of /NAMES list." magic_string = "End of /NAMES list."
banned = "Cannot join channel (You're banned)" banned = "Cannot join channel (You're banned)"
start = time.time()
while magic_string not in message: while magic_string not in message:
# Taking too long, escaping JOIN request
if time.time() - start == 2000:
return
try: try:
message = self.ircsock.recv(self.recv_size).decode() message = self.ircsock.recv(self.recv_size).decode()
if banned in message: if banned in message:
self.places.remove(chan) self.places.remove(chan)
return return
# message = message.strip(self.splitter) # message = message.strip(self.splitter)
self.logger.debug(message) #self.logger.debug(message)
except UnicodeDecodeError: except UnicodeDecodeError:
continue continue
@ -113,7 +120,7 @@ class Bot:
return text.split("!", 1)[0][1:] return text.split("!", 1)[0][1:]
def parse_name(self, name): def parse_name(self, name):
if name[0] == "~" or name[0] == "@" or name[0] == "+": if name[0] in ["~", "@", "+", "%"]:
return name[1:] return name[1:]
else: else:
return name return name
@ -138,8 +145,8 @@ class Bot:
name = self.parse_name(name) name = self.parse_name(name)
user = self.memories["users"][name] user = self.memories["users"][name]
del self.memories["users"][name]
self.memories["users"][new_name] = user self.memories["users"][new_name] = user
del self.memories["users"][name]
return user, new_name return user, new_name
def handle_invite(self, message): def handle_invite(self, message):
@ -240,19 +247,23 @@ class Bot:
magic_phrase = { magic_phrase = {
"has_registered": "Password", "has_registered": "Password",
"needs_to_register": "choose a different nick", "needs_to_register": "choose a different nick",
"needs_to_confirm": "Your account will expire" "needs_to_confirm": "Your account will expire",
"not_registered": "Your nickname is not registered"
#"ready_to_id": "is now your displayed host",
#"nickserv_missing": "No such nick/channel"
} }
authenticate = len(password) > 0 and len(confirm) > 0 authenticate = len(password) > 0 and len(confirm) > 0
magic_string = "MODE {} +r".format(self.botnick) magic_string = "MODE {} +r".format(self.botnick)
while magic_string not in message and authenticate: while magic_string not in message and authenticate:
try: try:
message = self.ircsock.recv(self.recv_size).decode() message = self.ircsock.recv(self.recv_size).decode()
print(message)
except UnicodeDecodeError: except UnicodeDecodeError:
continue continue
message = message.strip(self.splitter) message = message.strip(self.splitter)
self.logger.debug(message) #self.logger.debug(message)
if not registered and magic_phrase["has_registered"] in message: if not registered and magic_phrase["has_registered"] in message:
registered = True registered = True
if not registered and magic_phrase["needs_to_register"] in message: if not registered and magic_phrase["needs_to_register"] in message:
@ -260,6 +271,12 @@ class Bot:
if not confirmed and magic_phrase["needs_to_confirm"] in message: if not confirmed and magic_phrase["needs_to_confirm"] in message:
self.send_message("NickServ", "CONFIRM {}", self.confirm) self.send_message("NickServ", "CONFIRM {}", self.confirm)
confirmed = True confirmed = True
if not registered and magic_phrase["not_registered"] in message:
break
#if not registered and magic_phrase["ready_to_id"] in message:
# self.send_message("NickServ", "IDENTIFY {}", password)
#if not registered and magic_phrase["nickserv_missing"] in message:
# break
if not authenticate: if not authenticate:
time.sleep(3) time.sleep(3)
@ -291,40 +308,45 @@ class Bot:
messages = [msg for msg in _message.split(self.splitter) if msg] messages = [msg for msg in _message.split(self.splitter) if msg]
for message in messages: for message in messages:
self.logger.debug("{}".format(message))
if message[:4] == "PING": if message[:4] == "PING":
self.ping(message) self.ping(message)
if "ping" in callback: if "ping" in callback:
callback["ping"]() callback["ping"]()
elif "PRIVMSG " in message: elif "PRIVMSG " in message:
name, source, response = self.parse(message) name, source, response = self.parse(message)
self.logger.debug(message)
if source == self.botnick and "pm" in callback: if source == self.botnick and "pm" in callback:
callback["pm"](name, response) callback["pm"](name, response)
elif "message" in callback: elif "message" in callback:
callback["message"](name, source, response) callback["message"](name, source, response)
elif "MODE " in message: elif "MODE " in message:
channel, mode = self.handle_mode(message) channel, mode = self.handle_mode(message)
self.logger.debug(message)
if "mode" in callback: if "mode" in callback:
callback["mode"](channel, mode) callback["mode"](channel, mode)
elif "NICK " in message: elif "NICK " in message:
old_name, new_name = self.handle_rename(message) old_name, new_name = self.handle_rename(message)
self.logger.debug(message)
if "nick" in callback: if "nick" in callback:
callback["nick"](old_name, new_name) callback["nick"](old_name, new_name)
elif "KICK " in message: elif "KICK " in message:
kicker, source = self.handle_kick(message) kicker, source = self.handle_kick(message)
self.logger.debug(message)
if "kick" in callback: if "kick" in callback:
callback["kick"](kicker, source) callback["kick"](kicker, source)
elif "JOIN " in message: elif "JOIN " in message:
user = self.handle_join(message) user = self.handle_join(message)
self.logger.debug(message)
if "join" in callback: if "join" in callback:
callback["join"](user) callback["join"](user)
elif "PART " in message: elif "PART " in message:
user = self.handle_part(message) user = self.handle_part(message)
self.logger.debug(message)
if "part" in callback: if "part" in callback:
callback["part"](user) callback["part"](user)
elif "INVITE " in message: elif "INVITE " in message:
channel, name = self.handle_invite(message) channel, name = self.handle_invite(message)
self.logger.debug(message)
if "invite" in callback: if "invite" in callback:
callback["invite"](channel, name) callback["invite"](channel, name)
elif "unhandled" in callback: elif "unhandled" in callback:
@ -332,6 +354,7 @@ class Bot:
callback["unhandled"](message) callback["unhandled"](message)
elif ":Closing link:" in message: elif ":Closing link:" in message:
self.logger.warning(message) self.logger.warning(message)
self.logger.error("Activing crash mode")
if "crashed" in callback: if "crashed" in callback:
callback["crashed"]() callback["crashed"]()
break break

View File

@ -7,7 +7,8 @@ class Responses:
self.triggers = { self.triggers = {
"name": dict(), "name": dict(),
"source": dict(), "source": dict(),
"response": dict() "response": dict(),
"passive": dict()
} }
def add_trigger(self, trigger_type, pattern, callback): def add_trigger(self, trigger_type, pattern, callback):
@ -40,6 +41,12 @@ class Responses:
return True return True
def log(self, name, trigger):
if trigger != "response":
return
now = datetime.now().timestamp()
self.bot.memories["users"][name]["last_response"] = now
def parse(self, name, source, response): def parse(self, name, source, response):
users = self.bot.memories["users"] users = self.bot.memories["users"]
if name not in users: if name not in users:
@ -49,7 +56,8 @@ class Responses:
trig = { trig = {
"name": name, "name": name,
"source": source, "source": source,
"response": response.lower().strip() "response": response.lower().strip(),
"passive": response.lower().strip()
} }
for trigger in list(self.triggers.keys()): for trigger in list(self.triggers.keys()):
@ -57,6 +65,19 @@ class Responses:
if pattern[0] != "/" and pattern[-1] != "/": if pattern[0] != "/" and pattern[-1] != "/":
if pattern == check: if pattern == check:
if self.allowed(name, source): if self.allowed(name, source):
self.log(name, trigger)
callback(self, name, source, response)
elif "blacklist" in users[name]:
reason = users[name]["blacklist"]["reason"]
message = "You were banished for reason '{}'"
message = message.format(reason)
#self.bot.send_message(name, message)
return False
elif trigger != "passive":
regex = re.compile(pattern[1:-1])
if regex.match(trig[trigger]) is not None:
if self.allowed(name, source):
self.log(name, trigger)
callback(self, name, source, response) callback(self, name, source, response)
elif "blacklist" in users[name]: elif "blacklist" in users[name]:
reason = users[name]["blacklist"]["reason"] reason = users[name]["blacklist"]["reason"]
@ -67,14 +88,5 @@ class Responses:
else: else:
regex = re.compile(pattern[1:-1]) regex = re.compile(pattern[1:-1])
if regex.match(trig[trigger]) is not None: if regex.match(trig[trigger]) is not None:
if self.allowed(name, source): callback(self, name, source, response)
callback(self, name, source, response)
elif "blacklist" in users[name]:
reason = users[name]["blacklist"]["reason"]
message = "You were banished for reason '{}'"
message = message.format(reason)
#self.bot.send_message(name, message)
return False
now = datetime.now().timestamp()
self.bot.memories["users"][name]["last_response"] = now