From 8d0515ea50f4babd747c0209736a06ca900c54e1 Mon Sep 17 00:00:00 2001 From: cqst Date: Fri, 22 Aug 2025 20:40:37 -0700 Subject: ssl support and global cleanup --- notcqst.py | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/notcqst.py b/notcqst.py index b16c1b4..7df9199 100644 --- a/notcqst.py +++ b/notcqst.py @@ -1,4 +1,5 @@ import socket +import ssl import random import time from yt_dlp import YoutubeDL @@ -9,18 +10,20 @@ from yt_dlp import YoutubeDL reconnect = 0 text_str = 0 -server = "irc.libera.chat" +hostname = "irc.libera.chat" channel = "##tech-tangents" botnick = "notcqst" -irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +context = ssl.create_default_context() + user_str = ("USER " + botnick + " " + botnick + " " + botnick + " :" + botnick + "\n") nick_str = ("NICK " + botnick + "\n") channel_string = ("JOIN " + channel + "\n") send_to_channel_str = ("PRIVMSG " + channel + " :Hello " + "\r\n") -def ping(): +def ping(irc): if "PING" in text_str: split_str = text_str.split(" ") print(split_str) @@ -32,7 +35,7 @@ def ping(): irc.send((bytes("PONG :" + split_str[1] + "\r\n", 'utf-8'))) -def check_youtube(): +def check_youtube(irc): if "https://youtube.com" in text_str or "https://www.youtube.com" in text_str: split_str = text_str.split('https://') vid_url_www = split_str[1].strip() @@ -49,50 +52,52 @@ def check_youtube(): return 0 -def command_choose(split_spaces): +def command_choose(split_spaces, irc): choices = split_spaces[2:] decision = random.choice(choices) irc.send((bytes("PRIVMSG " + channel + " :" + decision + "\r\n", 'utf-8'))) -def command_hello(): +def command_hello(irc): irc.send((bytes("PRIVMSG " + channel + " :" + "Hello, " + "\r\n", 'utf-8'))) -def unknown_command(): +def unknown_command(irc): irc.send((bytes("PRIVMSG " + channel + " :" + "Sorry I don't understand." + "\r\n", 'utf-8'))) -def command_flag(): +def command_flag(irc): if ":n!" in text_str: split_text = text_str.split(':n!') split_spaces = split_text[1].split(' ') command = split_spaces[1].strip() match command: - case "choose": command_choose(split_spaces) + case "choose": command_choose(split_spaces, irc) case "hello": command_hello() case _: unknown_command() -def parse_cmd(): - ping() - command_flag() - check_youtube() +def parse_cmd(irc): + ping(irc) + command_flag(irc) + check_youtube(irc) def connect_checked(): if reconnect >= 3: exit(1) else: - irc.connect((server, 6667)) - irc.send(bytes(user_str, 'utf-8')) - irc.send(bytes(nick_str, 'utf-8')) - irc.send(bytes(channel_string, 'utf-8')) - irc.send(bytes(send_to_channel_str, 'utf-8')) - connect_loop() + # irc.connect((hostname, 6697)) + with socket.create_connection((hostname, 6697)) as sock: + with context.wrap_socket(sock, server_hostname=hostname) as irc: + irc.send(bytes(user_str, 'utf-8')) + irc.send(bytes(nick_str, 'utf-8')) + irc.send(bytes(channel_string, 'utf-8')) + irc.send(bytes(send_to_channel_str, 'utf-8')) + connect_loop(irc) -def connect_loop(): +def connect_loop(irc): while 1: text = irc.recv(2040) if not text: @@ -100,7 +105,7 @@ def connect_loop(): print(text) global text_str text_str = text.decode("utf-8") - parse_cmd() + parse_cmd(irc) irc.close() global reconnect reconnect += 1 @@ -108,7 +113,7 @@ def connect_loop(): connect_checked() -print("connecting to: " + server) +print("connecting to: " + hostname) connect_checked() -- cgit v1.2.3