source: python/wa.py @ ee8307c

Last change on this file since ee8307c was ee8307c, checked in by Wilmer van der Gaast <wilmer@…>, at 2015-07-30T23:22:47Z

Fix two callbacks in wa.py.

  • Property mode set to 100755
File size: 16.6 KB
Line 
1#!/usr/bin/python
2
3import collections
4import logging
5import threading
6import time
7
8import yowsup
9
10from yowsup.layers.auth                        import YowAuthenticationProtocolLayer
11from yowsup.layers.protocol_acks               import YowAckProtocolLayer
12from yowsup.layers.protocol_chatstate          import YowChatstateProtocolLayer
13from yowsup.layers.protocol_contacts           import YowContactsIqProtocolLayer
14from yowsup.layers.protocol_groups             import YowGroupsProtocolLayer
15from yowsup.layers.protocol_ib                 import YowIbProtocolLayer
16from yowsup.layers.protocol_iq                 import YowIqProtocolLayer
17from yowsup.layers.protocol_messages           import YowMessagesProtocolLayer
18from yowsup.layers.protocol_notifications      import YowNotificationsProtocolLayer
19from yowsup.layers.protocol_presence           import YowPresenceProtocolLayer
20from yowsup.layers.protocol_privacy            import YowPrivacyProtocolLayer
21from yowsup.layers.protocol_profiles           import YowProfilesProtocolLayer
22from yowsup.layers.protocol_receipts           import YowReceiptProtocolLayer
23from yowsup.layers.network                     import YowNetworkLayer
24from yowsup.layers.coder                       import YowCoderLayer
25from yowsup.stacks import YowStack, YowStackBuilder
26from yowsup.common import YowConstants
27from yowsup.layers import YowLayerEvent
28from yowsup.stacks import YowStack, YOWSUP_CORE_LAYERS
29from yowsup import env
30
31from yowsup.layers.interface                             import YowInterfaceLayer, ProtocolEntityCallback
32from yowsup.layers.protocol_acks.protocolentities        import *
33from yowsup.layers.protocol_chatstate.protocolentities   import *
34from yowsup.layers.protocol_contacts.protocolentities    import *
35from yowsup.layers.protocol_groups.protocolentities      import *
36from yowsup.layers.protocol_ib.protocolentities          import *
37from yowsup.layers.protocol_iq.protocolentities          import *
38from yowsup.layers.protocol_media.mediauploader import MediaUploader
39from yowsup.layers.protocol_media.protocolentities       import *
40from yowsup.layers.protocol_messages.protocolentities    import *
41from yowsup.layers.protocol_notifications.protocolentities import *
42from yowsup.layers.protocol_presence.protocolentities    import *
43from yowsup.layers.protocol_privacy.protocolentities     import *
44from yowsup.layers.protocol_profiles.protocolentities    import *
45from yowsup.layers.protocol_receipts.protocolentities    import *
46from yowsup.layers.axolotl.protocolentities.iq_key_get import GetKeysIqProtocolEntity
47from yowsup.layers.axolotl import YowAxolotlLayer
48from yowsup.common.tools import ModuleTools
49
50import implugin
51
52logger = logging.getLogger("yowsup.layers.logger.layer")
53logger.setLevel(logging.DEBUG)
54ch = logging.StreamHandler()
55ch.setLevel(logging.DEBUG)
56logger.addHandler(ch)
57
58"""
59TODO/Things I'm unhappy about:
60
61About the fact that WhatsApp is a rubbish protocol that happily rejects
62every second stanza you send it if you're trying to implement a client that
63doesn't keep local state. See how to cope with that.. It'd help if Yowsup
64came with docs on what a normal login sequence looks like instead of just
65throwing some stanzas over a wall but hey.
66
67The randomness of where which bits/state live, in the implugin and the
68yowsup layer. Can't really merge this but at least state should live in
69one place.
70
71Mix of silly CamelCase and proper_style. \o/
72
73Most important: This is NOT thread-clean. implugin can call into yowsup
74cleanly by throwing closures into a queue, but there's no mechanism in
75the opposite direction, I'll need to cook up some hack to make this
76possible through bjsonrpc's tiny event loop. I think I know how...
77
78And more. But let's first get this into a state where it even works..
79"""
80
81class BitlBeeLayer(YowInterfaceLayer):
82
83        def __init__(self, *a, **kwa):
84                super(BitlBeeLayer, self).__init__(*a, **kwa)
85                # Offline messages are sent while we're still logging in.
86                self.msg_queue = []
87
88        def receive(self, entity):
89                print "Received: %r" % entity
90                #print entity
91                super(BitlBeeLayer, self).receive(entity)
92
93        def Ship(self, entity):
94                """Send an entity into Yowsup, but through the correct thread."""
95                print "Queueing: %s" % entity.getTag()
96                #print entity
97                def doit():
98                        self.toLower(entity)
99                self.getStack().execDetached(doit)
100
101        @ProtocolEntityCallback("success")
102        def onSuccess(self, entity):
103                self.b = self.getStack().getProp("org.bitlbee.Bijtje")
104                self.cb = self.b.bee
105                self.b.yow = self
106               
107                self.cb.log("Authenticated, syncing contact list")
108               
109                # We're done once this set is empty.
110                self.todo = set(["contacts", "groups", "ping"])
111               
112                # Supposedly WA can also do national-style phone numbers without
113                # a + prefix BTW (relative to I guess the user's country?). I
114                # don't want to support this at least for now.
115                numbers = [("+" + x.split("@")[0]) for x in self.cb.get_local_contacts()]
116                self.toLower(GetSyncIqProtocolEntity(numbers))
117                self.toLower(ListGroupsIqProtocolEntity())
118                self.b.keepalive()
119               
120                try:
121                        self.toLower(PresenceProtocolEntity(name=self.b.setting("name")))
122                except KeyError:
123                        pass
124
125        def check_connected(self, done):
126                if not self.todo:
127                        return
128                self.todo.remove(done)
129                if not self.todo:
130                        self.cb.connected()
131                        self.flush_msg_queue()
132       
133        def flush_msg_queue(self):
134                for msg in self.msg_queue:
135                        self.onMessage(msg)
136                self.msg_queue = None
137       
138        @ProtocolEntityCallback("failure")
139        def onFailure(self, entity):
140                self.b = self.getStack().getProp("org.bitlbee.Bijtje")
141                self.cb = self.b.bee
142                self.cb.error(entity.getReason())
143                self.cb.logout(False)
144
145        def onEvent(self, event):
146                # TODO: Make this work without, hmm, over-recursing. (This handler
147                # getting called when we initiated the disconnect, which upsets yowsup.)
148                if event.getName() == "orgopenwhatsapp.yowsup.event.network.disconnected":
149                        self.cb.error(event.getArg("reason"))
150                        self.cb.logout(True)
151                        self.getStack().execDetached(self.daemon.StopDaemon)
152                else:
153                        print "Received event: %s name %s" % (event, event.getName())
154       
155        @ProtocolEntityCallback("presence")
156        def onPresence(self, pres):
157                if pres.getFrom() == self.b.account["user"]:
158                        # WA returns our own presence. Meh.
159                        return
160               
161                # Online/offline is not really how WA works. Let's show everyone
162                # as online but unavailable folks as away. This also solves the
163                # problem of offline->IRC /quit causing the persons to leave chat
164                # channels as well (and not reappearing there when they return).
165                status = 8 | 1  # MOBILE | ONLINE
166                if pres.getType() == "unavailable":
167                        status |= 4  # AWAY
168                self.cb.buddy_status(pres.getFrom(), status, None, None)
169               
170                try:
171                        # Last online time becomes idle time which I guess is
172                        # sane enough?
173                        self.cb.buddy_times(pres.getFrom(), 0, int(pres.getLast()))
174                except (ValueError, TypeError):
175                        # Could be "error" or, more likely, "deny", or None.
176                        pass
177       
178        @ProtocolEntityCallback("message")
179        def onMessage(self, msg):
180                if self.todo:
181                        # We're still logging in, so wait.
182                        self.msg_queue.append(msg)
183                        return
184
185                self.b.show_message(msg)
186
187                # ACK is required! So only use return above in case of errors.
188                # (So that we will/might get a retry after restarting.)
189                self.toLower(OutgoingReceiptProtocolEntity(msg.getId(), msg.getFrom()))
190
191        @ProtocolEntityCallback("receipt")
192        def onReceipt(self, entity):
193                ack = OutgoingAckProtocolEntity(entity.getId(), entity.getTag(),
194                                                entity.getType(), entity.getFrom())
195                self.toLower(ack)
196
197        @ProtocolEntityCallback("iq")
198        def onIq(self, entity):
199                if isinstance(entity, ResultSyncIqProtocolEntity):
200                        return self.onSyncResult(entity)
201                elif isinstance(entity, ListGroupsResultIqProtocolEntity):
202                        return self.onListGroupsResult(entity)
203                elif type(entity) == IqProtocolEntity:  # Pong has no type, sigh.
204                        self.b.last_pong = time.time()
205                        if self.todo:
206                                return self.onLoginPong()
207       
208        def onSyncResult(self, entity):
209                # TODO HERE AND ELSEWHERE: Thread idiocy happens when going
210                # from here to the IMPlugin. Check how bjsonrpc lets me solve that.
211                for num, jid in entity.inNumbers.iteritems():
212                        self.toLower(SubscribePresenceProtocolEntity(jid))
213                        self.cb.add_buddy(jid, "")
214                if entity.outNumbers:
215                        self.cb.error("Not on WhatsApp: %s" %
216                                      ", ".join(entity.outNumbers.keys()))
217                if entity.invalidNumbers:
218                        self.cb.error("Invalid numbers: %s" %
219                                      ", ".join(entity.invalidNumbers))
220
221                #self.getStatuses(entity.inNumbers.values())
222                self.check_connected("contacts")
223
224        def onSyncResultFail(self):
225                # Whatsapp rate-limits sync stanzas, so in case of failure
226                # just assume all contacts are valid.
227                for jid in self.cb.get_local_contacts():
228                        self.toLower(SubscribePresenceProtocolEntity(jid))
229                        self.cb.add_buddy(jid, "")
230                #self.getStatuses?
231                self.check_connected("contacts")
232
233        def onListGroupsResult(self, groups):
234                """Save group info for later if the user decides to join."""
235                for g in groups.getGroups():
236                        jid = g.getId()
237                        if "@" not in jid:
238                                jid += "@g.us"
239                        group = self.b.groups[jid]
240                        try:
241                                group["participants"] = g.getParticipants().keys()
242                        except AttributeError:
243                                # Depends on a change I made to yowsup that may
244                                # or may not get merged..
245                                group["participants"] = []
246                       
247                        # Save it. We're going to mix ListGroups elements and
248                        # Group-Subject notifications there, which don't have
249                        # consistent fieldnames for the same bits of info \o/
250                        g.getSubjectTimestamp = g.getSubjectTime
251                        group["topic"] = g
252
253                self.check_connected("groups")
254
255        def onLoginPong(self):
256                if "contacts" in self.todo:
257                        # Shitty Whatsapp rejected the sync request, and
258                        # annoying Yowsup doesn't inform on error responses.
259                        # So instead, if we received no response to it but
260                        # did get our ping back, declare failure.
261                        self.onSyncResultFail()
262                if "groups" in self.todo:
263                        # Well fuck this. Just reject ALL the things!
264                        # Maybe I don't need this one then.
265                        self.check_connected("groups")
266                self.check_connected("ping")
267
268        def getStatuses(self, contacts):
269                return # Disabled since yowsup won't give us the result...
270                self.toLower(GetStatusIqProtocolEntity(contacts))
271                self.todo.add("statuses")
272
273        @ProtocolEntityCallback("notification")
274        def onNotification(self, ent):
275                if isinstance(ent, StatusNotificationProtocolEntity):
276                        return self.onStatusNotification(ent)
277                elif isinstance(ent, SubjectGroupsNotificationProtocolEntity):
278                        return self.onGroupSubjectNotification(ent)
279
280        def onStatusNotification(self, status):
281                print "New status for %s: %s" % (status.getFrom(), status.status)
282                self.bee.buddy_status_msg(status.getFrom(), status.status)
283       
284        def onGroupSubjectNotification(self, sub):
285                print "New /topic for %s: %s" % (sub.getFrom(), sub.getSubject())
286                group = self.b.groups[sub.getFrom()]
287                group["topic"] = sub
288                id = group.get("id", None)
289                if id is not None:
290                        self.cb.chat_topic(id, sub.getSubjectOwner(),
291                                           sub.getSubject(), sub.getSubjectTimestamp())
292
293        @ProtocolEntityCallback("media")
294        def onMedia(self, med):
295                """Your PC better be MPC3 compliant!"""
296                print "YAY MEDIA! %r" % med
297                print med
298
299        #@ProtocolEntityCallback("chatstate")
300        #def onChatstate(self, entity):
301        #       print(entity)
302
303
304class YowsupDaemon(threading.Thread):
305        daemon = True
306        stack = None
307
308        class Terminate(Exception):
309                pass
310
311        def run(self):
312                try:
313                        self.stack.loop(timeout=0.2, discrete=0.2, count=1)
314                except YowsupDaemon.Terminate:
315                        print "Exiting loop!"
316                        pass
317       
318        def StopDaemon(self):
319                # Ugly, but yowsup offers no "run single iteration" version
320                # of their event loop :-(
321                raise YowsupDaemon.Terminate
322
323
324class YowsupIMPlugin(implugin.BitlBeeIMPlugin):
325        NAME = "wa"
326        SETTINGS = {
327                "cc": {
328                        # Country code. Seems to be required for registration only.
329                        "type": "int",
330                },
331                "name": {
332                        "flags": 0x100, # NULL_OK
333                },
334        }
335        AWAY_STATES = ["Away"]
336        ACCOUNT_FLAGS = 14 # HANDLE_DOMAINS + STATUS_MESSAGE + LOCAL_CONTACTS
337        # TODO: HANDLE_DOMAIN in right place (add ... ... nick bug)
338        PING_INTERVAL = 299 # seconds
339        PING_TIMEOUT = 360 # seconds
340
341        def login(self, account):
342                self.stack = self.build_stack(account)
343                self.daemon = YowsupDaemon(name="yowsup")
344                self.daemon.stack = self.stack
345                self.daemon.start()
346                self.bee.log("Started yowsup thread")
347               
348                self.groups = collections.defaultdict(dict)
349                self.groups_by_id = {}
350
351                self.next_ping = None
352                self.last_pong = time.time()
353
354        def keepalive(self):
355                if (time.time() - self.last_pong) > self.PING_TIMEOUT:
356                        self.cb.error("Ping timeout")
357                        self.cb.logout(True)
358                        return
359                if self.next_ping and (time.time() < self.next_ping):
360                        return
361                self.yow.Ship(PingIqProtocolEntity(to="s.whatsapp.net"))
362                self.next_ping = time.time() + self.PING_INTERVAL
363
364        def logout(self):
365                self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_DISCONNECT))
366                self.stack.execDetached(self.daemon.StopDaemon)
367
368        def buddy_msg(self, to, text, flags):
369                msg = TextMessageProtocolEntity(text, to=to)
370                self.yow.Ship(msg)
371
372        def add_buddy(self, handle, _group):
373                self.yow.Ship(GetSyncIqProtocolEntity(
374                    ["+" + handle.split("@")[0]], mode=GetSyncIqProtocolEntity.MODE_DELTA))
375
376        def remove_buddy(self, handle, _group):
377                self.yow.Ship(UnsubscribePresenceProtocolEntity(handle))
378
379        def set_away(self, state, status):
380                print "Trying to set status to %r, %r" % (state, status)
381                if state:
382                        # Only one option offered so None = available, not None = away.
383                        self.yow.Ship(AvailablePresenceProtocolEntity())
384                else:
385                        self.yow.Ship(UnavailablePresenceProtocolEntity())
386                if status:
387                        self.yow.Ship(SetStatusIqProtocolEntity(status))
388
389        def set_set_name(self, _key, value):
390                self.yow.Ship(PresenceProtocolEntity(name=value))
391
392        def chat_join(self, id, name, _nick, _password, settings):
393                print "New chat created with id: %d" % id
394                group = self.groups[name]
395                group.update({"id": id, "name": name})
396                self.groups_by_id[id] = group
397               
398                gi = group.get("topic", None)
399                if gi:
400                        self.bee.chat_topic(id, gi.getSubjectOwner(),
401                                            gi.getSubject(), gi.getSubjectTimestamp())
402               
403                # WA doesn't really have a concept of joined or not, just
404                # long-term membership. Let's just sync state (we have
405                # basic info but not yet a member list) and ACK the join
406                # once that's done.
407                # Well except that WA/YS killed this one. \o/
408                #self.yow.Ship(ParticipantsGroupsIqProtocolEntity(name))
409               
410                # So for now do without a participant list..
411                self.chat_join_participants(group)
412                self.chat_send_backlog(group)
413
414        def chat_join_participants(self, group):
415                for p in group.get("participants", []):
416                        if p != self.account["user"]:
417                                self.bee.chat_add_buddy(group["id"], p)
418
419        def chat_send_backlog(self, group):
420                # Add the user themselves last to avoid a visible join flood.
421                self.bee.chat_add_buddy(group["id"], self.account["user"])
422                for msg in group.setdefault("queue", []):
423                        self.show_message(msg)
424                del group["queue"]
425       
426        def chat_msg(self, id, text, flags):
427                msg = TextMessageProtocolEntity(text, to=self.groups_by_id[id]["name"])
428                self.yow.Ship(msg)
429
430        def chat_leave(self, id):
431                # WA never really let us leave, so just disconnect id and jid.
432                group = self.groups_by_id[id]
433                del self.groups_by_id[id]
434                del group["id"]
435
436        def build_stack(self, account):
437                self.account = account
438                creds = (account["user"].split("@")[0], account["pass"])
439
440                stack = (YowStackBuilder()
441                         .pushDefaultLayers(False)
442                         .push(BitlBeeLayer)
443                         .build())
444                stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, creds)
445                stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0])
446                stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN)
447                stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource())
448                try:
449                        stack.setProp(YowIqProtocolLayer.PROP_PING_INTERVAL, 0)
450                except AttributeError:
451                        # Ping setting only exists since May 2015.
452                        from yowsup.layers.protocol_iq.layer import YowPingThread
453                        YowPingThread.start = lambda x: None
454
455                stack.setProp("org.bitlbee.Bijtje", self)
456
457                stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
458
459                return stack
460
461
462        # Not RPCs from here on.
463        def show_message(self, msg):
464                if hasattr(msg, "getBody"):
465                        text = msg.getBody()
466                elif hasattr(msg, "getCaption") and hasattr(msg, "getMediaUrl"):
467                        lines = []
468                        if msg.getMediaUrl():
469                                lines.append(msg.getMediaUrl())
470                        else:
471                                lines.append("<Broken link>")
472                        if msg.getCaption():
473                                lines.append(msg.getCaption())
474                        text = "\n".join(lines)
475
476                if msg.getParticipant():
477                        group = self.groups[msg.getFrom()]
478                        if "id" in group:
479                                self.bee.chat_add_buddy(group["id"], msg.getParticipant())
480                                self.bee.chat_msg(group["id"], msg.getParticipant(), text, 0, msg.getTimestamp())
481                        else:
482                                self.bee.log("Warning: Activity in room %s" % msg.getFrom())
483                                self.groups[msg.getFrom()].setdefault("queue", []).append(msg)
484                else:
485                        self.bee.buddy_msg(msg.getFrom(), text, 0, msg.getTimestamp())
486
487
488implugin.RunPlugin(YowsupIMPlugin, debug=True)
Note: See TracBrowser for help on using the repository browser.