Jonny Rimkus: 25 dev: add "build tdlib_json" container move fix mime type suffix to core core: fix BaseGateway.send() dev: add "build tdlib_json" container core: fix BaseGateway.send() readme: pip → pipx continue development readme: pip → pipx continue development file suffix fixer first shot file suffix fixer first shot use new file_path apply suggestions use new file_path apply suggestions fix suggested changes fix suggested changes remove obsolete import remove obsolete import rename file_name instead of file_path rename file_name instead of file_path telegram: LegacyRoster: raise if user does not exists telegram: LegacyRoster: raise if user does not exists telegram: create private chat if it does not exist telegram: create private chat if it does not exist 43 files changed, 427 insertions(+), 171 deletions(-)
So, I think I got something working here: https://git.sr.ht/~nicoco/slidge/commit/a32355000d88 Let me know what you think. -- Nicolas
slidge/patches: FAILED in 2m21s [dev: add "build tdlib_json" container][0] from [Jonny Rimkus][1] [0]: https://lists.sr.ht/~nicoco/public-inbox/patches/38616 [1]: mailto:jonny@rimkus.it ✗ #932376 FAILED slidge/patches/debian.yml https://builds.sr.ht/~nicoco/job/932376 ✗ #932374 FAILED slidge/patches/ci.yml https://builds.sr.ht/~nicoco/job/932374 ✗ #932377 FAILED slidge/patches/whatsapp.yml https://builds.sr.ht/~nicoco/job/932377 ✗ #932375 FAILED slidge/patches/debian-arm.yml https://builds.sr.ht/~nicoco/job/932375
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~nicoco/public-inbox/patches/38616/mbox | git am -3Learn more about email & git
From: nicoco <nicoco@nicoco.fr> just to have a platform-independent reference on how to build this crap --- dev/Containerfile-tdlib | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 dev/Containerfile-tdlib diff --git a/dev/Containerfile-tdlib b/dev/Containerfile-tdlib new file mode 100644 index 0000000..486142e --- /dev/null +++ b/dev/Containerfile-tdlib @@ -0,0 +1,21 @@ +# container to build tdlib. example usage: +# docker buildx build --platform linux/i386 --file tdlib-build-dockerfile . -o /tmp/tdlib + +FROM docker.io/library/debian:stable AS builder-tdlib + +# everything telegram/tdlib-specific would be improved, ie removed, if we fixed +# https://github.com/pylakey/aiotdlib/issues/50 + +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt update +RUN apt install -y git g++ cmake zlib1g-dev gperf libssl-dev +RUN git clone https://github.com/pylakey/td --depth 1 +RUN mkdir td/build +WORKDIR td/build +RUN cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX:PATH=/tmp/tdlib/ -DTD_ENABLE_LTO=ON .. +RUN CMAKE_BUILD_PARALLEL_LEVEL=$(grep -c processor /proc/cpuinfo) cmake --build . --target install +RUN ls -la /tmp/tdlib/lib + +FROM scratch AS tdlib +COPY --from=builder-tdlib /tmp/tdlib/lib / -- 2.34.1
From: Jonny Rimkus <jonny.rimkus@hdi.de> --- slidge/core/config.py | 8 ++++++++ slidge/plugins/whatsapp/config.py | 9 --------- slidge/plugins/whatsapp/session.py | 11 +---------- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/slidge/core/config.py b/slidge/core/config.py index 4ced8b0..7e4a59e 100644 --- a/slidge/core/config.py +++ b/slidge/core/config.py @@ -155,3 +155,11 @@ LAST_MESSAGE_CORRECTION_RETRACTION_WORKAROUND__DOC = ( "If the legacy service does not support retractions and this is set to true, when " "XMPP clients attempt to correct, this will send a new message." ) + +FIX_FILENAME_SUFFIX_MIME_TYPE = False +FIX_FILENAME_SUFFIX_MIME_TYPE__DOC = ( + "Fix the Filename suffix based on the Mime Type of the file. " + "Some clients (eg Conversations) may not inline files that have a wrong suffix for the MIME Type. " + "Therefore the MIME Type of the file is checked, if the suffix is not valid for that MIME Type, " + "a valid one will be picked." +) diff --git a/slidge/plugins/whatsapp/config.py b/slidge/plugins/whatsapp/config.py index 05b3644..b69f46d 100644 --- a/slidge/plugins/whatsapp/config.py +++ b/slidge/plugins/whatsapp/config.py @@ -2,7 +2,6 @@ Config contains plugin-specific configuration for WhatsApp, and is loaded automatically by the core configuration framework. """ -from typing import Optional from slidge import global_config @@ -16,11 +15,3 @@ ALWAYS_SYNC_ROSTER__DOC = ( SKIP_VERIFY_TLS = False SKIP_VERIFY_TLS__DOC = "Whether or not HTTPS connections made by this plugin should verify TLS certificates." - -ADD_AUDIO_OGG_FILENAME_SUFFIX: Optional[str] = None -ADD_AUDIO_OGG_FILENAME_SUFFIX__DOC = ( - "Whatsapp audio attachments file names are suffixed with '.bin'. " - "Some clients (eg Conversations) may not inline audio files that have a .bin " - "suffix, so you can set this to '.oga' as a workaround to get audio attachments " - "inlined." -) diff --git a/slidge/plugins/whatsapp/session.py b/slidge/plugins/whatsapp/session.py index a14ef72..b6e1c1f 100644 --- a/slidge/plugins/whatsapp/session.py +++ b/slidge/plugins/whatsapp/session.py @@ -193,18 +193,9 @@ class Session( attachment_caption = ( attachment.Caption if attachment.Caption != "" else None ) - mime = attachment.MIME - file_name = attachment.Filename - if ( - mime - and file_name - and config.ADD_AUDIO_OGG_FILENAME_SUFFIX - and mime.startswith("audio/ogg") - ): - file_name += config.ADD_AUDIO_OGG_FILENAME_SUFFIX await contact.send_file( - file_name=file_name, + file_name=attachment.Filename, content_type=attachment.MIME, data=bytes(attachment.Data), legacy_msg_id=message.ID, -- 2.34.1
From: nicoco <nicoco@nicoco.fr> --- slidge/core/gateway.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/slidge/core/gateway.py b/slidge/core/gateway.py index 2f6bfbc..bc1c09a 100644 --- a/slidge/core/gateway.py +++ b/slidge/core/gateway.py @@ -320,6 +320,8 @@ class BaseGateway( # type:ignore def _send(self, stanza: Union[Message, Presence], **send_kwargs): stanza.set_from(self.boundjid.bare) + if mto := send_kwargs.get("mto"): + stanza.set_to(mto) stanza.send() async def get_muc_from_iq(self, iq: Iq): -- 2.34.1
From: nicoco <nicoco@nicoco.fr> just to have a platform-independent reference on how to build this crap --- dev/Containerfile-tdlib | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 dev/Containerfile-tdlib diff --git a/dev/Containerfile-tdlib b/dev/Containerfile-tdlib new file mode 100644 index 0000000..486142e --- /dev/null +++ b/dev/Containerfile-tdlib @@ -0,0 +1,21 @@ +# container to build tdlib. example usage: +# docker buildx build --platform linux/i386 --file tdlib-build-dockerfile . -o /tmp/tdlib + +FROM docker.io/library/debian:stable AS builder-tdlib + +# everything telegram/tdlib-specific would be improved, ie removed, if we fixed +# https://github.com/pylakey/aiotdlib/issues/50 + +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt update +RUN apt install -y git g++ cmake zlib1g-dev gperf libssl-dev +RUN git clone https://github.com/pylakey/td --depth 1 +RUN mkdir td/build +WORKDIR td/build +RUN cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX:PATH=/tmp/tdlib/ -DTD_ENABLE_LTO=ON .. +RUN CMAKE_BUILD_PARALLEL_LEVEL=$(grep -c processor /proc/cpuinfo) cmake --build . --target install +RUN ls -la /tmp/tdlib/lib + +FROM scratch AS tdlib +COPY --from=builder-tdlib /tmp/tdlib/lib / -- 2.34.1
From: nicoco <nicoco@nicoco.fr> --- slidge/core/gateway.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/slidge/core/gateway.py b/slidge/core/gateway.py index 2f6bfbc..bc1c09a 100644 --- a/slidge/core/gateway.py +++ b/slidge/core/gateway.py @@ -320,6 +320,8 @@ class BaseGateway( # type:ignore def _send(self, stanza: Union[Message, Presence], **send_kwargs): stanza.set_from(self.boundjid.bare) + if mto := send_kwargs.get("mto"): + stanza.set_to(mto) stanza.send() async def get_muc_from_iq(self, iq: Iq): -- 2.34.1
From: nicoco <nicoco@nicoco.fr> --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5e7e2d4..fcc2a0d 100644 --- a/README.md +++ b/README.md @@ -98,13 +98,14 @@ apt update && apt install slidge -y Refer to [the docs](https://slidge.readthedocs.io/en/latest/admin/launch.html#debian-packages) for information about how to use the provided systemd service files. -### pip +### pipx -Tagged releases are uploaded to [pypi](https://pypi.org/project/slidge/). +Tagged releases are uploaded to [pypi](https://pypi.org/project/slidge/) and should +be installable on any distro with `pipx`. ```sh -pip install slidge[signal] # you can replace signal with any network listed in the table above -python -m slidge --legacy-module=slidge.plugins.signal +pipx install slidge[signal] # you can replace signal with any network listed in the table above +slidge --legacy-module=slidge.plugins.signal ``` If you're looking for the bleeding edge, download an artifact -- 2.34.1
From: Jonny Rimkus <jonny.rimkus@hdi.de> --- pyproject.toml | 3 +- slidge/core/mixins/message.py | 5 +++- slidge/util/__init__.py | 2 ++ slidge/util/util.py | 53 ++++++++++++++++++++++++++++++++++- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 29bb9c0..0ea3c9d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ qrcode = ">=7.3" Pillow = ">=8.1.0" aiohttp = ">=3.6.0" ConfigArgParse = "^1.5.3" +filemagic = ">=1.6" aiotdlib = { version = "^0.19.2", optional = true } pydantic = { version = "*", optional = true } @@ -62,4 +63,4 @@ ignore_missing_imports = true check_untyped_defs = true [tool.black] -exclude = "xep_*" \ No newline at end of file +exclude = "xep_*" diff --git a/slidge/core/mixins/message.py b/slidge/core/mixins/message.py index 235ce06..5d23cc4 100644 --- a/slidge/core/mixins/message.py +++ b/slidge/core/mixins/message.py @@ -15,10 +15,11 @@ from slixmpp.types import MessageTypes from slidge.core import config from slidge.util.types import LegacyMessageType -from ...util import BiDict +from ...util import BiDict, FileSuffixFixer from ...util.types import ChatState, Marker, ProcessingHint from ...util.xep_0385.stanza import Sims from ...util.xep_0447.stanza import StatelessFileSharing + from .base import BaseSender @@ -332,6 +333,8 @@ class AttachmentMixin(MessageMaker): else: is_temp = False + suffixFixer = FileSuffixFixer(file_path, content_type) + if config.NO_UPLOAD_PATH: local_path, new_url = await self.__no_upload( file_path, file_name, legacy_file_id diff --git a/slidge/util/__init__.py b/slidge/util/__init__.py index d2d9617..87d91c5 100644 --- a/slidge/util/__init__.py +++ b/slidge/util/__init__.py @@ -5,6 +5,7 @@ from .util import ( SearchResult, SubclassableOnce, is_valid_phone_number, + FileSuffixFixer, ) __all__ = [ @@ -14,4 +15,5 @@ __all__ = [ "SubclassableOnce", "ABCSubclassableOnceAtMost", "is_valid_phone_number", + "FileSuffixFixer", ] diff --git a/slidge/util/util.py b/slidge/util/util.py index 5481669..0015f9f 100644 --- a/slidge/util/util.py +++ b/slidge/util/util.py @@ -1,10 +1,61 @@ import dataclasses import logging +import os.path import re +import mimetypes +import magic + from abc import ABCMeta from typing import Collection, Generic, Optional, TypeVar - from .types import FieldType +from slidge import ( + global_config +) + + +class FileSuffixFixer: + """ + checks if file suffix is valid for given mime type, and correct otherwise + """ + + def __init__(self, file_path, mime_type): + self.fixed_file_path = file_path + self.file_path = file_path + self.mime_type = mime_type + self.valid_extensions = mimetypes.guess_all_extensions(self.mime_type) + + def validate_mime(self): + guessed_type = mimetypes.guess_type(self.file_path) + if guessed_type != self.mime_type: + log.warning("the guessed mime Type %s for %s does not match the given mime type %s" % + (guessed_type, self.file_path, self.mime_type)) + s + + def is_enabled(self): + return global_config.FIX_FILENAME_SUFFIX_MIME_TYPE + + def is_valid_suffix(self): + suffix = os.path.splitext(self.file_path)[1] + if suffix not in self.valid_extensions: + log.debug("File suffix of %s is not in the list of valid %s for mime type: %s" % + (self.file_path, self.valid_extensions, self.mime_type)) + return False + else: + return True + + def fix_suffix(self): + if not self.is_enabled() or self.is_valid_suffix(): + return self.fixed_file_path + valid_extension = mimetypes.guess_extension(self.file_path) + + if valid_extension is None: + log.warning("could not determine File suffix for file %s with Mime Type %s" % + (self.file_path, self.mime_type)) + return self.fixed_file_path + else: + log.debug("using %s as file suffix for %s" % (valid_extension, self.file_path)) + self.fixed_file_path = self.file_path + valid_extension + return self.fixed_file_path @dataclasses.dataclass -- 2.34.1
From: nicoco <nicoco@nicoco.fr> --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5e7e2d4..fcc2a0d 100644 --- a/README.md +++ b/README.md @@ -98,13 +98,14 @@ apt update && apt install slidge -y Refer to [the docs](https://slidge.readthedocs.io/en/latest/admin/launch.html#debian-packages) for information about how to use the provided systemd service files. -### pip +### pipx -Tagged releases are uploaded to [pypi](https://pypi.org/project/slidge/). +Tagged releases are uploaded to [pypi](https://pypi.org/project/slidge/) and should +be installable on any distro with `pipx`. ```sh -pip install slidge[signal] # you can replace signal with any network listed in the table above -python -m slidge --legacy-module=slidge.plugins.signal +pipx install slidge[signal] # you can replace signal with any network listed in the table above +slidge --legacy-module=slidge.plugins.signal ``` If you're looking for the bleeding edge, download an artifact -- 2.34.1
From: Jonny Rimkus <jonny.rimkus@hdi.de> --- pyproject.toml | 3 +- slidge/core/mixins/message.py | 5 +++- slidge/util/__init__.py | 2 ++ slidge/util/util.py | 53 ++++++++++++++++++++++++++++++++++- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 29bb9c0..0ea3c9d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ qrcode = ">=7.3" Pillow = ">=8.1.0" aiohttp = ">=3.6.0" ConfigArgParse = "^1.5.3" +filemagic = ">=1.6" aiotdlib = { version = "^0.19.2", optional = true } pydantic = { version = "*", optional = true } @@ -62,4 +63,4 @@ ignore_missing_imports = true check_untyped_defs = true [tool.black] -exclude = "xep_*" \ No newline at end of file +exclude = "xep_*" diff --git a/slidge/core/mixins/message.py b/slidge/core/mixins/message.py index 235ce06..5d23cc4 100644 --- a/slidge/core/mixins/message.py +++ b/slidge/core/mixins/message.py @@ -15,10 +15,11 @@ from slixmpp.types import MessageTypes from slidge.core import config from slidge.util.types import LegacyMessageType -from ...util import BiDict +from ...util import BiDict, FileSuffixFixer from ...util.types import ChatState, Marker, ProcessingHint from ...util.xep_0385.stanza import Sims from ...util.xep_0447.stanza import StatelessFileSharing + from .base import BaseSender @@ -332,6 +333,8 @@ class AttachmentMixin(MessageMaker): else: is_temp = False + suffixFixer = FileSuffixFixer(file_path, content_type) + if config.NO_UPLOAD_PATH: local_path, new_url = await self.__no_upload( file_path, file_name, legacy_file_id diff --git a/slidge/util/__init__.py b/slidge/util/__init__.py index d2d9617..87d91c5 100644 --- a/slidge/util/__init__.py +++ b/slidge/util/__init__.py @@ -5,6 +5,7 @@ from .util import ( SearchResult, SubclassableOnce, is_valid_phone_number, + FileSuffixFixer, ) __all__ = [ @@ -14,4 +15,5 @@ __all__ = [ "SubclassableOnce", "ABCSubclassableOnceAtMost", "is_valid_phone_number", + "FileSuffixFixer", ] diff --git a/slidge/util/util.py b/slidge/util/util.py index 5481669..0015f9f 100644 --- a/slidge/util/util.py +++ b/slidge/util/util.py @@ -1,10 +1,61 @@ import dataclasses import logging +import os.path import re +import mimetypes +import magic + from abc import ABCMeta from typing import Collection, Generic, Optional, TypeVar - from .types import FieldType +from slidge import ( + global_config +) + + +class FileSuffixFixer: + """ + checks if file suffix is valid for given mime type, and correct otherwise + """ + + def __init__(self, file_path, mime_type): + self.fixed_file_path = file_path + self.file_path = file_path + self.mime_type = mime_type + self.valid_extensions = mimetypes.guess_all_extensions(self.mime_type) + + def validate_mime(self): + guessed_type = mimetypes.guess_type(self.file_path) + if guessed_type != self.mime_type: + log.warning("the guessed mime Type %s for %s does not match the given mime type %s" % + (guessed_type, self.file_path, self.mime_type)) + s + + def is_enabled(self): + return global_config.FIX_FILENAME_SUFFIX_MIME_TYPE + + def is_valid_suffix(self): + suffix = os.path.splitext(self.file_path)[1] + if suffix not in self.valid_extensions: + log.debug("File suffix of %s is not in the list of valid %s for mime type: %s" % + (self.file_path, self.valid_extensions, self.mime_type)) + return False + else: + return True + + def fix_suffix(self): + if not self.is_enabled() or self.is_valid_suffix(): + return self.fixed_file_path + valid_extension = mimetypes.guess_extension(self.file_path) + + if valid_extension is None: + log.warning("could not determine File suffix for file %s with Mime Type %s" % + (self.file_path, self.mime_type)) + return self.fixed_file_path + else: + log.debug("using %s as file suffix for %s" % (valid_extension, self.file_path)) + self.fixed_file_path = self.file_path + valid_extension + return self.fixed_file_path @dataclasses.dataclass -- 2.34.1
From: Jonny Rimkus <jonny.rimkus@hdi.de> --- slidge/core/mixins/message.py | 5 ++- slidge/util/util.py | 62 ++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/slidge/core/mixins/message.py b/slidge/core/mixins/message.py index 5d23cc4..81a97f3 100644 --- a/slidge/core/mixins/message.py +++ b/slidge/core/mixins/message.py @@ -333,7 +333,10 @@ class AttachmentMixin(MessageMaker): else: is_temp = False - suffixFixer = FileSuffixFixer(file_path, content_type) + file_suffix_fixer = FileSuffixFixer(file_path, content_type) + fixed_file_path = file_suffix_fixer.fix_suffix() + if fixed_file_path != file_path: + file_path.rename(fixed_file_path) if config.NO_UPLOAD_PATH: local_path, new_url = await self.__no_upload( diff --git a/slidge/util/util.py b/slidge/util/util.py index 0015f9f..10e6ec6 100644 --- a/slidge/util/util.py +++ b/slidge/util/util.py @@ -7,6 +7,9 @@ import magic from abc import ABCMeta from typing import Collection, Generic, Optional, TypeVar + +from magic import MagicError + from .types import FieldType from slidge import ( global_config @@ -19,43 +22,42 @@ class FileSuffixFixer: """ def __init__(self, file_path, mime_type): - self.fixed_file_path = file_path self.file_path = file_path self.mime_type = mime_type - self.valid_extensions = mimetypes.guess_all_extensions(self.mime_type) - def validate_mime(self): - guessed_type = mimetypes.guess_type(self.file_path) - if guessed_type != self.mime_type: - log.warning("the guessed mime Type %s for %s does not match the given mime type %s" % - (guessed_type, self.file_path, self.mime_type)) - s + def __validate_mime(self): + try: + with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m: + guessed_type = m.id_filename(self.file_path) + if guessed_type != self.mime_type: + log.warning("the guessed mime Type %s for %s does not match the given mime type %s" % + (guessed_type, self.file_path, self.mime_type)) + self.mime_type = guessed_type + except MagicError as err: + log.warning("exception during mime type check for file %s: %s" % + (self.file_path, err)) - def is_enabled(self): - return global_config.FIX_FILENAME_SUFFIX_MIME_TYPE + def fix_suffix(self): + if not global_config.FIX_FILENAME_SUFFIX_MIME_TYPE: + return self.file_path - def is_valid_suffix(self): - suffix = os.path.splitext(self.file_path)[1] - if suffix not in self.valid_extensions: + self.__validate_mime() + valid_extensions = mimetypes.guess_all_extensions(self.mime_type, strict=False) + file_suffix = os.path.splitext(self.file_path)[1] + if file_suffix not in valid_extensions: log.debug("File suffix of %s is not in the list of valid %s for mime type: %s" % - (self.file_path, self.valid_extensions, self.mime_type)) - return False - else: - return True - - def fix_suffix(self): - if not self.is_enabled() or self.is_valid_suffix(): - return self.fixed_file_path - valid_extension = mimetypes.guess_extension(self.file_path) - - if valid_extension is None: - log.warning("could not determine File suffix for file %s with Mime Type %s" % - (self.file_path, self.mime_type)) - return self.fixed_file_path + (self.file_path, valid_extensions, self.mime_type)) + valid_extension = mimetypes.guess_extension(self.mime_type, strict=False) + if valid_extension is None: + log.warning("could not determine File suffix for file %s with Mime Type %s" % + (self.file_path, self.mime_type)) + return self.file_path + else: + log.debug("using %s as file suffix for file %s with mime type %s" % + (valid_extension, self.file_path, self.mime_type)) + return self.file_path + valid_extension else: - log.debug("using %s as file suffix for %s" % (valid_extension, self.file_path)) - self.fixed_file_path = self.file_path + valid_extension - return self.fixed_file_path + return self.file_path @dataclasses.dataclass -- 2.34.1
So, I think I got something working here: https://git.sr.ht/~nicoco/slidge/commit/a32355000d88 Let me know what you think. -- Nicolas
From: Jonny Rimkus <jonny.rimkus@hdi.de> --- slidge/core/mixins/message.py | 5 ++- slidge/util/util.py | 62 ++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/slidge/core/mixins/message.py b/slidge/core/mixins/message.py index 5d23cc4..81a97f3 100644 --- a/slidge/core/mixins/message.py +++ b/slidge/core/mixins/message.py @@ -333,7 +333,10 @@ class AttachmentMixin(MessageMaker): else: is_temp = False - suffixFixer = FileSuffixFixer(file_path, content_type) + file_suffix_fixer = FileSuffixFixer(file_path, content_type) + fixed_file_path = file_suffix_fixer.fix_suffix() + if fixed_file_path != file_path: + file_path.rename(fixed_file_path) if config.NO_UPLOAD_PATH: local_path, new_url = await self.__no_upload( diff --git a/slidge/util/util.py b/slidge/util/util.py index 0015f9f..10e6ec6 100644 --- a/slidge/util/util.py +++ b/slidge/util/util.py @@ -7,6 +7,9 @@ import magic from abc import ABCMeta from typing import Collection, Generic, Optional, TypeVar + +from magic import MagicError + from .types import FieldType from slidge import ( global_config @@ -19,43 +22,42 @@ class FileSuffixFixer: """ def __init__(self, file_path, mime_type): - self.fixed_file_path = file_path self.file_path = file_path self.mime_type = mime_type - self.valid_extensions = mimetypes.guess_all_extensions(self.mime_type) - def validate_mime(self): - guessed_type = mimetypes.guess_type(self.file_path) - if guessed_type != self.mime_type: - log.warning("the guessed mime Type %s for %s does not match the given mime type %s" % - (guessed_type, self.file_path, self.mime_type)) - s + def __validate_mime(self): + try: + with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m: + guessed_type = m.id_filename(self.file_path) + if guessed_type != self.mime_type: + log.warning("the guessed mime Type %s for %s does not match the given mime type %s" % + (guessed_type, self.file_path, self.mime_type)) + self.mime_type = guessed_type + except MagicError as err: + log.warning("exception during mime type check for file %s: %s" % + (self.file_path, err)) - def is_enabled(self): - return global_config.FIX_FILENAME_SUFFIX_MIME_TYPE + def fix_suffix(self): + if not global_config.FIX_FILENAME_SUFFIX_MIME_TYPE: + return self.file_path - def is_valid_suffix(self): - suffix = os.path.splitext(self.file_path)[1] - if suffix not in self.valid_extensions: + self.__validate_mime() + valid_extensions = mimetypes.guess_all_extensions(self.mime_type, strict=False) + file_suffix = os.path.splitext(self.file_path)[1] + if file_suffix not in valid_extensions: log.debug("File suffix of %s is not in the list of valid %s for mime type: %s" % - (self.file_path, self.valid_extensions, self.mime_type)) - return False - else: - return True - - def fix_suffix(self): - if not self.is_enabled() or self.is_valid_suffix(): - return self.fixed_file_path - valid_extension = mimetypes.guess_extension(self.file_path) - - if valid_extension is None: - log.warning("could not determine File suffix for file %s with Mime Type %s" % - (self.file_path, self.mime_type)) - return self.fixed_file_path + (self.file_path, valid_extensions, self.mime_type)) + valid_extension = mimetypes.guess_extension(self.mime_type, strict=False) + if valid_extension is None: + log.warning("could not determine File suffix for file %s with Mime Type %s" % + (self.file_path, self.mime_type)) + return self.file_path + else: + log.debug("using %s as file suffix for file %s with mime type %s" % + (valid_extension, self.file_path, self.mime_type)) + return self.file_path + valid_extension else: - log.debug("using %s as file suffix for %s" % (valid_extension, self.file_path)) - self.fixed_file_path = self.file_path + valid_extension - return self.fixed_file_path + return self.file_path @dataclasses.dataclass -- 2.34.1
From: Jonny Rimkus <jonny.rimkus@hdi.de> --- slidge/core/mixins/message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slidge/core/mixins/message.py b/slidge/core/mixins/message.py index 81a97f3..00962df 100644 --- a/slidge/core/mixins/message.py +++ b/slidge/core/mixins/message.py @@ -336,7 +336,7 @@ class AttachmentMixin(MessageMaker): file_suffix_fixer = FileSuffixFixer(file_path, content_type) fixed_file_path = file_suffix_fixer.fix_suffix() if fixed_file_path != file_path: - file_path.rename(fixed_file_path) + file_path = file_path.rename(fixed_file_path) if config.NO_UPLOAD_PATH: local_path, new_url = await self.__no_upload( -- 2.34.1
From: nicoco <nicoco@nicoco.fr> --- slidge/util/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slidge/util/util.py b/slidge/util/util.py index 10e6ec6..69cfc42 100644 --- a/slidge/util/util.py +++ b/slidge/util/util.py @@ -21,7 +21,7 @@ class FileSuffixFixer: checks if file suffix is valid for given mime type, and correct otherwise """ - def __init__(self, file_path, mime_type): + def __init__(self, file_path: Path, mime_type: str): self.file_path = file_path self.mime_type = mime_type -- 2.34.1
From: Jonny Rimkus <jonny.rimkus@hdi.de> --- slidge/core/mixins/message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slidge/core/mixins/message.py b/slidge/core/mixins/message.py index 81a97f3..00962df 100644 --- a/slidge/core/mixins/message.py +++ b/slidge/core/mixins/message.py @@ -336,7 +336,7 @@ class AttachmentMixin(MessageMaker): file_suffix_fixer = FileSuffixFixer(file_path, content_type) fixed_file_path = file_suffix_fixer.fix_suffix() if fixed_file_path != file_path: - file_path.rename(fixed_file_path) + file_path = file_path.rename(fixed_file_path) if config.NO_UPLOAD_PATH: local_path, new_url = await self.__no_upload( -- 2.34.1
From: nicoco <nicoco@nicoco.fr> --- slidge/util/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slidge/util/util.py b/slidge/util/util.py index 10e6ec6..69cfc42 100644 --- a/slidge/util/util.py +++ b/slidge/util/util.py @@ -21,7 +21,7 @@ class FileSuffixFixer: checks if file suffix is valid for given mime type, and correct otherwise """ - def __init__(self, file_path, mime_type): + def __init__(self, file_path: Path, mime_type: str): self.file_path = file_path self.mime_type = mime_type -- 2.34.1
--- pyproject.toml | 3 +- slidge/core/mixins/message.py | 4 +- slidge/plugins/signal/contact.py | 2 +- slidge/util/__init__.py | 2 +- slidge/util/util.py | 74 +++++++++++++++++++++----------- 5 files changed, 55 insertions(+), 30 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0ea3c9d..1edf287 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,6 @@ qrcode = ">=7.3" Pillow = ">=8.1.0" aiohttp = ">=3.6.0" ConfigArgParse = "^1.5.3" -filemagic = ">=1.6" aiotdlib = { version = "^0.19.2", optional = true } pydantic = { version = "*", optional = true } @@ -26,6 +25,7 @@ SkPy = { version = "^0.10.4", optional = true } steam = {extras = ["client"], version = "^1.4.4", optional = true } "discord.py-self" = { version = "^1.9.2", optional = true } pickle-secure = "^0.9.99" +filemagic = { version = ">=1.6", optional = true } [tool.poetry.dev-dependencies] pytest = "^7.2.0" @@ -53,6 +53,7 @@ facebook = ["mautrix-facebook"] mattermost = ["mattermost-api-reference-client", "emoji"] skype = ["SkPy"] steam = ["steam", "asyncio-gevent"] +filemagic = ["filemagic"] [tool.poetry.scripts] slidge = 'slidge.__main__:main' diff --git a/slidge/core/mixins/message.py b/slidge/core/mixins/message.py index 00962df..31e030b 100644 --- a/slidge/core/mixins/message.py +++ b/slidge/core/mixins/message.py @@ -19,7 +19,6 @@ from ...util import BiDict, FileSuffixFixer from ...util.types import ChatState, Marker, ProcessingHint from ...util.xep_0385.stanza import Sims from ...util.xep_0447.stanza import StatelessFileSharing - from .base import BaseSender @@ -336,7 +335,8 @@ class AttachmentMixin(MessageMaker): file_suffix_fixer = FileSuffixFixer(file_path, content_type) fixed_file_path = file_suffix_fixer.fix_suffix() if fixed_file_path != file_path: - file_path = file_path.rename(fixed_file_path) + log.debug("fix file suffix, rename %s to %s", file_path, fixed_file_path) + file_path = fixed_file_path if config.NO_UPLOAD_PATH: local_path, new_url = await self.__no_upload( diff --git a/slidge/plugins/signal/contact.py b/slidge/plugins/signal/contact.py index 5c77b3c..178d4d6 100644 --- a/slidge/plugins/signal/contact.py +++ b/slidge/plugins/signal/contact.py @@ -58,7 +58,7 @@ class Contact(AttachmentSenderMixin, LegacyContact["Session", str]): if profile.profile_name or profile.name or profile.contact_name: return profile attempts += 1 - await asyncio.sleep(sleep * attempts**exp) + await asyncio.sleep(sleep * attempts ** exp) async def update_info(self, profile: Optional[sigapi.Profilev1] = None): if profile is None: diff --git a/slidge/util/__init__.py b/slidge/util/__init__.py index 87d91c5..4b5b652 100644 --- a/slidge/util/__init__.py +++ b/slidge/util/__init__.py @@ -1,11 +1,11 @@ from .util import ( ABCSubclassableOnceAtMost, BiDict, + FileSuffixFixer, FormField, SearchResult, SubclassableOnce, is_valid_phone_number, - FileSuffixFixer, ) __all__ = [ diff --git a/slidge/util/util.py b/slidge/util/util.py index 69cfc42..57d7966 100644 --- a/slidge/util/util.py +++ b/slidge/util/util.py @@ -1,19 +1,22 @@ import dataclasses import logging +import mimetypes import os.path import re -import mimetypes -import magic - from abc import ABCMeta +from pathlib import Path from typing import Collection, Generic, Optional, TypeVar -from magic import MagicError +try: + import magic + from magic import MagicError +except ImportError: + magic = None + MagicError = None + +from slidge import global_config from .types import FieldType -from slidge import ( - global_config -) class FileSuffixFixer: @@ -26,38 +29,59 @@ class FileSuffixFixer: self.mime_type = mime_type def __validate_mime(self): + if magic is None: + log.debug( + "filemagic dependency not available, mime type detection disabled" + ) + return try: with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m: guessed_type = m.id_filename(self.file_path) if guessed_type != self.mime_type: - log.warning("the guessed mime Type %s for %s does not match the given mime type %s" % - (guessed_type, self.file_path, self.mime_type)) + log.warning( + "the guessed mime Type %s for %s does not match the given mime type %s", + guessed_type, + self.file_path, + self.mime_type, + ) self.mime_type = guessed_type except MagicError as err: - log.warning("exception during mime type check for file %s: %s" % - (self.file_path, err)) + log.warning( + "exception during mime type check for file %s: %s", self.file_path, err + ) def fix_suffix(self): if not global_config.FIX_FILENAME_SUFFIX_MIME_TYPE: return self.file_path self.__validate_mime() - valid_extensions = mimetypes.guess_all_extensions(self.mime_type, strict=False) - file_suffix = os.path.splitext(self.file_path)[1] - if file_suffix not in valid_extensions: - log.debug("File suffix of %s is not in the list of valid %s for mime type: %s" % - (self.file_path, valid_extensions, self.mime_type)) - valid_extension = mimetypes.guess_extension(self.mime_type, strict=False) - if valid_extension is None: - log.warning("could not determine File suffix for file %s with Mime Type %s" % - (self.file_path, self.mime_type)) + valid_suffix_list = mimetypes.guess_all_extensions(self.mime_type, strict=False) + + if self.file_path.suffix not in valid_suffix_list: + log.debug( + "File suffix of %s is not in the list of valid %s for mime type: %s", + self.file_path, + valid_suffix_list, + self.mime_type, + ) + valid_suffix = mimetypes.guess_extension(self.mime_type, strict=False) + if valid_suffix is None: + log.warning( + "could not determine File suffix for file %s with Mime Type %s", + self.file_path, + self.mime_type, + ) return self.file_path else: - log.debug("using %s as file suffix for file %s with mime type %s" % - (valid_extension, self.file_path, self.mime_type)) - return self.file_path + valid_extension - else: - return self.file_path + log.debug( + "using %s as file suffix for file %s with mime type %s", + valid_suffix, + self.file_path, + self.mime_type, + ) + return self.file_path.with_suffix(valid_suffix) + + return self.file_path @dataclasses.dataclass -- 2.34.1
--- pyproject.toml | 3 +- slidge/core/mixins/message.py | 4 +- slidge/plugins/signal/contact.py | 2 +- slidge/util/__init__.py | 2 +- slidge/util/util.py | 74 +++++++++++++++++++++----------- 5 files changed, 55 insertions(+), 30 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0ea3c9d..1edf287 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,6 @@ qrcode = ">=7.3" Pillow = ">=8.1.0" aiohttp = ">=3.6.0" ConfigArgParse = "^1.5.3" -filemagic = ">=1.6" aiotdlib = { version = "^0.19.2", optional = true } pydantic = { version = "*", optional = true } @@ -26,6 +25,7 @@ SkPy = { version = "^0.10.4", optional = true } steam = {extras = ["client"], version = "^1.4.4", optional = true } "discord.py-self" = { version = "^1.9.2", optional = true } pickle-secure = "^0.9.99" +filemagic = { version = ">=1.6", optional = true } [tool.poetry.dev-dependencies] pytest = "^7.2.0" @@ -53,6 +53,7 @@ facebook = ["mautrix-facebook"] mattermost = ["mattermost-api-reference-client", "emoji"] skype = ["SkPy"] steam = ["steam", "asyncio-gevent"] +filemagic = ["filemagic"] [tool.poetry.scripts] slidge = 'slidge.__main__:main' diff --git a/slidge/core/mixins/message.py b/slidge/core/mixins/message.py index 00962df..31e030b 100644 --- a/slidge/core/mixins/message.py +++ b/slidge/core/mixins/message.py @@ -19,7 +19,6 @@ from ...util import BiDict, FileSuffixFixer from ...util.types import ChatState, Marker, ProcessingHint from ...util.xep_0385.stanza import Sims from ...util.xep_0447.stanza import StatelessFileSharing - from .base import BaseSender @@ -336,7 +335,8 @@ class AttachmentMixin(MessageMaker): file_suffix_fixer = FileSuffixFixer(file_path, content_type) fixed_file_path = file_suffix_fixer.fix_suffix() if fixed_file_path != file_path: - file_path = file_path.rename(fixed_file_path) + log.debug("fix file suffix, rename %s to %s", file_path, fixed_file_path) + file_path = fixed_file_path if config.NO_UPLOAD_PATH: local_path, new_url = await self.__no_upload( diff --git a/slidge/plugins/signal/contact.py b/slidge/plugins/signal/contact.py index 5c77b3c..178d4d6 100644 --- a/slidge/plugins/signal/contact.py +++ b/slidge/plugins/signal/contact.py @@ -58,7 +58,7 @@ class Contact(AttachmentSenderMixin, LegacyContact["Session", str]): if profile.profile_name or profile.name or profile.contact_name: return profile attempts += 1 - await asyncio.sleep(sleep * attempts**exp) + await asyncio.sleep(sleep * attempts ** exp) async def update_info(self, profile: Optional[sigapi.Profilev1] = None): if profile is None: diff --git a/slidge/util/__init__.py b/slidge/util/__init__.py index 87d91c5..4b5b652 100644 --- a/slidge/util/__init__.py +++ b/slidge/util/__init__.py @@ -1,11 +1,11 @@ from .util import ( ABCSubclassableOnceAtMost, BiDict, + FileSuffixFixer, FormField, SearchResult, SubclassableOnce, is_valid_phone_number, - FileSuffixFixer, ) __all__ = [ diff --git a/slidge/util/util.py b/slidge/util/util.py index 69cfc42..57d7966 100644 --- a/slidge/util/util.py +++ b/slidge/util/util.py @@ -1,19 +1,22 @@ import dataclasses import logging +import mimetypes import os.path import re -import mimetypes -import magic - from abc import ABCMeta +from pathlib import Path from typing import Collection, Generic, Optional, TypeVar -from magic import MagicError +try: + import magic + from magic import MagicError +except ImportError: + magic = None + MagicError = None + +from slidge import global_config from .types import FieldType -from slidge import ( - global_config -) class FileSuffixFixer: @@ -26,38 +29,59 @@ class FileSuffixFixer: self.mime_type = mime_type def __validate_mime(self): + if magic is None: + log.debug( + "filemagic dependency not available, mime type detection disabled" + ) + return try: with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m: guessed_type = m.id_filename(self.file_path) if guessed_type != self.mime_type: - log.warning("the guessed mime Type %s for %s does not match the given mime type %s" % - (guessed_type, self.file_path, self.mime_type)) + log.warning( + "the guessed mime Type %s for %s does not match the given mime type %s", + guessed_type, + self.file_path, + self.mime_type, + ) self.mime_type = guessed_type except MagicError as err: - log.warning("exception during mime type check for file %s: %s" % - (self.file_path, err)) + log.warning( + "exception during mime type check for file %s: %s", self.file_path, err + ) def fix_suffix(self): if not global_config.FIX_FILENAME_SUFFIX_MIME_TYPE: return self.file_path self.__validate_mime() - valid_extensions = mimetypes.guess_all_extensions(self.mime_type, strict=False) - file_suffix = os.path.splitext(self.file_path)[1] - if file_suffix not in valid_extensions: - log.debug("File suffix of %s is not in the list of valid %s for mime type: %s" % - (self.file_path, valid_extensions, self.mime_type)) - valid_extension = mimetypes.guess_extension(self.mime_type, strict=False) - if valid_extension is None: - log.warning("could not determine File suffix for file %s with Mime Type %s" % - (self.file_path, self.mime_type)) + valid_suffix_list = mimetypes.guess_all_extensions(self.mime_type, strict=False) + + if self.file_path.suffix not in valid_suffix_list: + log.debug( + "File suffix of %s is not in the list of valid %s for mime type: %s", + self.file_path, + valid_suffix_list, + self.mime_type, + ) + valid_suffix = mimetypes.guess_extension(self.mime_type, strict=False) + if valid_suffix is None: + log.warning( + "could not determine File suffix for file %s with Mime Type %s", + self.file_path, + self.mime_type, + ) return self.file_path else: - log.debug("using %s as file suffix for file %s with mime type %s" % - (valid_extension, self.file_path, self.mime_type)) - return self.file_path + valid_extension - else: - return self.file_path + log.debug( + "using %s as file suffix for file %s with mime type %s", + valid_suffix, + self.file_path, + self.mime_type, + ) + return self.file_path.with_suffix(valid_suffix) + + return self.file_path @dataclasses.dataclass -- 2.34.1
--- slidge/util/util.py | 1 - 1 file changed, 1 deletion(-) diff --git a/slidge/util/util.py b/slidge/util/util.py index 57d7966..2161ee7 100644 --- a/slidge/util/util.py +++ b/slidge/util/util.py @@ -1,7 +1,6 @@ import dataclasses import logging import mimetypes -import os.path import re from abc import ABCMeta from pathlib import Path -- 2.34.1
--- slidge/util/util.py | 1 - 1 file changed, 1 deletion(-) diff --git a/slidge/util/util.py b/slidge/util/util.py index 57d7966..2161ee7 100644 --- a/slidge/util/util.py +++ b/slidge/util/util.py @@ -1,7 +1,6 @@ import dataclasses import logging import mimetypes -import os.path import re from abc import ABCMeta from pathlib import Path -- 2.34.1
--- slidge/core/mixins/message.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/slidge/core/mixins/message.py b/slidge/core/mixins/message.py index 31e030b..bdf7f8d 100644 --- a/slidge/core/mixins/message.py +++ b/slidge/core/mixins/message.py @@ -336,7 +336,8 @@ class AttachmentMixin(MessageMaker): fixed_file_path = file_suffix_fixer.fix_suffix() if fixed_file_path != file_path: log.debug("fix file suffix, rename %s to %s", file_path, fixed_file_path) - file_path = fixed_file_path + file_name = fixed_file_path.stem + fixed_file_path.suffix + # file_path = fixed_file_path if config.NO_UPLOAD_PATH: local_path, new_url = await self.__no_upload( -- 2.34.1
--- slidge/core/mixins/message.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/slidge/core/mixins/message.py b/slidge/core/mixins/message.py index 31e030b..bdf7f8d 100644 --- a/slidge/core/mixins/message.py +++ b/slidge/core/mixins/message.py @@ -336,7 +336,8 @@ class AttachmentMixin(MessageMaker): fixed_file_path = file_suffix_fixer.fix_suffix() if fixed_file_path != file_path: log.debug("fix file suffix, rename %s to %s", file_path, fixed_file_path) - file_path = fixed_file_path + file_name = fixed_file_path.stem + fixed_file_path.suffix + # file_path = fixed_file_path if config.NO_UPLOAD_PATH: local_path, new_url = await self.__no_upload( -- 2.34.1
From: nicoco <nicoco@nicoco.fr> avoid crashing later --- slidge/plugins/telegram/contact.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/slidge/plugins/telegram/contact.py b/slidge/plugins/telegram/contact.py index bc67888..eb641fb 100644 --- a/slidge/plugins/telegram/contact.py +++ b/slidge/plugins/telegram/contact.py @@ -130,6 +130,10 @@ class Roster(LegacyRoster["Session", "Contact", int]): raise XMPPError("bad-request", "This is not a telegram user ID") else: if tg_id > 0: + try: + await self.session.tg.get_user(user_id=tg_id) + except tgapi.BadRequest as e: + raise XMPPError("item-not-found", e.message) return tg_id else: raise XMPPError("bad-request", "This looks like a telegram group ID") -- 2.34.1
From: nicoco <nicoco@nicoco.fr> avoid crashing later --- slidge/plugins/telegram/contact.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/slidge/plugins/telegram/contact.py b/slidge/plugins/telegram/contact.py index bc67888..eb641fb 100644 --- a/slidge/plugins/telegram/contact.py +++ b/slidge/plugins/telegram/contact.py @@ -130,6 +130,10 @@ class Roster(LegacyRoster["Session", "Contact", int]): raise XMPPError("bad-request", "This is not a telegram user ID") else: if tg_id > 0: + try: + await self.session.tg.get_user(user_id=tg_id) + except tgapi.BadRequest as e: + raise XMPPError("item-not-found", e.message) return tg_id else: raise XMPPError("bad-request", "This looks like a telegram group ID") -- 2.34.1
From: nicoco <nicoco@nicoco.fr> apparently needed when initiating a chat with a new contact --- slidge/plugins/telegram/session.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/slidge/plugins/telegram/session.py b/slidge/plugins/telegram/session.py index 1f678de..c55f8a4 100644 --- a/slidge/plugins/telegram/session.py +++ b/slidge/plugins/telegram/session.py @@ -20,14 +20,32 @@ from .group import MUC def catch_chat_not_found(coroutine): @functools.wraps(coroutine) - async def wrapped(*a, **k): + async def wrapped(self: "Session", *a, **k): try: - return await coroutine(*a, **k) + return await coroutine(self, *a, **k) except tgapi.BadRequest as e: if e.code == 400: - raise XMPPError(condition="item-not-found", text="Recipient not found") + # FIXME: Chat should always be the first arg for a cleaner API... + if len(a) == 1: + chat: Chat = a[0] + elif len(a) == 2: + chat = a[1] + else: + chat = k.get("chat", k.get("c")) + if chat is None: + raise RuntimeError(a, k) + try: + await self.tg.api.create_private_chat(chat.legacy_id) + except tgapi.BadRequest as e2: + if e.code == 400: + raise XMPPError(condition="item-not-found", text=e2.message) + else: + raise XMPPError( + condition="internal-server-error", text=e2.message + ) else: - raise + raise XMPPError(condition="internal-server-error", text=e.message) + return await coroutine(self, *a, **k) return wrapped -- 2.34.1
builds.sr.ht <builds@sr.ht>slidge/patches: FAILED in 2m21s [dev: add "build tdlib_json" container][0] from [Jonny Rimkus][1] [0]: https://lists.sr.ht/~nicoco/public-inbox/patches/38616 [1]: mailto:jonny@rimkus.it ✗ #932376 FAILED slidge/patches/debian.yml https://builds.sr.ht/~nicoco/job/932376 ✗ #932374 FAILED slidge/patches/ci.yml https://builds.sr.ht/~nicoco/job/932374 ✗ #932377 FAILED slidge/patches/whatsapp.yml https://builds.sr.ht/~nicoco/job/932377 ✗ #932375 FAILED slidge/patches/debian-arm.yml https://builds.sr.ht/~nicoco/job/932375
From: nicoco <nicoco@nicoco.fr> apparently needed when initiating a chat with a new contact --- slidge/plugins/telegram/session.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/slidge/plugins/telegram/session.py b/slidge/plugins/telegram/session.py index 1f678de..c55f8a4 100644 --- a/slidge/plugins/telegram/session.py +++ b/slidge/plugins/telegram/session.py @@ -20,14 +20,32 @@ from .group import MUC def catch_chat_not_found(coroutine): @functools.wraps(coroutine) - async def wrapped(*a, **k): + async def wrapped(self: "Session", *a, **k): try: - return await coroutine(*a, **k) + return await coroutine(self, *a, **k) except tgapi.BadRequest as e: if e.code == 400: - raise XMPPError(condition="item-not-found", text="Recipient not found") + # FIXME: Chat should always be the first arg for a cleaner API... + if len(a) == 1: + chat: Chat = a[0] + elif len(a) == 2: + chat = a[1] + else: + chat = k.get("chat", k.get("c")) + if chat is None: + raise RuntimeError(a, k) + try: + await self.tg.api.create_private_chat(chat.legacy_id) + except tgapi.BadRequest as e2: + if e.code == 400: + raise XMPPError(condition="item-not-found", text=e2.message) + else: + raise XMPPError( + condition="internal-server-error", text=e2.message + ) else: - raise + raise XMPPError(condition="internal-server-error", text=e.message) + return await coroutine(self, *a, **k) return wrapped -- 2.34.1