less nonsense

This commit is contained in:
Lain Iwakura 2025-01-10 23:01:50 -03:00
parent 072cb1f823
commit ac7a8896cf
Signed by: lain
GPG key ID: 89686F4239E80508

View file

@ -11,13 +11,13 @@ from mautrix.client import Client as MatrixClient
from mautrix.crypto import attachments
from mautrix.types import EncryptedFile, ImageInfo, MediaMessageEventContent, MessageType
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
from mautrix.errors import MForbidden
class Config(BaseProxyConfig):
def do_update(self, helper: ConfigUpdateHelper) -> None:
helper.copy("rooms")
async def download_encrypted_media(
file: EncryptedFile,
client: MatrixClient) -> bytes:
@ -111,18 +111,17 @@ async def send_unencrypted_message(
await client.send_message(room_id, content)
# BOT
class HateHeifBot(Plugin):
async def start(self) -> None:
await super().start()
self.config.load_and_update()
self.rooms = self.config['rooms'] if self.config['rooms'] else None
@classmethod
def get_config_class(cls) -> Type[BaseProxyConfig]:
return Config
@command.passive("", msgtypes=(MessageType.IMAGE,MessageType.FILE))
@command.passive("", msgtypes=(MessageType.IMAGE, MessageType.FILE))
async def hate_heif_message(
self,
evt: MessageEvent,
@ -136,24 +135,22 @@ class HateHeifBot(Plugin):
self.log.debug(f"Current room {evt.room_id} is not listed in {self.rooms} so I ignore it.")
return
# Double check if it is an image message
if evt.content.msgtype != MessageType.IMAGE and evt.content.msgtype != MessageType.FILE:
self.log.debug(f"Received message with an image with MIME: {evt.content.info.mimetype}")
if evt.content.info.mimetype not in ("image/heic", "image/heif"):
return
content: MediaMessageEventContent = evt.content
self.log.debug(f"Received message with an image with MIME: {content.info.mimetype}")
# We work only on "image/heic" mime at the moment!
if content.info.mimetype not in ("image/heic", "image/heif"):
return
if content.url: # content.url exists. File is not encrypted.
try:
await evt.redact(reason="Image converted to less appleish format bellow")
data = await download_unencrypted_media(content.url, evt.client)
except MForbidden:
self.log.error("No permission to redact!")
return
if evt.content.url: # url exists. File is not encrypted.
data = await download_unencrypted_media(evt.content.url, evt.client)
is_enc = False
elif content.file: # content.file exists. File is encrypted.
await client.redact(evt.room_id, evt.event_id, reason="Image converted to less appleish format bellow")
data = await download_encrypted_media(content.file, evt.client)
elif evt.content.file: # file exists. File is encrypted.
data = await download_encrypted_media(evt.content.file, evt.client)
is_enc = True
else: # shouldn't happen
self.log.warning("A message with IMAGE type received, but it does not contain a file.")
@ -168,8 +165,8 @@ class HateHeifBot(Plugin):
img = img_out.getvalue()
img_tst = Image.open(BytesIO(img))
self.log.debug(f"Created image parameters: {img_tst.format} {img_tst.size} {img_tst.mode}")
content.info.width=img_tst.size[0]
content.info.height=img_tst.size[1]
evt.content.info.width=img_tst.size[0]
evt.content.info.height=img_tst.size[1]
if is_enc:
img_enc = attachments.encrypt_attachment(img)
@ -177,18 +174,14 @@ class HateHeifBot(Plugin):
await send_encrypted_message(
img_enc,
evt.room_id,
content.info,
evt.content.info,
evt.client
)
else:
await send_unencrypted_message(
img,
evt.room_id,
content.info,
evt.content.info,
evt.client
)
@classmethod
def get_config_class(cls) -> Type[BaseProxyConfig]:
return Config
# the end.