jarvis-bot/jarvis/cogs/dev.py

154 lines
4.8 KiB
Python

import jarvis
import hashlib
import uuid
import re
import ulid
from discord.ext import commands
from jarvis.utils import build_embed, convert_bytesize
from jarvis.utils.field import Field
from bson import ObjectId
supported_hashes = {
x for x in hashlib.algorithms_guaranteed if "shake" not in x
}
OID_VERIFY = re.compile(r"^([1-9][0-9]{0,3}|0)(\.([1-9][0-9]{0,3}|0)){5,13}$")
URL_VERIFY = re.compile(
r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]"
+ r"|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
)
DN_VERIFY = re.compile(
r"^(?:(?P<cn>CN=(?P<name>[^,]*)),)"
+ r"?(?:(?P<path>(?:(?:CN|OU)=[^,]+,?)+),)"
+ r"?(?P<domain>(?:DC=[^,]+,?)+)$"
)
ULID_VERIFY = re.compile(r"^[0-9a-z]{26}$", re.IGNORECASE)
UUID_VERIFY = re.compile(
(
"[a-f0-9]{8}-"
+ "[a-f0-9]{4}-"
+ "[1-5]"
+ "[a-f0-9]{3}-"
+ "[89ab][a-f0-9]{3}-"
+ "[a-f0-9]{12}$"
),
re.IGNORECASE,
)
UUID_GET = {3: uuid.uuid3, 5: uuid.uuid5}
def hash_obj(hash, data, text: bool = True) -> str:
"""
Hash data with hash object
Data can be text or binary
"""
if text:
hash.update(data.encode("UTF-8"))
return hash.hexdigest()
BSIZE = 65536
block_idx = 0
while block_idx * BSIZE < len(data):
block = data[BSIZE * block_idx : BSIZE * (block_idx + 1)]
hash.update(block)
block_idx += 1
return hash.hexdigest()
class DevCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command(name="hash")
async def _hash(self, ctx, method: str, *, data: str = None):
if method not in supported_hashes:
algo_txt = ", ".join(f"`{x}`" for x in supported_hashes)
await ctx.send(
"Unsupported hash algorithm. Supported:\n" + algo_txt
)
return
if not data and len(ctx.message.attachments) == 0:
await ctx.send(
"No data to hash. Either attach a file or send text to hash"
)
return
text = True
if len(ctx.message.attachments) > 0:
text = False
data = await ctx.message.attachments[0].read()
# Default to sha256, just in case
hash = getattr(hashlib, method, hashlib.sha256)()
hex = hash_obj(hash, data, text)
data_size = convert_bytesize(len(data))
title = data if text else ctx.message.attachments[0].filename
description = "Hashed using " + method
fields = [
Field("Data Size", data_size, False),
Field("Hash", f"`{hex}`", False),
]
embed = build_embed(
title=title, description=description, fields=fields
)
await ctx.send(embed=embed)
@commands.command(name="uuid")
async def _uuid(self, ctx, version: str = None, data: str = None):
if not version:
await ctx.send("Supported UUID versions: 3, 4, 5")
return
if version not in ["3", "4", "5"]:
await ctx.send("Supported UUID versions: 3, 4, 5")
return
version = int(version)
if version in [3, 5] and not data:
await ctx.send(f"UUID{version} requires data.")
return
if version == 4:
await ctx.send(f"UUID4: `{uuid.uuid4()}`")
else:
to_send = None
if OID_VERIFY.match(data):
to_send = UUID_GET[version](uuid.NAMESPACE_OID, data)
elif URL_VERIFY.match(data):
to_send = UUID_GET[version](uuid.NAMESPACE_URL, data)
elif DN_VERIFY.match(data):
to_send = UUID_GET[version](uuid.NAMESPACE_X500, data)
else:
to_send = UUID_GET[version](uuid.NAMESPACE_DNS, data)
await ctx.send(f"UUID{version}: `{to_send}`")
@commands.command(name="objectid")
async def _objectid(self, ctx: commands.Context):
"""Generates new bson.ObjectId"""
await ctx.send(f"ObjectId: `{str(ObjectId())}`")
@commands.command(name="ulid")
async def _ulid(self, ctx: commands.Context):
"""Generates a new ULID"""
await ctx.send(f"ULID: `{ulid.new().str}`")
@commands.command(name="uuid2ulid")
async def _uuid2ulid(self, ctx: commands.Context, u):
"""Converts a UUID to a ULID"""
if UUID_VERIFY.match(u):
u = ulid.parse(u)
await ctx.send(f"ULID: `{u.str}`")
else:
await ctx.send("Invalid UUID")
@commands.command(name="ulid2uuid")
async def _ulid2uuid(self, ctx: commands.Context, u):
"""Converts a ULID to a UUID"""
if ULID_VERIFY.match(u):
u = ulid.parse(u)
await ctx.send(f"UUID: `{u.uuid}`")
else:
await ctx.send("Invalid ULID.")
def setup(bot):
bot.add_cog(DevCog(bot))