From 5ccca7d53e7a8f819e04872ce017bf417d58df94 Mon Sep 17 00:00:00 2001 From: Context 77 <126421199+ctx77@users.noreply.github.com> Date: Sun, 11 Aug 2024 22:32:02 +0200 Subject: [PATCH 1/4] f-stringify blockObs and only act on startswith --- FaustBot/Modules/BlockObserver.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/FaustBot/Modules/BlockObserver.py b/FaustBot/Modules/BlockObserver.py index 6c503da..2bf7374 100644 --- a/FaustBot/Modules/BlockObserver.py +++ b/FaustBot/Modules/BlockObserver.py @@ -2,6 +2,7 @@ from FaustBot.Communication.Connection import Connection from FaustBot.Modules.PrivMsgObserverPrototype import PrivMsgObserverPrototype from FaustBot.Model.BlockedUsers import BlockProvider + class BlockObserver(PrivMsgObserverPrototype): @staticmethod def cmd(): @@ -14,32 +15,33 @@ class BlockObserver(PrivMsgObserverPrototype): def update_on_priv_msg(self, data, connection: Connection): if not self._is_idented_mod(data, connection): return - if data['message'].find('.block ') != -1: + if data["message"].startswith(".block "): self.block(data, connection) - if data['message'].find ('.unblock')!=-1: + if data["message"].startswith(".unblock"): self.unblock(data, connection) - if data['message'].find('.isblocked') != -1: + if data["message"].startswith(".isblocked"): self.isBlocked(data, connection) - def block(self,data,connection): + def block(self, data, connection): blocklist = BlockProvider() blocklist.block(self.isolateTarget(data)) - connection.send_back("blocked: "+ self.isolateTarget(data), data) + connection.send_back(f"blocked: {self.isolateTarget(data)}", data) - def unblock(self,data,connection): + def unblock(self, data, connection): blocklist = BlockProvider() blocklist.delete_block(self.isolateTarget(data)) - connection.send_back("unblocked: "+ self.isolateTarget(data), data) + connection.send_back(f"unblocked: {self.isolateTarget(data)}", data) - def isBlocked(self,data,connection): - blocklist= BlockProvider() + def isBlocked(self, data, connection): + blocklist = BlockProvider() answ = blocklist.is_blocked(self.isolateTarget(data)) if answ: - connection.send_back(self.isolateTarget(data) + " ist geblocked", data) + connection.send_back(f"{self.isolateTarget(data)} ist geblocked", data) return - connection.send_back(self.isolateTarget(data)+" ist nicht geblocked", data) - def isolateTarget(self,data): - return data['message'].split(' ')[1] + connection.send_back(f"{self.isolateTarget(data)} ist nicht geblocked", data) + + def isolateTarget(self, data): + return data["message"].split(" ")[1] def _is_idented_mod(self, data: dict, connection: Connection): - return data['nick'] in self._config.mods and connection.is_idented(data['nick']) \ No newline at end of file + return data["nick"] in self._config.mods and connection.is_idented(data["nick"]) From fea9a88f8de866b7f8f3119fb585e1b33eb1190c Mon Sep 17 00:00:00 2001 From: Context 77 <126421199+ctx77@users.noreply.github.com> Date: Sun, 11 Aug 2024 22:32:36 +0200 Subject: [PATCH 2/4] f-stringify connections, and adding option to disable ssl for local testing --- FaustBot/Communication/Connection.py | 68 ++++++++++++++++------------ FaustBot/Model/ConnectionDetails.py | 28 ++++++++---- 2 files changed, 57 insertions(+), 39 deletions(-) diff --git a/FaustBot/Communication/Connection.py b/FaustBot/Communication/Connection.py index 0cfeea9..d130015 100644 --- a/FaustBot/Communication/Connection.py +++ b/FaustBot/Communication/Connection.py @@ -22,11 +22,12 @@ class Connection(object): details = None irc = None wraper = None + def sender(self): while True: msg = self.send_queue.get() - if msg[-1] != b'\n': - msg = msg + b'\n' + if msg[-1] != b"\n": + msg = msg + b"\n" self.irc.send(msg) time.sleep(1) @@ -35,14 +36,14 @@ class Connection(object): Send to channel :return: """ - self.raw_send("PRIVMSG " + self.details.get_channel() + " :" + text[0:]) + self.raw_send(f"PRIVMSG {self.details.get_channel()} :{text}") def send_to_user(self, user, text): """ Send to user :return: """ - self.raw_send('PRIVMSG ' + user + ' :' + text) + self.raw_send(f"PRIVMSG {user} :{text}") def send_back(self, text, data): """ @@ -51,13 +52,13 @@ class Connection(object): :param data: needed because of concurrency, there can't be a global variable holding where messages came from :return: """ - if data['channel'] == self.details.get_nick(): - self.send_to_user(data['nick'], text) + if data["channel"] == self.details.get_nick(): + self.send_to_user(data["nick"], text) else: self.send_channel(text) def raw_send(self, message): - self.send_queue.put(message.encode() + '\r\n'.encode()) + self.send_queue.put(f"{message}\r\n".encode()) def receive(self): """ @@ -69,8 +70,8 @@ class Connection(object): return False except socket.timeout: return False - data = data.decode('UTF-8', errors='replace') - #print('received: \n' + data) + data = data.decode("UTF-8", errors="replace") + # print('received: \n' + data) data_lines = self._receiver_buffer.append(data) if data is None: return False @@ -80,24 +81,24 @@ class Connection(object): data = data.rstrip() self.data = data - splited = data.split(' ') + splited = data.split(" ") if not len(splited) >= 2: continue command = splited[1] # print(command) - if data.split(' ')[0] == 'PING': + if data.split(" ")[0] == "PING": self.ping_observable.input(data, self) - elif command == 'JOIN': + elif command == "JOIN": self.join_observable.input(data, self) - elif command == 'PART' or command == 'QUIT': + elif command == "PART" or command == "QUIT": self.leave_observable.input(data, self) - elif command == 'KICK': + elif command == "KICK": self.kick_observable.input(data, self) - elif command == 'NICK': + elif command == "NICK": self.nick_change_observable.input(data, self) - elif command == 'NOTICE': + elif command == "NOTICE": self.notice_observable.input(data, self) - elif command == 'PRIVMSG': + elif command == "PRIVMSG": self.priv_msg_observable.input(data, self) else: try: @@ -109,7 +110,7 @@ class Connection(object): return True def is_idented(self, user: str): - self.send_to_user('NickServ', 'ACC ' + user) + self.send_to_user("NickServ", f"ACC {user}") with self.condition_lock: while user not in self.idented_look_up: self.condition_lock.wait() @@ -136,19 +137,28 @@ class Connection(object): """ establish the connection """ - self.wraper = ssl.create_default_context() self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - socker = socket.create_connection((self.details.get_server(), self.details.get_port())) - self.irc =self.wraper.wrap_socket(socker, server_hostname=self.details.get_server()) - #print(self.irc.recv(512)) - self.irc.send("NICK ".encode() + self.details.get_nick().encode() + "\r\n".encode()) + socker = socket.create_connection( + (self.details.get_server(), self.details.get_port()) + ) + if self.details.get_ssl().lower() != "false": + self.wraper = ssl.create_default_context() + self.irc = self.wraper.wrap_socket( + socker, server_hostname=self.details.get_server() + ) + else: + self.irc = socker + # print(self.irc.recv(512)) + self.irc.send(f"NICK {self.details.get_nick()}\r\n".encode()) self.irc.send("USER botty botty botty :Botty \n".encode()) - if (self.details.get_pwd() != ''): - self.send_to_user("NICKSERV", "identify " + self.details.get_nick() + " " + self.details.get_pwd() + ' ') - self.irc.send("JOIN ".encode() + self.details.get_channel().encode() + '\r\n'.encode()) - self.irc.send("WHO ".encode() + self.details.get_channel().encode() + '\r\n'.encode()) - self.irc.send("MODE ".encode()+self.details.get_nick().encode()+" -R".encode()+'\r\n'.encode()) - + if self.details.get_pwd() != "": + self.send_to_user( + "NICKSERV", + f"identify {self.details.get_nick()} {self.details.get_pwd()} ", + ) + self.irc.send(f"JOIN {self.details.get_channel()}\r\n".encode()) + self.irc.send(f"WHO {self.details.get_channel()}\r\n".encode()) + self.irc.send(f"MODE {self.details.get_nick()} -R\r\n".encode()) _thread.start_new_thread(self.sender, ()) diff --git a/FaustBot/Model/ConnectionDetails.py b/FaustBot/Model/ConnectionDetails.py index 9db70c0..c97c66a 100644 --- a/FaustBot/Model/ConnectionDetails.py +++ b/FaustBot/Model/ConnectionDetails.py @@ -3,36 +3,44 @@ class ConnectionDetails(object): """ :return: the server to connect to """ - return self._data['server'] + return self._data["server"] + + def get_ssl(self): + """ + :return: connect with ssl + """ + if self._data["ssl"] != "false": + return "true" + return "false" def get_nick(self): """ :return: own nick """ - return self._data['nick'] + return self._data["nick"] def get_channel(self): """ :return: the channel connected into """ - return self._data['channel'] + return self._data["channel"] def get_port(self): - return int(self._data['port']) + return int(self._data["port"]) def get_lang(self): - return self._data['lang'] + return self._data["lang"] def change_lang(self, lang): - self._data['lang'] = lang + self._data["lang"] = lang def get_mods(self): - return self._data['mods'] + return self._data["mods"] def get_pwd(self): - if self._data['pwd'] is None: - return '' - return self._data['pwd'] + if self._data["pwd"] is None: + return "" + return self._data["pwd"] def __init__(self, config): self._data = config From f8f83e442f5438c0f7c101286da44ade42ddcd75 Mon Sep 17 00:00:00 2001 From: Context 77 <126421199+ctx77@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:01:03 +0200 Subject: [PATCH 3/4] fix titleObserver --- FaustBot/Modules/TitleObserver.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/FaustBot/Modules/TitleObserver.py b/FaustBot/Modules/TitleObserver.py index 1dac8ec..8c509d2 100644 --- a/FaustBot/Modules/TitleObserver.py +++ b/FaustBot/Modules/TitleObserver.py @@ -44,9 +44,13 @@ class TitleObserver(PrivMsgObserverPrototype): raise (Exception("Refusing to parse bare IPv4 Addresses")) if re.search("https?://music.youtube.com/", url): url = url.replace("music.youtube.com/", "www.youtube.com/", 1) + if re.search("https?://youtu.be/", url): + url = url.replace("youtu.be/", "www.youtube.com/watch?v=", 1) if re.search("https?://[^/]*youtube.com/shorts/", url): - title_re = re.compile('''"reelPlayerHeaderRenderer":{"reelTitleText":{"runs":\[{"text":"([^"]*)"''') + title_re = re.compile( + '''"reelPlayerHeaderRenderer":{"reelTitleText":{"runs":\[{"text":"([^"]*)"''' + ) headers["User-Agent"] = "curl/7.81.0" elif re.search("https?://[^/]*youtube.com/", url): title_re = re.compile( From 0abca6fb9542b0c1c3f7dd07986c2d80c6038116 Mon Sep 17 00:00:00 2001 From: Context 77 <126421199+ctx77@users.noreply.github.com> Date: Sat, 14 Dec 2024 13:43:03 +0100 Subject: [PATCH 4/4] require .icd to resolve ICD10 codes and limit answer to at most 5 --- .../Modules/CustomUserModules/ICDObserver.py | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/FaustBot/Modules/CustomUserModules/ICDObserver.py b/FaustBot/Modules/CustomUserModules/ICDObserver.py index d69aaa9..d4715a9 100644 --- a/FaustBot/Modules/CustomUserModules/ICDObserver.py +++ b/FaustBot/Modules/CustomUserModules/ICDObserver.py @@ -21,17 +21,18 @@ class ICDObserver(PrivMsgObserverPrototype): return None def update_on_priv_msg(self, data, connection: Connection): - if data["channel"] == connection.details.get_channel(): - regex = r"\b(\w\d{2}\.?\d?\d?)\b" - codes = re.findall(regex, data["message"]) - for code in codes: - code = code.capitalize() - text = self.icd10_dict.get(code, False) - if text == False: - if "." in code: - code += "-" - else: - code += ".-" - text = self.icd10_dict.get(code, False) - if text: - connection.send_back(f"{code} - {text}", data) + if data["message"].startswith(".icd "): + if data["channel"] == connection.details.get_channel(): + regex = r"\b(\w\d{2}\.?\d?\d?)\b" + codes = re.findall(regex, data["message"]) + for code in codes[:5]: + code = code.capitalize() + text = self.icd10_dict.get(code, False) + if text == False: + if "." in code: + code += "-" + else: + code += ".-" + text = self.icd10_dict.get(code, False) + if text: + connection.send_back(f"{code} - {text}", data)