diff options
Diffstat (limited to 'plugins/upload.py')
-rw-r--r-- | plugins/upload.py | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/plugins/upload.py b/plugins/upload.py index 7e25070e..6926c075 100644 --- a/plugins/upload.py +++ b/plugins/upload.py @@ -16,12 +16,15 @@ This plugin adds a command to the chat tabs. """ + +from typing import Optional + import asyncio import traceback from os.path import expanduser from glob import glob -from slixmpp.plugins.xep_0363.http_upload import UploadServiceNotFound +from slixmpp.plugins.xep_0363.http_upload import FileTooBig, HTTPError, UploadServiceNotFound from poezio.plugin import BasePlugin from poezio.core.structs import Completion @@ -30,9 +33,19 @@ from poezio import tabs class Plugin(BasePlugin): + dependencies = {'embed'} + def init(self): + self.embed = self.refs['embed'] + if not self.core.xmpp['xep_0363']: raise Exception('slixmpp XEP-0363 plugin failed to load') + if not self.core.xmpp['xep_0454']: + self.api.information( + 'slixmpp XEP-0454 plugin failed to load. ' + 'Will not be able to encrypt uploaded files.', + 'Warning', + ) for _class in (tabs.PrivateTab, tabs.StaticConversationTab, tabs.DynamicConversationTab, tabs.MucTab): self.api.add_tab_command( _class, @@ -43,18 +56,29 @@ class Plugin(BasePlugin): short='Upload a file', completion=self.completion_filename) - async def async_upload(self, filename): + async def upload(self, filename, encrypted=False) -> Optional[str]: try: - url = await self.core.xmpp['xep_0363'].upload_file(filename) + upload_file = self.core.xmpp['xep_0363'].upload_file + if encrypted: + upload_file = self.core.xmpp['xep_0454'].upload_file + url = await upload_file(filename) except UploadServiceNotFound: self.api.information('HTTP Upload service not found.', 'Error') - return + return None + except (FileTooBig, HTTPError) as exn: + self.api.information(str(exn), 'Error') + return None except Exception: exception = traceback.format_exc() self.api.information('Failed to upload file: %s' % exception, 'Error') - return - self.core.insert_input_text(url) + return None + return url + + async def send_upload(self, filename, tab, encrypted=False): + url = await self.upload(filename, encrypted) + if url is not None: + self.embed.embed_image_url(url, tab) @command_args_parser.quoted(1) def command_upload(self, args): @@ -63,7 +87,9 @@ class Plugin(BasePlugin): return filename, = args filename = expanduser(filename) - asyncio.ensure_future(self.async_upload(filename)) + tab = self.api.current_tab() + encrypted = self.core.xmpp['xep_0454'] and tab.e2e_encryption is not None + asyncio.create_task(self.send_upload(filename, tab, encrypted)) @staticmethod def completion_filename(the_input): |