diff --git a/opt/ahenk/base/messaging/FileTransfer.py b/opt/ahenk/base/messaging/FileTransfer.py new file mode 100644 index 0000000..e3f4bfa --- /dev/null +++ b/opt/ahenk/base/messaging/FileTransfer.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Author: Volkan Şahin + +import asyncio + +import slixmpp +from slixmpp.exceptions import IqError, IqTimeout + +from base.Scope import Scope + + +class FileTransfer(slixmpp.ClientXMPP): + def __init__(self, file_path): + + print('init') + 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' + + slixmpp.ClientXMPP.__init__(self, self.my_jid, self.my_pass) + + 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) diff --git a/opt/ahenk/base/messaging/Messager.py b/opt/ahenk/base/messaging/Messager.py index 38f4642..acfa035 100644 --- a/opt/ahenk/base/messaging/Messager.py +++ b/opt/ahenk/base/messaging/Messager.py @@ -13,6 +13,8 @@ sys.path.append('../..') from slixmpp.exceptions import IqError, IqTimeout from base.Scope import Scope +from base.messaging.FileTransfer import FileTransfer + class Messager(slixmpp.ClientXMPP): global loop @@ -78,10 +80,13 @@ class Messager(slixmpp.ClientXMPP): def session_end(self): print("disconnect") - # TODO need check def send_file(self, file_path): - self.file = open(file_path, 'rb') + FileTransfer.run(file_path) + """ + @asyncio.coroutine + def send_file(self, file_path): + self.file = open('/tmp/volkan.txt', 'rb') # TODO read conf file check file size if file size is bigger than max size, divide and send parts.after all send message about them self.logger.debug('[Messager] Sending file: ' + self.file.name) try: @@ -103,6 +108,7 @@ class Messager(slixmpp.ClientXMPP): self.logger.debug('[Messager] File transfer finished successfully') finally: self.file.close() + """ def send_direct_message(self, msg): self.logger.debug('[Messager] Sending message: ' + msg) @@ -141,7 +147,7 @@ class Messager(slixmpp.ClientXMPP): self.register_plugin('xep_0045') # Multi-User Chat self.register_plugin('xep_0199') # XMPP Ping self.register_plugin('xep_0065', {'auto_accept': True}) # SOCKS5 Bytestreams - self.register_plugin('xep_0047', {'auto_accept': True}) # In-band Bytestreams + # self.register_plugin('xep_0047', {'auto_accept': True}) # In-band Bytestreams self.logger.debug('[Messager]Extension were registered: xep_0030,xep_0045,xep_0199,xep_0065,xep_0047') return True