diff options
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/confirm_answer.py | 1 | ||||
-rwxr-xr-x | examples/confirm_ask.py | 7 | ||||
-rwxr-xr-x | examples/disco_browser.py | 8 | ||||
-rwxr-xr-x | examples/download_avatars.py | 17 | ||||
-rwxr-xr-x | examples/http_upload.py | 16 | ||||
-rwxr-xr-x | examples/ibb_transfer/ibb_sender.py | 10 | ||||
-rwxr-xr-x | examples/mam.py | 1 | ||||
-rwxr-xr-x | examples/ping.py | 6 | ||||
-rwxr-xr-x | examples/pubsub_client.py | 46 | ||||
-rwxr-xr-x | examples/register_account.py | 4 | ||||
-rwxr-xr-x | examples/roster_browser.py | 7 | ||||
-rwxr-xr-x | examples/s5b_transfer/s5b_receiver.py | 1 | ||||
-rwxr-xr-x | examples/s5b_transfer/s5b_sender.py | 8 | ||||
-rwxr-xr-x | examples/set_avatar.py | 10 |
14 files changed, 63 insertions, 79 deletions
diff --git a/examples/confirm_answer.py b/examples/confirm_answer.py index 9cfe30f1..f2c4c273 100755 --- a/examples/confirm_answer.py +++ b/examples/confirm_answer.py @@ -15,7 +15,6 @@ from argparse import ArgumentParser import slixmpp from slixmpp.exceptions import XMPPError -from slixmpp import asyncio log = logging.getLogger(__name__) diff --git a/examples/confirm_ask.py b/examples/confirm_ask.py index e3cd184a..27fdfde7 100755 --- a/examples/confirm_ask.py +++ b/examples/confirm_ask.py @@ -51,18 +51,17 @@ class AskConfirm(slixmpp.ClientXMPP): else: self.confirmed.set_result(True) - @asyncio.coroutine - def start(self, event): + async def start(self, event): log.info('Sending confirm request %s to %s who wants to access %s using ' 'method %s...' % (self.id, self.recipient, self.url, self.method)) try: - confirmed = yield from self['xep_0070'].ask_confirm(self.recipient, + confirmed = await self['xep_0070'].ask_confirm(self.recipient, id=self.id, url=self.url, method=self.method, message='Plz say yes or no for {method} {url} ({id}).') if isinstance(confirmed, slixmpp.Message): - confirmed = yield from self.confirmed + confirmed = await self.confirmed else: confirmed = True except IqError: diff --git a/examples/disco_browser.py b/examples/disco_browser.py index a9e8711f..f0ece32d 100755 --- a/examples/disco_browser.py +++ b/examples/disco_browser.py @@ -15,7 +15,6 @@ from argparse import ArgumentParser import slixmpp from slixmpp.exceptions import IqError, IqTimeout -from slixmpp.xmlstream.asyncio import asyncio class Disco(slixmpp.ClientXMPP): @@ -54,8 +53,7 @@ class Disco(slixmpp.ClientXMPP): # our roster. self.add_event_handler("session_start", self.start) - @asyncio.coroutine - def start(self, event): + async def start(self, event): """ Process the session_start event. @@ -77,13 +75,13 @@ class Disco(slixmpp.ClientXMPP): try: if self.get in self.info_types: # function using the callback parameter. - info = yield from self['xep_0030'].get_info(jid=self.target_jid, + info = await self['xep_0030'].get_info(jid=self.target_jid, node=self.target_node) if self.get in self.items_types: # The same applies from above. Listen for the # disco_items event or pass a callback function # if you need to process a non-blocking request. - items = yield from self['xep_0030'].get_items(jid=self.target_jid, + items = await self['xep_0030'].get_items(jid=self.target_jid, node=self.target_node) if self.get not in self.info_types and self.get not in self.items_types: logging.error("Invalid disco request type.") diff --git a/examples/download_avatars.py b/examples/download_avatars.py index 408c2146..02591e3e 100755 --- a/examples/download_avatars.py +++ b/examples/download_avatars.py @@ -47,8 +47,7 @@ class AvatarDownloader(slixmpp.ClientXMPP): self.roster_received.set() self.presences_received.clear() - @asyncio.coroutine - def start(self, event): + async def start(self, event): """ Process the session_start event. @@ -65,16 +64,15 @@ class AvatarDownloader(slixmpp.ClientXMPP): self.get_roster(callback=self.roster_received_cb) print('Waiting for presence updates...\n') - yield from self.roster_received.wait() + await self.roster_received.wait() print('Roster received') - yield from self.presences_received.wait() + await self.presences_received.wait() self.disconnect() - @asyncio.coroutine - def on_vcard_avatar(self, pres): + async def on_vcard_avatar(self, pres): print("Received vCard avatar update from %s" % pres['from'].bare) try: - result = yield from self['xep_0054'].get_vcard(pres['from'].bare, cached=True, + result = await self['xep_0054'].get_vcard(pres['from'].bare, cached=True, timeout=5) except XMPPError: print("Error retrieving avatar for %s" % pres['from']) @@ -89,14 +87,13 @@ class AvatarDownloader(slixmpp.ClientXMPP): with open(filename, 'wb+') as img: img.write(avatar['BINVAL']) - @asyncio.coroutine - def on_avatar(self, msg): + async def on_avatar(self, msg): print("Received avatar update from %s" % msg['from']) metadata = msg['pubsub_event']['items']['item']['avatar_metadata'] for info in metadata['items']: if not info['url']: try: - result = yield from self['xep_0084'].retrieve_avatar(msg['from'].bare, info['id'], + result = await self['xep_0084'].retrieve_avatar(msg['from'].bare, info['id'], timeout=5) except XMPPError: print("Error retrieving avatar for %s" % msg['from']) diff --git a/examples/http_upload.py b/examples/http_upload.py index e34b7b01..268a22dc 100755 --- a/examples/http_upload.py +++ b/examples/http_upload.py @@ -14,7 +14,6 @@ from getpass import getpass from argparse import ArgumentParser import slixmpp -from slixmpp import asyncio log = logging.getLogger(__name__) @@ -25,18 +24,21 @@ 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): + def __init__(self, jid, password, recipient, filename, domain=None): slixmpp.ClientXMPP.__init__(self, jid, password) self.recipient = recipient self.filename = filename + self.domain = domain self.add_event_handler("session_start", self.start) - @asyncio.coroutine - def start(self, event): + async def start(self, event): log.info('Uploading file %s...', self.filename) - url = yield from self['xep_0363'].upload_file(self.filename) + def timeout_callback(arg): + raise TimeoutError("could not send message in time") + url = await self['xep_0363'].upload_file( + self.filename, domain=self.domain, timeout=10, timeout_callback=timeout_callback) log.info('Upload success!') log.info('Sending file to %s', self.recipient) @@ -70,6 +72,8 @@ if __name__ == '__main__': help="Recipient JID") parser.add_argument("-f", "--file", required=True, help="File to send") + parser.add_argument("--domain", + help="Domain to use for HTTP File Upload (leave out for your own server’s)") args = parser.parse_args() @@ -82,7 +86,7 @@ if __name__ == '__main__': if args.password is None: args.password = getpass("Password: ") - xmpp = HttpUpload(args.jid, args.password, args.recipient, args.file) + xmpp = HttpUpload(args.jid, args.password, args.recipient, args.file, args.domain) xmpp.register_plugin('xep_0071') xmpp.register_plugin('xep_0128') xmpp.register_plugin('xep_0363') diff --git a/examples/ibb_transfer/ibb_sender.py b/examples/ibb_transfer/ibb_sender.py index f1c0cab2..8600ac80 100755 --- a/examples/ibb_transfer/ibb_sender.py +++ b/examples/ibb_transfer/ibb_sender.py @@ -9,7 +9,6 @@ See the file LICENSE for copying permission. """ -import asyncio import logging from getpass import getpass from argparse import ArgumentParser @@ -39,8 +38,7 @@ class IBBSender(slixmpp.ClientXMPP): # our roster. self.add_event_handler("session_start", self.start) - @asyncio.coroutine - def start(self, event): + async def start(self, event): """ Process the session_start event. @@ -58,13 +56,13 @@ class IBBSender(slixmpp.ClientXMPP): try: # Open the IBB stream in which to write to. - stream = yield from self['xep_0047'].open_stream(self.receiver, use_messages=self.use_messages) + stream = await self['xep_0047'].open_stream(self.receiver, use_messages=self.use_messages) # If you want to send in-memory bytes, use stream.sendall() instead. - yield from stream.sendfile(self.file, timeout=10) + await stream.sendfile(self.file, timeout=10) # And finally close the stream. - yield from stream.close(timeout=10) + await stream.close(timeout=10) except (IqError, IqTimeout): print('File transfer errored') else: diff --git a/examples/mam.py b/examples/mam.py index 5f9dde3a..cd6b738a 100755 --- a/examples/mam.py +++ b/examples/mam.py @@ -15,7 +15,6 @@ from argparse import ArgumentParser import slixmpp from slixmpp.exceptions import XMPPError -from slixmpp import asyncio log = logging.getLogger(__name__) diff --git a/examples/ping.py b/examples/ping.py index 39118ac4..cb1bb968 100755 --- a/examples/ping.py +++ b/examples/ping.py @@ -13,7 +13,6 @@ import logging from getpass import getpass from argparse import ArgumentParser from slixmpp.exceptions import IqError, IqTimeout -from slixmpp import asyncio import slixmpp @@ -38,8 +37,7 @@ class PingTest(slixmpp.ClientXMPP): # our roster. self.add_event_handler("session_start", self.start) - @asyncio.coroutine - def start(self, event): + async def start(self, event): """ Process the session_start event. @@ -56,7 +54,7 @@ class PingTest(slixmpp.ClientXMPP): self.get_roster() try: - rtt = yield from self['xep_0199'].ping(self.pingjid, + rtt = await self['xep_0199'].ping(self.pingjid, timeout=10) logging.info("Success! RTT: %s", rtt) except IqError as e: diff --git a/examples/pubsub_client.py b/examples/pubsub_client.py index 9a1d4bb6..480c7d89 100755 --- a/examples/pubsub_client.py +++ b/examples/pubsub_client.py @@ -5,7 +5,6 @@ import logging from getpass import getpass from argparse import ArgumentParser -import asyncio import slixmpp from slixmpp.exceptions import XMPPError from slixmpp.xmlstream import ET, tostring @@ -32,87 +31,86 @@ class PubsubClient(slixmpp.ClientXMPP): self.add_event_handler('session_start', self.start) - @asyncio.coroutine - def start(self, event): + async def start(self, event): self.get_roster() self.send_presence() try: - yield from getattr(self, self.action)() + await getattr(self, self.action)() except: logging.exception('Could not execute %s:', self.action) self.disconnect() - def nodes(self): + async def nodes(self): try: - result = yield from self['xep_0060'].get_nodes(self.pubsub_server, self.node) + result = await self['xep_0060'].get_nodes(self.pubsub_server, self.node) for item in result['disco_items']['items']: logging.info(' - %s', str(item)) except XMPPError as error: logging.error('Could not retrieve node list: %s', error.format()) - def create(self): + async def create(self): try: - yield from self['xep_0060'].create_node(self.pubsub_server, self.node) + await self['xep_0060'].create_node(self.pubsub_server, self.node) logging.info('Created node %s', self.node) except XMPPError as error: logging.error('Could not create node %s: %s', self.node, error.format()) - def delete(self): + async def delete(self): try: - yield from self['xep_0060'].delete_node(self.pubsub_server, self.node) + await self['xep_0060'].delete_node(self.pubsub_server, self.node) logging.info('Deleted node %s', self.node) except XMPPError as error: logging.error('Could not delete node %s: %s', self.node, error.format()) - def get_configure(self): + async def get_configure(self): try: - configuration_form = yield from self['xep_0060'].get_node_config(self.pubsub_server, self.node) + configuration_form = await self['xep_0060'].get_node_config(self.pubsub_server, self.node) logging.info('Configure form received from node %s: %s', self.node, configuration_form['pubsub_owner']['configure']['form']) except XMPPError as error: logging.error('Could not retrieve configure form from node %s: %s', self.node, error.format()) - def publish(self): + async def publish(self): payload = ET.fromstring("<test xmlns='test'>%s</test>" % self.data) try: - result = yield from self['xep_0060'].publish(self.pubsub_server, self.node, payload=payload) + result = await self['xep_0060'].publish(self.pubsub_server, self.node, payload=payload) logging.info('Published at item id: %s', result['pubsub']['publish']['item']['id']) except XMPPError as error: logging.error('Could not publish to %s: %s', self.node, error.format()) - def get(self): + async def get(self): try: - result = yield from self['xep_0060'].get_item(self.pubsub_server, self.node, self.data) + result = await self['xep_0060'].get_item(self.pubsub_server, self.node, self.data) for item in result['pubsub']['items']['substanzas']: logging.info('Retrieved item %s: %s', item['id'], tostring(item['payload'])) except XMPPError as error: logging.error('Could not retrieve item %s from node %s: %s', self.data, self.node, error.format()) - def retract(self): + async def retract(self): try: - yield from self['xep_0060'].retract(self.pubsub_server, self.node, self.data) + await self['xep_0060'].retract(self.pubsub_server, self.node, self.data) logging.info('Retracted item %s from node %s', self.data, self.node) except XMPPError as error: logging.error('Could not retract item %s from node %s: %s', self.data, self.node, error.format()) - def purge(self): + async def purge(self): try: - yield from self['xep_0060'].purge(self.pubsub_server, self.node) + await self['xep_0060'].purge(self.pubsub_server, self.node) logging.info('Purged all items from node %s', self.node) except XMPPError as error: logging.error('Could not purge items from node %s: %s', self.node, error.format()) - def subscribe(self): + async def subscribe(self): try: - iq = yield from self['xep_0060'].subscribe(self.pubsub_server, self.node) + iq = await self['xep_0060'].subscribe(self.pubsub_server, self.node) subscription = iq['pubsub']['subscription'] logging.info('Subscribed %s to node %s', subscription['jid'], subscription['node']) except XMPPError as error: logging.error('Could not subscribe %s to node %s: %s', self.boundjid.bare, self.node, error.format()) - def unsubscribe(self): + async def unsubscribe(self): try: - yield from self['xep_0060'].unsubscribe(self.pubsub_server, self.node) + await self['xep_0060'].unsubscribe(self.pubsub_server, self.node) logging.info('Unsubscribed %s from node %s', self.boundjid.bare, self.node) except XMPPError as error: logging.error('Could not unsubscribe %s from node %s: %s', self.boundjid.bare, self.node, error.format()) diff --git a/examples/register_account.py b/examples/register_account.py index 8ec238e9..76b5fcfc 100755 --- a/examples/register_account.py +++ b/examples/register_account.py @@ -66,7 +66,7 @@ class RegisterBot(slixmpp.ClientXMPP): # We're only concerned about registering, so nothing more to do here. self.disconnect() - def register(self, iq): + async def register(self, iq): """ Fill out and submit a registration form. @@ -90,7 +90,7 @@ class RegisterBot(slixmpp.ClientXMPP): resp['register']['password'] = self.password try: - yield from resp.send() + await resp.send() logging.info("Account created for %s!" % self.boundjid) except IqError as e: logging.error("Could not register account: %s" % diff --git a/examples/roster_browser.py b/examples/roster_browser.py index eb92ad2a..6ad8b2a4 100755 --- a/examples/roster_browser.py +++ b/examples/roster_browser.py @@ -38,8 +38,7 @@ class RosterBrowser(slixmpp.ClientXMPP): self.received = set() self.presences_received = asyncio.Event() - @asyncio.coroutine - def start(self, event): + async def start(self, event): """ Process the session_start event. @@ -57,7 +56,7 @@ class RosterBrowser(slixmpp.ClientXMPP): future.set_result(None) try: self.get_roster(callback=callback) - yield from future + await future except IqError as err: print('Error: %s' % err.iq['error']['condition']) except IqTimeout: @@ -66,7 +65,7 @@ class RosterBrowser(slixmpp.ClientXMPP): print('Waiting for presence updates...\n') - yield from asyncio.sleep(10) + await asyncio.sleep(10) print('Roster for %s' % self.boundjid.bare) groups = self.client_roster.groups() diff --git a/examples/s5b_transfer/s5b_receiver.py b/examples/s5b_transfer/s5b_receiver.py index bedeaa04..d97d4777 100755 --- a/examples/s5b_transfer/s5b_receiver.py +++ b/examples/s5b_transfer/s5b_receiver.py @@ -9,7 +9,6 @@ See the file LICENSE for copying permission. """ -import asyncio import logging from getpass import getpass from argparse import ArgumentParser diff --git a/examples/s5b_transfer/s5b_sender.py b/examples/s5b_transfer/s5b_sender.py index 70a9704f..7ef5c99c 100755 --- a/examples/s5b_transfer/s5b_sender.py +++ b/examples/s5b_transfer/s5b_sender.py @@ -9,7 +9,6 @@ See the file LICENSE for copying permission. """ -import asyncio import logging from getpass import getpass from argparse import ArgumentParser @@ -36,8 +35,7 @@ class S5BSender(slixmpp.ClientXMPP): # and the XML streams are ready for use. self.add_event_handler("session_start", self.start) - @asyncio.coroutine - def start(self, event): + async def start(self, event): """ Process the session_start event. @@ -53,14 +51,14 @@ class S5BSender(slixmpp.ClientXMPP): try: # Open the S5B stream in which to write to. - proxy = yield from self['xep_0065'].handshake(self.receiver) + proxy = await self['xep_0065'].handshake(self.receiver) # Send the entire file. while True: data = self.file.read(1048576) if not data: break - yield from proxy.write(data) + await proxy.write(data) # And finally close the stream. proxy.transport.write_eof() diff --git a/examples/set_avatar.py b/examples/set_avatar.py index f9641b1e..6579b2e7 100755 --- a/examples/set_avatar.py +++ b/examples/set_avatar.py @@ -18,7 +18,6 @@ from argparse import ArgumentParser import slixmpp from slixmpp.exceptions import XMPPError -from slixmpp import asyncio class AvatarSetter(slixmpp.ClientXMPP): @@ -33,8 +32,7 @@ class AvatarSetter(slixmpp.ClientXMPP): self.filepath = filepath - @asyncio.coroutine - def start(self, event): + async def start(self, event): """ Process the session_start event. @@ -68,20 +66,20 @@ class AvatarSetter(slixmpp.ClientXMPP): used_xep84 = False print('Publish XEP-0084 avatar data') - result = yield from self['xep_0084'].publish_avatar(avatar) + result = await self['xep_0084'].publish_avatar(avatar) if isinstance(result, XMPPError): print('Could not publish XEP-0084 avatar') else: used_xep84 = True print('Update vCard with avatar') - result = yield from self['xep_0153'].set_avatar(avatar=avatar, mtype=avatar_type) + result = await self['xep_0153'].set_avatar(avatar=avatar, mtype=avatar_type) if isinstance(result, XMPPError): print('Could not set vCard avatar') if used_xep84: print('Advertise XEP-0084 avatar metadata') - result = yield from self['xep_0084'].publish_avatar_metadata([ + result = await self['xep_0084'].publish_avatar_metadata([ {'id': avatar_id, 'type': avatar_type, 'bytes': avatar_bytes} |