Changeset - eefe4c2c3981
[Not reviewed]
Merge
0 2 0
Jan Kaluza - 10 years ago 2015-12-03 11:13:43
jkaluza@redhat.com
herge branch 'master' of github.com:hanzz/libtransport
1 file changed with 75 insertions and 10 deletions:
0 comments (0 inline, 0 general)
plugin/python/NetworkPlugin.py
Show inline comments
 
@@ -16,23 +16,35 @@ class NetworkPlugin:
 
	
 
	def __init__(self):
 
		self.m_pingReceived = False
 
		self.m_data = ""
 
		self.m_init_res = 0
 

	
 
	def handleMessage(self, user, legacyName, msg, nickname = "", xhtml = ""):
 
	def handleMessage(self, user, legacyName, msg, nickname = "", xhtml = "", timestamp = ""):
 
		m = protocol_pb2.ConversationMessage()
 
		m.userName = user
 
		m.buddyName = legacyName
 
		m.message = msg
 
		m.nickname = nickname
 
		m.xhtml = xhtml
 
		m.timestamp = str(timestamp)
 

	
 
		message = WRAP(m.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_CONV_MESSAGE)
 
		self.send(message)
 

	
 
	def handleMessageAck(self, user, legacyName, ID):
 
		m = protocol_pb2.ConversationMessage()
 
		m.userName = user
 
		m.buddyName = legacyName
 
		m.message = ""
 
		m.id = ID
 

	
 
		message = WRAP(m.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_CONV_MESSAGE_ACK)
 
		self.send(message)
 

	
 

	
 
	def handleAttention(self, user, buddyName, msg):
 
		m = protocol_pb2.ConversationMessage()
 
		m.userName = user
 
		m.buddyName = buddyName
 
		m.message = msg
 

	
 
@@ -75,12 +87,19 @@ class NetworkPlugin:
 
		buddy.iconHash = iconHash
 
		buddy.blocked = blocked
 

	
 
		message = WRAP(buddy.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_BUDDY_CHANGED)
 
		self.send(message)
 

	
 
	def handleBuddyRemoved(self, user, buddyName):
 
		buddy = protocol_pb2.Buddy()
 
		buddy.userName = user
 
		buddy.buddyName = buddyName
 

	
 
		message = WRAP(buddy.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_BUDDY_REMOVED)
 
		self.send(message);
 

	
 
	def handleBuddyTyping(self, user, buddyName):
 
		buddy = protocol_pb2.Buddy()
 
		buddy.userName = user
 
		buddy.buddyName = buddyName
 

	
 
@@ -151,12 +170,22 @@ class NetworkPlugin:
 
		room.room = r
 
		room.password = ""
 

	
 
		message = WRAP(room.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_ROOM_NICKNAME_CHANGED)
 
		self.send(message);
 

	
 
	def handleRoomList(self, rooms):
 
		roomList = protocol_pb2.RoomList()
 

	
 
		for room in rooms:
 
			roomList.room.append(room[0])
 
			roomList.name.append(room[1])
 

	
 
		message = WRAP(roomList.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_ROOM_LIST)
 
		self.send(message);
 

	
 

	
 
	def handleFTStart(self, user, buddyName, fileName, size):
 
		room = protocol_pb2.File()
 
		room.userName = user
 
		room.buddyName = buddyName
 
		room.fileName = fileName
 
@@ -185,39 +214,55 @@ class NetworkPlugin:
 
		d.ftid = ftID
 
		d.data = data
 

	
 
		message = WRAP(d.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_FT_DATA);
 
		self.send(message)
 

	
 
	def handleBackendConfig(self, section, key, value):
 
		c = protocol_pb2.BackendConfig()
 
		c.config = "[%s]\n%s = %s\n" % (section, key, value)
 

	
 
		message = WRAP(c.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_BACKEND_CONFIG);
 
		self.send(message)
 

	
 
	def handleLoginPayload(self, data):
 
		payload = protocol_pb2.Login()
 
		if (payload.ParseFromString(data) == False):
 
			#TODO: ERROR
 
			return
 
		self.handleLoginRequest(payload.user, payload.legacyName, payload.password)
 
		self.handleLoginRequest(payload.user, payload.legacyName, payload.password, payload.extraFields)
 

	
 
	def handleLogoutPayload(self, data):
 
		payload = protocol_pb2.Logout()
 
		if (payload.ParseFromString(data) == False):
 
			#TODO: ERROR
 
			return
 
		self.handleLogoutRequest(self, payload.user, payload.legacyName)
 
		self.handleLogoutRequest(payload.user, payload.legacyName)
 
	
 
	def handleStatusChangedPayload(data):
 
	def handleStatusChangedPayload(self, data):
 
		payload = protocol_pb2.Status()
 
		if (payload.ParseFromString(data) == False):
 
			#TODO: ERROR
 
			return
 
		self.handleStatusChangeRequest(payload.userName, payload.status, payload.statusMessage)
 

	
 
	def handleConvMessagePayload(self, data):
 
		payload = protocol_pb2.ConversationMessage()
 
		if (payload.ParseFromString(data) == False):
 
			#TODO: ERROR
 
			return
 
		self.handleMessageSendRequest(payload.userName, payload.buddyName, payload.message, payload.xhtml)
 
		self.handleMessageSendRequest(payload.userName, payload.buddyName, payload.message, payload.xhtml, payload.id)
 
	
 
	def handleConvMessageAckPayload(self, data):
 
                payload = protocol_pb2.ConversationMessage()
 
                if (payload.ParseFromString(data) == False):
 
                        #TODO: ERROR
 
                        return
 
                self.handleMessageAckRequest(payload.userName, payload.buddyName, payload.id)
 

	
 

	
 

	
 
	def handleAttentionPayload(self, data):
 
		payload = protocol_pb2.ConversationMessage()
 
		if (payload.ParseFromString(data) == False):
 
			#TODO: ERROR
 
			return
 
@@ -367,15 +412,19 @@ class NetworkPlugin:
 
					self.handleFTFinishPayload(wrapper.payload)
 
			elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_FT_PAUSE:
 
					self.handleFTPausePayload(wrapper.payload)
 
			elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_FT_CONTINUE:
 
					self.handleFTContinuePayload(wrapper.payload)
 
			elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_EXIT:
 
					self.handleExitRequest()
 
			elif wrapper.type == Protocol_pb2.WrapperMessage.TYPE_BUDDIES:
 
					self.handleBuddiesPayload()
 
				self.handleExitRequest()
 
			elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_CONV_MESSAGE_ACK:
 
                                self.handleConvMessageAckPayload(wrapper.payload)
 
			elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_RAW_XML:
 
				self.handleRawXmlRequest(wrapper.payload)
 
			elif wrapper.type == protocol_pb2.WrapperMessage.TYPE_BUDDIES:
 
					self.handleBuddiesPayload(wrapper.payload)
 

	
 

	
 
	def send(self, data):
 
		header = struct.pack('!I',len(data))
 
		self.sendData(header + data)
 

	
 
@@ -408,13 +457,13 @@ class NetworkPlugin:
 
		stats.id = str(os.getpid())
 

	
 
		message = WRAP(stats.SerializeToString(), protocol_pb2.WrapperMessage.TYPE_STATS)
 
		self.send(message)
 

	
 

	
 
	def handleLoginRequest(self, user, legacyName, password):
 
	def handleLoginRequest(self, user, legacyName, password, extra):
 
		""" 
 
		Called when XMPP user wants to connect legacy network.
 
		You should connect him to legacy network and call handleConnected or handleDisconnected function later.
 
		@param user: XMPP JID of user for which this event occurs.
 
		@param legacyName: Legacy network name of this user used for login.
 
		@param password: Legacy network password of this user.
 
@@ -444,23 +493,36 @@ class NetworkPlugin:
 
		@param user: XMPP JID of user for which this event occurs.
 
		@param legacyName: Legacy network name of this user used for login.
 
		"""
 

	
 
		raise NotImplementedError, "Implement me"
 

	
 
	def handleMessageSendRequest(self, user, legacyName, message, xhtml = ""):
 
	def handleMessageSendRequest(self, user, legacyName, message, xhtml = "", ID = 0):
 
		"""
 
		Called when XMPP user sends message to legacy network.
 
		@param user: XMPP JID of user for which this event occurs.
 
		@param legacyName: Legacy network name of buddy or room.
 
		@param message: Plain text message.
 
		@param xhtml: XHTML message.
 
		@param ID: message ID
 
		"""
 

	
 
		raise NotImplementedError, "Implement me"
 

	
 
	def handleMessageAckRequest(self, user, legacyName, ID = 0):
 
                """
 
                Called when XMPP user sends message to legacy network.
 
                @param user: XMPP JID of user for which this event occurs.
 
                @param legacyName: Legacy network name of buddy or room.
 
                @param ID: message ID
 
                """
 

	
 
                # raise NotImplementedError, "Implement me"
 
		pass
 

	
 

	
 
	def handleVCardRequest(self, user, legacyName, ID):
 
		""" Called when XMPP user requests VCard of buddy.
 
		@param user: XMPP JID of user for which this event occurs.
 
		@param legacyName: Legacy network name of buddy whose VCard is requested.
 
		@param ID: ID which is associated with this request. You have to pass it to handleVCard function when you receive VCard."""
 
			
 
@@ -530,9 +592,12 @@ class NetworkPlugin:
 
	def handleMemoryUsage(self):
 
		return (0,0)
 

	
 
	def handleExitRequest(self):
 
		sys.exit(1)
 

	
 
	def handleRawXmlRequest(self, xml):
 
		pass
 

	
 
	def sendData(self, data):
 
		pass
 

	
0 comments (0 inline, 0 general)