From a468e1614092a4bc448f473272cc41ca992b4171 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 8 Mar 2018 15:04:59 +0100 Subject: Add HTTP File Upload support. --- doc/source/dev/xep.rst | 2 ++ doc/source/plugins/index.rst | 6 +++++ plugins/upload.py | 59 ++++++++++++++++++++++++++++++++++++++++++++ poezio/connection.py | 8 ++++++ 4 files changed, 75 insertions(+) create mode 100644 plugins/upload.py diff --git a/doc/source/dev/xep.rst b/doc/source/dev/xep.rst index c185a240..3071e82e 100644 --- a/doc/source/dev/xep.rst +++ b/doc/source/dev/xep.rst @@ -107,6 +107,8 @@ Table of all XEPs implemented in poezio. +----------+-------------------------+---------------------+ |0352 |Client State Indication |100% | +----------+-------------------------+---------------------+ +|0363 |HTTP File Upload |100% | ++----------+-------------------------+---------------------+ |0364 |Current OTR Usage |100% | +----------+-------------------------+---------------------+ |0375 |Compliance Suites 2016 |Advanced Client + | diff --git a/doc/source/plugins/index.rst b/doc/source/plugins/index.rst index ca410ea1..2bfea11f 100644 --- a/doc/source/plugins/index.rst +++ b/doc/source/plugins/index.rst @@ -293,6 +293,11 @@ Plugin index Add a ``/vcard`` command to retrieve and display a vCard. + Upload + :ref:`Documentation ` + + Add an ``/upload`` command to upload a file. + .. toctree:: :hidden: @@ -340,3 +345,4 @@ Plugin index marquee server_part vcard + upload diff --git a/plugins/upload.py b/plugins/upload.py new file mode 100644 index 00000000..98379c33 --- /dev/null +++ b/plugins/upload.py @@ -0,0 +1,59 @@ +""" +Upload a file and auto-complete the input with its URL. + +Usage +----- + +This plugin adds a command to the chat tabs. + +.. glossary:: + + /upload + **Usage:** ``/upload `` + + Uploads the file to the preferred HTTP File Upload + service (see XEP-0363) and fill the input with its URL. + + +""" +import asyncio +from os.path import expanduser +from glob import glob + +from poezio.plugin import BasePlugin +from poezio.core.structs import Completion +from poezio.decorators import command_args_parser +from poezio import tabs + +class Plugin(BasePlugin): + + def init(self): + for _class in (tabs.PrivateTab, tabs.ConversationTab, tabs.MucTab): + self.api.add_tab_command(_class, 'upload', self.command_upload, + usage='', + help='Upload a file and auto-complete the input with its URL.', + short='Upload a file', + completion=self.completion_filename) + + @asyncio.coroutine + def async_upload(self, filename): + try: + url = yield from self.core.xmpp['xep_0363'].upload_file(filename) + except Exception as e: + self.api.information('Failed to upload the file: %s(%s)' % (type(e), e), 'Error') + self.core.insert_input_text(url) + + @command_args_parser.quoted(1) + def command_upload(self, args): + if args is None: + self.core.command.help('upload') + return + filename, = args + filename = expanduser(filename) + asyncio.ensure_future(self.async_upload(filename)) + + @staticmethod + def completion_filename(the_input): + txt = expanduser(the_input.get_text()[8:]) + files = glob(txt + '*') + return Completion(the_input.auto_completion, files, quotify=False) diff --git a/poezio/connection.py b/poezio/connection.py index 61837ecb..3ccfa8ce 100644 --- a/poezio/connection.py +++ b/poezio/connection.py @@ -174,6 +174,14 @@ class Connection(slixmpp.ClientXMPP): self.register_plugin('xep_0319') self.register_plugin('xep_0334') self.register_plugin('xep_0352') + try: + self.register_plugin('xep_0363') + except SyntaxError: + log.error('Failed to load HTTP File Upload plugin, it can only be ' + 'used on Python 3.5+') + except slixmpp.plugins.base.PluginNotFound: + log.error('Failed to load HTTP File Upload plugin, it can only be ' + 'used with aiohttp installed') self.register_plugin('xep_0380') self.init_plugins() -- cgit v1.2.3