From 54b724c28b1048d8508f1523693d4f7df340fc13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Thu, 17 Mar 2022 20:56:03 +0100 Subject: examples/http_upload: Add some typing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- examples/http_upload.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'examples/http_upload.py') diff --git a/examples/http_upload.py b/examples/http_upload.py index a926fd47..cac8fc7e 100755 --- a/examples/http_upload.py +++ b/examples/http_upload.py @@ -5,11 +5,15 @@ # This file is part of Slixmpp. # See the file LICENSE for copying permission. +from typing import Optional + import logging +from pathlib import Path from getpass import getpass from argparse import ArgumentParser import slixmpp +from slixmpp import JID from slixmpp.exceptions import IqTimeout log = logging.getLogger(__name__) @@ -21,7 +25,14 @@ class HttpUpload(slixmpp.ClientXMPP): A basic client asking an entity if they confirm the access to an HTTP URL. """ - def __init__(self, jid, password, recipient, filename, domain=None): + def __init__( + self, + jid: JID, + password: str, + recipient: JID, + filename: Path, + domain: Optional[JID] = None, + ): slixmpp.ClientXMPP.__init__(self, jid, password) self.recipient = recipient @@ -86,11 +97,21 @@ if __name__ == '__main__': format='%(levelname)-8s %(message)s') if args.jid is None: - args.jid = input("Username: ") + args.jid = JID(input("Username: ")) if args.password is None: args.password = getpass("Password: ") - xmpp = HttpUpload(args.jid, args.password, args.recipient, args.file, args.domain) + domain = args.domain + if domain is not None: + domain = JID(domain) + + xmpp = HttpUpload( + jid=args.jid, + password=args.password, + recipient=JID(args.recipient), + filename=Path(args.file), + domain=domain, + ) xmpp.register_plugin('xep_0066') xmpp.register_plugin('xep_0071') xmpp.register_plugin('xep_0128') -- cgit v1.2.3 From bde5aaaf3e197f1fb71bbdabbeb597d8e8f9b4a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Fri, 18 Mar 2022 15:22:52 +0100 Subject: examples/http_upload.py: Add --encrypt parameter to send encrypted files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- examples/http_upload.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'examples/http_upload.py') diff --git a/examples/http_upload.py b/examples/http_upload.py index cac8fc7e..7f1c44bc 100755 --- a/examples/http_upload.py +++ b/examples/http_upload.py @@ -7,6 +7,7 @@ from typing import Optional +import sys import logging from pathlib import Path from getpass import getpass @@ -32,20 +33,25 @@ class HttpUpload(slixmpp.ClientXMPP): recipient: JID, filename: Path, domain: Optional[JID] = None, + encrypted: bool = False, ): slixmpp.ClientXMPP.__init__(self, jid, password) self.recipient = recipient self.filename = filename self.domain = domain + self.encrypted = encrypted self.add_event_handler("session_start", self.start) async def start(self, event): log.info('Uploading file %s...', self.filename) try: - url = await self['xep_0363'].upload_file( - self.filename, domain=self.domain, timeout=10 + upload_file = self['xep_0363'].upload_file + if self.encrypted: + upload_file = self['xep_0454'].upload_file + url = await upload_file( + self.filename, domain=self.domain, timeout=10, ) except IqTimeout: raise TimeoutError('Could not send message in time') @@ -90,6 +96,10 @@ if __name__ == '__main__': parser.add_argument("--domain", help="Domain to use for HTTP File Upload (leave out for your own server’s)") + parser.add_argument("-e", "--encrypt", dest="encrypted", + help="Whether to encrypt", action="store_true", + default=False) + args = parser.parse_args() # Setup logging. @@ -105,17 +115,27 @@ if __name__ == '__main__': if domain is not None: domain = JID(domain) + if args.encrypted: + print( + 'You are using the --encrypt flag. ' + 'Be aware that the transport being used is NOT end-to-end ' + 'encrypted. The server will be able to decrypt the file.', + file=sys.stderr, + ) + xmpp = HttpUpload( jid=args.jid, password=args.password, recipient=JID(args.recipient), filename=Path(args.file), domain=domain, + encrypted=args.encrypted, ) xmpp.register_plugin('xep_0066') xmpp.register_plugin('xep_0071') xmpp.register_plugin('xep_0128') xmpp.register_plugin('xep_0363') + xmpp.register_plugin('xep_0454') # Connect to the XMPP server and start processing XMPP stanzas. xmpp.connect() -- cgit v1.2.3 From 53d38a81154a0cf5adbe798af6a9af739079e65b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 19 Mar 2022 10:26:09 +0100 Subject: setup.py: add cryptography in extras_require; update example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- examples/http_upload.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'examples/http_upload.py') diff --git a/examples/http_upload.py b/examples/http_upload.py index 7f1c44bc..b62c736e 100755 --- a/examples/http_upload.py +++ b/examples/http_upload.py @@ -48,7 +48,15 @@ class HttpUpload(slixmpp.ClientXMPP): log.info('Uploading file %s...', self.filename) try: upload_file = self['xep_0363'].upload_file - if self.encrypted: + if self.encrypted and not self['xep_0454']: + print( + 'The xep_0454 module isn\'t available. ' + 'Ensure you have \'cryptography\' ' + 'from extras_require installed.', + file=sys.stderr, + ) + return + elif self.encrypted: upload_file = self['xep_0454'].upload_file url = await upload_file( self.filename, domain=self.domain, timeout=10, @@ -135,7 +143,13 @@ if __name__ == '__main__': xmpp.register_plugin('xep_0071') xmpp.register_plugin('xep_0128') xmpp.register_plugin('xep_0363') - xmpp.register_plugin('xep_0454') + try: + xmpp.register_plugin('xep_0454') + except slixmpp.plugins.base.PluginNotFound: + log.error( + 'Could not load xep_0454. ' + 'Ensure you have \'cryptography\' from extras_require installed.' + ) # Connect to the XMPP server and start processing XMPP stanzas. xmpp.connect() -- cgit v1.2.3