Changeset d832164


Ignore:
Timestamp:
2015-06-17T22:46:40Z (5 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Children:
b75671d
Parents:
c82a88d
Message:

Restore pings and try to finish login even if WA is rate limiting the sync.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/wa.py

    rc82a88d rd832164  
    101101               
    102102                # We're done once this set is empty.
    103                 self.todo = set(["contacts", "groups"])
     103                self.todo = set(["contacts", "groups", "ping"])
    104104               
    105105                # Supposedly WA can also do national-style phone numbers without
     
    109109                self.toLower(GetSyncIqProtocolEntity(numbers))
    110110                self.toLower(ListGroupsIqProtocolEntity())
     111                self.b.keepalive()
    111112               
    112113                try:
     
    205206                elif isinstance(entity, ListGroupsResultIqProtocolEntity):
    206207                        return self.onListGroupsResult(entity)
     208                elif "ping" in self.todo: # Pong has no type, sigh.
     209                        if "contacts" in self.todo:
     210                                # Shitty Whatsapp rejected the sync request, and
     211                                # annoying Yowsup doesn't inform on error responses.
     212                                # So instead, if we received no response to it but
     213                                # did get our ping back, declare failure.
     214                                self.onSyncResultFail()
     215                        self.check_connected("ping")
    207216       
    208217        def onSyncResult(self, entity):
     
    219228                                      ", ".join(entity.invalidNumbers))
    220229
    221                 # Disabled since yowsup won't give us the result...
    222                 if entity.inNumbers and False:
    223                         self.toLower(GetStatusIqProtocolEntity(entity.inNumbers.values()))
    224                         self.todo.add("statuses")
    225                        
     230                #self.getStatuses(entity.inNumbers.values())
     231                self.check_connected("contacts")
     232
     233        def onSyncResultFail(self):
     234                # Whatsapp rate-limits sync stanzas, so in case of failure
     235                # just assume all contacts are valid.
     236                for jid in self.cb.get_local_contacts():
     237                        self.toLower(SubscribePresenceProtocolEntity(jid))
     238                        self.cb.add_buddy(jid, "")
     239                #self.getStatuses?
    226240                self.check_connected("contacts")
    227241
     
    241255
    242256                self.check_connected("groups")
     257
     258        def getStatuses(self, contacts):
     259                return # Disabled since yowsup won't give us the result...
     260                self.toLower(GetStatusIqProtocolEntity(contacts))
     261                self.todo.add("statuses")
    243262
    244263        @ProtocolEntityCallback("notification")
     
    307326        ACCOUNT_FLAGS = 14 # HANDLE_DOMAINS + STATUS_MESSAGE + LOCAL_CONTACTS
    308327        # TODO: HANDLE_DOMAIN in right place (add ... ... nick bug)
     328        PING_INTERVAL = 299 # seconds
    309329
    310330        def login(self, account):
     
    318338                self.groups_by_id = {}
    319339
     340                self.next_ping = None
     341
    320342        def keepalive(self):
    321                 # Too noisy while debugging
    322                 # WTF yowsup is SPAWNING A THREAD just for this. Figure out
    323                 # how to kill that nonsense.
    324                 pass
    325                 #self.yow.Ship(PingIqProtocolEntity(to="s.whatsapp.net"))
     343                if self.next_ping and (time.time() < self.next_ping):
     344                        return
     345                self.yow.Ship(PingIqProtocolEntity(to="s.whatsapp.net"))
     346                self.next_ping = time.time() + self.PING_INTERVAL
    326347
    327348        def logout(self):
Note: See TracChangeset for help on using the changeset viewer.