Updating to use tildeverse links and fixes bugs for using bot with lite features

This commit is contained in:
aewens 2019-03-07 12:37:55 -05:00
parent 286c144bdf
commit 64d0208837
7 changed files with 47 additions and 22 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
# App-specific
settings.json
settings.demo.json
settings.test.json
data/*.json
logs/*.log*
logs/*.log.*

View File

@ -75,9 +75,8 @@ def summon(self, name, source, response):
self.bot.send_message(source, confirmation)
def how_dare_you(self, name, source, response):
user = response.split("!summon ")[1]
rude = "{}: You think you can just summon someone without a reason? Rude."
self.bot.send_message(source, rude.format(user))
self.bot.send_message(source, rude.format(name))
def whois(self, name, source, response):
botnick = self.bot.botnick
@ -104,4 +103,4 @@ def whois(self, name, source, response):
elif not (registered or nameservers):
self.bot.send_message(source, "{} is '{}'".format(domain, "available"))
else:
self.bot.send_message(source, "{} might be available".format(domain))
self.bot.send_message(source, "{} might be available".format(domain))

8
app.py
View File

@ -65,7 +65,9 @@ def handle_invite(channel, name):
if changed:
bot.thread(bot.save_memories)
def handle_kick(name):
def handle_kick(name, source):
if source in bot.settings.get("extras", dict()).get("rejoin", list()):
bot.join(source)
users = bot.memories["users"]
if name not in users:
bot.memories["users"][name] = dict()
@ -79,8 +81,8 @@ def handle_message(name, source, response):
bot.logger.debug(":: {}".format(bot.memories))
def handle_raw(message):
with open("/tmp/babs", "a") as babs:
babs.write(repr(message) + "\n")
if "KICK #chaos {}".format(bot.author) in message:
bot.send("INVITE {} :#chaos".format(bot.author))
def handle_crashed():
bot.logger.debug("Rebooting")

View File

@ -1,4 +1,6 @@
import re
import ssl
import time
import json
import socket
import os.path
@ -13,11 +15,12 @@ logging.basicConfig(
)
class Bot:
def __init__(self, server, port):
def __init__(self, server, port, secure=False):
self.ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.logger = logging.getLogger("")
self.server = server
self.port = port
self.secure = secure
self.channels = []
self.running = True
self.crashed = False
@ -31,6 +34,9 @@ class Bot:
self.recv_size = 2048
self.splitter = "\r\n"
if self.secure:
self.ircsock = ssl.wrap_socket(self.ircsock)
def send(self, message, *args):
response = message.format(*args) + "\n"
password = self.settings.get("password", None)
@ -65,9 +71,13 @@ class Bot:
message = ""
magic_string = "End of /NAMES list."
banned = "Cannot join channel (You're banned)"
while magic_string not in message:
try:
message = self.ircsock.recv(self.recv_size).decode()
if banned in message:
self.places.remove(chan)
return
# message = message.strip(self.splitter)
self.logger.debug(message)
except UnicodeDecodeError:
@ -141,8 +151,9 @@ class Bot:
def handle_kick(self, message):
before, after = message.split("KICK ", 1)
source = after.split(" ", 1)[0]
name = self.parse_name(self.get_name(before))
return name
return name, source
def handle_join(self, message):
before, after = message.split("JOIN ", 1)
@ -231,9 +242,10 @@ class Bot:
"needs_to_register": "choose a different nick",
"needs_to_confirm": "Your account will expire"
}
authenticate = len(password) > 0 and len(confirm) > 0
magic_string = "MODE {} +r".format(self.botnick)
while magic_string not in message:
while magic_string not in message and authenticate:
try:
message = self.ircsock.recv(self.recv_size).decode()
except UnicodeDecodeError:
@ -248,7 +260,9 @@ class Bot:
if not confirmed and magic_phrase["needs_to_confirm"] in message:
self.send_message("NickServ", "CONFIRM {}", self.confirm)
confirmed = True
if not authenticate:
time.sleep(3)
self.send("MODE {} +B".format(self.botnick))
print("DEBUG: Joining")
@ -298,9 +312,9 @@ class Bot:
if "nick" in callback:
callback["nick"](old_name, new_name)
elif "KICK " in message:
kicker = self.handle_kick(message)
kicker, source = self.handle_kick(message)
if "kick" in callback:
callback["kick"](kicker)
callback["kick"](kicker, source)
elif "JOIN " in message:
user = self.handle_join(message)
if "join" in callback:

View File

@ -20,10 +20,6 @@ class Responses:
if name not in users:
return False
if name in users and "blacklist" in users[name]:
reason = users[name]["blacklist"]["reason"]
return False
last_response = 0
if "last_response" in self.bot.memories["users"][name]:
last_response = self.bot.memories["users"][name]["last_response"]
@ -59,13 +55,26 @@ class Responses:
for trigger in list(self.triggers.keys()):
for pattern, callback in self.triggers[trigger].items():
if pattern[0] != "/" and pattern[-1] != "/":
if pattern == check and self.allowed(name, source):
callback(self, name, source, response)
if pattern == check:
if self.allowed(name, source):
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
else:
regex = re.compile(pattern[1:-1])
if regex.match(trig[trigger]) is not None:
if self.allowed(name, source):
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
self.bot.memories["users"][name]["last_response"] = now

View File

@ -18,7 +18,7 @@ coroutines = [
"state": {
"alias": "bbj",
"source": "http://localhost:7099/api",
"channels": ["#team"]
"channels": ["#team", "#tildeverse"]
}
},
{
@ -41,4 +41,4 @@ coroutines = [
"channels": ["#tildeverse"]
}
}
]
]

0
settings.example.json Normal file → Executable file
View File