diff options
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/ibb_transfer/ibb_receiver.py | 10 | ||||
-rw-r--r-- | examples/migrate_roster.py | 117 |
2 files changed, 121 insertions, 6 deletions
diff --git a/examples/ibb_transfer/ibb_receiver.py b/examples/ibb_transfer/ibb_receiver.py index 0169d63d..6aba98e3 100755 --- a/examples/ibb_transfer/ibb_receiver.py +++ b/examples/ibb_transfer/ibb_receiver.py @@ -38,7 +38,7 @@ class IBBReceiver(sleekxmpp.ClientXMPP): self.register_plugin('xep_0030') # Service Discovery self.register_plugin('xep_0047', { - 'accept_stream': self.accept_stream + 'auto_accept': True }) # In-band Bytestreams # The session_start event will be triggered when @@ -48,7 +48,7 @@ class IBBReceiver(sleekxmpp.ClientXMPP): # our roster. self.add_event_handler("session_start", self.start) - self.add_event_handler("ibb_stream_start", self.stream_opened) + self.add_event_handler("ibb_stream_start", self.stream_opened, threaded=True) self.add_event_handler("ibb_stream_data", self.stream_data) def start(self, event): @@ -69,7 +69,7 @@ class IBBReceiver(sleekxmpp.ClientXMPP): def accept_stream(self, iq): """ - Check that it is ok to accept a stream request. + Check that it is ok to accept a stream request. Controlling stream acceptance can be done via either: - setting 'auto_accept' to False in the plugin @@ -83,9 +83,7 @@ class IBBReceiver(sleekxmpp.ClientXMPP): return True def stream_opened(self, stream): - # NOTE: IBB streams are bi-directional, so the original sender is - # now the opened stream's receiver. - print('Stream opened: %s from %s' % (stream.sid, stream.receiver)) + print('Stream opened: %s from %s' % (stream.sid, stream.peer_jid)) # You could run a loop reading from the stream using stream.recv(), # or use the ibb_stream_data event. diff --git a/examples/migrate_roster.py b/examples/migrate_roster.py new file mode 100644 index 00000000..a93bdecd --- /dev/null +++ b/examples/migrate_roster.py @@ -0,0 +1,117 @@ +import sys +import logging +import getpass +from optparse import OptionParser + +import sleekxmpp + +# Python versions before 3.0 do not use UTF-8 encoding +# by default. To ensure that Unicode is handled properly +# throughout SleekXMPP, we will set the default encoding +# ourselves to UTF-8. +if sys.version_info < (3, 0): + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') +else: + raw_input = input + + +# Setup the command line arguments. +optp = OptionParser() + +# Output verbosity options. +optp.add_option('-q', '--quiet', help='set logging to ERROR', + action='store_const', dest='loglevel', + const=logging.ERROR, default=logging.INFO) +optp.add_option('-d', '--debug', help='set logging to DEBUG', + action='store_const', dest='loglevel', + const=logging.DEBUG, default=logging.INFO) +optp.add_option('-v', '--verbose', help='set logging to COMM', + action='store_const', dest='loglevel', + const=5, default=logging.INFO) + +# JID and password options. +optp.add_option("--oldjid", dest="old_jid", + help="JID of the old account") +optp.add_option("--oldpassword", dest="old_password", + help="password of the old account") + +optp.add_option("--newjid", dest="new_jid", + help="JID of the old account") +optp.add_option("--newpassword", dest="new_password", + help="password of the old account") + + +opts, args = optp.parse_args() + +# Setup logging. +logging.basicConfig(level=opts.loglevel, + format='%(levelname)-8s %(message)s') + +if opts.old_jid is None: + opts.old_jid = raw_input("Old JID: ") +if opts.old_password is None: + opts.old_password = getpass.getpass("Old Password: ") + +if opts.new_jid is None: + opts.new_jid = raw_input("New JID: ") +if opts.new_password is None: + opts.new_password = getpass.getpass("New Password: ") + + +old_xmpp = sleekxmpp.ClientXMPP(opts.old_jid, opts.old_password) + +# If you are connecting to Facebook and wish to use the +# X-FACEBOOK-PLATFORM authentication mechanism, you will need +# your API key and an access token. Then you'll set: +# xmpp.credentials['api_key'] = 'THE_API_KEY' +# xmpp.credentials['access_token'] = 'THE_ACCESS_TOKEN' + +# If you are connecting to MSN, then you will need an +# access token, and it does not matter what JID you +# specify other than that the domain is 'messenger.live.com', +# so '_@messenger.live.com' will work. You can specify +# the access token as so: +# xmpp.credentials['access_token'] = 'THE_ACCESS_TOKEN' + +# If you are working with an OpenFire server, you may need +# to adjust the SSL version used: +# xmpp.ssl_version = ssl.PROTOCOL_SSLv3 + +# If you want to verify the SSL certificates offered by a server: +# xmpp.ca_certs = "path/to/ca/cert" + +roster = [] + +def on_session(event): + roster.append(old_xmpp.get_roster()) + old_xmpp.disconnect() +old_xmpp.add_event_handler('session_start', on_session) + +if old_xmpp.connect(): + old_xmpp.process(block=True) + +if not roster: + print('No roster to migrate') + sys.exit() + +new_xmpp = sleekxmpp.ClientXMPP(opts.new_jid, opts.new_password) +def on_session2(event): + new_xmpp.get_roster() + new_xmpp.send_presence() + + logging.info(roster[0]) + data = roster[0]['roster']['items'] + logging.info(data) + + for jid, item in data.items(): + if item['subscription'] != 'none': + new_xmpp.send_presence(ptype='subscribe', pto=jid) + new_xmpp.update_roster(jid, + name = item['name'], + groups = item['groups']) + new_xmpp.disconnect() +new_xmpp.add_event_handler('session_start', on_session2) + +if new_xmpp.connect(): + new_xmpp.process(block=True) |