- Timestamp:
- 2015-05-20T12:56:47Z (9 years ago)
- Children:
- b09ce17
- Parents:
- 5f8ad281
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/wa.py
r5f8ad281 r2b4402f 3 3 import logging 4 4 import threading 5 import time 5 6 6 7 import yowsup … … 21 22 from yowsup.layers.network import YowNetworkLayer 22 23 from yowsup.layers.coder import YowCoderLayer 23 from yowsup.stacks import YowStack 24 from yowsup.stacks import YowStack, YowStackBuilder 24 25 from yowsup.common import YowConstants 25 26 from yowsup.layers import YowLayerEvent … … 60 61 61 62 def receive(self, entity): 62 print "Received: % s" % entity.getTag()63 print "Received: %r" % entity 63 64 print entity 64 65 super(BitlBeeLayer, self).receive(entity) … … 94 95 @ProtocolEntityCallback("presence") 95 96 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) 97 102 98 103 @ProtocolEntityCallback("message") … … 110 115 self.toLower(ack) 111 116 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 112 143 @ProtocolEntityCallback("chatstate") 113 144 def onChatstate(self, entity): … … 133 164 # of their event loop :-( 134 165 raise YowsupDaemon.Terminate 166 135 167 136 168 class YowsupIMPlugin(implugin.BitlBeeIMPlugin): … … 158 190 self.bee.log("Started yowsup thread") 159 191 192 self.logging_in = True 160 193 self.contacts = set() 161 194 … … 172 205 173 206 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)) 177 214 178 215 def remove_buddy(self, handle, _group): … … 180 217 181 218 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 182 225 # I think state is not supported? 183 226 print "Trying to set status to %r, %r" % (_state, status) 184 227 self.yow.Ship(SetStatusIqProtocolEntity(status)) 185 228 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 186 235 def set_set_name(self, _key, value): 187 self.yow.Ship(PresenceProtocolEntity( value))236 self.yow.Ship(PresenceProtocolEntity(name=value)) 188 237 189 238 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_LAYERS205 206 239 creds = (account["user"].split("@")[0], account["pass"]) 207 240 208 stack = YowStack(layers) 241 stack = (YowStackBuilder() 242 .pushDefaultLayers(False) 243 .push(BitlBeeLayer) 244 .build()) 209 245 stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, creds) 210 246 stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0])
Note: See TracChangeset
for help on using the changeset viewer.