added connectToServer and updated config and logger

This commit is contained in:
İsmail Başaran 2016-02-21 01:57:26 +02:00
parent cd904daac4
commit 79b8b083f1

View file

@ -1,8 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Author: Volkan Şahin <basaran.ismaill@gmail.com>
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
import configparser import configparser
import logging
import slixmpp import slixmpp
from slixmpp.exceptions import IqError, IqTimeout from slixmpp.exceptions import IqError, IqTimeout
@ -23,21 +24,21 @@ from slixmpp.exceptions import IqError, IqTimeout
class Messaging(slixmpp.ClientXMPP): class Messaging(slixmpp.ClientXMPP):
def __init__(self): def __init__(self,configurationManager,logger):
# configuration reading # logger comes from ahenk deamon
conf = configparser.ConfigParser() self.logger = logger
conf._interpolation = configparser.ExtendedInterpolation()
conf.read('conf.ini') # configurationManager comes from ahenk deamon
logging.info('Configuration parameters were read') self.configurationManager = configurationManager
#set parameters #set parameters
slixmpp.ClientXMPP.__init__(self, conf.get('Connection_Param', 'jid'), conf.get('Connection_Param', 'password')) slixmpp.ClientXMPP.__init__(self, self.configurationManager.get('CONNECTION', 'jid'), self.configurationManager.get('Connection_Param', 'password'))
self.nick = conf.get('Connection_Param', 'nick') self.nick = self.configurationManager.get('CONNECTION', 'nick')
self.receiver=conf.get('Receiver_Param','jid') self.receiver=self.configurationManager.get('CONNECTION','receiverJid')
self.sendfile=open(conf.get('Send_File_Param','path'), 'rb') self.sendfile=open(self.configurationManager.get('CONNECTION','sendFilePath'), 'rb')
self.receivefile=conf.get('Receive_File_Param', 'path') self.receivefile=self.configurationManager.get('CONNECTION', 'receiveFileParam')
logging.info('Parameters were established') self.logger.info('Parameters were established')
self.add_event_handler("session_start", self.start) self.add_event_handler("session_start", self.start)
self.room=self.add_event_handler("groupchat_invite", self.invite_auto_accept) self.room=self.add_event_handler("groupchat_invite", self.invite_auto_accept)
@ -56,19 +57,19 @@ class Messaging(slixmpp.ClientXMPP):
#self.add_event_handler("socks5_data", self.stream_data) #self.add_event_handler("socks5_data", self.stream_data)
#self.add_event_handler("socks5_closed", self.stream_closed) #self.add_event_handler("socks5_closed", self.stream_closed)
logging.info('Listeners were added') self.logger.info('Listeners were added')
def stream_opened(self, sid): def stream_opened(self, sid):
logging.info('Stream opened. %s', sid) self.logger.info('Stream opened. %s', sid)
return open(self.receivefile, 'wb') return open(self.receivefile, 'wb')
def stream_data(self, data): def stream_data(self, data):
logging.info('Stream data.') self.logger.info('Stream data.')
self.file.write(data) self.file.write(data)
def stream_closed(self, exception): def stream_closed(self, exception):
logging.info('Stream closed. %s', exception) self.logger.info('Stream closed. %s', exception)
self.file.close() self.file.close()
#self.disconnect() #self.disconnect()
@ -123,18 +124,33 @@ class Messaging(slixmpp.ClientXMPP):
if msg['type'] in ('chat', 'normal'): if msg['type'] in ('chat', 'normal'):
print ("%s : %s" % (msg['from'], msg['body'])) print ("%s : %s" % (msg['from'], msg['body']))
def connectToServer(self):
try:
self.register_plugin('xep_0030') # Service Discovery
self.register_plugin('xep_0045') # Multi-User Chat
self.register_plugin('xep_0199') # XMPP Ping
self.register_plugin('xep_0065') # SOCKS5 Bytestreams
self.logger.info('Plugins were registered: xep_0030,xep_0045,xep_0199,xep_0065')
self.connect()
self.process()
return True
except Exception as e:
self.logger.error('Connection to server failed!')
return False
if __name__ == '__main__': if __name__ == '__main__':
xmpp = Messaging() xmpp = Messaging(None,None)
xmpp.register_plugin('xep_0030') # Service Discovery xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0045') # Multi-User Chat xmpp.register_plugin('xep_0045') # Multi-User Chat
xmpp.register_plugin('xep_0199') # XMPP Ping xmpp.register_plugin('xep_0199') # XMPP Ping
xmpp.register_plugin('xep_0065') # SOCKS5 Bytestreams xmpp.register_plugin('xep_0065') # SOCKS5 Bytestreams
logging.info('Plugins were registered: xep_0030,xep_0045,xep_0199,xep_0065') self.logger.info('Plugins were registered: xep_0030,xep_0045,xep_0199,xep_0065')
# Connect to the XMPP server and start processing XMPP stanzas. # Connect to the XMPP server and start processing XMPP stanzas.
xmpp.connect() xmpp.connect()
xmpp.process() xmpp.process()