2016-04-09 22:21:58 +00:00
|
|
|
# use unicode encoding for all literals by default (for python2.x)
|
|
|
|
from __future__ import unicode_literals
|
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
|
2016-04-12 12:53:15 +00:00
|
|
|
country_code = "%s" % country_code
|
2016-01-07 15:57:41 +00:00
|
|
|
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')
|
2016-04-09 22:21:58 +00:00
|
|
|
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:
|
2016-04-09 22:21:58 +00:00
|
|
|
self.logger.warn('Unauthorised user (%s) attempting to send messages' %
|
2016-01-07 15:57:41 +00:00
|
|
|
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
|
2016-04-09 22:21:58 +00:00
|
|
|
out.append("%s: %s" % (k, v))
|
2016-01-07 17:31:05 +00:00
|
|
|
|
|
|
|
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
|