summaryrefslogtreecommitdiff
path: root/examples/http_upload.py
diff options
context:
space:
mode:
authorMaxime Buquet <pep@bouah.net>2022-03-21 17:01:40 +0100
committerMaxime Buquet <pep@bouah.net>2022-03-21 17:01:40 +0100
commit82ff68cfacf55aa1b79aa2f84f085ca346a75a74 (patch)
treedfc04e95d6c01c68d43b62d66d520c6cacf39a3c /examples/http_upload.py
parentfcec6742cf3cc4d46793d7a72236a101cfa13ffd (diff)
parent28d44ecf74385e4adeaa03c9fa8b561b3ca4d9a0 (diff)
downloadslixmpp-82ff68cfacf55aa1b79aa2f84f085ca346a75a74.tar.gz
slixmpp-82ff68cfacf55aa1b79aa2f84f085ca346a75a74.tar.bz2
slixmpp-82ff68cfacf55aa1b79aa2f84f085ca346a75a74.tar.xz
slixmpp-82ff68cfacf55aa1b79aa2f84f085ca346a75a74.zip
Merge branch 'upload-encrypt' into 'master'
XEP-0454: OMEMO Media Sharing See merge request poezio/slixmpp!189
Diffstat (limited to 'examples/http_upload.py')
-rwxr-xr-xexamples/http_upload.py65
1 files changed, 60 insertions, 5 deletions
diff --git a/examples/http_upload.py b/examples/http_upload.py
index a926fd47..b62c736e 100755
--- a/examples/http_upload.py
+++ b/examples/http_upload.py
@@ -5,11 +5,16 @@
# This file is part of Slixmpp.
# See the file LICENSE for copying permission.
+from typing import Optional
+
+import sys
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,20 +26,40 @@ 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,
+ 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 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,
)
except IqTimeout:
raise TimeoutError('Could not send message in time')
@@ -79,6 +104,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.
@@ -86,15 +115,41 @@ 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)
+
+ 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')
+ 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()