diff --git a/core/irc.py b/core/irc.py index 19c567a..17bad7b 100644 --- a/core/irc.py +++ b/core/irc.py @@ -127,7 +127,7 @@ class Irc: # Define the IrcSocket object - self.IrcSocket:Union[socket.socket, SSLSocket] = None + self.IrcSocket: Union[socket.socket, SSLSocket] = None self.__create_table() self.Base.create_thread(func=self.heartbeat, func_args=(self.beat, )) @@ -145,6 +145,7 @@ class Irc: self.init_service_user() self.__create_socket() self.__connect_to_irc(ircInstance) + except AssertionError as ae: self.Logs.critical(f'Assertion error: {ae}') @@ -197,6 +198,7 @@ class Irc: self.Logs.critical(f"AttributeError: {ae} - {soc.fileno()}") def __ssl_context(self) -> ssl.SSLContext: + ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE diff --git a/core/utils.py b/core/utils.py index 93188b1..d5aec4b 100644 --- a/core/utils.py +++ b/core/utils.py @@ -1,3 +1,6 @@ +''' +Main utils library. +''' from pathlib import Path from typing import Literal, Optional, Any from datetime import datetime diff --git a/mods/votekick/mod_votekick.py b/mods/votekick/mod_votekick.py index f1b2145..eb3c355 100644 --- a/mods/votekick/mod_votekick.py +++ b/mods/votekick/mod_votekick.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any import re from dataclasses import dataclass, field @@ -55,6 +55,9 @@ class Votekick(): # Add Channel object to the module self.Channel = ircInstance.Channel + # Add Utils. + self.Utils = ircInstance.Utils + # Créer les nouvelles commandes du module self.Irc.build_command(1, self.module_name, 'vote', 'The kick vote module') @@ -150,7 +153,7 @@ class Votekick(): Args: channel (str): le salon à enregistrer. """ - current_datetime = self.Base.get_datetime() + current_datetime = self.Utils.get_sdatetime() mes_donnees = {'channel': channel} response = self.Base.db_execute_query("SELECT id FROM votekick_channel WHERE channel = :channel", mes_donnees) @@ -256,7 +259,7 @@ class Votekick(): except Exception as err: self.Logs.error(f"General Error: {err}") - def hcmds(self, user:str, channel: any, cmd: list, fullcmd: list = []) -> None: + def hcmds(self, user:str, channel: Any, cmd: list, fullcmd: list = []) -> None: # cmd is the command starting from the user command # full cmd is sending the entire server response diff --git a/mods/votekick/schemas.py b/mods/votekick/schemas.py new file mode 100644 index 0000000..7b7c0c6 --- /dev/null +++ b/mods/votekick/schemas.py @@ -0,0 +1,11 @@ +from typing import Optional +from core.definition import MainModel, dataclass, field + + +@dataclass +class VoteChannelModel(MainModel): + channel_name: Optional[str] = None + target_user: Optional[str] = None + voter_users: list = field(default_factory=list) + vote_for: int = 0 + vote_against: int = 0 diff --git a/mods/votekick/votekick_manager.py b/mods/votekick/votekick_manager.py new file mode 100644 index 0000000..8694ffe --- /dev/null +++ b/mods/votekick/votekick_manager.py @@ -0,0 +1,98 @@ +from typing import TYPE_CHECKING, Optional +from mods.votekick.schemas import VoteChannelModel + +if TYPE_CHECKING: + from mods.votekick.mod_votekick import Votekick + +class VotekickManager: + + VOTE_CHANNEL_DB:list[VoteChannelModel] = [] + + def __init__(self, uplink: 'Votekick'): + self.uplink = uplink + self.Logs = uplink.Logs + self.Utils = uplink.Utils + + def activate_new_channel(self, channel_name: str) -> bool: + """Activate a new channel in the votekick systeme + + Args: + channel_name (str): The channel name you want to activate + + Returns: + bool: True if it was activated + """ + votec = self.get_vote_channel_model(channel_name) + + if votec is None: + self.VOTE_CHANNEL_DB.append( + VoteChannelModel( + channel_name=channel_name, + target_user='', + voter_users=[], + vote_for=0, + vote_against=0 + ) + ) + self.Logs.debug(f"[VOTEKICK MANAGER] {channel_name} has been activated.") + return True + + return False + + def get_vote_channel_model(self, channel_name: str) -> Optional[VoteChannelModel]: + """Get Vote Channel Object model + + Args: + channel_name (str): The channel name you want to activate + + Returns: + (VoteChannelModel | None): The VoteChannelModel if exist + """ + for vote in self.VOTE_CHANNEL_DB: + if vote.channel_name.lower() == channel_name.lower(): + self.Logs.debug(f"[VOTEKICK MANAGER] {channel_name} has been found in the VOTE_CHANNEL_DB") + return vote + + return None + + def drop_vote_channel_model(self, channel_name: str) -> bool: + """Drop a channel from the votekick system. + + Args: + channel_name (str): The channel name you want to drop + + Returns: + bool: True if the channel has been droped. + """ + votec = self.get_vote_channel_model(channel_name) + + if votec: + self.VOTE_CHANNEL_DB.remove(votec) + self.Logs.debug(f"[VOTEKICK MANAGER] {channel_name} has been removed from the VOTE_CHANNEL_DB") + return True + + return False + + def is_vote_ongoing(self, channel_name: str) -> bool: + """Check if there is an angoing vote on the channel provided + + Args: + channel_name (str): The channel name to check + + Returns: + bool: True if there is an ongoing vote on the channel provided. + """ + + votec = self.get_vote_channel_model(channel_name) + + if votec is None: + self.Logs.debug(f"[VOTEKICK MANAGER] {channel_name} is not activated!") + return False + + if votec.target_user: + self.Logs.debug(f'[VOTEKICK MANAGER] A vote is ongoing on {channel_name}') + return True + + self.Logs.debug(f'[VOTEKICK MANAGER] {channel_name} is activated but there is no ongoing vote!') + + return False