Changeset a852b2b


Ignore:
Timestamp:
2015-05-25T05:30:26Z (9 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Children:
f16c64d
Parents:
8c3637b
Message:

WA: Request contact list during login, only call the connected callback when
we're *really* done.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/wa.py

    r8c3637b ra852b2b  
    7878                self.cb = self.b.bee
    7979                self.b.yow = self
    80                 self.cb.connected()
     80               
     81                self.cb.log("Authenticated, syncing contact list")
     82               
     83                # We're done once this set is empty.
     84                self.todo = set(["contacts", "groups"])
     85               
     86                # Supposedly WA can also do national-style phone numbers without
     87                # a + prefix BTW (relative to I guess the user's country?). I
     88                # don't want to support this at least for now.
     89                numbers = [("+" + x.split("@")[0]) for x in self.cb.get_local_contacts()]
     90                self.toLower(GetSyncIqProtocolEntity(numbers))
    8191                self.toLower(ListGroupsIqProtocolEntity())
     92               
    8293                try:
    8394                        self.toLower(PresenceProtocolEntity(name=self.b.setting("name")))
    8495                except KeyError:
    8596                        pass
    86                 # Should send the contact list now, but BitlBee hasn't given
    87                 # it yet. See set_away() and send_initial_contacts() below.
     97
     98        def check_connected(self, done):
     99                if self.todo is None:
     100                        return
     101                self.todo.remove(done)
     102                if not self.todo:
     103                        self.todo = None
     104                        self.cb.connected()
    88105       
    89106        @ProtocolEntityCallback("failure")
     
    101118        @ProtocolEntityCallback("presence")
    102119        def onPresence(self, pres):
     120                if pres.getFrom() == self.b.account["user"]:
     121                        # WA returns our own presence. Meh.
     122                        return
     123               
    103124                status = 8 # MOBILE
    104125                if pres.getType() != "unavailable":
    105126                        status |= 1 # ONLINE
    106127                self.cb.buddy_status(pres.getFrom(), status, None, None)
     128               
    107129                try:
    108130                        # Last online time becomes idle time which I guess is
     
    137159        def onIq(self, entity):
    138160                if isinstance(entity, ResultSyncIqProtocolEntity):
    139                         print "XXX SYNC RESULT RECEIVED!"
    140161                        return self.onSyncResult(entity)
    141162                elif isinstance(entity, ListParticipantsResultIqProtocolEntity):
     
    147168                # TODO HERE AND ELSEWHERE: Thread idiocy happens when going
    148169                # from here to the IMPlugin. Check how bjsonrpc lets me solve that.
    149                 # ALSO TODO: See why this one doesn't seem to be called for adds later.
    150                 ok = set(jid.lower() for jid in entity.inNumbers.values())
    151                 for handle in self.b.contacts:
    152                         if handle.lower() in ok:
    153                                 self.toLower(SubscribePresenceProtocolEntity(handle))
    154                                 self.cb.add_buddy(handle, "")
     170                for num, jid in entity.inNumbers.iteritems():
     171                        self.toLower(SubscribePresenceProtocolEntity(jid))
     172                        self.cb.add_buddy(jid, "")
    155173                if entity.outNumbers:
    156174                        self.cb.error("Not on WhatsApp: %s" %
     
    159177                        self.cb.error("Invalid numbers: %s" %
    160178                                      ", ".join(entity.invalidNumbers.keys()))
     179
     180                self.check_connected("contacts")
    161181
    162182        def onListGroupsResult(self, groups):
     
    169189                        group["info"] = g
    170190
     191                self.check_connected("groups")
     192
    171193        @ProtocolEntityCallback("notification")
    172194        def onNotification(self, ent):
     
    213235        SETTINGS = {
    214236                "cc": {
     237                        # Country code. Seems to be required for registration only.
    215238                        "type": "int",
    216239                },
     
    233256                self.bee.log("Started yowsup thread")
    234257               
    235                 self.logging_in = True
    236                 self.contacts = set()
    237258                self.groups = {}
    238259                self.groups_by_id = {}
     
    252273
    253274        def add_buddy(self, handle, _group):
    254                 if self.logging_in:
    255                         # Need to batch up the initial adds. This is a "little" ugly.
    256                         self.contacts.add(handle)
    257                 else:
    258                         self.yow.Ship(GetSyncIqProtocolEntity(
    259                             ["+" + handle.split("@")[0]], mode=GetSyncIqProtocolEntity.MODE_DELTA))
    260                         self.yow.Ship(SubscribePresenceProtocolEntity(handle))
     275                self.yow.Ship(GetSyncIqProtocolEntity(
     276                    ["+" + handle.split("@")[0]], mode=GetSyncIqProtocolEntity.MODE_DELTA))
    261277
    262278        def remove_buddy(self, handle, _group):
     
    264280
    265281        def set_away(self, state, status):
    266                 # When our first status is set, we've finalised login.
    267                 # Which means sync the full contact list now.
    268                 if self.logging_in:
    269                         self.logging_in = False
    270                         self.send_initial_contacts()
    271                
    272282                print "Trying to set status to %r, %r" % (state, status)
    273283                if state:
     
    278288                if status:
    279289                        self.yow.Ship(SetStatusIqProtocolEntity(status))
    280 
    281         def send_initial_contacts(self):
    282                 if not self.contacts:
    283                         return
    284                 numbers = [("+" + x.split("@")[0]) for x in self.contacts]
    285                 self.yow.Ship(GetSyncIqProtocolEntity(numbers))
    286290
    287291        def set_set_name(self, _key, value):
Note: See TracChangeset for help on using the changeset viewer.