2016-01-07 15:57:41 +00:00
|
|
|
from Spectrum2 import protocol_pb2
|
|
|
|
|
|
|
|
from yowsupwrapper import YowsupApp
|
|
|
|
import logging
|
|
|
|
import threadutils
|
2016-01-07 17:31:05 +00:00
|
|
|
import sys
|
2016-01-07 15:57:41 +00:00
|
|
|
|
|
|
|
class RegisterSession(YowsupApp):
|
|
|
|
"""
|
|
|
|
A dummy Session object that is used to register a user to whatsapp
|
|
|
|
"""
|
|
|
|
WANT_CC = 0
|
|
|
|
WANT_SMS = 1
|
|
|
|
def __init__(self, backend, user, legacyName, extra):
|
|
|
|
self.user = user
|
|
|
|
self.number = legacyName
|
|
|
|
self.backend = backend
|
2016-01-07 17:31:05 +00:00
|
|
|
self.countryCode = ''
|
2016-01-07 15:57:41 +00:00
|
|
|
self.logger = logging.getLogger(self.__class__.__name__)
|
|
|
|
self.state = self.WANT_CC
|
|
|
|
|
|
|
|
def login(self, password=""):
|
|
|
|
self.backend.handleConnected(self.user)
|
|
|
|
self.backend.handleBuddyChanged(self.user, 'bot', 'bot',
|
|
|
|
['Admin'], protocol_pb2.STATUS_ONLINE)
|
|
|
|
self.backend.handleMessage(self.user, 'bot',
|
|
|
|
'Please enter your country code')
|
|
|
|
|
|
|
|
def sendMessageToWA(self, buddy, message, ID='', xhtml=''):
|
|
|
|
if buddy == 'bot' and self.state == self.WANT_CC:
|
|
|
|
try:
|
|
|
|
country_code = int(message.strip())
|
|
|
|
except ValueError:
|
|
|
|
self.backend.handleMessage(self.user, 'bot',
|
|
|
|
'Country code must be a number')
|
|
|
|
else: # Succeded in decoding country code
|
|
|
|
country_code = str(country_code)
|
|
|
|
if country_code != self.number[:len(country_code)]:
|
|
|
|
self.backend.handleMessage(self.user,
|
|
|
|
'bot', 'Number does not start with provided country code')
|
|
|
|
else:
|
|
|
|
self.backend.handleMessage(self.user, 'bot', 'Requesting sms code')
|
|
|
|
self.logger.debug('Requesting SMS code for %s', self.user)
|
2016-01-07 17:31:05 +00:00
|
|
|
self.countryCode = country_code
|
|
|
|
self._requestSMSCodeNonBlock()
|
2016-01-07 15:57:41 +00:00
|
|
|
elif buddy == 'bot' and self.state == self.WANT_SMS:
|
2016-01-07 17:31:05 +00:00
|
|
|
code = message.strip()
|
|
|
|
if self._checkSMSFormat(code):
|
|
|
|
self._requestPassword(code)
|
|
|
|
else:
|
|
|
|
self.backend.handleMessage(self.user,
|
|
|
|
'bot', 'Invalid code. Must be of the form XXX-XXX.')
|
2016-01-07 15:57:41 +00:00
|
|
|
else:
|
|
|
|
self.logger.warn('Unauthorised user (%s) attempting to send messages',
|
|
|
|
self.user)
|
|
|
|
self.backend.handleMessage(self.user, buddy,
|
|
|
|
'You are not logged in yet. You can only send messages to bot.')
|
|
|
|
|
2016-01-07 17:31:05 +00:00
|
|
|
def _checkSMSFormat(self, sms):
|
|
|
|
splitting = sms.split('-')
|
|
|
|
if len(splitting) != 2:
|
|
|
|
return False
|
|
|
|
a, b = splitting
|
|
|
|
if len(a) != 3 and len(b) != 3:
|
|
|
|
return False
|
|
|
|
try:
|
|
|
|
int(a)
|
|
|
|
int(b)
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _requestSMSCodeNonBlock(self):
|
|
|
|
number = self.number[len(self.countryCode):]
|
|
|
|
threadFunc = lambda: self.requestSMSCode(self.countryCode, number)
|
2016-01-07 15:57:41 +00:00
|
|
|
threadutils.runInThread(threadFunc, self._confirmation)
|
2016-01-07 17:31:05 +00:00
|
|
|
self.backend.handleMessage(self.user, 'bot', 'SMS Code Sent')
|
2016-01-07 15:57:41 +00:00
|
|
|
|
|
|
|
def _confirmation(self, result):
|
|
|
|
self.state = self.WANT_SMS
|
2016-01-07 17:31:05 +00:00
|
|
|
resultStr = self._resultToString(result)
|
|
|
|
self.backend.handleMessage(self.user, 'bot', 'Response:')
|
|
|
|
self.backend.handleMessage(self.user, 'bot', resultStr)
|
2016-01-07 15:57:41 +00:00
|
|
|
self.backend.handleMessage(self.user, 'bot', 'Please enter SMS Code')
|
|
|
|
|
2016-01-07 17:31:05 +00:00
|
|
|
def _requestPassword(self, smsCode):
|
|
|
|
cc = self.countryCode
|
|
|
|
number = self.number[len(cc):]
|
|
|
|
threadFunc = lambda: self.requestPassword(cc, number, smsCode)
|
|
|
|
threadutils.runInThread(threadFunc, self._gotPassword)
|
|
|
|
self.backend.handleMessage(self.user, 'bot', 'Getting Password')
|
|
|
|
|
|
|
|
def _gotPassword(self, result):
|
|
|
|
resultStr = self._resultToString(result)
|
|
|
|
self.backend.handleMessage(self.user, 'bot', 'Response:')
|
|
|
|
self.backend.handleMessage(self.user, 'bot', resultStr)
|
|
|
|
self.backend.handleMessage(self.user, 'bot', 'Logging you in')
|
|
|
|
password = result['pw']
|
|
|
|
self.backend.relogin(self.user, self.number, password, None)
|
|
|
|
|
|
|
|
def _resultToString(self, result):
|
|
|
|
unistr = str if sys.version_info >= (3, 0) else unicode
|
|
|
|
out = []
|
|
|
|
for k, v in result.items():
|
|
|
|
if v is None:
|
|
|
|
continue
|
|
|
|
out.append("%s: %s" %(k, v.encode("utf-8") if type(v) is unistr else v))
|
|
|
|
|
|
|
|
return "\n".join(out)
|
|
|
|
|
2016-01-07 15:57:41 +00:00
|
|
|
# Dummy methods. Whatsapp backend might call these, but they should have no
|
|
|
|
# effect
|
|
|
|
def logout(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def joinRoom(self, room, nickname):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def leaveRoom(self, room):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def changeStatusMessage(self, statusMessage):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def changeStatus(self, status):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def loadBuddies(self, buddies):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def updateBuddy(self, buddies):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def removeBuddy(self, buddies):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def sendTypingStarted(self, buddy):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def sendTypingStopped(self, buddy):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def requestVCard(self, buddy, ID):
|
|
|
|
pass
|
2015-12-25 14:38:09 +00:00
|
|
|
|
|
|
|
def setProfilePicture(self, previewPicture, fullPicture = None):
|
|
|
|
pass
|