Subscribe to presence only after user has logged in

Buddy.py would try to subscribe to presence before the user had logged
in to whatsapp. A loginQueue was added, so that functions could be
deferred to login.
This commit is contained in:
moyamo 2015-12-02 19:32:55 +02:00
parent 8385888e97
commit 5a8874c9b9
3 changed files with 26 additions and 4 deletions

View file

@ -76,7 +76,12 @@ class BuddyList(dict):
contacts = self.keys() contacts = self.keys()
if self.synced == False: if self.synced == False:
if self.session.loggedIn:
self.session.sendSync(contacts, delta = False, interactive = True) self.session.sendSync(contacts, delta = False, interactive = True)
else:
self.session.loginQueue.append(
lambda: self.session.sendSync(contacts, delta = False, interactive = True)
)
self.synced = True self.synced = True
# add = set(new) - set(old) # add = set(new) - set(old)
@ -92,7 +97,17 @@ class BuddyList(dict):
# self.unsubscribePresence(number) # self.unsubscribePresence(number)
# #
for number in contacts: for number in contacts:
self.logger.debug("Attempting to subscribe to %s", number)
if self.session.loggedIn and number != 'bot':
self.session.subscribePresence(number) self.session.subscribePresence(number)
elif number != 'bot':
# Bah! Python scoping rules aren't nice. This function is needed
# preserve the value of number
def deferredLogin(number):
self.session.loginQueue.append(
lambda: self.session.subscribePresence(number)
)
deferredLogin(number)
buddy = self[number] buddy = self[number]
self.backend.handleBuddyChanged(self.user, number, buddy.nick, self.backend.handleBuddyChanged(self.user, number, buddy.nick,
buddy.groups, protocol_pb2.STATUS_NONE, buddy.groups, protocol_pb2.STATUS_NONE,

View file

@ -74,12 +74,14 @@ class Session(YowsupApp):
self.groups = {} self.groups = {}
self.gotGroupList = False self.gotGroupList = False
# Functions to exectute when logged in via yowsup
self.loginQueue = []
self.joinRoomQueue = [] self.joinRoomQueue = []
self.presenceRequested = [] self.presenceRequested = []
self.offlineQueue = [] self.offlineQueue = []
self.msgIDs = { } self.msgIDs = { }
self.groupOfflineQueue = { } self.groupOfflineQueue = { }
self.shouldBeConnected = False self.loggedIn = False
self.timer = None self.timer = None
self.password = None self.password = None
@ -102,6 +104,7 @@ class Session(YowsupApp):
def logout(self): def logout(self):
self.logger.info("%s logged out", self.user) self.logger.info("%s logged out", self.user)
super(Session, self).logout() super(Session, self).logout()
self.loggedIn = False
def login(self, password): def login(self, password):
self.logger.info("%s attempting login", self.user) self.logger.info("%s attempting login", self.user)
@ -245,16 +248,19 @@ class Session(YowsupApp):
#self.bot.call("welcome") #self.bot.call("welcome")
self.initialized = True self.initialized = True
self.sendPresence(True) self.sendPresence(True)
for func in self.loginQueue:
func()
self.logger.debug('Requesting groups list') self.logger.debug('Requesting groups list')
self.requestGroupsList(self._updateGroups) self.requestGroupsList(self._updateGroups)
self.loggedIn = True
# Called by superclass # Called by superclass
def onAuthFailed(self, reason): def onAuthFailed(self, reason):
self.logger.info("Auth failed: %s (%s)", self.user, reason) self.logger.info("Auth failed: %s (%s)", self.user, reason)
self.backend.handleDisconnected(self.user, 0, reason) self.backend.handleDisconnected(self.user, 0, reason)
self.password = None self.password = None
self.shouldBeConnected = False self.loggedIn = False
# Called by superclass # Called by superclass
def onDisconnect(self): def onDisconnect(self):

View file

@ -216,6 +216,7 @@ class YowsupApp(object):
- phone_number: (str) The cellphone number of the person to - phone_number: (str) The cellphone number of the person to
subscribe to subscribe to
""" """
self.logger.debug("Subscribing to Presence updates from %s", (phone_number))
jid = phone_number + '@s.whatsapp.net' jid = phone_number + '@s.whatsapp.net'
entity = SubscribePresenceProtocolEntity(jid) entity = SubscribePresenceProtocolEntity(jid)
self.sendEntity(entity) self.sendEntity(entity)