ahenk file send implementation

This commit is contained in:
Volkan Şahin 2016-03-07 12:55:14 +02:00
parent 19e76a1b3a
commit fbe9b68fe1
4 changed files with 123 additions and 76 deletions

View File

@ -13,5 +13,5 @@ port = 5222
receiverjid = lider_sunucu
servicename = im.mys.pardus.org.tr
nick = nick
sendfilepath = sendFilePath
receivefileparam = receiveFileParam
sendfilepath = /home/volkan/Desktop/a.txt
receivefileparam = /home/volkan/devzone/

View File

@ -48,8 +48,6 @@ class AhenkDeamon(BaseDaemon):
globalscope.setEventManager(eventManager)
eventManager.register_event('registration_ok',self.reload)
pluginManager = PluginManager()
pluginManager.loadPlugins()
globalscope.setPluginManager(pluginManager)
@ -60,10 +58,12 @@ class AhenkDeamon(BaseDaemon):
registration=Registration()
globalscope.setRegistration(registration)
if registration.is_registered() is False:
message_sender=MessageSender(registration.get_registration_request_message())
message_sender.connect_to_server()
registration.confirm_registration()
while registration.is_registered() is False:
registration.registration_request()
#message_sender=MessageSender(None,'/home/volkan/Desktop/batman.png')
#message_sender.connect_to_server()
""""
message_receiver = MessageReceiver()
@ -71,25 +71,6 @@ class AhenkDeamon(BaseDaemon):
rec_process.start()
"""
"""
else:
"""
"""
#TODO send register message according to register status
print("sending registration message")
message_sender = MessageSender(registration.get_registration_message())
message_sender.connect_to_server()
print("registration message were sent")
#TODO add sender to scope
message_receiver = MessageReceiver()
rec_process = Process(target=message_receiver.connect_to_server)
rec_process.start()
print("receiver online")
#set parameters which will use for message sending
"""
"""
this is must be created after message services

View File

@ -12,24 +12,9 @@ from multiprocessing import Process
from slixmpp.exceptions import IqError, IqTimeout
from base.Scope import Scope
"""
--fetch parameters of connection from conf file
--connect xmpp
--send direct message
--receive direct message
--send muc message
--receive muc message
--listen to muc invites
--auto accept muc invites
--send auto reply to muc messages
--receive file (0065)
--send file (0065)
"""
class MessageSender(slixmpp.ClientXMPP):
def __init__(self,message):
def __init__(self,message,file_path):
# global scope of ahenk
scope = Scope().getInstance()
@ -39,58 +24,131 @@ class MessageSender(slixmpp.ClientXMPP):
self.logger = scope.getLogger()
self.configurationManager = scope.getConfigurationManager()
self.registration=scope.getRegistration()
#set parameters
#slixmpp.ClientXMPP.__init__(self, self.configurationManager.get('CONNECTION', 'jid'), self.configurationManager.get('Connection_Param', 'password'))
#is user want to create connection as anonymous
if self.configurationManager.get('CONNECTION', 'uid') == "" or self.configurationManager.get('CONNECTION', 'uid') is None:
print("uid not found")
slixmpp.ClientXMPP.__init__(self, self.configurationManager.get('CONNECTION', 'host'), None)
else:
print("uid:"+self.configurationManager.get('CONNECTION', 'uid'))
slixmpp.ClientXMPP.__init__(self, self.configurationManager.get('CONNECTION', 'uid'), self.configurationManager.get('CONNECTION', 'password'))
self.my_jid=self.get_jid_id()
self.my_pass=self.get_password()
self.receiver="caner@localhost"
#TODO lider account
slixmpp.ClientXMPP.__init__(self, self.my_jid,self.my_pass)
#slixmpp.ClientXMPP.__init__(self,'volkan@localhost','volkan')
#slixmpp.ClientXMPP.__init__(self, "volkan@localhost", "volkan")
#self.receiver="caner@localhost"
"""
self.nick = self.configurationManager.get('CONNECTION', 'nick')
self.receiver=self.configurationManager.get('CONNECTION','receiverJid')
self.sendfile=open(self.configurationManager.get('CONNECTION','sendFilePath'), 'rb')
self.receivefile=self.configurationManager.get('CONNECTION', 'receiveFileParam')
self.logger.info('Parameters were established')
"""
self.message=None
self.file=None
self.room=None
self.message=message
self.receiver=self.configurationManager.get('CONNECTION', 'receiverjid')+'@'+self.configurationManager.get('CONNECTION', 'host')+'/Smack'
self.nick = self.configurationManager.get('CONNECTION', 'nick')
self.receivefile=self.configurationManager.get('CONNECTION', 'receiveFileParam')
if file_path is not None and file_path!='':
self.file=open(file_path, 'rb')
print('file path-'+file_path+"-"+self.my_jid+"-"+self.my_pass)
if message is not None and message!='':
self.message=message
self.add_event_handler("session_start", self.session_start)
self.add_event_handler("message", self.recv_direct_message)
self.add_event_handler("socks5_connected", self.stream_opened)
self.add_event_handler("socks5_data", self.stream_data)
self.add_event_handler("socks5_closed", self.stream_closed)
self.register_extensions()
#!!! you have to use modified slixmpp for file transfering
#self.send_file()
def get_jid_id(self):
if self.configurationManager.get('CONNECTION', 'uid') == "" or self.configurationManager.get('CONNECTION', 'uid') is None:
return str(self.configurationManager.get('CONNECTION', 'host')) #is user want to create connection as anonymous
else:
return str(self.configurationManager.get('CONNECTION', 'uid')+'@'+self.configurationManager.get('CONNECTION', 'host'))
def get_password(self):
if self.configurationManager.get('CONNECTION', 'password') == "" or self.configurationManager.get('CONNECTION', 'password') is None:
return None
else:
return str(self.configurationManager.get('CONNECTION', 'password'))
def recv_direct_message(self, msg):
if msg['type'] in ('chat', 'normal'):
print ("%s : %s" % (msg['from'], msg['body']))
self.registration.registration_reply=str(msg['body'])
self.disconnect(wait=False)
@asyncio.coroutine
def session_start(self, event):
self.get_roster()
self.send_presence()
self.send_direct_message(self.message)
if self.message is not None:
self.send_direct_message(self.message)
if self.file is not None:
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):
print('File transfer errored')
else:
print('File transfer finished')
finally:
self.file.close()
self.disconnect()
@asyncio.coroutine
def send_file(self,event):
print("send file")
try:
# Open the S5B stream in which to write to.
print("try")
proxy = yield from self['xep_0065'].handshake(self.receiver)
print("proxy")
# Send the entire file.
while True:
data = self.file.read(1048576)
if not data:
print("not data")
break
yield from proxy.write(data)
# And finally close the stream.
print("while bitti")
proxy.transport.write_eof()
except (IqError, IqTimeout):
print('File transfer errored')
else:
print('File transfer finished')
finally:
print("close")
self.file.close()
self.disconnect()
def stream_opened(self, sid):
#self.logger.info('Stream opened. %s', sid)
return open(self.receivefile, 'wb')
def stream_data(self, data):
#self.logger.info('Stream data.')
self.file.write(data)
def stream_closed(self, exception):
#self.logger.info('Stream closed. %s', exception)
self.file.close()
#self.disconnect()
def send_direct_message(self,msg):
#need connection control
self.send_message(mto=self.receiver,mbody=msg,mtype='chat')
if self.configurationManager.get('CONNECTION', 'uid') != "" and self.configurationManager.get('CONNECTION', 'uid') is not None:
print("diskooo")
self.disconnect()
def connect_to_server(self):# Connect to the XMPP server and start processing XMPP stanzas.
try:
print("connec")
self.connect()
self.process(forever=False)
#self.logger.info('Connection were established successfully')

View File

@ -5,6 +5,7 @@
from base.config.ConfigManager import ConfigManager
from base.logger.AhenkLogger import Logger
from base.Scope import Scope
from base.messaging.MessageSender import MessageSender
from uuid import getnode as get_mac
import json
import uuid
@ -32,10 +33,15 @@ class Registration():
else:
self.logger.debug('[Registration] already registered')
else:
self.register()
self.register(True)
self.registration_reply=""
self.logger.debug('[Registration] ')
def registration_request(self):
message_sender=MessageSender(self.get_registration_request_message(),None)
message_sender.connect_to_server()
self.confirm_registration()
def confirm_registration(self):
if self.registration_reply != "":
j = json.loads(self.registration_reply)
@ -53,13 +59,12 @@ class Registration():
self.conf_manager.write(configfile)
self.logger.info('[REGISTRATION] registered successfully')
return True
elif(status=='registration_error'):
self.logger.info('[REGISTRATION] registration error')
return False
elif(status=='already_registered'):
self.logger.info('[REGISTRATION]already registered')
return False
self.re_register()
self.registration_request()
def is_registered(self):
if self.conf_manager.has_section('REGISTRATION') and (self.conf_manager.get('REGISTRATION', 'registered')=='true'):
@ -89,7 +94,7 @@ class Registration():
print("reg sec yok :(")
return None
def register(self):
def register(self,uuid_depend_mac):
self.logger.debug('[Registration] configuration parameters of registration is checking')
if self.conf_manager.has_section('REGISTRATION'):
self.logger.debug('[Registration] REGISTRATION section is already created')
@ -97,7 +102,7 @@ class Registration():
self.logger.debug('[Registration] creating REGISTRATION section')
self.conf_manager.add_section('REGISTRATION')
self.conf_manager.set('REGISTRATION', 'from',str(self.generate_uuid(True)))
self.conf_manager.set('REGISTRATION', 'from',str(self.generate_uuid(uuid_depend_mac)))
self.conf_manager.set('REGISTRATION', 'mac_address',str(':'.join(("%012X" % get_mac())[i:i+2] for i in range(0, 12, 2))))
self.conf_manager.set('REGISTRATION', 'ip_address',str(self.get_ip_addresses()))
self.conf_manager.set('REGISTRATION', 'hostname',str(socket.gethostname()))
@ -108,7 +113,7 @@ class Registration():
#TODO self.conf_manager.configurationFilePath attribute error ? READ olacak
self.logger.debug('[Registration] parameters were set up, section will write to configuration file')
with open('/etc/ahenk/ahenk.conf', 'a') as configfile:
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
self.conf_manager.write(configfile)
self.logger.debug('[Registration] REGISTRATION section wrote to configuration file successfully')
@ -120,6 +125,9 @@ class Registration():
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
self.conf_manager.write(configfile)
def re_register(self):
self.unregister()
self.register(False)
def generate_uuid(self,depend_mac=True):
self.logger.debug('[Registration] universal user id will be created')