2016-04-09 22:21:58 +00:00
|
|
|
# use unicode encoding for all literals by default (for python2.x)
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
__author__ = "Steffen Vogel"
|
|
|
|
__copyright__ = "Copyright 2015, Steffen Vogel"
|
|
|
|
__license__ = "GPLv3"
|
|
|
|
__maintainer__ = "Steffen Vogel"
|
|
|
|
__email__ = "post@steffenvogel.de"
|
2013-08-01 23:45:51 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2013-08-01 23:45:51 +00:00
|
|
|
This file is part of transWhat
|
|
|
|
|
|
|
|
transWhat is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
transwhat is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with transWhat. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
2016-04-09 22:21:58 +00:00
|
|
|
|
2013-05-31 18:53:21 +00:00
|
|
|
from Spectrum2.backend import SpectrumBackend
|
|
|
|
from Spectrum2 import protocol_pb2
|
|
|
|
|
|
|
|
from session import Session
|
2016-01-07 15:57:41 +00:00
|
|
|
from registersession import RegisterSession
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2016-04-09 22:21:58 +00:00
|
|
|
|
2013-05-31 18:53:21 +00:00
|
|
|
class WhatsAppBackend(SpectrumBackend):
|
2015-12-02 21:06:56 +00:00
|
|
|
def __init__(self, io, spectrum_jid):
|
2013-05-31 18:53:21 +00:00
|
|
|
SpectrumBackend.__init__(self)
|
2013-06-28 19:37:39 +00:00
|
|
|
self.logger = logging.getLogger(self.__class__.__name__)
|
2013-05-31 18:53:21 +00:00
|
|
|
self.io = io
|
|
|
|
self.sessions = { }
|
2015-10-26 16:47:17 +00:00
|
|
|
self.spectrum_jid = spectrum_jid
|
2015-09-05 09:39:34 +00:00
|
|
|
# Used to prevent duplicate messages
|
2016-01-06 13:00:00 +00:00
|
|
|
self.lastMsgId = {}
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
self.logger.debug("Backend started")
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
# RequestsHandlers
|
|
|
|
def handleLoginRequest(self, user, legacyName, password, extra):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleLoginRequest(user=%s, legacyName=%s)" % (user, legacyName))
|
2016-01-07 17:31:05 +00:00
|
|
|
# Key word means we should register a new password
|
2016-01-07 15:57:41 +00:00
|
|
|
if password == 'register':
|
|
|
|
if user not in self.sessions:
|
|
|
|
self.sessions[user] = RegisterSession(self, user, legacyName, extra)
|
|
|
|
else:
|
|
|
|
if user not in self.sessions:
|
|
|
|
self.sessions[user] = Session(self, user, legacyName, extra)
|
2013-06-28 19:37:39 +00:00
|
|
|
|
|
|
|
self.sessions[user].login(password)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
def handleLogoutRequest(self, user, legacyName):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleLogoutRequest(user=%s, legacyName=%s)" % (user, legacyName))
|
2013-06-28 19:37:39 +00:00
|
|
|
if user in self.sessions:
|
|
|
|
self.sessions[user].logout()
|
|
|
|
del self.sessions[user]
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2016-01-06 13:00:00 +00:00
|
|
|
def handleMessageSendRequest(self, user, buddy, message, xhtml="", ID=""):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleMessageSendRequest(user=%s, buddy=%s, message=%s, xhtml=%s, ID=%s)" %
|
|
|
|
( user, buddy, message, xhtml, ID))
|
2015-09-05 09:39:34 +00:00
|
|
|
# For some reason spectrum occasionally sends to identical messages to
|
2016-01-06 13:00:00 +00:00
|
|
|
# a buddy, one to the bare jid and one to the /bot resource. This
|
|
|
|
# causes duplicate messages. Thus we should not send consecutive
|
|
|
|
# messages with the same id
|
|
|
|
if ID == '':
|
2016-01-06 14:57:17 +00:00
|
|
|
self.sessions[user].sendMessageToWA(buddy, message, ID, xhtml)
|
2016-01-06 13:00:00 +00:00
|
|
|
elif user not in self.lastMsgId or self.lastMsgId[user] != ID:
|
2016-01-06 14:57:17 +00:00
|
|
|
self.sessions[user].sendMessageToWA(buddy, message, ID, xhtml)
|
2016-01-06 13:00:00 +00:00
|
|
|
self.lastMsgId[user] = ID
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
def handleJoinRoomRequest(self, user, room, nickname, pasword):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleJoinRoomRequest(user=%s, room=%s, nickname=%s)" % (user, room, nickname))
|
2013-06-28 19:37:39 +00:00
|
|
|
self.sessions[user].joinRoom(room, nickname)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
2015-11-23 15:06:14 +00:00
|
|
|
def handleLeaveRoomRequest(self, user, room):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleLeaveRoomRequest(user=%s, room=%s)" % (user, room))
|
2015-11-23 15:06:14 +00:00
|
|
|
self.sessions[user].leaveRoom(room)
|
|
|
|
|
2013-05-31 18:53:21 +00:00
|
|
|
def handleStatusChangeRequest(self, user, status, statusMessage):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleStatusChangeRequest(user=%s, status=%d, statusMessage=%s)" % (user, status, statusMessage))
|
2013-08-06 19:21:37 +00:00
|
|
|
self.sessions[user].changeStatusMessage(statusMessage)
|
2013-05-31 18:53:21 +00:00
|
|
|
self.sessions[user].changeStatus(status)
|
|
|
|
|
2015-12-01 19:33:02 +00:00
|
|
|
def handleBuddies(self, buddies):
|
2015-12-31 19:43:10 +00:00
|
|
|
"""Called when user logs in. Used to initialize roster."""
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleBuddies(buddies=%s)" % buddies)
|
2015-12-01 21:21:38 +00:00
|
|
|
buddies = [b for b in buddies.buddy]
|
2015-12-04 18:44:37 +00:00
|
|
|
if len(buddies) > 0:
|
|
|
|
user = buddies[0].userName
|
|
|
|
self.sessions[user].loadBuddies(buddies)
|
2015-12-01 19:33:02 +00:00
|
|
|
|
2013-05-31 18:53:21 +00:00
|
|
|
def handleBuddyUpdatedRequest(self, user, buddy, nick, groups):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleBuddyUpdatedRequest(user=%s, buddy=%s, nick=%s, groups=%s)" % (user, buddy, nick, groups))
|
2013-05-31 18:53:21 +00:00
|
|
|
self.sessions[user].updateBuddy(buddy, nick, groups)
|
|
|
|
|
|
|
|
def handleBuddyRemovedRequest(self, user, buddy, groups):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleBuddyRemovedRequest(user=%s, buddy=%s, groups=%s)" % (user, buddy, groups))
|
2013-05-31 18:53:21 +00:00
|
|
|
self.sessions[user].removeBuddy(buddy)
|
|
|
|
|
|
|
|
def handleTypingRequest(self, user, buddy):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleTypingRequest(user=%s, buddy=%s)" % (user, buddy))
|
2013-05-31 18:53:21 +00:00
|
|
|
self.sessions[user].sendTypingStarted(buddy)
|
|
|
|
|
|
|
|
def handleTypedRequest(self, user, buddy):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleTypedRequest(user=%s, buddy=%s)" % (user, buddy))
|
2013-05-31 18:53:21 +00:00
|
|
|
self.sessions[user].sendTypingStopped(buddy)
|
|
|
|
|
|
|
|
def handleStoppedTypingRequest(self, user, buddy):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleStoppedTypingRequest(user=%s, buddy=%s)" % (user, buddy))
|
2013-05-31 18:53:21 +00:00
|
|
|
self.sessions[user].sendTypingStopped(buddy)
|
|
|
|
|
2015-09-05 22:14:12 +00:00
|
|
|
def handleVCardRequest(self, user, buddy, ID):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleVCardRequest(user=%s, buddy=%s, ID=%s)" % (user, buddy, ID))
|
2015-09-05 22:14:12 +00:00
|
|
|
self.sessions[user].requestVCard(buddy, ID)
|
|
|
|
|
2015-12-25 14:38:09 +00:00
|
|
|
def handleVCardUpdatedRequest(self, user, photo, nickname):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleVCardUpdatedRequest(user=%s, nickname=%s)" % (user, nickname))
|
2016-01-08 13:32:06 +00:00
|
|
|
self.sessions[user].setProfilePicture(photo)
|
2015-12-25 14:38:09 +00:00
|
|
|
|
2016-01-09 15:12:59 +00:00
|
|
|
def handleBuddyBlockToggled(self, user, buddy, blocked):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("handleBuddyBlockedToggled(user=%s, buddy=%s, blocked=%s)" % (user, buddy, blocked))
|
2016-01-09 15:12:59 +00:00
|
|
|
|
2016-01-07 17:31:05 +00:00
|
|
|
def relogin(self, user, legacyName, password, extra):
|
|
|
|
"""
|
|
|
|
Used to re-initialize the session object. Used when finished with
|
|
|
|
registration session and the user needs to login properly
|
|
|
|
"""
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug("relogin(user=%s, legacyName=%s)" % (user, legacyName))
|
2016-01-07 17:31:05 +00:00
|
|
|
# Change password in spectrum database
|
|
|
|
self.handleQuery('register %s %s %s' % (user, legacyName, password))
|
|
|
|
# Key word means we should register a new password
|
|
|
|
if password == 'register': # This shouldn't happen, but just in case
|
|
|
|
self.sessions[user] = RegisterSession(self, user, legacyName, extra)
|
|
|
|
else:
|
|
|
|
self.sessions[user] = Session(self, user, legacyName, extra)
|
|
|
|
self.sessions[user].login(password)
|
2015-09-21 18:30:11 +00:00
|
|
|
|
2013-06-28 19:37:39 +00:00
|
|
|
# TODO
|
2013-05-31 18:53:21 +00:00
|
|
|
def handleAttentionRequest(self, user, buddy, message):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handleFTStartRequest(self, user, buddy, fileName, size, ftID):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.debug('File send request %s, for user %s, from %s, size: %s' %
|
|
|
|
(fileName, user, buddy, size))
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
def handleFTFinishRequest(self, user, buddy, fileName, size, ftID):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handleFTPauseRequest(self, ftID):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handleFTContinueRequest(self, ftID):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handleRawXmlRequest(self, xml):
|
|
|
|
pass
|
|
|
|
|
2015-11-09 22:09:12 +00:00
|
|
|
def handleMessageAckRequest(self, user, legacyName, ID = 0):
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.info("Meassage ACK request for %s !!" % legacyName)
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2013-05-31 18:53:21 +00:00
|
|
|
def sendData(self, data):
|
|
|
|
self.io.sendData(data)
|
|
|
|
|