mirror of
https://github.com/iio612/DEFENDER.git
synced 2026-02-13 19:24:23 +00:00
V6.1.5 i forgot
This commit is contained in:
@@ -111,7 +111,7 @@ class Clone():
|
||||
except TypeError as te:
|
||||
self.Logs.critical(te)
|
||||
|
||||
def unload(self) -> None:
|
||||
def unload(self, reloading: bool = False) -> None:
|
||||
"""Cette methode sera executée a chaque désactivation ou
|
||||
rechargement de module
|
||||
"""
|
||||
|
||||
@@ -164,15 +164,14 @@ class Command:
|
||||
"""
|
||||
self.Base.db_update_core_config(self.module_name, self.ModConfig, param_key, param_value)
|
||||
|
||||
def unload(self) -> None:
|
||||
def unload(self, reloading: bool = False) -> None:
|
||||
|
||||
return None
|
||||
|
||||
def cmd(self, data: list[str]) -> None:
|
||||
try:
|
||||
# service_id = self.Config.SERVICE_ID
|
||||
|
||||
dnickname = self.Config.SERVICE_NICKNAME
|
||||
# dchanlog = self.Config.SERVICE_CHANLOG
|
||||
red = self.Config.COLORS.red
|
||||
green = self.Config.COLORS.green
|
||||
bold = self.Config.COLORS.bold
|
||||
@@ -303,7 +302,7 @@ class Command:
|
||||
service_id = self.Config.SERVICE_ID
|
||||
dchanlog = self.Config.SERVICE_CHANLOG
|
||||
self.user_to_notice = uidornickname
|
||||
fromuser = uidornickname
|
||||
fromuser = self.User.get_nickname(uidornickname)
|
||||
fromchannel = channel_name
|
||||
|
||||
match command:
|
||||
@@ -853,9 +852,9 @@ class Command:
|
||||
|
||||
case 'topic':
|
||||
try:
|
||||
if len(cmd) == 1:
|
||||
self.Protocol.send_notice(nick_from=dnickname, nick_to=fromuser, msg=f"/msg {dnickname} TOPIC #channel THE_TOPIC_MESSAGE")
|
||||
self.Protocol.send_notice(nick_from=dnickname, nick_to=fromuser, msg=f" /msg {dnickname} TOPIC #channel THE_TOPIC_MESSAGE")
|
||||
# TOPIC <#CHANNEL> <THE TOPIC DESCRIPTION>
|
||||
if len(cmd) < 2:
|
||||
self.Protocol.send_notice(nick_from=dnickname, nick_to=fromuser, msg=f"/msg {dnickname} {command.upper()} <#channel> THE_TOPIC_MESSAGE")
|
||||
return None
|
||||
|
||||
chan = str(cmd[1])
|
||||
@@ -864,10 +863,10 @@ class Command:
|
||||
self.Protocol.send_notice(nick_from=dnickname, nick_to=fromuser, msg=f"/msg {dnickname} TOPIC #channel THE_TOPIC_MESSAGE")
|
||||
return None
|
||||
|
||||
topic_msg = ' '.join(cmd[2:]).strip()
|
||||
topic_msg: str = ' '.join(cmd[2:]).strip()
|
||||
|
||||
if topic_msg:
|
||||
self.Protocol.send2socket(f':{dnickname} TOPIC {chan} :{topic_msg}')
|
||||
self.Protocol.send_topic_chan(channel_name=chan, topic_msg=topic_msg)
|
||||
else:
|
||||
self.Protocol.send_notice(nick_from=dnickname, nick_to=fromuser, msg="You need to specify the topic")
|
||||
|
||||
|
||||
@@ -200,7 +200,7 @@ class Defender():
|
||||
|
||||
self.Base.db_update_core_config(self.module_name, self.ModConfig, param_key, param_value)
|
||||
|
||||
def unload(self) -> None:
|
||||
def unload(self, reloading: bool = False) -> None:
|
||||
"""Cette methode sera executée a chaque désactivation ou
|
||||
rechargement de module
|
||||
"""
|
||||
|
||||
@@ -182,7 +182,7 @@ class Jsonrpc():
|
||||
"""
|
||||
self.Base.db_update_core_config(self.module_name, self.ModConfig, param_key, param_value)
|
||||
|
||||
def unload(self) -> None:
|
||||
def unload(self, reloading: bool = False) -> None:
|
||||
if self.UnrealIrcdRpcLive.Error.code != -1:
|
||||
self.UnrealIrcdRpcLive.unsubscribe()
|
||||
return None
|
||||
|
||||
367
mods/mod_nickserv.py
Normal file
367
mods/mod_nickserv.py
Normal file
@@ -0,0 +1,367 @@
|
||||
import socket
|
||||
import json
|
||||
import time
|
||||
import re
|
||||
import psutil
|
||||
import requests
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Union, TYPE_CHECKING
|
||||
from core.classes import user
|
||||
import core.definition as df
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from core.irc import Irc
|
||||
|
||||
|
||||
class Nickserv():
|
||||
|
||||
@dataclass
|
||||
class ModConfModel:
|
||||
active: bool = False
|
||||
|
||||
def __init__(self, ircInstance: 'Irc') -> None:
|
||||
|
||||
# Module name (Mandatory)
|
||||
self.module_name = 'mod_' + str(self.__class__.__name__).lower()
|
||||
|
||||
# Add Irc Object to the module (Mandatory)
|
||||
self.Irc = ircInstance
|
||||
|
||||
# Add Loader Object to the module (Mandatory)
|
||||
self.Loader = ircInstance.Loader
|
||||
|
||||
# Add server protocol Object to the module (Mandatory)
|
||||
self.Protocol = ircInstance.Protocol
|
||||
|
||||
# Add Global Configuration to the module (Mandatory)
|
||||
self.Config = ircInstance.Config
|
||||
|
||||
# Add Base object to the module (Mandatory)
|
||||
self.Base = ircInstance.Base
|
||||
|
||||
# Add logs object to the module (Mandatory)
|
||||
self.Logs = ircInstance.Base.logs
|
||||
|
||||
# Add User object to the module (Mandatory)
|
||||
self.User = ircInstance.User
|
||||
|
||||
# Add Client object to the module (Mandatory)
|
||||
self.Client = ircInstance.Client
|
||||
|
||||
# Add Channel object to the module (Mandatory)
|
||||
self.Channel = ircInstance.Channel
|
||||
|
||||
# Add Reputation object to the module (Optional)
|
||||
self.Reputation = ircInstance.Reputation
|
||||
|
||||
# Check if NickServ already exist
|
||||
# for user_obj in self.User.UID_DB:
|
||||
# if user_obj.nickname.lower() == 'nickserv':
|
||||
# self.Logs.warning(f"The NickServ service already exist, impossible to load 2 NickServ services")
|
||||
# return None
|
||||
|
||||
# Create module commands (Mandatory)
|
||||
self.Irc.build_command(0, self.module_name, 'register', f'Register your nickname /msg NickServ REGISTER <password> <email>')
|
||||
self.Irc.build_command(0, self.module_name, 'identify', f'Identify yourself with your password /msg NickServ IDENTIFY <account> <password>')
|
||||
self.Irc.build_command(0, self.module_name, 'logout', 'Reverse the effect of the identify command')
|
||||
|
||||
# Init the module (Mandatory)
|
||||
self.__init_module()
|
||||
|
||||
# Log the module
|
||||
self.Logs.debug(f'-- Module {self.module_name} loaded ...')
|
||||
|
||||
def __init_module(self) -> None:
|
||||
|
||||
# Create you own tables if needed (Mandatory)
|
||||
self.__create_tables()
|
||||
|
||||
# Load module configuration (Mandatory)
|
||||
self.__load_module_configuration()
|
||||
# End of mandatory methods you can start your customization #
|
||||
|
||||
self.build_nickserv_information()
|
||||
|
||||
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_autoop = '''CREATE TABLE IF NOT EXISTS defender_autoop (
|
||||
# id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
# datetime TEXT,
|
||||
# nickname TEXT,
|
||||
# channel TEXT
|
||||
# )
|
||||
# '''
|
||||
|
||||
# self.Base.db_execute_query(table_autoop)
|
||||
# self.Base.db_execute_query(table_config)
|
||||
# self.Base.db_execute_query(table_trusted)
|
||||
return None
|
||||
|
||||
def __load_module_configuration(self) -> None:
|
||||
"""### Load Module Configuration
|
||||
"""
|
||||
try:
|
||||
# Variable qui va contenir les options de configuration du module Defender
|
||||
self.ModConfig = self.ModConfModel(active=True)
|
||||
|
||||
# Sync the configuration with core configuration (Mandatory)
|
||||
self.Base.db_sync_core_config(self.module_name, self.ModConfig)
|
||||
|
||||
return None
|
||||
|
||||
except TypeError as te:
|
||||
self.Logs.critical(te)
|
||||
|
||||
def __update_configuration(self, param_key: str, param_value: str):
|
||||
|
||||
self.Base.db_update_core_config(self.module_name, self.ModConfig, param_key, param_value)
|
||||
|
||||
def build_nickserv_information(self) -> None:
|
||||
|
||||
self.NICKSERV_NICKNAME = 'NickServ'
|
||||
|
||||
if self.User.is_exist(self.NICKSERV_NICKNAME):
|
||||
# If NickServ already exist just return None
|
||||
self.NICKSERV_UID = self.User.get_uid(self.NICKSERV_NICKNAME)
|
||||
self.Irc.Settings.NICKSERV_UID = self.NICKSERV_UID
|
||||
return None
|
||||
|
||||
self.NICKSERV_UID = self.Config.SERVEUR_ID + self.Base.get_random(6)
|
||||
nickserv_uid = self.NICKSERV_UID
|
||||
self.Irc.Settings.NICKSERV_UID = self.NICKSERV_UID
|
||||
|
||||
self.Protocol.send_uid(
|
||||
nickname=self.NICKSERV_NICKNAME, username='NickServ', hostname='nickserv.deb.biz.st', uid=nickserv_uid, umodes='+ioqBS',
|
||||
vhost='nickserv.deb.biz.st', remote_ip='127.0.0.1', realname='Nick Service'
|
||||
)
|
||||
|
||||
new_user_obj = self.Loader.Definition.MUser(uid=nickserv_uid, nickname=self.NICKSERV_NICKNAME, username='NickServ', realname='Nick Service',
|
||||
hostname='nickserv.deb.biz.st', umodes='+ioqBS', vhost='nickserv.deb.biz.st', remote_ip='127.0.0.1')
|
||||
|
||||
self.User.insert(new_user_obj)
|
||||
self.Protocol.send_sjoin(nickserv_uid, self.Config.SERVICE_CHANLOG)
|
||||
return None
|
||||
|
||||
def timer_force_change_nickname(self, nickname: str) -> None:
|
||||
|
||||
client_obj = self.Client.get_Client(nickname)
|
||||
|
||||
if client_obj is None:
|
||||
self.Protocol.send_svs_nick(nickname, self.Base.get_random(8))
|
||||
|
||||
return None
|
||||
|
||||
def unload(self, reloading: bool = False) -> None:
|
||||
"""Cette methode sera executée a chaque désactivation ou
|
||||
rechargement de module
|
||||
"""
|
||||
if reloading:
|
||||
pass
|
||||
else:
|
||||
# NickServ will be disconnected
|
||||
self.Protocol.send_quit(self.NICKSERV_UID, f'Stopping {self.module_name} module')
|
||||
|
||||
del self.NICKSERV_NICKNAME
|
||||
del self.NICKSERV_UID
|
||||
|
||||
return None
|
||||
|
||||
def cmd(self, data: list[str]) -> None:
|
||||
try:
|
||||
service_id = self.Config.SERVICE_ID # Defender serveur id
|
||||
original_serv_response = list(data).copy()
|
||||
|
||||
parsed_protocol = self.Protocol.parse_server_msg(data.copy())
|
||||
|
||||
match parsed_protocol:
|
||||
|
||||
case 'UID':
|
||||
try:
|
||||
# ['@...', ':001', 'UID', 'adator', '0', '1733865747', '...', '192.168.1.10', '00118YU01', '0', '+iotwxz', 'netadmin.irc.dev.local', '745A32E5.421A4718.F33AB65A.IP', 'wKgBCg==', ':...']
|
||||
get_uid = str(original_serv_response[8])
|
||||
user_obj = self.User.get_User(get_uid)
|
||||
self.Protocol.send_notice(nick_from=self.NICKSERV_NICKNAME, nick_to=user_obj.nickname,
|
||||
msg='NickServ is here to protect your nickname'
|
||||
)
|
||||
self.Protocol.send_notice(nick_from=self.NICKSERV_NICKNAME, nick_to=user_obj.nickname,
|
||||
msg='To register a nickname tape /msg NickServ register password email'
|
||||
)
|
||||
|
||||
if self.Client.db_is_account_exist(user_obj.nickname):
|
||||
self.Protocol.send_notice(nick_from=self.NICKSERV_NICKNAME, nick_to=user_obj.nickname, msg='The nickname is protected please login to keep it')
|
||||
self.Protocol.send_notice(nick_from=self.NICKSERV_NICKNAME, nick_to=user_obj.nickname, msg=f'/msg {self.NICKSERV_NICKNAME} IDENTIFY <ACCOUNT> <PASSWORD>')
|
||||
self.Base.create_timer(60, self.timer_force_change_nickname, (user_obj.nickname, ))
|
||||
|
||||
except IndexError as ie:
|
||||
self.Logs.error(f'cmd reputation: index error: {ie}')
|
||||
|
||||
case None:
|
||||
self.Logs.debug(f"** TO BE HANDLE {original_serv_response} {__name__}")
|
||||
|
||||
except KeyError as ke:
|
||||
self.Logs.error(f"{ke} / {original_serv_response} / length {str(len(original_serv_response))}")
|
||||
except IndexError as ie:
|
||||
self.Logs.error(f"{ie} / {original_serv_response} / length {str(len(original_serv_response))}")
|
||||
except Exception as err:
|
||||
self.Logs.error(f"General Error: {err}")
|
||||
|
||||
def hcmds(self, user: str, channel: str, cmd: list, fullcmd: list = []) -> None:
|
||||
|
||||
command = str(cmd[0]).lower()
|
||||
fromuser = user
|
||||
fromchannel = channel if self.Channel.Is_Channel(channel) else None
|
||||
dnickname = self.Config.SERVICE_NICKNAME # Defender nickname
|
||||
dchanlog = self.Config.SERVICE_CHANLOG # Defender chan log
|
||||
dumodes = self.Config.SERVICE_UMODES # Les modes de Defender
|
||||
service_id = self.Config.SERVICE_ID # Defender serveur id
|
||||
|
||||
match command:
|
||||
|
||||
case 'register':
|
||||
# Register PASSWORD EMAIL
|
||||
try:
|
||||
|
||||
if len(cmd) < 3:
|
||||
self.Protocol.send_notice(
|
||||
nick_from=self.NICKSERV_NICKNAME,
|
||||
nick_to=fromuser,
|
||||
msg=f'/msg {self.NICKSERV_NICKNAME} {command.upper()} <PASSWORD> <EMAIL>'
|
||||
)
|
||||
return None
|
||||
|
||||
password = cmd[1]
|
||||
email = cmd[2]
|
||||
|
||||
if not self.Base.is_valid_email(email_to_control=email):
|
||||
self.Protocol.send_notice(
|
||||
nick_from=self.NICKSERV_NICKNAME,
|
||||
nick_to=fromuser,
|
||||
msg='The email is not valid. You must provide a valid email address (first.name@email.extension)'
|
||||
)
|
||||
return None
|
||||
|
||||
user_obj = self.User.get_User(fromuser)
|
||||
|
||||
if user_obj is None:
|
||||
self.Logs.error(f"Nickname ({fromuser}) doesn't exist, it is impossible to register this nickname")
|
||||
return None
|
||||
|
||||
# If the account already exist.
|
||||
if self.Client.db_is_account_exist(fromuser):
|
||||
self.Protocol.send_notice(
|
||||
nick_from=self.NICKSERV_NICKNAME,
|
||||
nick_to=fromuser,
|
||||
msg=f"Your account already exist, please try to login instead /msg {self.NICKSERV_NICKNAME} IDENTIFY <account> <password>"
|
||||
)
|
||||
return None
|
||||
|
||||
# If the account doesn't exist then insert into database
|
||||
data_to_record = {
|
||||
'createdOn': self.Base.get_datetime(), 'account': fromuser,
|
||||
'nickname': user_obj.nickname, 'hostname': user_obj.hostname, 'vhost': user_obj.vhost, 'realname': user_obj.realname, 'email': email,
|
||||
'password': self.Base.crypt_password(password=password), 'level': 0
|
||||
}
|
||||
|
||||
insert_to_db = self.Base.db_execute_query(f"""
|
||||
INSERT INTO {self.Config.TABLE_CLIENT}
|
||||
(createdOn, account, nickname, hostname, vhost, realname, email, password, level)
|
||||
VALUES
|
||||
(:createdOn, :account, :nickname, :hostname, :vhost, :realname, :email, :password, :level)
|
||||
""", data_to_record)
|
||||
|
||||
if insert_to_db.rowcount > 0:
|
||||
self.Protocol.send_notice(
|
||||
nick_from=self.NICKSERV_NICKNAME,
|
||||
nick_to=fromuser,
|
||||
msg=f"You have register your nickname successfully"
|
||||
)
|
||||
|
||||
return None
|
||||
|
||||
except ValueError as ve:
|
||||
self.Logs.error(f"Value Error : {ve}")
|
||||
self.Protocol.send_notice(nick_from=self.NICKSERV_NICKNAME, nick_to=fromuser, msg=f" {self.Config.SERVICE_PREFIX}{command.upper()} <PASSWORD> <EMAIL>")
|
||||
|
||||
case 'identify':
|
||||
try:
|
||||
# IDENTIFY <ACCOUNT> <PASSWORD>
|
||||
if len(cmd) < 3:
|
||||
self.Protocol.send_notice(
|
||||
nick_from=dnickname,
|
||||
nick_to=fromuser,
|
||||
msg=f'/msg {self.NICKSERV_NICKNAME} {command.upper()} <ACCOUNT> <PASSWORD>'
|
||||
)
|
||||
return None
|
||||
|
||||
account = str(cmd[1]) # account
|
||||
encrypted_password = self.Base.crypt_password(cmd[2])
|
||||
user_obj = self.User.get_User(fromuser)
|
||||
client_obj = self.Client.get_Client(user_obj.uid)
|
||||
|
||||
if client_obj is not None:
|
||||
self.Protocol.send_notice(nick_from=self.NICKSERV_NICKNAME, nick_to=fromuser, msg=f"You are already logged in")
|
||||
return None
|
||||
|
||||
db_query = f"SELECT account FROM {self.Config.TABLE_CLIENT} WHERE account = :account AND password = :password"
|
||||
db_param = {'account': account, 'password': encrypted_password}
|
||||
exec_query = self.Base.db_execute_query(
|
||||
db_query,
|
||||
db_param
|
||||
)
|
||||
result_query = exec_query.fetchone()
|
||||
if result_query:
|
||||
account = result_query[0]
|
||||
self.Protocol.send_notice(nick_from=self.NICKSERV_NICKNAME, nick_to=fromuser, msg=f"You are now logged in")
|
||||
user_obj_dict = self.User.get_User_AsDict(fromuser)
|
||||
client = self.Loader.Definition.MClient(
|
||||
account=account,
|
||||
**user_obj_dict
|
||||
)
|
||||
self.Client.insert(client)
|
||||
self.Protocol.send_svs2mode(service_uid=self.NICKSERV_UID, nickname=fromuser, user_mode='+r')
|
||||
else:
|
||||
self.Protocol.send_notice(nick_from=self.NICKSERV_NICKNAME, nick_to=fromuser, msg=f"Wrong password or account")
|
||||
except TypeError as te:
|
||||
self.Logs.error(f"Type Error -> {te}")
|
||||
except ValueError as ve:
|
||||
self.Logs.error(f"Value Error -> {ve}")
|
||||
|
||||
case 'logout':
|
||||
try:
|
||||
# LOGOUT <account>
|
||||
if len(cmd) < 2:
|
||||
self.Protocol.send_notice(nick_from=self.NICKSERV_NICKNAME, nick_to=fromuser, msg=f"/msg {self.NICKSERV_NICKNAME} {command.upper()} <ACCOUNT>")
|
||||
return None
|
||||
|
||||
user_obj = self.User.get_User(fromuser)
|
||||
if user_obj is None:
|
||||
self.Logs.error(f"The User [{fromuser}] is not available in the database")
|
||||
return None
|
||||
|
||||
client_obj = self.Client.get_Client(user_obj.uid)
|
||||
|
||||
if client_obj is None:
|
||||
self.Protocol.send_notice(nick_from=self.NICKSERV_NICKNAME, nick_to=fromuser, msg="Nothing to logout. please login first")
|
||||
return None
|
||||
|
||||
self.Protocol.send_svs2mode(service_uid=self.NICKSERV_UID, nickname=fromuser, user_mode='-r')
|
||||
self.Client.delete(user_obj.uid)
|
||||
self.Protocol.send_notice(nick_from=self.NICKSERV_NICKNAME, nick_to=fromuser, msg=f"You have been logged out successfully")
|
||||
|
||||
except ValueError as ve:
|
||||
self.Logs.error(f"Value Error: {ve}")
|
||||
self.Protocol.send_notice(nick_from=self.NICKSERV_NICKNAME, nick_to=fromuser, msg=f"/msg {self.NICKSERV_NICKNAME} {command.upper()} <account>")
|
||||
except Exception as err:
|
||||
self.Logs.error(f"General Error: {err}")
|
||||
self.Protocol.send_notice(nick_from=self.NICKSERV_NICKNAME, nick_to=fromuser, msg=f"/msg {self.NICKSERV_NICKNAME} {command.upper()} <account>")
|
||||
@@ -113,7 +113,7 @@ class Test():
|
||||
"""
|
||||
self.Base.db_update_core_config(self.module_name, self.ModConfig, param_key, param_value)
|
||||
|
||||
def unload(self) -> None:
|
||||
def unload(self, reloading: bool = False) -> None:
|
||||
|
||||
return None
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ class Votekick():
|
||||
self.Base.db_execute_query(table_vote)
|
||||
return None
|
||||
|
||||
def unload(self) -> None:
|
||||
def unload(self, reloading: bool = False) -> None:
|
||||
try:
|
||||
for chan in self.VOTE_CHANNEL_DB:
|
||||
self.Protocol.send_part_chan(uidornickname=self.Config.SERVICE_ID, channel=chan.channel_name)
|
||||
|
||||
258
mods/mod_weather.py
Normal file
258
mods/mod_weather.py
Normal file
@@ -0,0 +1,258 @@
|
||||
from urllib import request
|
||||
import requests
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Union, TYPE_CHECKING
|
||||
from core.classes import user
|
||||
import core.definition as df
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from core.irc import Irc
|
||||
|
||||
|
||||
class Weather():
|
||||
|
||||
@dataclass
|
||||
class ModConfModel:
|
||||
active: bool = False
|
||||
|
||||
def __init__(self, ircInstance: 'Irc') -> None:
|
||||
|
||||
# Module name (Mandatory)
|
||||
self.module_name = 'mod_' + str(self.__class__.__name__).lower()
|
||||
|
||||
# Add Irc Object to the module (Mandatory)
|
||||
self.Irc = ircInstance
|
||||
|
||||
# Add Loader Object to the module (Mandatory)
|
||||
self.Loader = ircInstance.Loader
|
||||
|
||||
# Add server protocol Object to the module (Mandatory)
|
||||
self.Protocol = ircInstance.Protocol
|
||||
|
||||
# Add Global Configuration to the module (Mandatory)
|
||||
self.Config = ircInstance.Config
|
||||
|
||||
# Add Base object to the module (Mandatory)
|
||||
self.Base = ircInstance.Base
|
||||
|
||||
# Add logs object to the module (Mandatory)
|
||||
self.Logs = ircInstance.Base.logs
|
||||
|
||||
# Add User object to the module (Mandatory)
|
||||
self.User = ircInstance.User
|
||||
|
||||
# Add Client object to the module (Mandatory)
|
||||
self.Client = ircInstance.Client
|
||||
|
||||
# Add Channel object to the module (Mandatory)
|
||||
self.Channel = ircInstance.Channel
|
||||
|
||||
# Add Reputation object to the module (Optional)
|
||||
self.Reputation = ircInstance.Reputation
|
||||
|
||||
# Create module commands (Mandatory)
|
||||
self.Irc.build_command(0, self.module_name, 'meteo', 'Get the meteo of a current city')
|
||||
|
||||
# Init the module (Mandatory)
|
||||
self.__init_module()
|
||||
|
||||
# Log the module
|
||||
self.Logs.debug(f'-- Module {self.module_name} loaded ...')
|
||||
|
||||
def __init_module(self) -> None:
|
||||
|
||||
# Create you own tables if needed (Mandatory)
|
||||
self.__create_tables()
|
||||
|
||||
# Load module configuration (Mandatory)
|
||||
self.__load_module_configuration()
|
||||
# End of mandatory methods you can start your customization #
|
||||
|
||||
self.from_user = None
|
||||
self.from_channel = None
|
||||
|
||||
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_autoop = '''CREATE TABLE IF NOT EXISTS defender_autoop (
|
||||
# id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
# datetime TEXT,
|
||||
# nickname TEXT,
|
||||
# channel TEXT
|
||||
# )
|
||||
# '''
|
||||
|
||||
# self.Base.db_execute_query(table_autoop)
|
||||
# self.Base.db_execute_query(table_config)
|
||||
# self.Base.db_execute_query(table_trusted)
|
||||
return None
|
||||
|
||||
def __load_module_configuration(self) -> None:
|
||||
"""### Load Module Configuration
|
||||
"""
|
||||
try:
|
||||
# Variable qui va contenir les options de configuration du module Defender
|
||||
self.ModConfig = self.ModConfModel(active=True)
|
||||
|
||||
# Sync the configuration with core configuration (Mandatory)
|
||||
self.Base.db_sync_core_config(self.module_name, self.ModConfig)
|
||||
|
||||
return None
|
||||
|
||||
except TypeError as te:
|
||||
self.Logs.critical(te)
|
||||
|
||||
def __update_configuration(self, param_key: str, param_value: str):
|
||||
|
||||
self.Base.db_update_core_config(self.module_name, self.ModConfig, param_key, param_value)
|
||||
|
||||
def get_geo_information(self, city: str) -> tuple[str, str, float, float]:
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
city (str): The city you want to get
|
||||
|
||||
Returns:
|
||||
tuple[str, str, float, float]: Country Code, City Name Latitude, Longitude
|
||||
"""
|
||||
api_key = 'fd36a3f3715c93f6770a13f5a34ae1e3'
|
||||
geo_api_url = "http://api.openweathermap.org/geo/1.0/direct"
|
||||
|
||||
response = requests.get(
|
||||
url=geo_api_url,
|
||||
params={'q': city, 'limit': 1, 'appid': api_key}
|
||||
)
|
||||
|
||||
geo_data = response.json()
|
||||
if not geo_data:
|
||||
return (None, None, 0, 0)
|
||||
|
||||
country_code = geo_data[0]['country']
|
||||
city_name = geo_data[0]['name']
|
||||
latitude: float = geo_data[0]['lat']
|
||||
longitude: float = geo_data[0]['lon']
|
||||
|
||||
return (country_code, city_name, latitude, longitude)
|
||||
|
||||
def get_meteo_information(self, city: str, to_nickname: str) -> None:
|
||||
|
||||
api_key = 'fd36a3f3715c93f6770a13f5a34ae1e3'
|
||||
meteo_api_url = "https://api.openweathermap.org/data/2.5/weather" #?lat={lat}&lon={lon}&appid={API key}
|
||||
|
||||
country_code, city_name, latitude, longitude = self.get_geo_information(city=city)
|
||||
|
||||
response_meteo = requests.get(
|
||||
url=meteo_api_url,
|
||||
params={'lat': latitude, 'lon': longitude, 'appid': api_key, 'units': "metric"}
|
||||
)
|
||||
|
||||
meteo_data = response_meteo.json()
|
||||
|
||||
if not meteo_data or city_name is None:
|
||||
self.Protocol.send_notice(nick_from=self.Config.SERVICE_NICKNAME, nick_to=to_nickname,
|
||||
msg=f"Impossible to find the meteo for [{city}]")
|
||||
return None
|
||||
|
||||
temp_cur = meteo_data['main']['temp']
|
||||
temp_min = meteo_data['main']['temp_min']
|
||||
temp_max = meteo_data['main']['temp_max']
|
||||
|
||||
if self.from_channel is None:
|
||||
self.Protocol.send_notice(nick_from=self.Config.SERVICE_NICKNAME, nick_to=to_nickname,
|
||||
msg=f"Weather of {city_name} located in {country_code} is:")
|
||||
self.Protocol.send_notice(nick_from=self.Config.SERVICE_NICKNAME, nick_to=to_nickname,
|
||||
msg=f"Current temperature: {temp_cur} °C")
|
||||
self.Protocol.send_notice(nick_from=self.Config.SERVICE_NICKNAME, nick_to=to_nickname,
|
||||
msg=f"Minimum temperature: {temp_min} °C")
|
||||
self.Protocol.send_notice(nick_from=self.Config.SERVICE_NICKNAME, nick_to=to_nickname,
|
||||
msg=f"Maximum temperature: {temp_max} °C")
|
||||
else:
|
||||
self.Protocol.send_priv_msg(nick_from=self.Config.SERVICE_NICKNAME, channel=self.from_channel,
|
||||
msg=f"Weather of {city_name} located in {country_code} is:")
|
||||
self.Protocol.send_priv_msg(nick_from=self.Config.SERVICE_NICKNAME, channel=self.from_channel,
|
||||
msg=f"Current temperature: {temp_cur} °C")
|
||||
self.Protocol.send_priv_msg(nick_from=self.Config.SERVICE_NICKNAME, channel=self.from_channel,
|
||||
msg=f"Minimum temperature: {temp_min} °C")
|
||||
self.Protocol.send_priv_msg(nick_from=self.Config.SERVICE_NICKNAME, channel=self.from_channel,
|
||||
msg=f"Maximum temperature: {temp_max} °C")
|
||||
|
||||
return None
|
||||
|
||||
def unload(self, reloading: bool = False) -> None:
|
||||
"""Cette methode sera executée a chaque désactivation ou
|
||||
rechargement de module
|
||||
"""
|
||||
if reloading:
|
||||
pass
|
||||
else:
|
||||
self.Protocol.send_quit(self.NICKSERV_UID, f'Stopping {self.module_name} module')
|
||||
|
||||
return None
|
||||
|
||||
def cmd(self, data: list[str]) -> None:
|
||||
try:
|
||||
service_id = self.Config.SERVICE_ID # Defender serveur id
|
||||
original_serv_response = list(data).copy()
|
||||
|
||||
parsed_protocol = self.Protocol.parse_server_msg(data.copy())
|
||||
|
||||
match parsed_protocol:
|
||||
|
||||
case 'UID':
|
||||
try:
|
||||
pass
|
||||
|
||||
except IndexError as ie:
|
||||
self.Logs.error(f'cmd reputation: index error: {ie}')
|
||||
|
||||
case None:
|
||||
self.Logs.debug(f"** TO BE HANDLE {original_serv_response} {__name__}")
|
||||
|
||||
except KeyError as ke:
|
||||
self.Logs.error(f"{ke} / {original_serv_response} / length {str(len(original_serv_response))}")
|
||||
except IndexError as ie:
|
||||
self.Logs.error(f"{ie} / {original_serv_response} / length {str(len(original_serv_response))}")
|
||||
except Exception as err:
|
||||
self.Logs.error(f"General Error: {err}")
|
||||
|
||||
def hcmds(self, user: str, channel: any, cmd: list, fullcmd: list = []) -> None:
|
||||
|
||||
command = str(cmd[0]).lower()
|
||||
fromuser = user
|
||||
fromchannel = channel if self.Channel.Is_Channel(channel) else None
|
||||
self.from_channel = fromchannel
|
||||
self.from_user = fromuser
|
||||
dnickname = self.Config.SERVICE_NICKNAME # Defender nickname
|
||||
dchanlog = self.Config.SERVICE_CHANLOG # Defender chan log
|
||||
dumodes = self.Config.SERVICE_UMODES # Les modes de Defender
|
||||
service_id = self.Config.SERVICE_ID # Defender serveur id
|
||||
|
||||
match command:
|
||||
|
||||
case 'meteo':
|
||||
try:
|
||||
# meteo <CITY>
|
||||
if len(cmd) < 2:
|
||||
self.Protocol.send_notice(nick_from=dnickname, nick_to=fromuser, msg=f"/msg {self.Config.SERVICE_NICKNAME} {command.upper()} METEO <City name>")
|
||||
return None
|
||||
|
||||
city = str(' '.join(cmd[1:]))
|
||||
to_nickname = fromuser
|
||||
|
||||
self.Base.create_thread(self.get_meteo_information, (city, to_nickname))
|
||||
|
||||
except TypeError as te:
|
||||
self.Logs.error(f"Type Error -> {te}")
|
||||
except ValueError as ve:
|
||||
self.Logs.error(f"Value Error -> {ve}")
|
||||
|
||||
Reference in New Issue
Block a user