From f3262c59a6b9169aaf47c3cbd311606c4887adc6 Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Sun, 27 Apr 2025 14:14:31 -0400 Subject: [PATCH 1/4] Start the refactor IRC branch --- Pipfile | 3 +-- Pipfile.lock | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Pipfile b/Pipfile index 0fe65f1a..ea7313cc 100644 --- a/Pipfile +++ b/Pipfile @@ -20,9 +20,8 @@ flake8-variables-names = "==0.0.6" gino = "==1.0.1" gitpython = "==3.1.44" hypothesis = "==6.131.9" -ib3 = "==0.2.0" inflect = "==7.3.1" -irc = "==20.1.0" +irc = "==20.5.0" isort = "==6.0.1" munch = "==4.0.0" typing_extensions = "==4.8.0" diff --git a/Pipfile.lock b/Pipfile.lock index 011fb517..fca75d3a 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "8b501084faec421941fcced69588718c272c7efe9ed0efab61ee5db6af292c54" + "sha256": "527efb36731b385d149192ae37331314e1ecd6a98d8c21cd6127941e41b31d2f" }, "pipfile-spec": 6, "requires": { @@ -262,11 +262,11 @@ }, "certifi": { "hashes": [ - "sha256:3d5da6925056f6f18f119200434a4780a94263f10d1c21d032a6f6b2baa20651", - "sha256:ca78db4565a652026a4db2bcdf68f2fb589ea80d0be70e03929ed730746b84fe" + "sha256:0a816057ea3cdefcef70270d2c515e4506bbc954f417fa5ade2021213bb8f0c6", + "sha256:30350364dfe371162649852c63336a15c70c6510c2ad5015b21c2345311805f3" ], "markers": "python_version >= '3.6'", - "version": "==2025.1.31" + "version": "==2025.4.26" }, "cffi": { "hashes": [ @@ -621,14 +621,6 @@ "markers": "python_version >= '3.9'", "version": "==6.131.9" }, - "ib3": { - "hashes": [ - "sha256:23023c8998b3eec660bc222ecf6e9ef75ebbf59f4b63e1b18c17021cc836a3ad", - "sha256:bc4ea0eba083ad1ed9bc9a7a57f306e3784f9dc2cd8f926dd56016bc5659a5ff" - ], - "index": "pypi", - "version": "==0.2.0" - }, "idna": { "hashes": [ "sha256:12f65c9b470abda6dc35cf8e63cc574b1c52b11df2c86030af0ac09b01b13ea9", @@ -637,6 +629,14 @@ "markers": "python_version >= '3.6'", "version": "==3.10" }, + "importlib-resources": { + "hashes": [ + "sha256:185f87adef5bcc288449d98fb4fba07cea78bc036455dd44c5fc4a2fe78fed2c", + "sha256:789cfdc3ed28c78b67a06acb8126751ced69a3d5f79c095a98298cd8a760ccec" + ], + "markers": "python_version >= '3.9'", + "version": "==6.5.2" + }, "inflect": { "hashes": [ "sha256:bedbae76877b054ecf0597153725677ab618fdd69abf189cc82e0f7a6720669d", @@ -656,12 +656,12 @@ }, "irc": { "hashes": [ - "sha256:5114aee1247ff634abed80f3349d75ccab35616a4b76be2a25add7ef189db0e5", - "sha256:b6f737932dd4791f3b18e319de7b7daf02d2285a6bea263d101f4d8e553807ec" + "sha256:712c6065c41ceaf75c874d623b02edaa5e6c7d605532da81c52d39629a8e9fd0", + "sha256:8ddbfd19f71204ceceba7b7c72724b15b3fa87bab5e81e45a75bef736a1a3c76" ], "index": "pypi", - "markers": "python_version >= '3.7'", - "version": "==20.1.0" + "markers": "python_version >= '3.8'", + "version": "==20.5.0" }, "isort": { "hashes": [ @@ -1373,11 +1373,11 @@ }, "setuptools": { "hashes": [ - "sha256:128ce7b8f33c3079fd1b067ecbb4051a66e8526e7b65f6cec075dfc650ddfa88", - "sha256:e147c0549f27767ba362f9da434eab9c5dc0045d5304feb602a0af001089fc51" + "sha256:a38f898dcd6e5380f4da4381a87ec90bd0a7eec23d204a5552e80ee3cab6bd27", + "sha256:c40a5b3729d58dd749c0f08f1a07d134fb8a0a3d7f87dc33e7c5e1f762138650" ], "markers": "python_version >= '3.9'", - "version": "==79.0.1" + "version": "==80.0.0" }, "six": { "hashes": [ From f6365573d74d550a1171271d9f4ab5f93f21fed8 Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Sun, 27 Apr 2025 16:43:53 -0400 Subject: [PATCH 2/4] Update IRC authentication system --- techsupport_bot/bot.py | 22 +++++--- techsupport_bot/ircrelay/__init__.py | 2 +- techsupport_bot/ircrelay/{irc.py => relay.py} | 55 +++++++++++++++---- 3 files changed, 61 insertions(+), 18 deletions(-) rename techsupport_bot/ircrelay/{irc.py => relay.py} (90%) diff --git a/techsupport_bot/bot.py b/techsupport_bot/bot.py index 773dd16a..1634d089 100644 --- a/techsupport_bot/bot.py +++ b/techsupport_bot/bot.py @@ -1041,12 +1041,13 @@ async def can_run( # IRC Stuff - async def start_irc(self: Self) -> None: - """Starts the IRC connection in a seperate thread""" - irc_config = self.file_config.api.irc + async def start_irc(self: Self): + """Starts the IRC bot in a separate thread.""" main_loop = asyncio.get_running_loop() + irc_config = self.file_config.api.irc - irc_bot = ircrelay.IRCBot( + # Create the bot instance + irc_bot = ircrelay.relay.IRCBot( loop=main_loop, server=irc_config.server, port=irc_config.port, @@ -1054,10 +1055,17 @@ async def start_irc(self: Self) -> None: username=irc_config.name, password=irc_config.password, ) + self.irc = irc_bot - irc_thread = threading.Thread(target=irc_bot.start) + def run_in_thread(): + """Run the IRC bot in a separate thread.""" + irc_bot.start_bot() + + # Start the bot in a new thread await self.logger.send_log( - message="Logging in to IRC", level=LogLevel.INFO, console_only=True + message="Logging into IRC", level=LogLevel.INFO, console_only=True ) - irc_thread.start() + + bot_thread = threading.Thread(target=run_in_thread) + bot_thread.start() diff --git a/techsupport_bot/ircrelay/__init__.py b/techsupport_bot/ircrelay/__init__.py index 1fd80926..8c48666b 100644 --- a/techsupport_bot/ircrelay/__init__.py +++ b/techsupport_bot/ircrelay/__init__.py @@ -1,4 +1,4 @@ """Allows python to find the irc packages""" from .formatting import * -from .irc import * +from .relay import * diff --git a/techsupport_bot/ircrelay/irc.py b/techsupport_bot/ircrelay/relay.py similarity index 90% rename from techsupport_bot/ircrelay/irc.py rename to techsupport_bot/ircrelay/relay.py index 725f8d36..3916f641 100644 --- a/techsupport_bot/ircrelay/irc.py +++ b/techsupport_bot/ircrelay/relay.py @@ -4,21 +4,24 @@ from __future__ import annotations import asyncio +import functools import logging import os +import ssl import threading from typing import Self import commands import discord -import ib3.auth + +# import formatting import irc.bot import irc.client -import irc.strings +import irc.connection from ircrelay import formatting -class IRCBot(ib3.auth.SASL, irc.bot.SingleServerIRCBot): +class IRCBot(irc.bot.SingleServerIRCBot): """The IRC bot class. This is the class that runs the entire IRC side of the bot The class to start the entire IRC bot @@ -58,18 +61,28 @@ def __init__( username: str, password: str, ) -> None: - self.loop = loop + self.username = username + self.password = password + self.join_channel_list = channels + + # SSL context setup + context = ssl.create_default_context() + factory = irc.connection.Factory( + wrapper=functools.partial(context.wrap_socket, server_hostname=server) + ) + + # Pass the correct server info and password super().__init__( - server_list=[(server, port)], + server_list=[ + (server, port, password) + ], # Ensure this has the correct password realname=username, nickname=username, - ident_password=password, - channels=channels, + connect_factory=factory, ) - self.join_channel_list = channels - self.username = username - self.password = password + + # Reconnect handler if disconnected self._on_disconnect = self.reconnect_from_disconnect def exit_irc(self: Self) -> None: @@ -77,6 +90,16 @@ def exit_irc(self: Self) -> None: # pylint: disable=protected-access os._exit(1) + def start_bot(self): + """Start the bot and handle SASL authentication.""" + try: + self.connection.set_rate_limit(1) # Be nice to server + self.connection.username = self.username + self.connection.sasl_login = self.username + self.start() # Starts the IRC bot's main loop + except Exception as e: + os._exit(1) + def reconnect_from_disconnect( self: Self, connection: irc.client.ServerConnection, event: irc.client.Event ) -> None: @@ -317,3 +340,15 @@ def is_bot_op_on_channel(self: Self, channel_name: str) -> bool: bool: True if the bot is an operator, False if it's not """ return self.channels[channel_name].is_oper(self.username) + + +""" +IRCBot( + loop="main_loop", + server="irc.libera.chat", + port=6697, + channels=["#techsupport-development", "##techsupport-dev-testing"], + username="TSDevBot", + password="Blemish2-Outback-Nag-Overplay", +).start_bot() +""" From a129504887f706114f821198f6bfb372e7dc6080 Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Sun, 27 Apr 2025 16:46:56 -0400 Subject: [PATCH 3/4] Formatting --- techsupport_bot/bot.py | 4 ++-- techsupport_bot/ircrelay/relay.py | 25 +++++-------------------- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/techsupport_bot/bot.py b/techsupport_bot/bot.py index 1634d089..133ffbf1 100644 --- a/techsupport_bot/bot.py +++ b/techsupport_bot/bot.py @@ -1041,7 +1041,7 @@ async def can_run( # IRC Stuff - async def start_irc(self: Self): + async def start_irc(self: Self) -> None: """Starts the IRC bot in a separate thread.""" main_loop = asyncio.get_running_loop() irc_config = self.file_config.api.irc @@ -1058,7 +1058,7 @@ async def start_irc(self: Self): self.irc = irc_bot - def run_in_thread(): + def run_in_thread() -> None: """Run the IRC bot in a separate thread.""" irc_bot.start_bot() diff --git a/techsupport_bot/ircrelay/relay.py b/techsupport_bot/ircrelay/relay.py index 3916f641..67216269 100644 --- a/techsupport_bot/ircrelay/relay.py +++ b/techsupport_bot/ircrelay/relay.py @@ -90,15 +90,12 @@ def exit_irc(self: Self) -> None: # pylint: disable=protected-access os._exit(1) - def start_bot(self): + def start_bot(self: Self) -> None: """Start the bot and handle SASL authentication.""" - try: - self.connection.set_rate_limit(1) # Be nice to server - self.connection.username = self.username - self.connection.sasl_login = self.username - self.start() # Starts the IRC bot's main loop - except Exception as e: - os._exit(1) + self.connection.set_rate_limit(1) # Be nice to server + self.connection.username = self.username + self.connection.sasl_login = self.username + self.start() # Starts the IRC bot's main loop def reconnect_from_disconnect( self: Self, connection: irc.client.ServerConnection, event: irc.client.Event @@ -340,15 +337,3 @@ def is_bot_op_on_channel(self: Self, channel_name: str) -> bool: bool: True if the bot is an operator, False if it's not """ return self.channels[channel_name].is_oper(self.username) - - -""" -IRCBot( - loop="main_loop", - server="irc.libera.chat", - port=6697, - channels=["#techsupport-development", "##techsupport-dev-testing"], - username="TSDevBot", - password="Blemish2-Outback-Nag-Overplay", -).start_bot() -""" From 1c775383678ead2bfe2f2d7c8e5886f4f6351150 Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Sun, 27 Apr 2025 16:48:18 -0400 Subject: [PATCH 4/4] Formatting --- techsupport_bot/ircrelay/relay.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/techsupport_bot/ircrelay/relay.py b/techsupport_bot/ircrelay/relay.py index 67216269..23e668df 100644 --- a/techsupport_bot/ircrelay/relay.py +++ b/techsupport_bot/ircrelay/relay.py @@ -13,8 +13,6 @@ import commands import discord - -# import formatting import irc.bot import irc.client import irc.connection