2016-04-13 11:55:26 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
import slixmpp
|
|
|
|
from slixmpp.exceptions import IqError, IqTimeout
|
|
|
|
|
|
|
|
from base.Scope import Scope
|
|
|
|
|
|
|
|
|
|
|
|
class FileTransfer(slixmpp.ClientXMPP):
|
|
|
|
def __init__(self, file_path):
|
|
|
|
|
2016-04-13 14:20:01 +03:00
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
|
2016-04-13 11:55:26 +03:00
|
|
|
scope = Scope().getInstance()
|
|
|
|
|
|
|
|
self.logger = scope.getLogger()
|
|
|
|
self.configuration_manager = scope.getConfigurationManager()
|
|
|
|
|
|
|
|
self.file = open(file_path, 'rb')
|
|
|
|
self.my_jid = str(self.configuration_manager.get('CONNECTION', 'uid') + '@' + self.configuration_manager.get('CONNECTION', 'servicename') + '/sender')
|
|
|
|
self.my_pass = str(self.configuration_manager.get('CONNECTION', 'password'))
|
|
|
|
self.receiver = self.configuration_manager.get('CONNECTION', 'receiverjid') + '@' + self.configuration_manager.get('CONNECTION', 'servicename') + '/Smack'
|
|
|
|
|
2016-04-13 14:20:01 +03:00
|
|
|
try:
|
|
|
|
slixmpp.ClientXMPP.__init__(self, self.my_jid, self.my_pass)
|
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
2016-04-13 11:55:26 +03:00
|
|
|
|
|
|
|
self.register_plugin('xep_0030') # Service Discovery
|
|
|
|
self.register_plugin('xep_0065')
|
|
|
|
|
|
|
|
self.add_event_handler("session_start", self.start)
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def start(self, event):
|
|
|
|
print('sending...')
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Open the S5B stream in which to write to.
|
|
|
|
proxy = yield from 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)
|
|
|
|
|
|
|
|
# And finally close the stream.
|
|
|
|
proxy.transport.write_eof()
|
|
|
|
except (IqError, IqTimeout) as e:
|
|
|
|
print('File transfer errored' + str(e))
|
|
|
|
else:
|
|
|
|
print('File transfer finished')
|
|
|
|
finally:
|
|
|
|
self.file.close()
|
|
|
|
self.disconnect()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def run(file_path):
|
|
|
|
xmpp = FileTransfer(file_path)
|
|
|
|
xmpp.connect()
|
|
|
|
xmpp.process(forever=False)
|