From 78a5ac9939527a1a546752f91f85c1d65421adc3 Mon Sep 17 00:00:00 2001 From: Romain J Date: Tue, 2 Mar 2021 19:00:08 +0100 Subject: [PATCH] fix(commands:iplocalise|Network): change regex for domain validation --- tuxbot/cogs/Admin/locales/messages.pot | 2 +- tuxbot/cogs/Custom/locales/messages.pot | 6 +- tuxbot/cogs/Logs/locales/messages.pot | 10 ++- tuxbot/cogs/Logs/logs.py | 5 +- tuxbot/cogs/Network/functions/consts.py | 3 + tuxbot/cogs/Network/functions/converters.py | 59 ++---------------- tuxbot/cogs/Network/functions/exceptions.py | 14 +++-- tuxbot/cogs/Network/functions/utils.py | 67 +++++++++++++++++++-- tuxbot/cogs/Network/locales/fr-FR.po | 31 +++++----- tuxbot/cogs/Network/locales/messages.pot | 51 +++++++--------- tuxbot/cogs/Network/network.py | 39 ++++++------ tuxbot/cogs/Polls/locales/messages.pot | 2 +- tuxbot/cogs/Utils/locales/messages.pot | 2 +- 13 files changed, 157 insertions(+), 134 deletions(-) create mode 100644 tuxbot/cogs/Network/functions/consts.py diff --git a/tuxbot/cogs/Admin/locales/messages.pot b/tuxbot/cogs/Admin/locales/messages.pot index 7cc7274..827fcdc 100644 --- a/tuxbot/cogs/Admin/locales/messages.pot +++ b/tuxbot/cogs/Admin/locales/messages.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Tuxbot-bot\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n" -"POT-Creation-Date: 2021-01-26 16:12+0100\n" +"POT-Creation-Date: 2021-03-01 14:59+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/tuxbot/cogs/Custom/locales/messages.pot b/tuxbot/cogs/Custom/locales/messages.pot index 431ee70..198e7b9 100644 --- a/tuxbot/cogs/Custom/locales/messages.pot +++ b/tuxbot/cogs/Custom/locales/messages.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Tuxbot-bot\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n" -"POT-Creation-Date: 2021-01-26 16:12+0100\n" +"POT-Creation-Date: 2021-03-01 14:59+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -26,12 +26,12 @@ msgstr "" msgid "List of available locales: " msgstr "" -#: tuxbot/cogs/Custom/custom.py:89 +#: tuxbot/cogs/Custom/custom.py:93 #, python-brace-format msgid "The alias `{alias}` is already defined for the command `{command}`" msgstr "" -#: tuxbot/cogs/Custom/custom.py:102 +#: tuxbot/cogs/Custom/custom.py:106 #, python-brace-format msgid "The alias `{alias}` for the command `{command}` was successfully created" msgstr "" diff --git a/tuxbot/cogs/Logs/locales/messages.pot b/tuxbot/cogs/Logs/locales/messages.pot index cf3d625..7ddb391 100644 --- a/tuxbot/cogs/Logs/locales/messages.pot +++ b/tuxbot/cogs/Logs/locales/messages.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Tuxbot-bot\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n" -"POT-Creation-Date: 2021-01-26 16:12+0100\n" +"POT-Creation-Date: 2021-03-01 14:59+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -17,10 +17,14 @@ msgstr "" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" -#: tuxbot/cogs/Logs/logs.py:295 +#: tuxbot/cogs/Logs/logs.py:274 +msgid "```An error occurred, the bot owner has been advertised...```" +msgstr "" + +#: tuxbot/cogs/Logs/logs.py:333 msgid "Sockets stats" msgstr "" -#: tuxbot/cogs/Logs/logs.py:297 +#: tuxbot/cogs/Logs/logs.py:335 msgid "{} socket events observed ({:.2f}/minute):" msgstr "" diff --git a/tuxbot/cogs/Logs/logs.py b/tuxbot/cogs/Logs/logs.py index 07993cd..fbfed45 100644 --- a/tuxbot/cogs/Logs/logs.py +++ b/tuxbot/cogs/Logs/logs.py @@ -126,7 +126,10 @@ class Logs(commands.Cog, name="Logs"): emoji = types.get(record.levelname, ":heavy_multiplication_x:") dt = datetime.datetime.utcfromtimestamp(record.created) - msg = f"{emoji} `[{dt:%Y-%m-%d %H:%M:%S}] {await shorten(self.bot.session, record.msg, 1500)}`" + msg = ( + f"{emoji} `[{dt:%Y-%m-%d %H:%M:%S}] " + f"{await shorten(self.bot.session, record.msg, 1500)}`" + ) await self.webhook("gateway").send(msg) def clear_gateway_data(self): diff --git a/tuxbot/cogs/Network/functions/consts.py b/tuxbot/cogs/Network/functions/consts.py new file mode 100644 index 0000000..f97b8ef --- /dev/null +++ b/tuxbot/cogs/Network/functions/consts.py @@ -0,0 +1,3 @@ +DOMAIN_PATTERN = r"^(((.*)+)*\.)+[a-z\-]{2,}\.?$" +IPV4_PATTERN = r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" +IPV6_PATTERN = r"^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))" diff --git a/tuxbot/cogs/Network/functions/converters.py b/tuxbot/cogs/Network/functions/converters.py index acda2ac..d480c3f 100644 --- a/tuxbot/cogs/Network/functions/converters.py +++ b/tuxbot/cogs/Network/functions/converters.py @@ -13,27 +13,18 @@ def _(x): return x -DOMAIN_PATTERN = r"^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$" -IPV4_PATTERN = r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" -IPV6_PATTERN = r"^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))" - - class IPConverter(commands.Converter): async def convert(self, ctx, argument): # skipcq: PYL-W0613 argument = argument.replace("http://", "").replace("https://", "") argument = argument.rstrip("/") - check_domain = re.match(DOMAIN_PATTERN, argument) - check_ipv4 = re.match(IPV4_PATTERN, argument) - check_ipv6 = re.match(IPV6_PATTERN, argument) + if argument.startswith("`") and argument.endswith("`"): + argument = argument.lstrip("`").rstrip("`") - if check_domain or check_ipv4 or check_ipv6: - return argument - - raise InvalidIp(_("Invalid ip or domain")) + return argument.lower() -class IPCheckerConverter(commands.Converter): +class DomainConverter(commands.Converter): async def convert(self, ctx, argument): # skipcq: PYL-W0613 if not argument.startswith("http"): return f"http://{argument}" @@ -41,42 +32,9 @@ class IPCheckerConverter(commands.Converter): return argument -class DomainCheckerConverter(commands.Converter): - async def convert(self, ctx, argument): # skipcq: PYL-W0613 - argument = argument.replace("http://", "").replace("https://", "") - - check_domain = re.match(DOMAIN_PATTERN, argument) - - if check_domain: - return argument - - raise InvalidDomain(_("Invalid domain")) - - class QueryTypeConverter(commands.Converter): async def convert(self, ctx, argument): # skipcq: PYL-W0613 - argument = argument.lower() - query_types = [ - "a", - "aaaa", - "cname", - "ns", - "ds", - "dnskey", - "soa", - "txt", - "ptr", - "mx", - ] - - if argument in query_types: - return argument - - raise InvalidQueryType( - _( - "Supported queries : A, AAAA, CNAME, NS, DS, DNSKEY, SOA, TXT, PTR, MX" - ) - ) + return argument.lower() class IPVersionConverter(commands.Converter): @@ -84,9 +42,4 @@ class IPVersionConverter(commands.Converter): if not argument: return argument - argument = argument.replace("-", "").replace("ip", "").replace("v", "") - - if argument not in ["4", "6"]: - raise InvalidIp(_("Invalid ip version")) - - return argument + return argument.replace("-", "").replace("ip", "").replace("v", "") diff --git a/tuxbot/cogs/Network/functions/exceptions.py b/tuxbot/cogs/Network/functions/exceptions.py index fce718a..94e950f 100644 --- a/tuxbot/cogs/Network/functions/exceptions.py +++ b/tuxbot/cogs/Network/functions/exceptions.py @@ -1,21 +1,25 @@ from discord.ext import commands -class RFC18(commands.UserNotFound): +class NetworkException(commands.BadArgument): pass -class InvalidIp(commands.BadArgument): +class RFC18(NetworkException): pass -class InvalidDomain(commands.BadArgument): +class InvalidIp(NetworkException): pass -class InvalidQueryType(commands.BadArgument): +class InvalidDomain(NetworkException): pass -class VersionNotFound(commands.BadArgument): +class InvalidQueryType(NetworkException): + pass + + +class VersionNotFound(NetworkException): pass diff --git a/tuxbot/cogs/Network/functions/utils.py b/tuxbot/cogs/Network/functions/utils.py index 1c0d69b..5330f64 100644 --- a/tuxbot/cogs/Network/functions/utils.py +++ b/tuxbot/cogs/Network/functions/utils.py @@ -1,6 +1,8 @@ +import re import socket from typing import Union, NoReturn +import discord import ipinfo import ipwhois import pydig @@ -8,15 +10,22 @@ from ipinfo.exceptions import RequestQuotaExceededError from ipwhois import Net from ipwhois.asn import IPASN +from tuxbot.cogs.Network.functions.consts import DOMAIN_PATTERN, IPV4_PATTERN, \ + IPV6_PATTERN -from tuxbot.cogs.Network.functions.exceptions import VersionNotFound, RFC18 +from tuxbot.cogs.Network.functions.exceptions import ( + VersionNotFound, + RFC18, + InvalidIp, + InvalidQueryType, +) def _(x): return x -async def get_ip(ip: str, inet: str = "") -> str: +async def get_ip(ip: str, inet: str = "", tmp: discord.Message = None) -> str: if inet == "6": inet = socket.AF_INET6 elif inet == "4": @@ -27,6 +36,9 @@ async def get_ip(ip: str, inet: str = "") -> str: try: return socket.getaddrinfo(str(ip), None, inet)[1][4][0] except socket.gaierror as e: + if tmp: + await tmp.delete() + raise VersionNotFound( _( "Impossible to collect information on this in the given " @@ -35,14 +47,14 @@ async def get_ip(ip: str, inet: str = "") -> str: ) from e -def get_hostname(ip: str) -> str: +async def get_hostname(ip: str) -> str: try: return socket.gethostbyaddr(ip)[0] except socket.herror: return "N/A" -def get_ipwhois_result(ip_address: str) -> Union[NoReturn, dict]: +async def get_ipwhois_result(ip_address: str, tmp: discord.Message = None) -> Union[NoReturn, dict]: try: net = Net(ip_address) obj = IPASN(net) @@ -50,6 +62,9 @@ def get_ipwhois_result(ip_address: str) -> Union[NoReturn, dict]: except ipwhois.exceptions.ASNRegistryError: return {} except ipwhois.exceptions.IPDefinedError as e: + if tmp: + await tmp.delete() + raise RFC18( _( "IP address {ip_address} is already defined as Private-Use" @@ -101,7 +116,7 @@ def merge_ipinfo_ipwhois(ipinfo_result: dict, ipwhois_result: dict) -> dict: return output -def get_pydig_result( +async def get_pydig_result( domain: str, query_type: str, dnssec: Union[str, bool] ) -> list: additional_args = [] if dnssec is False else ["+dnssec"] @@ -115,3 +130,45 @@ def get_pydig_result( ) return resolver.query(domain, query_type) + + +def check_ip_or_raise(ip: str) -> Union[bool, NoReturn]: + check_domain = re.match(DOMAIN_PATTERN, ip) + check_ipv4 = re.match(IPV4_PATTERN, ip) + check_ipv6 = re.match(IPV6_PATTERN, ip) + + if check_domain or check_ipv4 or check_ipv6: + return True + + raise InvalidIp(_("Invalid ip or domain")) + + +def check_ip_version_or_raise(version: str) -> Union[bool, NoReturn]: + if version in ["4", "6", ""]: + return True + + raise InvalidIp(_("Invalid ip version")) + + +def check_query_type_or_raise(query_type: str) -> Union[bool, NoReturn]: + query_types = [ + "a", + "aaaa", + "cname", + "ns", + "ds", + "dnskey", + "soa", + "txt", + "ptr", + "mx", + ] + + if query_type in query_types: + return True + + raise InvalidQueryType( + _( + "Supported queries : A, AAAA, CNAME, NS, DS, DNSKEY, SOA, TXT, PTR, MX" + ) + ) diff --git a/tuxbot/cogs/Network/locales/fr-FR.po b/tuxbot/cogs/Network/locales/fr-FR.po index b7494de..da19423 100644 --- a/tuxbot/cogs/Network/locales/fr-FR.po +++ b/tuxbot/cogs/Network/locales/fr-FR.po @@ -18,62 +18,61 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n" -#: tuxbot/cogs/Network/functions/converters.py:32 -#: tuxbot/cogs/Network/functions/converters.py:52 +#: tuxbot/cogs/Network/functions/utils.py:39 msgid "Invalid ip or domain" msgstr "Nom de domaine ou adresse IP invalide" -#: tuxbot/cogs/Network/functions/converters.py:64 +#: tuxbot/cogs/Network/functions/utils.py:62 +#, python-brace-format msgid "Invalid domain" msgstr "Domaine invalide" -#: tuxbot/cogs/Network/functions/converters.py:88 +#: tuxbot/cogs/Network/functions/utils.py:135 msgid "Supported queries : A, AAAA, CNAME, NS, DS, DNSKEY, SOA, TXT, PTR, MX" msgstr "Requêtes supportées : A, AAAA, CNAME, NS, DS, DNSKEY, SOA, TXT, PTR, MX" -#: tuxbot/cogs/Network/functions/converters.py:101 +#: tuxbot/cogs/Network/functions/utils.py:142 msgid "Invalid ip version" msgstr "Version d'adresse IP invalide" -#: tuxbot/cogs/Network/functions/utils.py:32 +#: tuxbot/cogs/Network/functions/utils.py:164 msgid "Impossible to collect information on this in the given version" msgstr "Impossible de collecter des informations pour cette IP avec la version donnée" -#: tuxbot/cogs/Network/functions/utils.py:55 -#, python-brace-format +#: tuxbot/cogs/Network/functions/utils.py:164 msgid "IP address {ip_address} is already defined as Private-Use Networks via RFC 1918." msgstr "L'adresse ip {ip_address} est est reservée à un usage local selon la RFC 1918" -#: tuxbot/cogs/Network/network.py:89 +#: tuxbot/cogs/Network/network.py:94 msgid "*Retrieving information...*" msgstr "*Récupération des informations...*" -#: tuxbot/cogs/Network/network.py:107 +#: tuxbot/cogs/Network/network.py:112 #, python-brace-format msgid "Information for ``{ip} ({ip_address})``" msgstr "Informations pour ``{ip} ({ip_address})``" -#: tuxbot/cogs/Network/network.py:113 +#: tuxbot/cogs/Network/network.py:118 msgid "Belongs to:" msgstr "Appartient à :" -#: tuxbot/cogs/Network/network.py:123 +#: tuxbot/cogs/Network/network.py:128 msgid "Region:" msgstr "Région :" -#: tuxbot/cogs/Network/network.py:131 +#: tuxbot/cogs/Network/network.py:136 #, python-brace-format msgid "Hostname: {hostname}" msgstr "Nom d'hôte : {hostname}" -#: tuxbot/cogs/Network/network.py:161 +#: tuxbot/cogs/Network/network.py:180 msgid "[show all]({})" msgstr "[tout afficher]({})" -#: tuxbot/cogs/Network/network.py:171 +#: tuxbot/cogs/Network/network.py:190 msgid "Cannot connect to host {}" msgstr "Impossible de se connecter à l'hôte {}" -#: tuxbot/cogs/Network/network.py:195 +#: tuxbot/cogs/Network/network.py:218 msgid "No result..." msgstr "Aucun résultat..." diff --git a/tuxbot/cogs/Network/locales/messages.pot b/tuxbot/cogs/Network/locales/messages.pot index 9331503..d9333e4 100644 --- a/tuxbot/cogs/Network/locales/messages.pot +++ b/tuxbot/cogs/Network/locales/messages.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Tuxbot-bot\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n" -"POT-Creation-Date: 2021-01-26 16:12+0100\n" +"POT-Creation-Date: 2021-03-01 14:59+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -17,62 +17,57 @@ msgstr "" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" -#: tuxbot/cogs/Network/functions/converters.py:32 -#: tuxbot/cogs/Network/functions/converters.py:52 -msgid "Invalid ip or domain" -msgstr "" - -#: tuxbot/cogs/Network/functions/converters.py:64 -msgid "Invalid domain" -msgstr "" - -#: tuxbot/cogs/Network/functions/converters.py:88 -msgid "Supported queries : A, AAAA, CNAME, NS, DS, DNSKEY, SOA, TXT, PTR, MX" -msgstr "" - -#: tuxbot/cogs/Network/functions/converters.py:101 -msgid "Invalid ip version" -msgstr "" - -#: tuxbot/cogs/Network/functions/utils.py:32 +#: tuxbot/cogs/Network/functions/utils.py:39 msgid "Impossible to collect information on this in the given version" msgstr "" -#: tuxbot/cogs/Network/functions/utils.py:55 +#: tuxbot/cogs/Network/functions/utils.py:62 #, python-brace-format msgid "IP address {ip_address} is already defined as Private-Use Networks via RFC 1918." msgstr "" -#: tuxbot/cogs/Network/network.py:89 +#: tuxbot/cogs/Network/functions/utils.py:135 +msgid "Invalid ip or domain" +msgstr "" + +#: tuxbot/cogs/Network/functions/utils.py:142 +msgid "Invalid ip version" +msgstr "" + +#: tuxbot/cogs/Network/functions/utils.py:164 +msgid "Supported queries : A, AAAA, CNAME, NS, DS, DNSKEY, SOA, TXT, PTR, MX" +msgstr "" + +#: tuxbot/cogs/Network/network.py:94 msgid "*Retrieving information...*" msgstr "" -#: tuxbot/cogs/Network/network.py:107 +#: tuxbot/cogs/Network/network.py:112 #, python-brace-format msgid "Information for ``{ip} ({ip_address})``" msgstr "" -#: tuxbot/cogs/Network/network.py:113 +#: tuxbot/cogs/Network/network.py:118 msgid "Belongs to:" msgstr "" -#: tuxbot/cogs/Network/network.py:123 +#: tuxbot/cogs/Network/network.py:128 msgid "Region:" msgstr "" -#: tuxbot/cogs/Network/network.py:131 +#: tuxbot/cogs/Network/network.py:136 #, python-brace-format msgid "Hostname: {hostname}" msgstr "" -#: tuxbot/cogs/Network/network.py:161 +#: tuxbot/cogs/Network/network.py:180 msgid "[show all]({})" msgstr "" -#: tuxbot/cogs/Network/network.py:171 +#: tuxbot/cogs/Network/network.py:190 msgid "Cannot connect to host {}" msgstr "" -#: tuxbot/cogs/Network/network.py:195 +#: tuxbot/cogs/Network/network.py:218 msgid "No result..." msgstr "" diff --git a/tuxbot/cogs/Network/network.py b/tuxbot/cogs/Network/network.py index ec11f73..531ce85 100644 --- a/tuxbot/cogs/Network/network.py +++ b/tuxbot/cogs/Network/network.py @@ -13,8 +13,7 @@ from structured_config import ConfigFile from tuxbot.cogs.Network.functions.converters import ( IPConverter, IPVersionConverter, - IPCheckerConverter, - DomainCheckerConverter, + DomainConverter, QueryTypeConverter, ) from tuxbot.cogs.Network.functions.exceptions import ( @@ -42,6 +41,9 @@ from .functions.utils import ( get_ipwhois_result, merge_ipinfo_ipwhois, get_pydig_result, + check_ip_or_raise, + check_query_type_or_raise, + check_ip_version_or_raise, ) log = logging.getLogger("tuxbot.cogs.Network") @@ -49,8 +51,6 @@ _ = Translator("Network", __file__) class Network(commands.Cog, name="Network"): - _tmp: discord.Message - def __init__(self, bot: Tux): self.bot = bot self.__config: NetworkConfig = ConfigFile( @@ -59,6 +59,9 @@ class Network(commands.Cog, name="Network"): ).config async def cog_command_error(self, ctx, error): + self.bot.console.log("Pass here") + self.bot.console.log(error) + self.bot.console.log(type(error)) if isinstance( error, ( @@ -70,8 +73,7 @@ class Network(commands.Cog, name="Network"): VersionNotFound, ), ): - if self._tmp: - await self._tmp.delete() + self.bot.console.log("Pass there") await ctx.send(_(str(error), ctx, self.bot.config)) @@ -85,20 +87,21 @@ class Network(commands.Cog, name="Network"): ip: IPConverter, version: IPVersionConverter = "", ): - self._tmp = await ctx.send( + check_ip_or_raise(str(ip)) + check_ip_version_or_raise(str(version)) + + tmp = await ctx.send( _("*Retrieving information...*", ctx, self.bot.config), deletable=False, ) - ip_address = await get_ip(str(ip), str(version)) - ip_hostname = get_hostname(ip_address) + ip_address = await get_ip(str(ip), str(version), tmp) + ip_hostname = await get_hostname(ip_address) ipinfo_result = await get_ipinfo_result( self.__config.ipinfoKey, ip_address ) - ipwhois_result = await self.bot.loop.run_in_executor( - None, functools.partial(get_ipwhois_result, ip_address) - ) + ipwhois_result = await get_ipwhois_result(ip_address, tmp) merged_results = merge_ipinfo_ipwhois(ipinfo_result, ipwhois_result) @@ -133,12 +136,12 @@ class Network(commands.Cog, name="Network"): ), ) - await self._tmp.delete() + await tmp.delete() await ctx.send(embed=e) @command_extra(name="getheaders", aliases=["headers"], deletable=True) async def _getheaders( - self, ctx: ContextPlus, ip: IPCheckerConverter, *, user_agent: str = "" + self, ctx: ContextPlus, ip: DomainConverter, *, user_agent: str = "" ): try: headers = {"User-Agent": user_agent} @@ -189,10 +192,12 @@ class Network(commands.Cog, name="Network"): async def _dig( self, ctx: ContextPlus, - domain: DomainCheckerConverter, + domain: IPConverter, query_type: QueryTypeConverter, dnssec: Union[str, bool] = False, ): + check_query_type_or_raise(str(query_type)) + pydig_result = await self.bot.loop.run_in_executor( None, functools.partial(get_pydig_result, domain, query_type, dnssec), @@ -200,9 +205,9 @@ class Network(commands.Cog, name="Network"): e = discord.Embed(title=f"DIG {domain} {query_type}", color=0x5858D7) - for value in pydig_result: + for i, value in enumerate(pydig_result): e.add_field( - name=f"DIG {domain} IN {query_type}", value=f"```{value}```" + name=f"#{i}", value=f"```{value}```" ) if not pydig_result: diff --git a/tuxbot/cogs/Polls/locales/messages.pot b/tuxbot/cogs/Polls/locales/messages.pot index f6bd62b..2fe574a 100644 --- a/tuxbot/cogs/Polls/locales/messages.pot +++ b/tuxbot/cogs/Polls/locales/messages.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Tuxbot-bot\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n" -"POT-Creation-Date: 2021-01-26 16:12+0100\n" +"POT-Creation-Date: 2021-03-01 14:59+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/tuxbot/cogs/Utils/locales/messages.pot b/tuxbot/cogs/Utils/locales/messages.pot index 1a8b586..a87ee75 100644 --- a/tuxbot/cogs/Utils/locales/messages.pot +++ b/tuxbot/cogs/Utils/locales/messages.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Tuxbot-bot\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n" -"POT-Creation-Date: 2021-01-26 16:12+0100\n" +"POT-Creation-Date: 2021-03-01 14:59+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n"