mirror of
https://github.com/iio612/DEFENDER.git
synced 2026-02-13 19:24:23 +00:00
Compare commits
61 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6dade09257 | ||
|
|
d7fab2d701 | ||
|
|
9533b010b2 | ||
|
|
dbfc04a936 | ||
|
|
824db73590 | ||
|
|
bfb449f804 | ||
|
|
96bf4b6f80 | ||
|
|
8c772f5882 | ||
|
|
922336363e | ||
|
|
e5ceada997 | ||
|
|
e4781614f4 | ||
|
|
5ba4e27e3d | ||
|
|
eda0edb92a | ||
|
|
deae79db57 | ||
|
|
ab593b0ae6 | ||
|
|
e4a0c530a3 | ||
|
|
b0325d4db5 | ||
|
|
b9e4878764 | ||
|
|
22cec8a0ef | ||
|
|
1837edf1c2 | ||
|
|
62b10313a4 | ||
|
|
c369d86a22 | ||
|
|
61813e38ae | ||
|
|
743069f8e0 | ||
|
|
de69a1af63 | ||
|
|
c8c5f782d7 | ||
|
|
a639964701 | ||
|
|
ff0f880fcd | ||
|
|
41b9582e43 | ||
|
|
dba0301190 | ||
|
|
6e6d001605 | ||
|
|
e27a027ae9 | ||
|
|
19b7f85ec7 | ||
|
|
7068d88168 | ||
|
|
884c5bf0cd | ||
|
|
7e24d7cf4c | ||
|
|
d16b73656f | ||
|
|
8acbb7187c | ||
|
|
51bea90e6f | ||
|
|
4cb54b5b2e | ||
|
|
310f732a5b | ||
|
|
018fd8d959 | ||
|
|
07fa518fcc | ||
|
|
37dcd23353 | ||
|
|
748e7bffc9 | ||
|
|
681e0da041 | ||
|
|
667281ffb4 | ||
|
|
4b18675e8b | ||
|
|
f63aabfb7a | ||
|
|
b4c1df7f4a | ||
|
|
4c1a929867 | ||
|
|
0f1aa6f946 | ||
|
|
20684339d3 | ||
|
|
d53a3c58c9 | ||
|
|
5c7f0e3ad0 | ||
|
|
168f8db5ab | ||
|
|
91a6218692 | ||
|
|
02164e4580 | ||
|
|
a30fd9bb50 | ||
|
|
6c6a79a843 | ||
|
|
298da8bfab |
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
.pyenv/
|
||||
db/
|
||||
logs/
|
||||
__pycache__/
|
||||
configuration.json
|
||||
test.py
|
||||
98
README.md
98
README.md
@@ -2,70 +2,54 @@
|
||||
Defender est un service IRC basé sur la sécurité des réseaux IRC ( UnrealIRCD )
|
||||
Il permet d'ajouter une sécurité supplémentaire pour vérifier les users connectés au réseau
|
||||
en demandant aux user un code de validation.
|
||||
Il permet aux opérateurs de gérer efficacement un canal, tout en offrant aux utilisateurs des outils d'interaction et de décision collective.
|
||||
|
||||
Pré-requis :
|
||||
# Fonctionnalités principales
|
||||
Commandes opérateurs complètes:
|
||||
Kick: Expulser un utilisateur du canal.
|
||||
Ban: Interdire définitivement l'accès au canal.
|
||||
Unban: Lever une interdiction.
|
||||
Op/Deop: Attribuer ou retirer les droits d'opérateur.
|
||||
Halfop/Dehalfop: Attribuer ou retirer les droits
|
||||
Voice/Devoice: Attribuer ou retirer les droits de voix.
|
||||
|
||||
- Python version >= 3.10
|
||||
- Pip de python installé sur la machine
|
||||
- Python librairies psutil & sqlalchemy & requests
|
||||
- IRC Serveur Version >= UnrealIRCd-6.1.2.2
|
||||
Système de quarantaine:
|
||||
Mise en quarantaine: Isoler temporairement un utilisateur dans un canal privé.
|
||||
Libération: Permettre à un utilisateur de quitter la quarantaine en entrant un code spécifique.
|
||||
|
||||
Lancement de Defender :
|
||||
Système de vote:
|
||||
Kick: Les utilisateurs peuvent voter pour expulser un membre du canal.
|
||||
Autres actions: Possibilité d'étendre le système de vote à d'autres actions (ban, etc.).
|
||||
|
||||
- Installer les librairies python : psutil & sqlalchemy & requests
|
||||
- pip3 install psutil sqlalchemy requests ou pip install psutil sqlalchemy requests
|
||||
- Ne pas lancer Defender en tant que root
|
||||
- Créer plutot un service qui lancera Defender en tant qu'utilisateur non root
|
||||
- Un fichier PID sera crée.
|
||||
# Installation et utilisation
|
||||
Prérequis:
|
||||
- Python version >= 3.10
|
||||
- Pip de python installé sur la machine
|
||||
- Python librairies psutil & sqlalchemy & requests
|
||||
- IRC Serveur Version >= UnrealIRCd-6.1.2.2
|
||||
|
||||
# TO DO LIST
|
||||
Installation:
|
||||
|
||||
- Optimiser le systeme de réputation:
|
||||
- lorsque les users ce connectent, Ils entrent dans un salon puis une fraction de seconde le service les bans
|
||||
Cloner le dépôt:
|
||||
Bash
|
||||
git clone https://github.com/adator85/IRC_DEFENDER_MODULES.git
|
||||
Utilisez ce code avec précaution.
|
||||
|
||||
# VERSION 1
|
||||
Configuration (configuration.json):
|
||||
Le fichier configuration.json permet de personnaliser le service:
|
||||
Serveur IRC: Adresse du serveur IRC.
|
||||
Port: Port du serveur IRC.
|
||||
Canal: Canal auquel se connecter.
|
||||
Nom du Service: Nom d'utilisateur du bot sur le serveur.
|
||||
Mot de passe: Mot de passe du link (si nécessaire).
|
||||
Préfixes de commandes: Caractères utilisés pour déclencher les commandes.
|
||||
Et bien d'autres...
|
||||
|
||||
[02.01.2024]
|
||||
- Rajout de l'activation de la commande flood
|
||||
- Les deux variables RESTART et INIT ont été déplacées vers le module Irc
|
||||
- Nouvelle class Install:
|
||||
- Le programme va vérifier si les 3 librairies sont installées (SQLAlchemy & requests & psutil)
|
||||
- Une fois la vérification, il va mêtre a jour pip puis installera les dépendances
|
||||
Extension:
|
||||
Le code est modulaire et conçu pour être facilement étendu. Vous pouvez ajouter de nouvelles commandes, de nouvelles fonctionnalités (mods/mod_test.py est un exemple pour bien demarrer la création de son module).
|
||||
|
||||
[28.12.2023]
|
||||
- Changement de méthode pour récuperer la version actuelle de python
|
||||
- Ajout de la réponse a une PING de la part d'un utilisateur
|
||||
- Installation automatique des packages sqlalchemy, requests et psutil
|
||||
Contributions:
|
||||
Les contributions sont les bienvenues ! N'hésitez pas à ouvrir des issues ou des pull requests.
|
||||
|
||||
# BUG FIX
|
||||
|
||||
[29.12.2023]
|
||||
- Correction des messages de receptions trop longs > 4070 caractéres;
|
||||
- la méthode boucle et incrémente la réponse tant que le nombre de caractére reçu est supérieur a 4072
|
||||
- Rajout du protocol MTAGS a la connexion du service
|
||||
- Impact majeur dans la lecture des messages reçu du serveur ( PRIVMSG, SLOGS, UID, QUIT, NICK, PONG, SJOIN)
|
||||
|
||||
# ALREADY IMPLEMENTED
|
||||
|
||||
- Connexion en tant que service
|
||||
- Gestion des messages reçus/envoyés par le serveur
|
||||
- Gestion des caractéres spéciaux
|
||||
- Gestion des logs (salon, fichiers et console)
|
||||
- Mode debug : gestion des logs coté console
|
||||
- Création du systeme de gestion de commandes
|
||||
- Defender reconnait les commandes qui commence par le suffix définit dans la configuration
|
||||
- Defender reconnait aussi reconnaitre les commandes qui viennent de /msg Defender [commande]
|
||||
- Identifications
|
||||
- Systéme d'identification [OK]
|
||||
- Systéme de changement d'information [OK]
|
||||
- Suppression d'un admin
|
||||
- Systéme de groupe d'accés [OK]
|
||||
|
||||
Reputation security
|
||||
- Activation ou désaction du systéme --> OK | .reputation ON/off
|
||||
- Le user sera en mesure de changer la limite de la réputation --> OK | .reputation set limit 120
|
||||
- Defender devra envoyer l'utilisateur dans un salon définit dans la configuration --> OK
|
||||
- Defender bannira l'utilisateur de la totalité des salons, il le bannira aussi lorsqu'il souhaitera accéder a de nouveau salon --> OK
|
||||
- Defender devra envoyer un message du type "Merci de taper cette comande /msg {nomdudefender} code {un code générer aléatoirement} --> OK
|
||||
- Defender devra reconnaitre le code --> OK
|
||||
- Defender devra liberer l'utilisateur et l'envoyer vers un salon définit dans la configuration --> OK
|
||||
Avertissement:
|
||||
Ce bot est fourni "tel quel" sans aucune garantie. Utilisez-le à vos risques et périls.
|
||||
359
core/Model.py
Normal file
359
core/Model.py
Normal file
@@ -0,0 +1,359 @@
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from typing import Union
|
||||
from core.base import Base
|
||||
|
||||
class User:
|
||||
|
||||
@dataclass
|
||||
class UserModel:
|
||||
uid: str
|
||||
nickname: str
|
||||
username: str
|
||||
hostname: str
|
||||
umodes: str
|
||||
vhost: str
|
||||
isWebirc: bool
|
||||
remote_ip: str
|
||||
score_connexion: int
|
||||
connexion_datetime: datetime = field(default=datetime.now())
|
||||
|
||||
UID_DB: list[UserModel] = []
|
||||
|
||||
def __init__(self, Base: Base) -> None:
|
||||
self.log = Base.logs
|
||||
pass
|
||||
|
||||
def insert(self, newUser: UserModel) -> bool:
|
||||
"""Insert a new User object
|
||||
|
||||
Args:
|
||||
newUser (UserModel): New userModel object
|
||||
|
||||
Returns:
|
||||
bool: True if inserted
|
||||
"""
|
||||
result = False
|
||||
exist = False
|
||||
|
||||
for record in self.UID_DB:
|
||||
if record.uid == newUser.uid:
|
||||
exist = True
|
||||
self.log.debug(f'{record.uid} already exist')
|
||||
|
||||
if not exist:
|
||||
self.UID_DB.append(newUser)
|
||||
result = True
|
||||
self.log.debug(f'New User Created: ({newUser})')
|
||||
|
||||
if not result:
|
||||
self.log.critical(f'The User Object was not inserted {newUser}')
|
||||
|
||||
return result
|
||||
|
||||
def update(self, uid: str, newNickname: str) -> bool:
|
||||
"""Update the nickname starting from the UID
|
||||
|
||||
Args:
|
||||
uid (str): UID of the user
|
||||
newNickname (str): New nickname
|
||||
|
||||
Returns:
|
||||
bool: True if updated
|
||||
"""
|
||||
result = False
|
||||
|
||||
for record in self.UID_DB:
|
||||
if record.uid == uid:
|
||||
record.nickname = newNickname
|
||||
result = True
|
||||
self.log.debug(f'UID ({record.uid}) has been updated with new nickname {newNickname}')
|
||||
|
||||
if not result:
|
||||
self.log.critical(f'The new nickname {newNickname} was not updated, uid = {uid}')
|
||||
|
||||
return result
|
||||
|
||||
def delete(self, uid: str) -> bool:
|
||||
"""Delete the User starting from the UID
|
||||
|
||||
Args:
|
||||
uid (str): UID of the user
|
||||
|
||||
Returns:
|
||||
bool: True if deleted
|
||||
"""
|
||||
result = False
|
||||
|
||||
for record in self.UID_DB:
|
||||
if record.uid == uid:
|
||||
self.UID_DB.remove(record)
|
||||
result = True
|
||||
self.log.debug(f'UID ({record.uid}) has been deleted')
|
||||
|
||||
if not result:
|
||||
self.log.critical(f'The UID {uid} was not deleted')
|
||||
|
||||
return result
|
||||
|
||||
def get_User(self, uidornickname: str) -> Union[UserModel, None]:
|
||||
"""Get The User Object model
|
||||
|
||||
Args:
|
||||
uidornickname (str): UID or Nickname
|
||||
|
||||
Returns:
|
||||
UserModel|None: The UserModel Object | None
|
||||
"""
|
||||
User = None
|
||||
for record in self.UID_DB:
|
||||
if record.uid == uidornickname:
|
||||
User = record
|
||||
elif record.nickname == uidornickname:
|
||||
User = record
|
||||
|
||||
self.log.debug(f'Search {uidornickname} -- result = {User}')
|
||||
|
||||
return User
|
||||
|
||||
def get_uid(self, uidornickname:str) -> Union[str, None]:
|
||||
"""Get the UID of the user starting from the UID or the Nickname
|
||||
|
||||
Args:
|
||||
uidornickname (str): UID or Nickname
|
||||
|
||||
Returns:
|
||||
str|None: Return the UID
|
||||
"""
|
||||
uid = None
|
||||
for record in self.UID_DB:
|
||||
if record.uid == uidornickname:
|
||||
uid = record.uid
|
||||
if record.nickname == uidornickname:
|
||||
uid = record.uid
|
||||
|
||||
self.log.debug(f'The UID that you are looking for {uidornickname} has been found {uid}')
|
||||
return uid
|
||||
|
||||
def get_nickname(self, uidornickname:str) -> Union[str, None]:
|
||||
"""Get the Nickname starting from UID or the nickname
|
||||
|
||||
Args:
|
||||
uidornickname (str): UID or Nickname of the user
|
||||
|
||||
Returns:
|
||||
str|None: the nickname
|
||||
"""
|
||||
nickname = None
|
||||
for record in self.UID_DB:
|
||||
if record.nickname == uidornickname:
|
||||
nickname = record.nickname
|
||||
if record.uid == uidornickname:
|
||||
nickname = record.nickname
|
||||
self.log.debug(f'The value to check {uidornickname} -> {nickname}')
|
||||
return nickname
|
||||
|
||||
class Admin:
|
||||
|
||||
@dataclass
|
||||
class AdminModel:
|
||||
uid: str
|
||||
nickname: str
|
||||
username: str
|
||||
hostname: str
|
||||
umodes: str
|
||||
vhost: str
|
||||
level: int
|
||||
connexion_datetime: datetime = field(default=datetime.now())
|
||||
|
||||
UID_ADMIN_DB: list[AdminModel] = []
|
||||
|
||||
def __init__(self, Base: Base) -> None:
|
||||
self.log = Base.logs
|
||||
pass
|
||||
|
||||
def insert(self, newAdmin: AdminModel) -> bool:
|
||||
|
||||
result = False
|
||||
exist = False
|
||||
|
||||
for record in self.UID_ADMIN_DB:
|
||||
if record.uid == newAdmin.uid:
|
||||
exist = True
|
||||
self.log.debug(f'{record.uid} already exist')
|
||||
|
||||
if not exist:
|
||||
self.UID_ADMIN_DB.append(newAdmin)
|
||||
result = True
|
||||
self.log.debug(f'UID ({newAdmin.uid}) has been created')
|
||||
|
||||
if not result:
|
||||
self.log.critical(f'The User Object was not inserted {newAdmin}')
|
||||
|
||||
return result
|
||||
|
||||
def update(self, uid: str, newNickname: str) -> bool:
|
||||
|
||||
result = False
|
||||
|
||||
for record in self.UID_ADMIN_DB:
|
||||
if record.uid == uid:
|
||||
record.nickname = newNickname
|
||||
result = True
|
||||
self.log.debug(f'UID ({record.uid}) has been updated with new nickname {newNickname}')
|
||||
|
||||
if not result:
|
||||
self.log.critical(f'The new nickname {newNickname} was not updated, uid = {uid}')
|
||||
|
||||
return result
|
||||
|
||||
def delete(self, uid: str) -> bool:
|
||||
|
||||
result = False
|
||||
|
||||
for record in self.UID_ADMIN_DB:
|
||||
if record.uid == uid:
|
||||
self.UID_ADMIN_DB.remove(record)
|
||||
result = True
|
||||
self.log.debug(f'UID ({record.uid}) has been created')
|
||||
|
||||
if not result:
|
||||
self.log.critical(f'The UID {uid} was not deleted')
|
||||
|
||||
return result
|
||||
|
||||
def get_Admin(self, uidornickname: str) -> Union[AdminModel, None]:
|
||||
|
||||
Admin = None
|
||||
for record in self.UID_ADMIN_DB:
|
||||
if record.uid == uidornickname:
|
||||
Admin = record
|
||||
elif record.nickname == uidornickname:
|
||||
Admin = record
|
||||
|
||||
self.log.debug(f'Search {uidornickname} -- result = {Admin}')
|
||||
|
||||
return Admin
|
||||
|
||||
def get_uid(self, uidornickname:str) -> Union[str, None]:
|
||||
|
||||
uid = None
|
||||
for record in self.UID_ADMIN_DB:
|
||||
if record.uid == uidornickname:
|
||||
uid = record.uid
|
||||
if record.nickname == uidornickname:
|
||||
uid = record.uid
|
||||
|
||||
self.log.debug(f'The UID that you are looking for {uidornickname} has been found {uid}')
|
||||
return uid
|
||||
|
||||
def get_nickname(self, uidornickname:str) -> Union[str, None]:
|
||||
|
||||
nickname = None
|
||||
for record in self.UID_ADMIN_DB:
|
||||
if record.nickname == uidornickname:
|
||||
nickname = record.nickname
|
||||
if record.uid == uidornickname:
|
||||
nickname = record.nickname
|
||||
self.log.debug(f'The value {uidornickname} -- {nickname}')
|
||||
return nickname
|
||||
|
||||
class Channel:
|
||||
|
||||
@dataclass
|
||||
class ChannelModel:
|
||||
name: str
|
||||
uids: list
|
||||
|
||||
UID_CHANNEL_DB: list[ChannelModel] = []
|
||||
|
||||
def __init__(self, Base: Base) -> None:
|
||||
self.log = Base.logs
|
||||
self.Base = Base
|
||||
pass
|
||||
|
||||
def insert(self, newChan: ChannelModel) -> bool:
|
||||
|
||||
result = False
|
||||
exist = False
|
||||
|
||||
for record in self.UID_CHANNEL_DB:
|
||||
if record.name == newChan.name:
|
||||
exist = True
|
||||
self.log.debug(f'{record.name} already exist')
|
||||
|
||||
for user in newChan.uids:
|
||||
record.uids.append(user)
|
||||
|
||||
# Supprimer les doublons
|
||||
del_duplicates = list(set(record.uids))
|
||||
record.uids = del_duplicates
|
||||
self.log.debug(f'Updating a new UID to the channel {record}')
|
||||
|
||||
|
||||
if not exist:
|
||||
self.UID_CHANNEL_DB.append(newChan)
|
||||
result = True
|
||||
self.log.debug(f'New Channel Created: ({newChan})')
|
||||
|
||||
if not result:
|
||||
self.log.critical(f'The Channel Object was not inserted {newChan}')
|
||||
|
||||
return result
|
||||
|
||||
def delete(self, name: str) -> bool:
|
||||
|
||||
result = False
|
||||
|
||||
for record in self.UID_CHANNEL_DB:
|
||||
if record.name == name:
|
||||
self.UID_CHANNEL_DB.remove(record)
|
||||
result = True
|
||||
self.log.debug(f'Channel ({record.name}) has been created')
|
||||
|
||||
if not result:
|
||||
self.log.critical(f'The Channel {name} was not deleted')
|
||||
|
||||
return result
|
||||
|
||||
def delete_user_from_channel(self, chan_name: str, uid:str) -> bool:
|
||||
try:
|
||||
result = False
|
||||
|
||||
for record in self.UID_CHANNEL_DB:
|
||||
if record.name == chan_name:
|
||||
for user_id in record.uids:
|
||||
if self.Base.clean_uid(user_id) == uid:
|
||||
record.uids.remove(user_id)
|
||||
self.log.debug(f'uid {uid} has been removed, here is the new object: {record}')
|
||||
result = True
|
||||
|
||||
for record in self.UID_CHANNEL_DB:
|
||||
if not record.uids:
|
||||
self.UID_CHANNEL_DB.remove(record)
|
||||
self.log.debug(f'Channel {record.name} has been removed, here is the new object: {record}')
|
||||
|
||||
return result
|
||||
except ValueError as ve:
|
||||
self.log.error(f'{ve}')
|
||||
|
||||
def get_Channel(self, name: str) -> Union[ChannelModel, None]:
|
||||
|
||||
Channel = None
|
||||
for record in self.UID_CHANNEL_DB:
|
||||
if record.name == name:
|
||||
Channel = record
|
||||
|
||||
self.log.debug(f'Search {name} -- result = {Channel}')
|
||||
|
||||
return Channel
|
||||
|
||||
def get_mode(self, name:str) -> Union[str, None]:
|
||||
|
||||
mode = None
|
||||
for record in self.UID_CHANNEL_DB:
|
||||
if record.name == name:
|
||||
mode = record.mode
|
||||
|
||||
self.log.debug(f'The mode of the channel {name} has been found: {mode}')
|
||||
return mode
|
||||
284
core/base.py
284
core/base.py
@@ -1,24 +1,22 @@
|
||||
import time, threading, os, random, socket, hashlib
|
||||
import time, threading, os, random, socket, hashlib, ipaddress, logging, requests, json, re
|
||||
from typing import Union
|
||||
from base64 import b64decode
|
||||
from datetime import datetime
|
||||
from sqlalchemy import create_engine, Engine, Connection, CursorResult
|
||||
from sqlalchemy.sql import text
|
||||
from core.configuration import Config
|
||||
from core.loadConf import ConfigDataModel
|
||||
|
||||
class Base:
|
||||
|
||||
CORE_DB_PATH = 'core' + os.sep + 'db' + os.sep # Le dossier bases de données core
|
||||
MODS_DB_PATH = 'mods' + os.sep + 'db' + os.sep # Le dossier bases de données des modules
|
||||
PYTHON_MIN_VERSION = '3.10' # Version min de python
|
||||
DB_SCHEMA:list[str] = {
|
||||
'admins': 'sys_admins',
|
||||
'commandes': 'sys_commandes',
|
||||
'logs': 'sys_logs',
|
||||
'modules': 'sys_modules'
|
||||
}
|
||||
|
||||
def __init__(self, Config: Config) -> None:
|
||||
def __init__(self, Config: ConfigDataModel) -> None:
|
||||
|
||||
self.Config = Config # Assigner l'objet de configuration
|
||||
self.init_log_system() # Demarrer le systeme de log
|
||||
self.check_for_new_version(True) # Verifier si une nouvelle version est disponible
|
||||
|
||||
self.running_timers:list[threading.Timer] = [] # Liste des timers en cours
|
||||
self.running_threads:list[threading.Thread] = [] # Liste des threads en cours
|
||||
@@ -32,7 +30,82 @@ class Base:
|
||||
|
||||
self.db_create_first_admin() # Créer un nouvel admin si la base de données est vide
|
||||
|
||||
def get_unixtime(self)->int:
|
||||
def __set_current_defender_version(self) -> None:
|
||||
"""This will put the current version of Defender
|
||||
located in version.json
|
||||
"""
|
||||
|
||||
version_filename = f'.{os.sep}version.json'
|
||||
with open(version_filename, 'r') as version_data:
|
||||
current_version:dict[str, str] = json.load(version_data)
|
||||
|
||||
# self.DEFENDER_VERSION = current_version["version"]
|
||||
self.Config.current_version = current_version['version']
|
||||
|
||||
return None
|
||||
|
||||
def __get_latest_defender_version(self) -> None:
|
||||
try:
|
||||
self.logs.debug(f'Looking for a new version available on Github')
|
||||
print(f'===> Looking for a new version available on Github')
|
||||
token = ''
|
||||
json_url = f'https://raw.githubusercontent.com/adator85/IRC_DEFENDER_MODULES/main/version.json'
|
||||
headers = {
|
||||
'Authorization': f'token {token}',
|
||||
'Accept': 'application/vnd.github.v3.raw' # Indique à GitHub que nous voulons le contenu brut du fichier
|
||||
}
|
||||
|
||||
if token == '':
|
||||
response = requests.get(json_url, timeout=self.Config.API_TIMEOUT)
|
||||
else:
|
||||
response = requests.get(json_url, headers=headers, timeout=self.Config.API_TIMEOUT)
|
||||
|
||||
response.raise_for_status() # Vérifie si la requête a réussi
|
||||
json_response:dict = response.json()
|
||||
# self.LATEST_DEFENDER_VERSION = json_response["version"]
|
||||
self.Config.latest_version = json_response['version']
|
||||
|
||||
return None
|
||||
except requests.HTTPError as err:
|
||||
self.logs.error(f'Github not available to fetch latest version: {err}')
|
||||
except:
|
||||
self.logs.warning(f'Github not available to fetch latest version')
|
||||
|
||||
def check_for_new_version(self, online:bool) -> bool:
|
||||
try:
|
||||
self.logs.debug(f'Checking for a new service version')
|
||||
|
||||
# Assigner la version actuelle de Defender
|
||||
self.__set_current_defender_version()
|
||||
# Récuperer la dernier version disponible dans github
|
||||
if online:
|
||||
self.logs.debug(f'Retrieve the latest version from Github')
|
||||
self.__get_latest_defender_version()
|
||||
|
||||
isNewVersion = False
|
||||
latest_version = self.Config.latest_version
|
||||
current_version = self.Config.current_version
|
||||
|
||||
curr_major , curr_minor, curr_patch = current_version.split('.')
|
||||
last_major, last_minor, last_patch = latest_version.split('.')
|
||||
|
||||
if int(last_major) > int(curr_major):
|
||||
self.logs.info(f'New version available: {current_version} >>> {latest_version}')
|
||||
isNewVersion = True
|
||||
elif int(last_major) == int(curr_major) and int(last_minor) > int(curr_minor):
|
||||
self.logs.info(f'New version available: {current_version} >>> {latest_version}')
|
||||
isNewVersion = True
|
||||
elif int(last_major) == int(curr_major) and int(last_minor) == int(curr_minor) and int(last_patch) > int(curr_patch):
|
||||
self.logs.info(f'New version available: {current_version} >>> {latest_version}')
|
||||
isNewVersion = True
|
||||
else:
|
||||
isNewVersion = False
|
||||
|
||||
return isNewVersion
|
||||
except ValueError as ve:
|
||||
self.logs.error(f'Impossible to convert in version number : {ve}')
|
||||
|
||||
def get_unixtime(self) -> int:
|
||||
"""
|
||||
Cette fonction retourne un UNIXTIME de type 12365456
|
||||
Return: Current time in seconds since the Epoch (int)
|
||||
@@ -40,13 +113,23 @@ class Base:
|
||||
unixtime = int( time.time() )
|
||||
return unixtime
|
||||
|
||||
def get_datetime(self)->str:
|
||||
def get_datetime(self) -> str:
|
||||
"""
|
||||
Retourne une date au format string (24-12-2023 20:50:59)
|
||||
"""
|
||||
currentdate = datetime.now().strftime('%d-%m-%Y %H:%M:%S')
|
||||
return currentdate
|
||||
|
||||
def get_all_modules(self) -> list:
|
||||
|
||||
all_files = os.listdir('mods/')
|
||||
all_modules: list = []
|
||||
for module in all_files:
|
||||
if module.endswith('.py') and not module == '__init__.py':
|
||||
all_modules.append(module.replace('.py', '').lower())
|
||||
|
||||
return all_modules
|
||||
|
||||
def create_log(self, log_message: str) -> None:
|
||||
"""Enregiste les logs
|
||||
|
||||
@@ -56,19 +139,43 @@ class Base:
|
||||
Returns:
|
||||
None: Aucun retour
|
||||
"""
|
||||
sql_insert = f"INSERT INTO {self.DB_SCHEMA['logs']} (datetime, server_msg) VALUES (:datetime, :server_msg)"
|
||||
sql_insert = f"INSERT INTO {self.Config.table_log} (datetime, server_msg) VALUES (:datetime, :server_msg)"
|
||||
mes_donnees = {'datetime': str(self.get_datetime()),'server_msg': f'{log_message}'}
|
||||
self.db_execute_query(sql_insert, mes_donnees)
|
||||
|
||||
return None
|
||||
|
||||
def init_log_system(self) -> None:
|
||||
# Create folder if not available
|
||||
logs_directory = f'logs{os.sep}'
|
||||
if not os.path.exists(f'{logs_directory}'):
|
||||
os.makedirs(logs_directory)
|
||||
|
||||
# Init logs object
|
||||
self.logs = logging
|
||||
self.logs.basicConfig(level=self.Config.DEBUG_LEVEL,
|
||||
filename='logs/defender.log',
|
||||
encoding='UTF-8',
|
||||
format='%(asctime)s - %(levelname)s - %(filename)s - %(lineno)d - %(funcName)s - %(message)s')
|
||||
|
||||
self.logs.info('#################### STARTING INTERCEPTOR HQ ####################')
|
||||
|
||||
return None
|
||||
|
||||
def log_cmd(self, user_cmd:str, cmd:str) -> None:
|
||||
"""Enregistre les commandes envoyées par les utilisateurs
|
||||
|
||||
Args:
|
||||
cmd (str): la commande a enregistrer
|
||||
"""
|
||||
insert_cmd_query = f"INSERT INTO {self.DB_SCHEMA['commandes']} (datetime, user, commande) VALUES (:datetime, :user, :commande)"
|
||||
cmd_list = cmd.split()
|
||||
if len(cmd_list) == 3:
|
||||
if cmd_list[0].replace('.', '') == 'auth':
|
||||
cmd_list[1] = '*******'
|
||||
cmd_list[2] = '*******'
|
||||
cmd = ' '.join(cmd_list)
|
||||
|
||||
insert_cmd_query = f"INSERT INTO {self.Config.table_commande} (datetime, user, commande) VALUES (:datetime, :user, :commande)"
|
||||
mes_donnees = {'datetime': self.get_datetime(), 'user': user_cmd, 'commande': cmd}
|
||||
self.db_execute_query(insert_cmd_query, mes_donnees)
|
||||
|
||||
@@ -83,7 +190,7 @@ class Base:
|
||||
Returns:
|
||||
bool: True si le module existe déja dans la base de données sinon False
|
||||
"""
|
||||
query = f"SELECT id FROM {self.DB_SCHEMA['modules']} WHERE module = :module"
|
||||
query = f"SELECT id FROM {self.Config.table_module} WHERE module = :module"
|
||||
mes_donnes = {'module': module_name}
|
||||
results = self.db_execute_query(query, mes_donnes)
|
||||
|
||||
@@ -100,13 +207,13 @@ class Base:
|
||||
"""
|
||||
|
||||
if not self.db_isModuleExist(module_name):
|
||||
self.__debug(f"Le module {module_name} n'existe pas alors ont le créer")
|
||||
insert_cmd_query = f"INSERT INTO {self.DB_SCHEMA['modules']} (datetime, user, module) VALUES (:datetime, :user, :module)"
|
||||
self.logs.debug(f"Le module {module_name} n'existe pas alors ont le créer")
|
||||
insert_cmd_query = f"INSERT INTO {self.Config.table_module} (datetime, user, module) VALUES (:datetime, :user, :module)"
|
||||
mes_donnees = {'datetime': self.get_datetime(), 'user': user_cmd, 'module': module_name}
|
||||
self.db_execute_query(insert_cmd_query, mes_donnees)
|
||||
# self.db_close_session(self.session)
|
||||
else:
|
||||
self.__debug(f"Le module {module_name} existe déja dans la base de données")
|
||||
self.logs.debug(f"Le module {module_name} existe déja dans la base de données")
|
||||
|
||||
return False
|
||||
|
||||
@@ -116,7 +223,7 @@ class Base:
|
||||
Args:
|
||||
cmd (str): le module a enregistrer
|
||||
"""
|
||||
insert_cmd_query = f"DELETE FROM {self.DB_SCHEMA['modules']} WHERE module = :module"
|
||||
insert_cmd_query = f"DELETE FROM {self.Config.table_module} WHERE module = :module"
|
||||
mes_donnees = {'module': module_name}
|
||||
self.db_execute_query(insert_cmd_query, mes_donnees)
|
||||
|
||||
@@ -124,20 +231,26 @@ class Base:
|
||||
|
||||
def db_create_first_admin(self) -> None:
|
||||
|
||||
user = self.db_execute_query(f"SELECT id FROM {self.DB_SCHEMA['admins']}")
|
||||
user = self.db_execute_query(f"SELECT id FROM {self.Config.table_admin}")
|
||||
if not user.fetchall():
|
||||
admin = self.Config.OWNER
|
||||
password = self.crypt_password(self.Config.PASSWORD)
|
||||
|
||||
mes_donnees = {'createdOn': self.get_datetime(), 'user': admin, 'password': password, 'hostname': '*', 'vhost': '*', 'level': 5}
|
||||
mes_donnees = {'createdOn': self.get_datetime(),
|
||||
'user': admin,
|
||||
'password': password,
|
||||
'hostname': '*',
|
||||
'vhost': '*',
|
||||
'level': 5
|
||||
}
|
||||
self.db_execute_query(f"""
|
||||
INSERT INTO {self.DB_SCHEMA['admins']}
|
||||
INSERT INTO {self.Config.table_admin}
|
||||
(createdOn, user, password, hostname, vhost, level)
|
||||
VALUES
|
||||
(:createdOn, :user, :password, :hostname, :vhost, :level)"""
|
||||
, mes_donnees)
|
||||
|
||||
pass
|
||||
return None
|
||||
|
||||
def create_timer(self, time_to_wait: float, func: object, func_args: tuple = ()) -> None:
|
||||
|
||||
@@ -148,14 +261,20 @@ class Base:
|
||||
|
||||
self.running_timers.append(t)
|
||||
|
||||
self.__debug(f"Timer ID : {str(t.ident)} | Running Threads : {len(threading.enumerate())}")
|
||||
self.logs.debug(f"Timer ID : {str(t.ident)} | Running Threads : {len(threading.enumerate())}")
|
||||
|
||||
except AssertionError as ae:
|
||||
self.__debug(f'Assertion Error -> {ae}')
|
||||
self.logs.error(f'Assertion Error -> {ae}')
|
||||
|
||||
def create_thread(self, func:object, func_args: tuple = ()) -> None:
|
||||
def create_thread(self, func:object, func_args: tuple = (), run_once:bool = False) -> None:
|
||||
try:
|
||||
func_name = func.__name__
|
||||
|
||||
if run_once:
|
||||
for thread in self.running_threads:
|
||||
if thread.getName() == func_name:
|
||||
return None
|
||||
|
||||
# if func_name in self.running_threads:
|
||||
# print(f"HeartBeat is running")
|
||||
# return None
|
||||
@@ -164,110 +283,107 @@ class Base:
|
||||
th.start()
|
||||
|
||||
self.running_threads.append(th)
|
||||
self.__debug(f"Thread ID : {str(th.ident)} | Thread name : {th.getName()} | Running Threads : {len(threading.enumerate())}")
|
||||
self.logs.debug(f"Thread ID : {str(th.ident)} | Thread name : {th.getName()} | Running Threads : {len(threading.enumerate())}")
|
||||
|
||||
except AssertionError as ae:
|
||||
self.__debug(f'Assertion Error -> {ae}')
|
||||
self.logs.error(f'{ae}')
|
||||
|
||||
def garbage_collector_timer(self) -> None:
|
||||
"""Methode qui supprime les timers qui ont finis leurs job
|
||||
"""
|
||||
try:
|
||||
self.__debug(f"=======> Checking for Timers to stop")
|
||||
# print(f"{self.running_timers}")
|
||||
|
||||
for timer in self.running_timers:
|
||||
if not timer.is_alive():
|
||||
timer.cancel()
|
||||
self.running_timers.remove(timer)
|
||||
self.__debug(f"Timer {str(timer)} removed")
|
||||
self.logs.info(f"Timer {str(timer)} removed")
|
||||
else:
|
||||
self.__debug(f"===> Timer {str(timer)} Still running ...")
|
||||
self.logs.debug(f"===> Timer {str(timer)} Still running ...")
|
||||
|
||||
except AssertionError as ae:
|
||||
print(f'Assertion Error -> {ae}')
|
||||
self.logs.error(f'Assertion Error -> {ae}')
|
||||
|
||||
def garbage_collector_thread(self) -> None:
|
||||
"""Methode qui supprime les threads qui ont finis leurs job
|
||||
"""
|
||||
try:
|
||||
self.__debug(f"=======> Checking for Threads to stop")
|
||||
for thread in self.running_threads:
|
||||
if thread.getName() != 'heartbeat':
|
||||
if not thread.is_alive():
|
||||
self.running_threads.remove(thread)
|
||||
self.__debug(f"Thread {str(thread.getName())} {str(thread.native_id)} removed")
|
||||
self.logs.debug(f"Thread {str(thread.getName())} {str(thread.native_id)} removed")
|
||||
|
||||
# print(threading.enumerate())
|
||||
except AssertionError as ae:
|
||||
self.__debug(f'Assertion Error -> {ae}')
|
||||
self.logs.error(f'Assertion Error -> {ae}')
|
||||
|
||||
def garbage_collector_sockets(self) -> None:
|
||||
|
||||
self.__debug(f"=======> Checking for Sockets to stop")
|
||||
for soc in self.running_sockets:
|
||||
while soc.fileno() != -1:
|
||||
self.__debug(soc.fileno())
|
||||
self.logs.debug(soc.fileno())
|
||||
soc.close()
|
||||
|
||||
soc.close()
|
||||
self.running_sockets.remove(soc)
|
||||
self.__debug(f"Socket ==> closed {str(soc.fileno())}")
|
||||
self.logs.debug(f"Socket ==> closed {str(soc.fileno())}")
|
||||
|
||||
def shutdown(self) -> None:
|
||||
"""Methode qui va préparer l'arrêt complêt du service
|
||||
"""
|
||||
# Nettoyage des timers
|
||||
print(f"=======> Checking for Timers to stop")
|
||||
self.logs.debug(f"=======> Checking for Timers to stop")
|
||||
for timer in self.running_timers:
|
||||
while timer.is_alive():
|
||||
print(f"> waiting for {timer.getName()} to close")
|
||||
self.logs.debug(f"> waiting for {timer.getName()} to close")
|
||||
timer.cancel()
|
||||
time.sleep(0.2)
|
||||
self.running_timers.remove(timer)
|
||||
print(f"> Cancelling {timer.getName()} {timer.native_id}")
|
||||
self.logs.debug(f"> Cancelling {timer.getName()} {timer.native_id}")
|
||||
|
||||
print(f"=======> Checking for Threads to stop")
|
||||
self.logs.debug(f"=======> Checking for Threads to stop")
|
||||
for thread in self.running_threads:
|
||||
if thread.getName() == 'heartbeat' and thread.is_alive():
|
||||
self.execute_periodic_action()
|
||||
print(f"> Running the last periodic action")
|
||||
self.logs.debug(f"> Running the last periodic action")
|
||||
self.running_threads.remove(thread)
|
||||
print(f"> Cancelling {thread.getName()} {thread.native_id}")
|
||||
self.logs.debug(f"> Cancelling {thread.getName()} {thread.native_id}")
|
||||
|
||||
print(f"=======> Checking for Sockets to stop")
|
||||
self.logs.debug(f"=======> Checking for Sockets to stop")
|
||||
for soc in self.running_sockets:
|
||||
soc.close()
|
||||
while soc.fileno() != -1:
|
||||
soc.close()
|
||||
|
||||
self.running_sockets.remove(soc)
|
||||
print(f"> Socket ==> closed {str(soc.fileno())}")
|
||||
self.logs.debug(f"> Socket ==> closed {str(soc.fileno())}")
|
||||
|
||||
pass
|
||||
return None
|
||||
|
||||
def db_init(self) -> tuple[Engine, Connection]:
|
||||
|
||||
db_directory = self.Config.DEFENDER_DB_PATH
|
||||
full_path_db = self.Config.DEFENDER_DB_PATH + self.Config.DEFENDER_DB_NAME
|
||||
db_directory = self.Config.db_path
|
||||
full_path_db = self.Config.db_path + self.Config.db_name
|
||||
|
||||
if not os.path.exists(db_directory):
|
||||
os.makedirs(db_directory)
|
||||
|
||||
engine = create_engine(f'sqlite:///{full_path_db}.db', echo=False)
|
||||
cursor = engine.connect()
|
||||
|
||||
self.logs.info("database connexion has been initiated")
|
||||
return engine, cursor
|
||||
|
||||
def __create_db(self) -> None:
|
||||
|
||||
table_logs = f'''CREATE TABLE IF NOT EXISTS {self.DB_SCHEMA['logs']} (
|
||||
table_logs = f'''CREATE TABLE IF NOT EXISTS {self.Config.table_log} (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
datetime TEXT,
|
||||
server_msg TEXT
|
||||
)
|
||||
'''
|
||||
|
||||
table_cmds = f'''CREATE TABLE IF NOT EXISTS {self.DB_SCHEMA['commandes']} (
|
||||
table_cmds = f'''CREATE TABLE IF NOT EXISTS {self.Config.table_commande} (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
datetime TEXT,
|
||||
user TEXT,
|
||||
@@ -275,7 +391,7 @@ class Base:
|
||||
)
|
||||
'''
|
||||
|
||||
table_modules = f'''CREATE TABLE IF NOT EXISTS {self.DB_SCHEMA['modules']} (
|
||||
table_modules = f'''CREATE TABLE IF NOT EXISTS {self.Config.table_module} (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
datetime TEXT,
|
||||
user TEXT,
|
||||
@@ -283,7 +399,7 @@ class Base:
|
||||
)
|
||||
'''
|
||||
|
||||
table_admins = f'''CREATE TABLE IF NOT EXISTS {self.DB_SCHEMA['admins']} (
|
||||
table_admins = f'''CREATE TABLE IF NOT EXISTS {self.Config.table_admin} (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
createdOn TEXT,
|
||||
user TEXT,
|
||||
@@ -319,7 +435,7 @@ class Base:
|
||||
try:
|
||||
self.cursor.close()
|
||||
except AttributeError as ae:
|
||||
self.__debug(f"Attribute Error : {ae}")
|
||||
self.logs.error(f"Attribute Error : {ae}")
|
||||
|
||||
def crypt_password(self, password:str) -> str:
|
||||
"""Retourne un mot de passe chiffré en MD5
|
||||
@@ -352,6 +468,28 @@ class Base:
|
||||
except TypeError:
|
||||
return value
|
||||
|
||||
def is_valid_ip(self, ip_to_control:str) -> bool:
|
||||
|
||||
try:
|
||||
if ip_to_control in self.Config.WHITELISTED_IP:
|
||||
return False
|
||||
|
||||
ipaddress.ip_address(ip_to_control)
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
def decode_ip(self, ip_b64encoded: str) -> Union[str, None]:
|
||||
|
||||
binary_ip = b64decode(ip_b64encoded)
|
||||
try:
|
||||
decoded_ip = ipaddress.ip_address(binary_ip)
|
||||
|
||||
return decoded_ip.exploded
|
||||
except ValueError as ve:
|
||||
self.logs.critical(f'This remote ip is not valid : {ve}')
|
||||
return None
|
||||
|
||||
def get_random(self, lenght:int) -> str:
|
||||
"""
|
||||
Retourn une chaîne aléatoire en fonction de la longueur spécifiée.
|
||||
@@ -367,7 +505,7 @@ class Base:
|
||||
# Run Garbage Collector Timer
|
||||
self.garbage_collector_timer()
|
||||
self.garbage_collector_thread()
|
||||
self.garbage_collector_sockets()
|
||||
# self.garbage_collector_sockets()
|
||||
return None
|
||||
|
||||
for key, value in self.periodic_func.items():
|
||||
@@ -380,9 +518,35 @@ class Base:
|
||||
# Vider le dictionnaire de fonction
|
||||
self.periodic_func.clear()
|
||||
|
||||
def __debug(self, debug_msg:str) -> None:
|
||||
def clean_uid(self, uid:str) -> str:
|
||||
"""Clean UID by removing @ / % / + / Owner / and *
|
||||
|
||||
if self.Config.DEBUG == 1:
|
||||
print(f"[{self.get_datetime()}] - {debug_msg}")
|
||||
Args:
|
||||
uid (str): The UID to clean
|
||||
|
||||
return None
|
||||
Returns:
|
||||
str: Clean UID without any sign
|
||||
"""
|
||||
|
||||
pattern = fr'[@|%|\+|~|\*]*'
|
||||
parsed_UID = re.sub(pattern, '', uid)
|
||||
|
||||
return parsed_UID
|
||||
|
||||
def Is_Channel(self, channelToCheck: str) -> bool:
|
||||
"""Check if the string has the # caractere and return True if this is a channel
|
||||
|
||||
Args:
|
||||
channelToCheck (str): The string to test if it is a channel or not
|
||||
|
||||
Returns:
|
||||
bool: True if the string is a channel / False if this is not a channel
|
||||
"""
|
||||
|
||||
pattern = fr'^#'
|
||||
isChannel = re.findall(pattern, channelToCheck)
|
||||
|
||||
if not isChannel:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
274
core/dataClass.py
Normal file
274
core/dataClass.py
Normal file
@@ -0,0 +1,274 @@
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from typing import Union
|
||||
|
||||
class User:
|
||||
|
||||
@dataclass
|
||||
class UserDataClass:
|
||||
uid: str
|
||||
nickname: str
|
||||
username: str
|
||||
hostname: str
|
||||
umodes: str
|
||||
vhost: str
|
||||
isWebirc: bool
|
||||
connexion_datetime: datetime = field(default=datetime.now())
|
||||
|
||||
UID_DB:list[UserDataClass] = []
|
||||
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
def insert(self, user: UserDataClass) -> bool:
|
||||
"""Insert new user
|
||||
|
||||
Args:
|
||||
user (UserDataClass): The User dataclass
|
||||
|
||||
Returns:
|
||||
bool: True if the record has been created
|
||||
"""
|
||||
exists = False
|
||||
inserted = False
|
||||
|
||||
for record in self.UID_DB:
|
||||
if record.uid == user.uid:
|
||||
exists = True
|
||||
print(f'{user.uid} already exist')
|
||||
|
||||
if not exists:
|
||||
self.UID_DB.append(user)
|
||||
print(f'New record with uid: {user.uid}')
|
||||
inserted = True
|
||||
|
||||
return inserted
|
||||
|
||||
def update(self, uid: str, newnickname: str) -> bool:
|
||||
"""Updating a single record with a new nickname
|
||||
|
||||
Args:
|
||||
uid (str): the uid of the user
|
||||
newnickname (str): the new nickname
|
||||
|
||||
Returns:
|
||||
bool: True if the record has been updated
|
||||
"""
|
||||
status = False
|
||||
for user in self.UID_DB:
|
||||
if user.uid == uid:
|
||||
user.nickname = newnickname
|
||||
status = True
|
||||
print(f'Updating record with uid: {uid}')
|
||||
|
||||
return status
|
||||
|
||||
def delete(self, uid: str) -> bool:
|
||||
"""Delete a user based on his uid
|
||||
|
||||
Args:
|
||||
uid (str): The UID of the user
|
||||
|
||||
Returns:
|
||||
bool: True if the record has been deleted
|
||||
"""
|
||||
status = False
|
||||
for user in self.UID_DB:
|
||||
if user.uid == uid:
|
||||
self.UID_DB.remove(user)
|
||||
status = True
|
||||
print(f'Removing record with uid: {uid}')
|
||||
|
||||
return status
|
||||
|
||||
def isexist(self, uidornickname:str) -> bool:
|
||||
"""do the UID or Nickname exist ?
|
||||
|
||||
Args:
|
||||
uidornickname (str): The UID or the Nickname
|
||||
|
||||
Returns:
|
||||
bool: True if exist or False if don't exist
|
||||
"""
|
||||
result = False
|
||||
for record in self.UID_DB:
|
||||
if record.uid == uidornickname:
|
||||
result = True
|
||||
if record.nickname == uidornickname:
|
||||
result = True
|
||||
|
||||
return result
|
||||
|
||||
def get_User(self, uidornickname) -> Union[UserDataClass, None]:
|
||||
|
||||
UserObject = None
|
||||
for record in self.UID_DB:
|
||||
if record.uid == uidornickname:
|
||||
UserObject = record
|
||||
elif record.nickname == uidornickname:
|
||||
UserObject = record
|
||||
|
||||
return UserObject
|
||||
|
||||
def get_uid(self, uidornickname:str) -> Union[str, None]:
|
||||
|
||||
uid = None
|
||||
for record in self.UID_DB:
|
||||
if record.uid == uidornickname:
|
||||
uid = record.uid
|
||||
if record.nickname == uidornickname:
|
||||
uid = record.uid
|
||||
|
||||
return uid
|
||||
|
||||
def get_nickname(self, uidornickname:str) -> Union[str, None]:
|
||||
|
||||
nickname = None
|
||||
for record in self.UID_DB:
|
||||
if record.nickname == uidornickname:
|
||||
nickname = record.nickname
|
||||
if record.uid == uidornickname:
|
||||
nickname = record.nickname
|
||||
|
||||
return nickname
|
||||
|
||||
class Admin:
|
||||
@dataclass
|
||||
class AdminDataClass:
|
||||
uid: str
|
||||
nickname: str
|
||||
username: str
|
||||
hostname: str
|
||||
umodes: str
|
||||
vhost: str
|
||||
level: int
|
||||
connexion_datetime: datetime = field(default=datetime.now())
|
||||
|
||||
UID_ADMIN_DB:list[AdminDataClass] = []
|
||||
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
def insert(self, admin: AdminDataClass) -> bool:
|
||||
"""Insert new user
|
||||
|
||||
Args:
|
||||
user (UserDataClass): The User dataclass
|
||||
|
||||
Returns:
|
||||
bool: True if the record has been created
|
||||
"""
|
||||
exists = False
|
||||
inserted = False
|
||||
|
||||
for record in self.UID_ADMIN_DB:
|
||||
if record.uid == admin.uid:
|
||||
exists = True
|
||||
print(f'{admin.uid} already exist')
|
||||
|
||||
if not exists:
|
||||
self.UID_ADMIN_DB.append(admin)
|
||||
print(f'New record with uid: {admin.uid}')
|
||||
inserted = True
|
||||
|
||||
return inserted
|
||||
|
||||
def update(self, uid: str, newnickname: str) -> bool:
|
||||
"""Updating a single record with a new nickname
|
||||
|
||||
Args:
|
||||
uid (str): the uid of the user
|
||||
newnickname (str): the new nickname
|
||||
|
||||
Returns:
|
||||
bool: True if the record has been updated
|
||||
"""
|
||||
status = False
|
||||
for admin in self.UID_ADMIN_DB:
|
||||
if admin.uid == uid:
|
||||
admin.nickname = newnickname
|
||||
status = True
|
||||
print(f'Updating record with uid: {uid}')
|
||||
|
||||
return status
|
||||
|
||||
def delete(self, uid: str) -> bool:
|
||||
"""Delete a user based on his uid
|
||||
|
||||
Args:
|
||||
uid (str): The UID of the user
|
||||
|
||||
Returns:
|
||||
bool: True if the record has been deleted
|
||||
"""
|
||||
status = False
|
||||
for admin in self.UID_ADMIN_DB:
|
||||
if admin.uid == uid:
|
||||
self.UID_ADMIN_DB.remove(admin)
|
||||
status = True
|
||||
print(f'Removing record with uid: {uid}')
|
||||
|
||||
return status
|
||||
|
||||
def isexist(self, uidornickname:str) -> bool:
|
||||
"""do the UID or Nickname exist ?
|
||||
|
||||
Args:
|
||||
uidornickname (str): The UID or the Nickname
|
||||
|
||||
Returns:
|
||||
bool: True if exist or False if don't exist
|
||||
"""
|
||||
result = False
|
||||
for record in self.UID_ADMIN_DB:
|
||||
if record.uid == uidornickname:
|
||||
result = True
|
||||
if record.nickname == uidornickname:
|
||||
result = True
|
||||
|
||||
return result
|
||||
|
||||
def get_Admin(self, uidornickname) -> Union[AdminDataClass, None]:
|
||||
|
||||
AdminObject = None
|
||||
for record in self.UID_ADMIN_DB:
|
||||
if record.uid == uidornickname:
|
||||
AdminObject = record
|
||||
elif record.nickname == uidornickname:
|
||||
AdminObject = record
|
||||
|
||||
return AdminObject
|
||||
|
||||
def get_uid(self, uidornickname:str) -> Union[str, None]:
|
||||
|
||||
uid = None
|
||||
for record in self.UID_ADMIN_DB:
|
||||
if record.uid == uidornickname:
|
||||
uid = record.uid
|
||||
if record.nickname == uidornickname:
|
||||
uid = record.uid
|
||||
|
||||
return uid
|
||||
|
||||
def get_nickname(self, uidornickname:str) -> Union[str, None]:
|
||||
|
||||
nickname = None
|
||||
for record in self.UID_ADMIN_DB:
|
||||
if record.nickname == uidornickname:
|
||||
nickname = record.nickname
|
||||
if record.uid == uidornickname:
|
||||
nickname = record.nickname
|
||||
|
||||
return nickname
|
||||
|
||||
def get_level(self, uidornickname:str) -> int:
|
||||
|
||||
level = 0
|
||||
for record in self.UID_ADMIN_DB:
|
||||
if record.uid == uidornickname:
|
||||
level = record.level
|
||||
if record.nickname == uidornickname:
|
||||
level = record.level
|
||||
|
||||
return level
|
||||
|
||||
48
core/exemple_configuration.json
Normal file
48
core/exemple_configuration.json
Normal file
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"SERVEUR_IP": "0.0.0.0",
|
||||
"SERVEUR_HOSTNAME": "your.host.name",
|
||||
"SERVEUR_LINK": "your.link.to.server",
|
||||
"SERVEUR_PORT": 7002,
|
||||
"SERVEUR_PASSWORD": "link_password",
|
||||
"SERVEUR_ID": "006",
|
||||
"SERVEUR_SSL": true,
|
||||
|
||||
"SERVICE_NAME": "defender",
|
||||
"SERVICE_NICKNAME": "BotNickname",
|
||||
"SERVICE_REALNAME": "BotRealname",
|
||||
"SERVICE_USERNAME": "BotUsername",
|
||||
"SERVICE_HOST": "your.service.hostname",
|
||||
"SERVICE_INFO": "Network IRC Service",
|
||||
"SERVICE_CHANLOG": "#services",
|
||||
"SERVICE_SMODES": "+ioqBS",
|
||||
"SERVICE_CMODES": "ntsO",
|
||||
"SERVICE_UMODES": "o",
|
||||
"SERVICE_PREFIX": "!",
|
||||
|
||||
"OWNER": "admin",
|
||||
"PASSWORD": "password",
|
||||
|
||||
"SALON_JAIL": "#jail",
|
||||
"SALON_JAIL_MODES": "sS",
|
||||
"SALON_LIBERER": "#welcome",
|
||||
|
||||
"API_TIMEOUT": 2,
|
||||
|
||||
"PORTS_TO_SCAN": [3028, 8080, 1080, 1085, 4145, 9050],
|
||||
"WHITELISTED_IP": ["127.0.0.1"],
|
||||
"GLINE_DURATION": "30",
|
||||
|
||||
"DEBUG_LEVEL": 20,
|
||||
|
||||
"CONFIG_COLOR": {
|
||||
"blanche": "\\u0003\\u0030",
|
||||
"noire": "\\u0003\\u0031",
|
||||
"bleue": "\\u0003\\u0020",
|
||||
"verte": "\\u0003\\u0033",
|
||||
"rouge": "\\u0003\\u0034",
|
||||
"jaune": "\\u0003\\u0036",
|
||||
"gras": "\\u0002",
|
||||
"nogc": "\\u0002\\u0003"
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
import os
|
||||
##########################################
|
||||
# CONFIGURATION FILE : #
|
||||
# Rename file to : configuration.py #
|
||||
##########################################
|
||||
|
||||
class Config:
|
||||
|
||||
DEFENDER_VERSION = '1.1.0' # MAJOR.MINOR.BATCH
|
||||
DEFENDER_DB_PATH = 'db' + os.sep # Séparateur en fonction de l'OS
|
||||
DEFENDER_DB_NAME = 'defender' # Le nom de la base de données principale
|
||||
SERVICE_NAME = 'defender' # Le nom du service
|
||||
|
||||
SERVEUR_IP = '8.8.8.8' # IP ou host du serveur à rejoindre
|
||||
SERVEUR_HOSTNAME = 'your hostname' # Le hostname du serveur IRC
|
||||
SERVEUR_LINK = 'your link' # Host attendu par votre IRCd (ex. dans votre link block pour Unrealircd)
|
||||
SERVEUR_PORT = 6666 # Port du link
|
||||
SERVEUR_PASSWORD = 'your link password' # Mot de passe du link (Privilégiez argon2 sur Unrealircd)
|
||||
SERVEUR_ID = '002' # SID (identification) du bot en tant que Services
|
||||
|
||||
SERVICE_NICKNAME = 'BotName' # Nick du bot sur IRC
|
||||
SERVICE_REALNAME = 'BotRealname' # Realname du bot
|
||||
SERVICE_USERNAME = 'BotIdent' # Ident du bot
|
||||
SERVICE_HOST = 'your service host' # Host du bot
|
||||
SERVICE_INFO = 'Network IRC Service' # swhois du bot
|
||||
SERVICE_CHANLOG = '#services' # Salon des logs et autres messages issus du bot
|
||||
SERVICE_SMODES = '+ioqBS' # Mode du service
|
||||
SERVICE_CMODES = 'ntsO' # Mode du salon (#ChanLog) que le bot appliquera à son entrée
|
||||
SERVICE_UMODES = 'o' # Mode que le bot pourra se donner à sa connexion au salon chanlog
|
||||
SERVICE_PREFIX = '.' # Prefix pour envoyer les commandes au bot
|
||||
SERVICE_ID = SERVEUR_ID + 'AAAAAB' # L'identifiant du service
|
||||
|
||||
OWNER = 'admin' # Identifiant du compte admin
|
||||
PASSWORD = 'password' # Mot de passe du compte admin
|
||||
|
||||
SALON_JAIL = '#JAIL' # Salon pot de miel
|
||||
SALON_JAIL_MODES = 'sS' # Mode du salon pot de miel
|
||||
SALON_LIBERER = '#welcome' # Le salon ou sera envoyé l'utilisateur clean
|
||||
|
||||
PORTS_TO_SCAN = [3028, 8080, 1080, 1085, 4145, 9050] # Liste des ports a scanné pour une detection de proxy
|
||||
|
||||
DEBUG = 0 # Afficher l'ensemble des messages du serveurs dans la console
|
||||
|
||||
CONFIG_COLOR = {
|
||||
'blanche': '\x0300', # Couleur blanche
|
||||
'noire': '\x0301', # Couleur noire
|
||||
'bleue': '\x0302', # Couleur Bleue
|
||||
'verte': '\x0303', # Couleur Verte
|
||||
'rouge': '\x0304', # Couleur rouge
|
||||
'jaune': '\x0306', # Couleur jaune
|
||||
'gras': '\x02', # Gras
|
||||
'nogc': '\x02\x03' # Retirer gras et couleur
|
||||
}
|
||||
@@ -37,7 +37,7 @@ class Install:
|
||||
print(f"===> Version of python : {python_version()} ==> OK")
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def checkDependencies(self) -> None:
|
||||
"""### Verifie les dépendances si elles sont installées
|
||||
- Test si les modules sont installés
|
||||
|
||||
926
core/irc.py
926
core/irc.py
File diff suppressed because it is too large
Load Diff
126
core/loadConf.py
Normal file
126
core/loadConf.py
Normal file
@@ -0,0 +1,126 @@
|
||||
import json
|
||||
from os import sep
|
||||
from typing import Union
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
##########################################
|
||||
# CONFIGURATION FILE #
|
||||
##########################################
|
||||
|
||||
@dataclass
|
||||
class ConfigDataModel:
|
||||
|
||||
SERVEUR_IP: str
|
||||
SERVEUR_HOSTNAME: str # Le hostname du serveur IRC
|
||||
SERVEUR_LINK: str # Host attendu par votre IRCd (ex. dans votre link block pour Unrealircd)
|
||||
SERVEUR_PORT: int # Port du link
|
||||
SERVEUR_PASSWORD: str # Mot de passe du link (Privilégiez argon2 sur Unrealircd)
|
||||
SERVEUR_ID: str # SID (identification) du bot en tant que Services
|
||||
SERVEUR_SSL: bool # Activer la connexion SSL
|
||||
|
||||
SERVICE_NAME: str # Le nom du service
|
||||
SERVICE_NICKNAME: str # Nick du bot sur IRC
|
||||
SERVICE_REALNAME: str # Realname du bot
|
||||
SERVICE_USERNAME: str # Ident du bot
|
||||
SERVICE_HOST: str # Host du bot
|
||||
SERVICE_INFO: str # swhois du bot
|
||||
SERVICE_CHANLOG: str # Salon des logs et autres messages issus du bot
|
||||
SERVICE_SMODES: str # Mode du service
|
||||
SERVICE_CMODES: str # Mode du salon (#ChanLog) que le bot appliquera à son entrée
|
||||
SERVICE_UMODES: str # Mode que le bot pourra se donner à sa connexion au salon chanlog
|
||||
SERVICE_PREFIX: str # Prefix pour envoyer les commandes au bot
|
||||
SERVICE_ID: str = field(init=False) # L'identifiant du service
|
||||
|
||||
OWNER: str # Identifiant du compte admin
|
||||
PASSWORD: str # Mot de passe du compte admin
|
||||
|
||||
SALON_JAIL: str # Salon pot de miel
|
||||
SALON_JAIL_MODES: str # Mode du salon pot de miel
|
||||
SALON_LIBERER: str # Le salon ou sera envoyé l'utilisateur clean
|
||||
|
||||
API_TIMEOUT: int # Timeout des api's
|
||||
|
||||
PORTS_TO_SCAN: list # Liste des ports a scanné pour une detection de proxy
|
||||
WHITELISTED_IP: list # IP a ne pas scanner
|
||||
GLINE_DURATION: str # La durée du gline
|
||||
|
||||
DEBUG_LEVEL: int # Le niveau des logs DEBUG 10 | INFO 20 | WARNING 30 | ERROR 40 | CRITICAL 50
|
||||
|
||||
CONFIG_COLOR: dict[str, str]
|
||||
|
||||
table_admin: str
|
||||
table_commande: str
|
||||
table_log: str
|
||||
table_module: str
|
||||
|
||||
current_version: str
|
||||
latest_version: str
|
||||
db_name: str
|
||||
db_path: str
|
||||
|
||||
def __post_init__(self):
|
||||
# Initialiser SERVICE_ID après la création de l'objet
|
||||
self.SERVICE_ID:str = f"{self.SERVEUR_ID}AAAAAB"
|
||||
|
||||
class Config:
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.ConfigObject: ConfigDataModel = self.__load_service_configuration()
|
||||
return None
|
||||
|
||||
def __load_json_service_configuration(self):
|
||||
|
||||
conf_filename = f'core{sep}configuration.json'
|
||||
with open(conf_filename, 'r') as configuration_data:
|
||||
configuration:dict[str, Union[str, int, list, dict]] = json.load(configuration_data)
|
||||
|
||||
for key, value in configuration['CONFIG_COLOR'].items():
|
||||
configuration['CONFIG_COLOR'][key] = str(value).encode('utf-8').decode('unicode_escape')
|
||||
|
||||
return configuration
|
||||
|
||||
def __load_service_configuration(self) -> ConfigDataModel:
|
||||
import_config = self.__load_json_service_configuration()
|
||||
|
||||
ConfigObject: ConfigDataModel = ConfigDataModel(
|
||||
SERVEUR_IP=import_config["SERVEUR_IP"],
|
||||
SERVEUR_HOSTNAME=import_config["SERVEUR_HOSTNAME"],
|
||||
SERVEUR_LINK=import_config["SERVEUR_LINK"],
|
||||
SERVEUR_PORT=import_config["SERVEUR_PORT"],
|
||||
SERVEUR_PASSWORD=import_config["SERVEUR_PASSWORD"],
|
||||
SERVEUR_ID=import_config["SERVEUR_ID"],
|
||||
SERVEUR_SSL=import_config["SERVEUR_SSL"],
|
||||
SERVICE_NAME=import_config["SERVICE_NAME"],
|
||||
SERVICE_NICKNAME=import_config["SERVICE_NICKNAME"],
|
||||
SERVICE_REALNAME=import_config["SERVICE_REALNAME"],
|
||||
SERVICE_USERNAME=import_config["SERVICE_USERNAME"],
|
||||
SERVICE_HOST=import_config["SERVICE_HOST"],
|
||||
SERVICE_INFO=import_config["SERVICE_INFO"],
|
||||
SERVICE_CHANLOG=import_config["SERVICE_CHANLOG"],
|
||||
SERVICE_SMODES=import_config["SERVICE_SMODES"],
|
||||
SERVICE_CMODES=import_config["SERVICE_CMODES"],
|
||||
SERVICE_UMODES=import_config["SERVICE_UMODES"],
|
||||
SERVICE_PREFIX=import_config["SERVICE_PREFIX"],
|
||||
OWNER=import_config["OWNER"],
|
||||
PASSWORD=import_config["PASSWORD"],
|
||||
SALON_JAIL=import_config["SALON_JAIL"],
|
||||
SALON_JAIL_MODES=import_config["SALON_JAIL_MODES"],
|
||||
SALON_LIBERER=import_config["SALON_LIBERER"],
|
||||
API_TIMEOUT=import_config["API_TIMEOUT"],
|
||||
PORTS_TO_SCAN=import_config["PORTS_TO_SCAN"],
|
||||
WHITELISTED_IP=import_config["WHITELISTED_IP"],
|
||||
GLINE_DURATION=import_config["GLINE_DURATION"],
|
||||
DEBUG_LEVEL=import_config["DEBUG_LEVEL"],
|
||||
CONFIG_COLOR=import_config["CONFIG_COLOR"],
|
||||
table_admin='sys_admins',
|
||||
table_commande='sys_commandes',
|
||||
table_log='sys_logs',
|
||||
table_module='sys_modules',
|
||||
current_version='',
|
||||
latest_version='',
|
||||
db_name='defender',
|
||||
db_path=f'db{sep}'
|
||||
)
|
||||
|
||||
return ConfigObject
|
||||
1287
mods/mod_defender.py
1287
mods/mod_defender.py
File diff suppressed because it is too large
Load Diff
@@ -1,11 +1,10 @@
|
||||
import threading
|
||||
from core.irc import Irc
|
||||
|
||||
# Le module crée devra réspecter quelques conditions
|
||||
# 1. Importer le module de configuration
|
||||
# 2. Le nom de class devra toujours s'appeler comme le module exemple => nom de class Dktmb | nom du module mod_dktmb
|
||||
# 3. la fonction __init__ devra toujours avoir les parametres suivant (self, irc:object)
|
||||
# 1 . Créer la variable irc dans le module
|
||||
# 1 . Créer la variable Irc dans le module
|
||||
# 2 . Récuperer la configuration dans une variable
|
||||
# 3 . Définir et enregistrer les nouvelles commandes
|
||||
# 4. une fonction _hcmds(self, user:str, cmd: list) devra toujours etre crée.
|
||||
@@ -13,37 +12,59 @@ from core.irc import Irc
|
||||
class Test():
|
||||
|
||||
def __init__(self, ircInstance:Irc) -> None:
|
||||
print(f'Module {self.__class__.__name__} loaded ...')
|
||||
|
||||
self.irc = ircInstance # Ajouter l'object mod_irc a la classe
|
||||
# Add Irc Object to the module
|
||||
self.Irc = ircInstance
|
||||
|
||||
self.config = ircInstance.Config # Ajouter la configuration a la classe
|
||||
# Add Global Configuration to the module
|
||||
self.Config = ircInstance.Config
|
||||
|
||||
# Add Base object to the module
|
||||
self.Base = ircInstance.Base
|
||||
|
||||
# Add logs object to the module
|
||||
self.Logs = ircInstance.Base.logs
|
||||
|
||||
# Add User object to the module
|
||||
self.User = ircInstance.User
|
||||
|
||||
# Add Channel object to the module
|
||||
self.Channel = ircInstance.Channel
|
||||
|
||||
# Créer les nouvelles commandes du module
|
||||
self.commands = ['test']
|
||||
self.commands_level = {
|
||||
0: ['test'],
|
||||
1: ['test_level_1']
|
||||
}
|
||||
|
||||
self.__set_commands(self.commands) # Enrigstrer les nouvelles commandes dans le code
|
||||
# Init the module
|
||||
self.__init_module()
|
||||
|
||||
self.core = ircInstance.Base # Instance du module Base
|
||||
# Log the module
|
||||
self.Logs.debug(f'Module {self.__class__.__name__} loaded ...')
|
||||
|
||||
self.session = '' # Instancier une session pour la base de données
|
||||
self.__create_db('mod_test') # Créer la base de données si necessaire
|
||||
def __init_module(self) -> None:
|
||||
|
||||
def __set_commands(self, commands:list) -> None:
|
||||
"""Rajoute les commandes du module au programme principal
|
||||
self.__set_commands(self.commands_level)
|
||||
self.__create_tables()
|
||||
|
||||
return None
|
||||
|
||||
def __set_commands(self, commands:dict[int, list[str]]) -> None:
|
||||
"""### Rajoute les commandes du module au programme principal
|
||||
|
||||
Args:
|
||||
commands (list): Liste des commandes du module
|
||||
|
||||
Returns:
|
||||
None: Aucun retour attendu
|
||||
"""
|
||||
for command in commands:
|
||||
self.irc.commands.append(command)
|
||||
for level, com in commands.items():
|
||||
for c in commands[level]:
|
||||
if not c in self.Irc.commands:
|
||||
self.Irc.commands_level[level].append(c)
|
||||
self.Irc.commands.append(c)
|
||||
|
||||
return True
|
||||
return None
|
||||
|
||||
def __create_db(self, db_name:str) -> None:
|
||||
def __create_tables(self) -> None:
|
||||
"""Methode qui va créer la base de donnée si elle n'existe pas.
|
||||
Une Session unique pour cette classe sera crée, qui sera utilisé dans cette classe / module
|
||||
Args:
|
||||
@@ -52,32 +73,36 @@ class Test():
|
||||
Returns:
|
||||
None: Aucun retour n'es attendu
|
||||
"""
|
||||
db_directory = self.core.MODS_DB_PATH
|
||||
|
||||
self.session = self.core.db_init(db_directory, db_name)
|
||||
|
||||
table_logs = '''CREATE TABLE IF NOT EXISTS logs (
|
||||
table_logs = '''CREATE TABLE IF NOT EXISTS test_logs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
datetime TEXT,
|
||||
server_msg TEXT
|
||||
)
|
||||
'''
|
||||
|
||||
self.core.db_execute_query(self.session, table_logs)
|
||||
self.Base.db_execute_query(table_logs)
|
||||
return None
|
||||
|
||||
def _hcmds(self, user:str, cmd: list) -> None:
|
||||
def unload(self) -> None:
|
||||
|
||||
command = cmd[0].lower()
|
||||
return None
|
||||
|
||||
def cmd(self, data:list) -> None:
|
||||
return None
|
||||
|
||||
def _hcmds(self, user:str, cmd: list, fullcmd: list = []) -> None:
|
||||
|
||||
command = str(cmd[0]).lower()
|
||||
dnickname = self.Config.SERVICE_NICKNAME
|
||||
fromuser = user
|
||||
|
||||
match command:
|
||||
|
||||
case 'test':
|
||||
try:
|
||||
user_action = cmd[1]
|
||||
self.irc.send2socket(f'PRIVMSG #webmail Je vais voicer {user}')
|
||||
self.irc.send2socket(f'MODE #webmail +v {user_action}')
|
||||
self.core.create_log(f"MODE +v sur {user_action}")
|
||||
|
||||
self.Irc.send2socket(f":{dnickname} NOTICE {fromuser} : test command ready ...")
|
||||
self.Logs.debug(f"Test logs ready")
|
||||
except KeyError as ke:
|
||||
self.core.create_log(f"Key Error : {ke}")
|
||||
|
||||
self.Logs.error(f"Key Error : {ke}")
|
||||
441
mods/mod_votekick.py
Normal file
441
mods/mod_votekick.py
Normal file
@@ -0,0 +1,441 @@
|
||||
from core.irc import Irc
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
# Activer le systeme sur un salon (activate #salon)
|
||||
# Le service devra se connecter au salon
|
||||
# Le service devra se mettre en op
|
||||
# Soumettre un nom de user (submit nickname)
|
||||
# voter pour un ban (vote_for)
|
||||
# voter contre un ban (vote_against)
|
||||
|
||||
|
||||
|
||||
class Votekick():
|
||||
|
||||
@dataclass
|
||||
class VoteChannelModel:
|
||||
channel_name: str
|
||||
target_user: str
|
||||
voter_users: list
|
||||
vote_for: int
|
||||
vote_against: int
|
||||
|
||||
VOTE_CHANNEL_DB:list[VoteChannelModel] = []
|
||||
|
||||
def __init__(self, ircInstance:Irc) -> None:
|
||||
# Add Irc Object to the module
|
||||
self.Irc = ircInstance
|
||||
|
||||
# Add Global Configuration to the module
|
||||
self.Config = ircInstance.Config
|
||||
|
||||
# Add Base object to the module
|
||||
self.Base = ircInstance.Base
|
||||
|
||||
# Add logs object to the module
|
||||
self.Logs = ircInstance.Base.logs
|
||||
|
||||
# Add User object to the module
|
||||
self.User = ircInstance.User
|
||||
|
||||
# Add Channel object to the module
|
||||
self.Channel = ircInstance.Channel
|
||||
|
||||
# Créer les nouvelles commandes du module
|
||||
self.commands_level = {
|
||||
0: ['vote_for', 'vote_against'],
|
||||
1: ['activate', 'deactivate', 'submit', 'vote_stat', 'vote_verdict', 'vote_cancel']
|
||||
}
|
||||
|
||||
# Init the module
|
||||
self.__init_module()
|
||||
|
||||
# Log the module
|
||||
self.Logs.debug(f'Module {self.__class__.__name__} loaded ...')
|
||||
|
||||
def __init_module(self) -> None:
|
||||
|
||||
self.__set_commands(self.commands_level)
|
||||
self.__create_tables()
|
||||
self.join_saved_channels()
|
||||
|
||||
return None
|
||||
|
||||
def __set_commands(self, commands:dict[int, list[str]]) -> None:
|
||||
"""### Rajoute les commandes du module au programme principal
|
||||
|
||||
Args:
|
||||
commands (list): Liste des commandes du module
|
||||
"""
|
||||
for level, com in commands.items():
|
||||
for c in commands[level]:
|
||||
if not c in self.Irc.commands:
|
||||
self.Irc.commands_level[level].append(c)
|
||||
self.Irc.commands.append(c)
|
||||
|
||||
return None
|
||||
|
||||
def __create_tables(self) -> None:
|
||||
"""Methode qui va créer la base de donnée si elle n'existe pas.
|
||||
Une Session unique pour cette classe sera crée, qui sera utilisé dans cette classe / module
|
||||
Args:
|
||||
database_name (str): Nom de la base de données ( pas d'espace dans le nom )
|
||||
|
||||
Returns:
|
||||
None: Aucun retour n'es attendu
|
||||
"""
|
||||
|
||||
table_logs = '''CREATE TABLE IF NOT EXISTS votekick_logs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
datetime TEXT,
|
||||
server_msg TEXT
|
||||
)
|
||||
'''
|
||||
|
||||
table_vote = '''CREATE TABLE IF NOT EXISTS votekick_channel (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
datetime TEXT,
|
||||
channel TEXT
|
||||
)
|
||||
'''
|
||||
|
||||
self.Base.db_execute_query(table_logs)
|
||||
self.Base.db_execute_query(table_vote)
|
||||
return None
|
||||
|
||||
def unload(self) -> None:
|
||||
try:
|
||||
for chan in self.VOTE_CHANNEL_DB:
|
||||
self.Irc.send2socket(f":{self.Config.SERVICE_NICKNAME} PART {chan.channel_name}")
|
||||
|
||||
self.VOTE_CHANNEL_DB = []
|
||||
self.Logs.debug(f'Delete memory DB VOTE_CHANNEL_DB: {self.VOTE_CHANNEL_DB}')
|
||||
|
||||
return None
|
||||
except UnboundLocalError as ne:
|
||||
self.Logs.error(f'{ne}')
|
||||
except NameError as ue:
|
||||
self.Logs.error(f'{ue}')
|
||||
except:
|
||||
self.Logs.error('Error on the module')
|
||||
|
||||
def init_vote_system(self, channel: str) -> bool:
|
||||
|
||||
response = False
|
||||
for chan in self.VOTE_CHANNEL_DB:
|
||||
if chan.channel_name == channel:
|
||||
chan.target_user = ''
|
||||
chan.voter_users = []
|
||||
chan.vote_against = 0
|
||||
chan.vote_for = 0
|
||||
response = True
|
||||
|
||||
return response
|
||||
|
||||
def insert_vote_channel(self, ChannelObject: VoteChannelModel) -> bool:
|
||||
result = False
|
||||
found = False
|
||||
for chan in self.VOTE_CHANNEL_DB:
|
||||
if chan.channel_name == ChannelObject.channel_name:
|
||||
found = True
|
||||
|
||||
if not found:
|
||||
self.VOTE_CHANNEL_DB.append(ChannelObject)
|
||||
self.Logs.debug(f"The channel has been added {ChannelObject}")
|
||||
self.db_add_vote_channel(ChannelObject.channel_name)
|
||||
|
||||
return result
|
||||
|
||||
def db_add_vote_channel(self, channel:str) -> bool:
|
||||
"""Cette fonction ajoute les salons ou seront autoriser les votes
|
||||
|
||||
Args:
|
||||
channel (str): le salon à enregistrer.
|
||||
"""
|
||||
current_datetime = self.Base.get_datetime()
|
||||
mes_donnees = {'channel': channel}
|
||||
|
||||
response = self.Base.db_execute_query("SELECT id FROM votekick_channel WHERE channel = :channel", mes_donnees)
|
||||
|
||||
isChannelExist = response.fetchone()
|
||||
|
||||
if isChannelExist is None:
|
||||
mes_donnees = {'datetime': current_datetime, 'channel': channel}
|
||||
insert = self.Base.db_execute_query(f"INSERT INTO votekick_channel (datetime, channel) VALUES (:datetime, :channel)", mes_donnees)
|
||||
if insert.rowcount > 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
else:
|
||||
return False
|
||||
|
||||
def db_delete_vote_channel(self, channel: str) -> bool:
|
||||
"""Cette fonction supprime les salons de join de Defender
|
||||
|
||||
Args:
|
||||
channel (str): le salon à enregistrer.
|
||||
"""
|
||||
mes_donnes = {'channel': channel}
|
||||
response = self.Base.db_execute_query("DELETE FROM votekick_channel WHERE channel = :channel", mes_donnes)
|
||||
|
||||
affected_row = response.rowcount
|
||||
|
||||
if affected_row > 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def join_saved_channels(self) -> None:
|
||||
|
||||
result = self.Base.db_execute_query("SELECT id, channel FROM votekick_channel")
|
||||
channels = result.fetchall()
|
||||
unixtime = self.Base.get_unixtime()
|
||||
|
||||
for channel in channels:
|
||||
id, chan = channel
|
||||
self.insert_vote_channel(self.VoteChannelModel(channel_name=chan, target_user='', voter_users=[], vote_for=0, vote_against=0))
|
||||
self.Irc.send2socket(f":{self.Config.SERVEUR_ID} SJOIN {unixtime} {chan} + :{self.Config.SERVICE_ID}")
|
||||
self.Irc.send2socket(f":{self.Config.SERVICE_NICKNAME} SAMODE {chan} +o {self.Config.SERVICE_NICKNAME}")
|
||||
|
||||
return None
|
||||
|
||||
def is_vote_ongoing(self, channel: str) -> bool:
|
||||
|
||||
response = False
|
||||
for vote in self.VOTE_CHANNEL_DB:
|
||||
if vote.channel_name == channel:
|
||||
if vote.target_user:
|
||||
response = True
|
||||
|
||||
return response
|
||||
|
||||
def timer_vote_verdict(self, channel: str) -> None:
|
||||
|
||||
dnickname = self.Config.SERVICE_NICKNAME
|
||||
|
||||
for chan in self.VOTE_CHANNEL_DB:
|
||||
if chan.channel_name == channel:
|
||||
target_user = self.User.get_nickname(chan.target_user)
|
||||
if chan.vote_for > chan.vote_against:
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :The user {self.Config.CONFIG_COLOR["gras"]}{target_user}{self.Config.CONFIG_COLOR["nogc"]} will be kicked from this channel')
|
||||
self.Irc.send2socket(f":{dnickname} KICK {channel} {target_user} Following the vote, you are not welcome in {channel}")
|
||||
self.Channel.delete_user_from_channel(channel, self.User.get_uid(target_user))
|
||||
elif chan.vote_for <= chan.vote_against:
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :This user will stay on this channel')
|
||||
|
||||
# Init the system
|
||||
if self.init_vote_system(channel):
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :System vote re initiated')
|
||||
|
||||
return None
|
||||
|
||||
def cmd(self, data:list) -> None:
|
||||
cmd = list(data).copy()
|
||||
|
||||
match cmd[2]:
|
||||
case 'SJOIN':
|
||||
pass
|
||||
case _:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
def _hcmds(self, user:str, cmd: list, fullcmd: list = []) -> None:
|
||||
# cmd is the command starting from the user command
|
||||
# full cmd is sending the entire server response
|
||||
|
||||
command = str(cmd[0]).lower()
|
||||
dnickname = self.Config.SERVICE_NICKNAME
|
||||
fromuser = user
|
||||
|
||||
if len(fullcmd) >= 3:
|
||||
fromchannel = str(fullcmd[2]).lower() if self.Base.Is_Channel(str(fullcmd[2]).lower()) else None
|
||||
else:
|
||||
fromchannel = None
|
||||
|
||||
if len(cmd) >= 2:
|
||||
sentchannel = str(cmd[1]).lower() if self.Base.Is_Channel(str(cmd[1]).lower()) else None
|
||||
else:
|
||||
sentchannel = None
|
||||
|
||||
if not fromchannel is None:
|
||||
channel = fromchannel
|
||||
elif not sentchannel is None:
|
||||
channel = sentchannel
|
||||
else:
|
||||
channel = None
|
||||
|
||||
match command:
|
||||
|
||||
case 'vote_cancel':
|
||||
try:
|
||||
if channel is None:
|
||||
self.Logs.error(f"The channel is not known, defender can't cancel the vote")
|
||||
self.Irc.send2socket(f':{dnickname} NOTICE {fromuser} :You need to specify the channel => /msg {dnickname} vote_cancel #channel')
|
||||
|
||||
for vote in self.VOTE_CHANNEL_DB:
|
||||
if vote.channel_name == channel:
|
||||
self.init_vote_system(channel)
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :Vote system re-initiated')
|
||||
|
||||
except IndexError as ke:
|
||||
self.Irc.send2socket(f':{dnickname} NOTICE {fromuser} :/msg {dnickname} vote_cancel #channel')
|
||||
self.Logs.error(f'Index Error: {ke}')
|
||||
|
||||
case 'vote_for':
|
||||
try:
|
||||
# vote_for
|
||||
channel = str(fullcmd[2]).lower()
|
||||
for chan in self.VOTE_CHANNEL_DB:
|
||||
if chan.channel_name == channel:
|
||||
if fromuser in chan.voter_users:
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :You already submitted a vote')
|
||||
else:
|
||||
chan.vote_for += 1
|
||||
chan.voter_users.append(fromuser)
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :Vote recorded, thank you')
|
||||
|
||||
except KeyError as ke:
|
||||
self.Logs.error(f'Key Error: {ke}')
|
||||
except IndexError as ie:
|
||||
self.Irc.send2socket(f':{dnickname} NOTICE {fromuser} :/msg {dnickname} vote_cancel #channel')
|
||||
self.Logs.error(f'Index Error: {ie}')
|
||||
|
||||
case 'vote_against':
|
||||
try:
|
||||
# vote_against
|
||||
channel = str(fullcmd[2]).lower()
|
||||
for chan in self.VOTE_CHANNEL_DB:
|
||||
if chan.channel_name == channel:
|
||||
if fromuser in chan.voter_users:
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :You already submitted a vote')
|
||||
else:
|
||||
chan.vote_against += 1
|
||||
chan.voter_users.append(fromuser)
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :Vote recorded, thank you')
|
||||
|
||||
except KeyError as ke:
|
||||
self.Logs.error(f'Key Error: {ke}')
|
||||
|
||||
case 'vote_stat':
|
||||
try:
|
||||
# channel = str(fullcmd[2]).lower()
|
||||
for chan in self.VOTE_CHANNEL_DB:
|
||||
if chan.channel_name == channel:
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :Channel: {chan.channel_name} | Target: {self.User.get_nickname(chan.target_user)} | For: {chan.vote_for} | Against: {chan.vote_against} | Number of voters: {str(len(chan.voter_users))}')
|
||||
|
||||
except KeyError as ke:
|
||||
self.Logs.error(f'Key Error: {ke}')
|
||||
|
||||
case 'vote_verdict':
|
||||
try:
|
||||
# channel = str(fullcmd[2]).lower()
|
||||
for chan in self.VOTE_CHANNEL_DB:
|
||||
if chan.channel_name == channel:
|
||||
target_user = self.User.get_nickname(chan.target_user)
|
||||
if chan.vote_for > chan.vote_against:
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :The user {self.Config.CONFIG_COLOR["gras"]}{target_user}{self.Config.CONFIG_COLOR["nogc"]} will be kicked from this channel')
|
||||
self.Irc.send2socket(f":{dnickname} KICK {channel} {target_user} Following the vote, you are not welcome in {channel}")
|
||||
elif chan.vote_for <= chan.vote_against:
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :This user will stay on this channel')
|
||||
|
||||
# Init the system
|
||||
if self.init_vote_system(channel):
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :System vote re initiated')
|
||||
|
||||
except KeyError as ke:
|
||||
self.Logs.error(f'Key Error: {ke}')
|
||||
|
||||
case 'submit':
|
||||
# submit nickname
|
||||
try:
|
||||
nickname_submitted = cmd[1]
|
||||
# channel = str(fullcmd[2]).lower()
|
||||
uid_submitted = self.User.get_uid(nickname_submitted)
|
||||
user_submitted = self.User.get_User(nickname_submitted)
|
||||
|
||||
# check if there is an ongoing vote
|
||||
if self.is_vote_ongoing(channel):
|
||||
for vote in self.VOTE_CHANNEL_DB:
|
||||
if vote.channel_name == channel:
|
||||
ongoing_user = self.User.get_nickname(vote.target_user)
|
||||
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :There is an ongoing vote on {ongoing_user}')
|
||||
return False
|
||||
|
||||
# check if the user exist
|
||||
if user_submitted is None:
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :This nickname <{nickname_submitted}> do not exist')
|
||||
return False
|
||||
|
||||
uid_cleaned = self.Base.clean_uid(uid_submitted)
|
||||
ChannelInfo = self.Channel.get_Channel(channel)
|
||||
|
||||
clean_uids_in_channel: list = []
|
||||
for uid in ChannelInfo.uids:
|
||||
clean_uids_in_channel.append(self.Base.clean_uid(uid))
|
||||
|
||||
if not uid_cleaned in clean_uids_in_channel:
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :This nickname <{nickname_submitted}> is not available in this channel')
|
||||
return False
|
||||
|
||||
# check if Ircop or Service or Bot
|
||||
pattern = fr'[o|B|S]'
|
||||
operator_user = re.findall(pattern, user_submitted.umodes)
|
||||
if operator_user:
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :You cant vote for this user ! he/she is protected')
|
||||
return False
|
||||
|
||||
for chan in self.VOTE_CHANNEL_DB:
|
||||
if chan.channel_name == channel:
|
||||
chan.target_user = self.User.get_uid(nickname_submitted)
|
||||
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :{nickname_submitted} has been targeted for a vote')
|
||||
|
||||
self.Base.create_timer(60, self.timer_vote_verdict, (channel, ))
|
||||
self.Irc.send2socket(f':{dnickname} PRIVMSG {channel} :This vote will end after 60 secondes')
|
||||
|
||||
except KeyError as ke:
|
||||
self.Logs.error(f'Key Error: {ke}')
|
||||
except TypeError as te:
|
||||
self.Logs.error(te)
|
||||
|
||||
case 'activate':
|
||||
try:
|
||||
# activate #channel
|
||||
# channel = str(cmd[1]).lower()
|
||||
|
||||
self.insert_vote_channel(
|
||||
self.VoteChannelModel(
|
||||
channel_name=channel,
|
||||
target_user='',
|
||||
voter_users=[],
|
||||
vote_for=0,
|
||||
vote_against=0
|
||||
)
|
||||
)
|
||||
|
||||
self.Irc.send2socket(f":{dnickname} JOIN {channel}")
|
||||
self.Irc.send2socket(f":{dnickname} SAMODE {channel} +o {dnickname}")
|
||||
self.Irc.send2socket(f":{dnickname} PRIVMSG {channel} :You can now use !submit <nickname> to decide if he will stay or not on this channel ")
|
||||
|
||||
except KeyError as ke:
|
||||
self.Logs.error(f"Key Error : {ke}")
|
||||
|
||||
case 'deactivate':
|
||||
try:
|
||||
# deactivate #channel
|
||||
# channel = str(cmd[1]).lower()
|
||||
|
||||
self.Irc.send2socket(f":{dnickname} SAMODE {channel} -o {dnickname}")
|
||||
self.Irc.send2socket(f":{dnickname} PART {channel}")
|
||||
|
||||
for chan in self.VOTE_CHANNEL_DB:
|
||||
if chan.channel_name == channel:
|
||||
self.VOTE_CHANNEL_DB.remove(chan)
|
||||
self.db_delete_vote_channel(chan.channel_name)
|
||||
|
||||
self.Logs.debug(f"Test logs ready")
|
||||
except KeyError as ke:
|
||||
self.Logs.error(f"Key Error : {ke}")
|
||||
3
version.json
Normal file
3
version.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"version": "5.0.5"
|
||||
}
|
||||
Reference in New Issue
Block a user