Changeset 2b4402f


Ignore:
Timestamp:
2015-05-20T12:56:47Z (9 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Children:
b09ce17
Parents:
5f8ad281
Message:

Fixed bugs by not doing things according to the docs but according to the
example implementation.

Contact list handling works better now. It figures out which contacts are
valid and which ones are not, etc. Also, status/presence is starting to
work though for some reason yowsup is not giving me the incoming presence
stanzas. Yeah no idea why. \o/

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/wa.py

    r5f8ad281 r2b4402f  
    33import logging
    44import threading
     5import time
    56
    67import yowsup
     
    2122from yowsup.layers.network                     import YowNetworkLayer
    2223from yowsup.layers.coder                       import YowCoderLayer
    23 from yowsup.stacks import YowStack
     24from yowsup.stacks import YowStack, YowStackBuilder
    2425from yowsup.common import YowConstants
    2526from yowsup.layers import YowLayerEvent
     
    6061
    6162        def receive(self, entity):
    62                 print "Received: %s" % entity.getTag()
     63                print "Received: %r" % entity
    6364                print entity
    6465                super(BitlBeeLayer, self).receive(entity)
     
    9495        @ProtocolEntityCallback("presence")
    9596        def onPresence(self, pres):
    96                 print pres
     97                status = 8 # MOBILE
     98                online = isinstance(pres, AvailablePresenceProtocolEntity)
     99                if online:
     100                        status += 1 # ONLINE
     101                imcb_buddy_status(pres.getFrom(), status, None, None)
    97102       
    98103        @ProtocolEntityCallback("message")
     
    110115                self.toLower(ack)
    111116
     117        @ProtocolEntityCallback("iq")
     118        def onIq(self, entity):
     119                if isinstance(entity, ResultSyncIqProtocolEntity):
     120                        return self.onSyncResult(entity)
     121       
     122        def onSyncResult(self, entity):
     123                # TODO HERE AND ELSEWHERE: Threat idiocy happens when going
     124                # from here to the IMPlugin. Check how bjsonrpc lets me solve that.
     125                ok = set(num.lstrip("+") for num in entity.inNumbers)
     126                for handle in self.b.contacts:
     127                        if handle.split("@")[0] in ok:
     128                                self.toLower(SubscribePresenceProtocolEntity(handle))
     129                                self.cb.add_buddy(handle, "")
     130                if entity.outNumbers:
     131                        self.cb.error("Not on WhatsApp: %s" % ", ".join(entity.outNumbers))
     132                if entity.invalidNumbers:
     133                        self.cb.error("Invalid numbers: %s" % ", ".join(entity.invalidNumbers))
     134
     135        @ProtocolEntityCallback("notification")
     136        def onNotification(self, ent):
     137                if isinstance(ent, StatusNotificationProtocolEntity):
     138                        return self.onStatusNotification(ent)
     139       
     140        def onStatusNotification(self, status):
     141                print "New status for %s: %s" % (status.getFrom(), status.status)
     142
    112143        @ProtocolEntityCallback("chatstate")
    113144        def onChatstate(self, entity):
     
    133164                # of their event loop :-(
    134165                raise YowsupDaemon.Terminate
     166
    135167
    136168class YowsupIMPlugin(implugin.BitlBeeIMPlugin):
     
    158190                self.bee.log("Started yowsup thread")
    159191               
     192                self.logging_in = True
    160193                self.contacts = set()
    161194
     
    172205
    173206        def add_buddy(self, handle, _group):
    174                 self.yow.Ship(SubscribePresenceProtocolEntity(handle))
    175                 # Need to confirm additions. See if this can be done based on server ACKs.
    176                 self.bee.add_buddy(handle, "")
     207                if self.logging_in:
     208                        # Need to batch up the initial adds. This is a "little" ugly.
     209                        self.contacts.add(handle)
     210                else:
     211                        self.yow.Ship(GetSyncIqProtocolEntity(
     212                            ["+" + handle.split("@")[0]], mode=GetSyncIqProtocolEntity.MODE_DELTA))
     213                        self.yow.Ship(SubscribePresenceProtocolEntity(handle))
    177214
    178215        def remove_buddy(self, handle, _group):
     
    180217
    181218        def set_away(self, _state, status):
     219                # When our first status is set, we've finalised login.
     220                # Which means sync the full contact list now.
     221                if self.logging_in:
     222                        self.logging_in = False
     223                        self.send_initial_contacts()
     224               
    182225                # I think state is not supported?
    183226                print "Trying to set status to %r, %r" % (_state, status)
    184227                self.yow.Ship(SetStatusIqProtocolEntity(status))
    185228
     229        def send_initial_contacts(self):
     230                if not self.contacts:
     231                        return
     232                numbers = [("+" + x.split("@")[0]) for x in self.contacts]
     233                self.yow.Ship(GetSyncIqProtocolEntity(numbers))
     234
    186235        def set_set_name(self, _key, value):
    187                 self.yow.Ship(PresenceProtocolEntity(value))
     236                self.yow.Ship(PresenceProtocolEntity(name=value))
    188237
    189238        def build_stack(self, account):
    190                 layers = (
    191                         BitlBeeLayer,
    192                        
    193                         (
    194                          YowAckProtocolLayer,
    195                          YowAuthenticationProtocolLayer,
    196                          YowIbProtocolLayer,
    197                          YowIqProtocolLayer,
    198                          YowMessagesProtocolLayer,
    199                          YowNotificationsProtocolLayer,
    200                          YowPresenceProtocolLayer,
    201                          YowReceiptProtocolLayer,
    202                         )
    203 
    204                 ) + YOWSUP_CORE_LAYERS
    205                
    206239                creds = (account["user"].split("@")[0], account["pass"])
    207240
    208                 stack = YowStack(layers)
     241                stack = (YowStackBuilder()
     242                         .pushDefaultLayers(False)
     243                         .push(BitlBeeLayer)
     244                         .build())
    209245                stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, creds)
    210246                stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0])
Note: See TracChangeset for help on using the changeset viewer.