2016-03-02 18:31:29 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
|
|
|
|
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
|
2016-03-11 17:51:26 +02:00
|
|
|
import json, os, asyncio, slixmpp, sys
|
2016-03-02 18:31:29 +02:00
|
|
|
sys.path.append('../..')
|
|
|
|
from base.Scope import Scope
|
|
|
|
|
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
|
2016-03-02 18:31:29 +02:00
|
|
|
class MessageReceiver(slixmpp.ClientXMPP):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
# global scope of ahenk
|
2016-03-04 19:23:21 +02:00
|
|
|
scope = Scope().getInstance()
|
2016-03-02 18:31:29 +02:00
|
|
|
|
|
|
|
self.logger = scope.getLogger()
|
2016-03-11 17:51:26 +02:00
|
|
|
self.configuration_manager = scope.getConfigurationManager()
|
2016-03-07 00:16:36 +02:00
|
|
|
self.event_manger=scope.getEventManager()
|
2016-03-11 17:51:26 +02:00
|
|
|
self.execution_manager=scope.getExecutionManager()
|
2016-03-02 18:31:29 +02:00
|
|
|
|
2016-03-07 17:59:02 +02:00
|
|
|
self.my_jid=self.get_jid_id()
|
|
|
|
self.my_pass=self.get_password()
|
2016-03-02 18:31:29 +02:00
|
|
|
|
2016-03-07 17:59:02 +02:00
|
|
|
slixmpp.ClientXMPP.__init__(self, self.my_jid,self.my_pass)
|
2016-03-02 18:31:29 +02:00
|
|
|
|
2016-03-07 17:59:02 +02:00
|
|
|
self.room=None
|
2016-03-11 17:51:26 +02:00
|
|
|
self.receiver=self.configuration_manager.get('CONNECTION', 'receiverjid')+'@'+self.configuration_manager.get('CONNECTION', 'host')+'/Smack'
|
|
|
|
self.nick = self.configuration_manager.get('CONNECTION', 'nick')
|
|
|
|
self.receive_file_path=self.configuration_manager.get('CONNECTION', 'receiveFileParam')
|
|
|
|
self.logger.debug('[MessageReceiver] XMPP Receiver parameters were set')
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-03-02 18:31:29 +02:00
|
|
|
self.register_extensions()
|
|
|
|
self.add_listeners()
|
|
|
|
self.connect()
|
|
|
|
|
2016-03-07 17:59:02 +02:00
|
|
|
def get_jid_id(self):
|
2016-03-11 17:51:26 +02:00
|
|
|
if self.configuration_manager.get('CONNECTION', 'uid') == "" or self.configuration_manager.get('CONNECTION', 'uid') is None:
|
|
|
|
self.logger.debug('[MessageReceiver] Parameters were set as anonymous account')
|
|
|
|
return str(self.configuration_manager.get('CONNECTION', 'host'))
|
2016-03-07 17:59:02 +02:00
|
|
|
else:
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.debug('[MessageReceiver] Parameters were set as defined account')
|
|
|
|
return str(self.configuration_manager.get('CONNECTION', 'uid')+'@'+self.configuration_manager.get('CONNECTION', 'host')+'/receiver')
|
2016-03-02 18:31:29 +02:00
|
|
|
|
2016-03-07 17:59:02 +02:00
|
|
|
def get_password(self):
|
2016-03-11 17:51:26 +02:00
|
|
|
if self.configuration_manager.get('CONNECTION', 'password') == "" or self.configuration_manager.get('CONNECTION', 'password') is None:
|
2016-03-07 17:59:02 +02:00
|
|
|
return None
|
|
|
|
else:
|
2016-03-11 17:51:26 +02:00
|
|
|
return str(self.configuration_manager.get('CONNECTION', 'password'))
|
2016-03-02 18:31:29 +02:00
|
|
|
|
2016-03-07 17:59:02 +02:00
|
|
|
def add_listeners(self):
|
2016-03-02 18:31:29 +02:00
|
|
|
self.add_event_handler("session_start", self.session_start)
|
|
|
|
self.add_event_handler("message", self.recv_direct_message)
|
2016-03-11 17:51:26 +02:00
|
|
|
|
2016-03-07 17:59:02 +02:00
|
|
|
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)
|
2016-03-02 18:31:29 +02:00
|
|
|
|
2016-03-08 18:08:07 +02:00
|
|
|
self.add_event_handler("ibb_stream_start", self.stream_opened)
|
|
|
|
self.add_event_handler("ibb_stream_data", self.stream_data)
|
|
|
|
self.add_event_handler("ibb_stream_end", self.stream_closed)
|
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.debug('[MessageReceiver] Event handlers were added')
|
2016-03-10 09:53:07 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
def stream_opened(self, sid):
|
|
|
|
self.logger.debug('[MessageReceiver] Stream was opened. Stream id: '+str(self.stream_id))
|
2016-03-08 18:08:07 +02:00
|
|
|
self.file = open(self.receive_file_path+self.stream_id, 'wb')
|
|
|
|
return self.file
|
2016-03-02 18:31:29 +02:00
|
|
|
|
|
|
|
def stream_data(self, data):
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.debug('[MessageReceiver] Receiving file...')
|
2016-03-02 18:31:29 +02:00
|
|
|
self.file.write(data)
|
|
|
|
|
|
|
|
def stream_closed(self, exception):
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.debug('[MessageReceiver] Stream was closed')
|
2016-03-02 18:31:29 +02:00
|
|
|
self.file.close()
|
2016-03-11 17:51:26 +02:00
|
|
|
self.set_file_name_to_md5()
|
2016-03-02 18:31:29 +02:00
|
|
|
|
|
|
|
def session_start(self, event):
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.debug('[MessageReceiver] Session was started')
|
2016-03-02 18:31:29 +02:00
|
|
|
self.get_roster()
|
|
|
|
self.send_presence()
|
|
|
|
|
2016-03-10 09:53:07 +02:00
|
|
|
def send_direct_message(self,msg):
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.debug('[MessageReceiver] Sending message: '+msg)
|
2016-03-10 09:53:07 +02:00
|
|
|
self.send_message(mto=self.receiver,mbody=msg,mtype='normal')
|
2016-03-02 18:31:29 +02:00
|
|
|
|
|
|
|
def invite_auto_accept(self, inv):
|
|
|
|
self.room=inv['from']
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.debug('[MessageReceiver] (%s) invite is accepted' % str(self.room))
|
2016-03-02 18:31:29 +02:00
|
|
|
self.plugin['xep_0045'].joinMUC(self.room,self.nick,wait=True)
|
|
|
|
self.send_message(mto=self.room.bare,mbody="Hi all!",mtype='groupchat')
|
|
|
|
return self.room
|
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
def recv_muc_message(self, msg):
|
2016-03-02 18:31:29 +02:00
|
|
|
if msg['mucnick'] != self.nick:
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.debug('[MessageReceiver] %s : %s' % (str(msg['from']),str(msg['body'])) )
|
2016-03-02 18:31:29 +02:00
|
|
|
self.send_message(mto=msg['from'].bare,mbody="I got it, %s." % msg['mucnick'],mtype='groupchat')
|
|
|
|
else:
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.debug('[MessageReceiver] %s : %s' % (str(msg['mucnick']),str(msg['body'])))
|
2016-03-02 18:31:29 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
def recv_direct_message(self, msg):
|
2016-03-02 18:31:29 +02:00
|
|
|
if msg['type'] in ('chat', 'normal'):
|
2016-03-07 00:16:36 +02:00
|
|
|
j = json.loads(str(msg['body']))
|
|
|
|
type =j['type']
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.debug('[MessageReceiver] Fired event is: '+type)
|
2016-03-10 09:53:07 +02:00
|
|
|
self.event_manger.fireEvent(type,str(msg['body']).lower())
|
2016-03-07 00:16:36 +02:00
|
|
|
|
2016-03-02 18:31:29 +02:00
|
|
|
def connect_to_server(self):# Connect to the XMPP server and start processing XMPP stanzas.
|
|
|
|
try:
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.debug('[MessageReceiver] Connecting to server as thread')
|
2016-03-02 18:31:29 +02:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.run_until_complete(self.process())
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.debug('[MessageReceiver] Connection were established successfully')
|
2016-03-02 18:31:29 +02:00
|
|
|
return True
|
|
|
|
except Exception as e:
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.error('[MessageReceiver] Connection to server is failed! '+e)
|
2016-03-02 18:31:29 +02:00
|
|
|
return False
|
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
def set_file_name_to_md5(self):
|
|
|
|
self.logger.debug('[MessageReceiver] Renaming file as md5 hash')
|
|
|
|
md5_hash=self.execution_manager.get_md5_file(self.file.name)
|
|
|
|
os.rename(self.file.name,self.receive_file_path+md5_hash)
|
|
|
|
|
2016-03-02 18:31:29 +02:00
|
|
|
def register_extensions(self):
|
|
|
|
try:
|
|
|
|
self.register_plugin('xep_0030') # Service Discovery
|
|
|
|
self.register_plugin('xep_0045') # Multi-User Chat
|
|
|
|
self.register_plugin('xep_0199') # XMPP Ping
|
2016-03-08 18:08:07 +02:00
|
|
|
self.register_plugin('xep_0065', {'auto_accept': True}) # SOCKS5 Bytestreams
|
|
|
|
self.register_plugin('xep_0047', {'auto_accept': True}) # In-band Bytestreams
|
2016-03-02 18:31:29 +02:00
|
|
|
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.debug('Extension were registered: xep_0030,xep_0045,xep_0199,xep_0065,xep_0047')
|
2016-03-02 18:31:29 +02:00
|
|
|
return True
|
|
|
|
except Exception as e:
|
2016-03-11 17:51:26 +02:00
|
|
|
self.logger.error('Extension registration is failed!(%s)\n' % (e.errno, e.strerror))
|
2016-03-02 18:31:29 +02:00
|
|
|
return False
|