1 | #!/usr/bin/python |
---|
2 | |
---|
3 | import logging |
---|
4 | import threading |
---|
5 | import time |
---|
6 | |
---|
7 | import yowsup |
---|
8 | |
---|
9 | from yowsup.layers.auth import YowAuthenticationProtocolLayer |
---|
10 | from yowsup.layers.protocol_acks import YowAckProtocolLayer |
---|
11 | from yowsup.layers.protocol_chatstate import YowChatstateProtocolLayer |
---|
12 | from yowsup.layers.protocol_contacts import YowContactsIqProtocolLayer |
---|
13 | from yowsup.layers.protocol_groups import YowGroupsProtocolLayer |
---|
14 | from yowsup.layers.protocol_ib import YowIbProtocolLayer |
---|
15 | from yowsup.layers.protocol_iq import YowIqProtocolLayer |
---|
16 | from yowsup.layers.protocol_messages import YowMessagesProtocolLayer |
---|
17 | from yowsup.layers.protocol_notifications import YowNotificationsProtocolLayer |
---|
18 | from yowsup.layers.protocol_presence import YowPresenceProtocolLayer |
---|
19 | from yowsup.layers.protocol_privacy import YowPrivacyProtocolLayer |
---|
20 | from yowsup.layers.protocol_profiles import YowProfilesProtocolLayer |
---|
21 | from yowsup.layers.protocol_receipts import YowReceiptProtocolLayer |
---|
22 | from yowsup.layers.network import YowNetworkLayer |
---|
23 | from yowsup.layers.coder import YowCoderLayer |
---|
24 | from yowsup.stacks import YowStack, YowStackBuilder |
---|
25 | from yowsup.common import YowConstants |
---|
26 | from yowsup.layers import YowLayerEvent |
---|
27 | from yowsup.stacks import YowStack, YOWSUP_CORE_LAYERS |
---|
28 | from yowsup import env |
---|
29 | |
---|
30 | from yowsup.layers.interface import YowInterfaceLayer, ProtocolEntityCallback |
---|
31 | from yowsup.layers.protocol_acks.protocolentities import * |
---|
32 | from yowsup.layers.protocol_chatstate.protocolentities import * |
---|
33 | from yowsup.layers.protocol_contacts.protocolentities import * |
---|
34 | from yowsup.layers.protocol_groups.protocolentities import * |
---|
35 | from yowsup.layers.protocol_ib.protocolentities import * |
---|
36 | from yowsup.layers.protocol_iq.protocolentities import * |
---|
37 | from yowsup.layers.protocol_media.mediauploader import MediaUploader |
---|
38 | from yowsup.layers.protocol_media.protocolentities import * |
---|
39 | from yowsup.layers.protocol_messages.protocolentities import * |
---|
40 | from yowsup.layers.protocol_notifications.protocolentities import * |
---|
41 | from yowsup.layers.protocol_presence.protocolentities import * |
---|
42 | from yowsup.layers.protocol_privacy.protocolentities import * |
---|
43 | from yowsup.layers.protocol_profiles.protocolentities import * |
---|
44 | from yowsup.layers.protocol_receipts.protocolentities import * |
---|
45 | from yowsup.layers.axolotl.protocolentities.iq_key_get import GetKeysIqProtocolEntity |
---|
46 | from yowsup.layers.axolotl import YowAxolotlLayer |
---|
47 | from yowsup.common.tools import ModuleTools |
---|
48 | |
---|
49 | import implugin |
---|
50 | |
---|
51 | logger = logging.getLogger("yowsup.layers.network.layer") |
---|
52 | logger.setLevel(logging.DEBUG) |
---|
53 | ch = logging.StreamHandler() |
---|
54 | ch.setLevel(logging.DEBUG) |
---|
55 | logger.addHandler(ch) |
---|
56 | |
---|
57 | |
---|
58 | # Tried this but yowsup is not passing back the result, will have to update the library. :-( |
---|
59 | class GetStatusIqProtocolEntity(IqProtocolEntity): |
---|
60 | def __init__(self, jids=None): |
---|
61 | super(GetStatusIqProtocolEntity, self).__init__("status", None, _type="get", to="s.whatsapp.net") |
---|
62 | self.jids = jids or [] |
---|
63 | |
---|
64 | def toProtocolTreeNode(self): |
---|
65 | from yowsup.structs import ProtocolTreeNode |
---|
66 | |
---|
67 | node = super(GetStatusIqProtocolEntity, self).toProtocolTreeNode() |
---|
68 | sr = ProtocolTreeNode("status") |
---|
69 | node.addChild(sr) |
---|
70 | for jid in self.jids: |
---|
71 | sr.addChild(ProtocolTreeNode("user", {"jid": jid})) |
---|
72 | return node |
---|
73 | |
---|
74 | |
---|
75 | class BitlBeeLayer(YowInterfaceLayer): |
---|
76 | |
---|
77 | def __init__(self, *a, **kwa): |
---|
78 | super(BitlBeeLayer, self).__init__(*a, **kwa) |
---|
79 | |
---|
80 | def receive(self, entity): |
---|
81 | print "Received: %r" % entity |
---|
82 | #print entity |
---|
83 | super(BitlBeeLayer, self).receive(entity) |
---|
84 | |
---|
85 | def Ship(self, entity): |
---|
86 | """Send an entity into Yowsup, but through the correct thread.""" |
---|
87 | print "Queueing: %s" % entity.getTag() |
---|
88 | #print entity |
---|
89 | def doit(): |
---|
90 | self.toLower(entity) |
---|
91 | self.getStack().execDetached(doit) |
---|
92 | |
---|
93 | @ProtocolEntityCallback("success") |
---|
94 | def onSuccess(self, entity): |
---|
95 | self.b = self.getStack().getProp("org.bitlbee.Bijtje") |
---|
96 | self.cb = self.b.bee |
---|
97 | self.b.yow = self |
---|
98 | |
---|
99 | self.cb.log("Authenticated, syncing contact list") |
---|
100 | |
---|
101 | # We're done once this set is empty. |
---|
102 | self.todo = set(["contacts", "groups"]) |
---|
103 | |
---|
104 | # Supposedly WA can also do national-style phone numbers without |
---|
105 | # a + prefix BTW (relative to I guess the user's country?). I |
---|
106 | # don't want to support this at least for now. |
---|
107 | numbers = [("+" + x.split("@")[0]) for x in self.cb.get_local_contacts()] |
---|
108 | self.toLower(GetSyncIqProtocolEntity(numbers)) |
---|
109 | self.toLower(ListGroupsIqProtocolEntity()) |
---|
110 | |
---|
111 | try: |
---|
112 | self.toLower(PresenceProtocolEntity(name=self.b.setting("name"))) |
---|
113 | except KeyError: |
---|
114 | pass |
---|
115 | |
---|
116 | def check_connected(self, done): |
---|
117 | if self.todo is None: |
---|
118 | return |
---|
119 | self.todo.remove(done) |
---|
120 | if not self.todo: |
---|
121 | self.todo = None |
---|
122 | self.cb.connected() |
---|
123 | |
---|
124 | @ProtocolEntityCallback("failure") |
---|
125 | def onFailure(self, entity): |
---|
126 | self.b = self.getStack().getProp("org.bitlbee.Bijtje") |
---|
127 | self.cb = self.b.bee |
---|
128 | self.cb.error(entity.getReason()) |
---|
129 | self.cb.logout(False) |
---|
130 | |
---|
131 | def onEvent(self, event): |
---|
132 | print "Received event: %s name %s" % (event, event.getName()) |
---|
133 | if event.getName() == "disconnect": |
---|
134 | self.getStack().execDetached(self.daemon.StopDaemon) |
---|
135 | |
---|
136 | @ProtocolEntityCallback("presence") |
---|
137 | def onPresence(self, pres): |
---|
138 | if pres.getFrom() == self.b.account["user"]: |
---|
139 | # WA returns our own presence. Meh. |
---|
140 | return |
---|
141 | |
---|
142 | status = 8 # MOBILE |
---|
143 | if pres.getType() != "unavailable": |
---|
144 | status |= 1 # ONLINE |
---|
145 | self.cb.buddy_status(pres.getFrom(), status, None, None) |
---|
146 | |
---|
147 | try: |
---|
148 | # Last online time becomes idle time which I guess is |
---|
149 | # sane enough? |
---|
150 | self.cb.buddy_times(pres.getFrom(), 0, int(pres.getLast())) |
---|
151 | except (ValueError, TypeError): |
---|
152 | # Could be "error" or, more likely, "deny", or None. |
---|
153 | pass |
---|
154 | |
---|
155 | @ProtocolEntityCallback("message") |
---|
156 | def onMessage(self, msg): |
---|
157 | receipt = OutgoingReceiptProtocolEntity(msg.getId(), msg.getFrom()) |
---|
158 | self.toLower(receipt) |
---|
159 | |
---|
160 | if msg.getParticipant(): |
---|
161 | group = self.b.groups.get(msg.getFrom(), None) |
---|
162 | if not group or "id" not in group: |
---|
163 | self.cb.log("Warning: Activity in room %s" % msg.getFrom()) |
---|
164 | self.b.groups.setdefault(msg.getFrom(), {}).setdefault("queue", []).append(msg) |
---|
165 | return |
---|
166 | self.cb.chat_msg(group["id"], msg.getParticipant(), msg.getBody(), 0, msg.getTimestamp()) |
---|
167 | else: |
---|
168 | self.cb.buddy_msg(msg.getFrom(), msg.getBody(), 0, msg.getTimestamp()) |
---|
169 | |
---|
170 | @ProtocolEntityCallback("receipt") |
---|
171 | def onReceipt(self, entity): |
---|
172 | ack = OutgoingAckProtocolEntity(entity.getId(), entity.getTag(), |
---|
173 | entity.getType(), entity.getFrom()) |
---|
174 | self.toLower(ack) |
---|
175 | |
---|
176 | @ProtocolEntityCallback("iq") |
---|
177 | def onIq(self, entity): |
---|
178 | if isinstance(entity, ResultSyncIqProtocolEntity): |
---|
179 | return self.onSyncResult(entity) |
---|
180 | elif isinstance(entity, ListParticipantsResultIqProtocolEntity): |
---|
181 | return self.b.chat_join_participants(entity) |
---|
182 | elif isinstance(entity, ListGroupsResultIqProtocolEntity): |
---|
183 | return self.onListGroupsResult(entity) |
---|
184 | |
---|
185 | def onSyncResult(self, entity): |
---|
186 | # TODO HERE AND ELSEWHERE: Thread idiocy happens when going |
---|
187 | # from here to the IMPlugin. Check how bjsonrpc lets me solve that. |
---|
188 | for num, jid in entity.inNumbers.iteritems(): |
---|
189 | self.toLower(SubscribePresenceProtocolEntity(jid)) |
---|
190 | self.cb.add_buddy(jid, "") |
---|
191 | if entity.outNumbers: |
---|
192 | self.cb.error("Not on WhatsApp: %s" % |
---|
193 | ", ".join(entity.outNumbers.keys())) |
---|
194 | if entity.invalidNumbers: |
---|
195 | self.cb.error("Invalid numbers: %s" % |
---|
196 | ", ".join(entity.invalidNumbers.keys())) |
---|
197 | |
---|
198 | # Disabled since yowsup won't give us the result... |
---|
199 | if entity.inNumbers and False: |
---|
200 | self.toLower(GetStatusIqProtocolEntity(entity.inNumbers.values())) |
---|
201 | self.todo.add("statuses") |
---|
202 | |
---|
203 | self.check_connected("contacts") |
---|
204 | |
---|
205 | def onListGroupsResult(self, groups): |
---|
206 | """Save group info for later if the user decides to join.""" |
---|
207 | for g in groups.getGroups(): |
---|
208 | jid = g.getId() |
---|
209 | if "@" not in jid: |
---|
210 | jid += "@g.us" |
---|
211 | group = self.b.groups.setdefault(jid, {}) |
---|
212 | group["info"] = g |
---|
213 | |
---|
214 | self.check_connected("groups") |
---|
215 | |
---|
216 | @ProtocolEntityCallback("notification") |
---|
217 | def onNotification(self, ent): |
---|
218 | if isinstance(ent, StatusNotificationProtocolEntity): |
---|
219 | return self.onStatusNotification(ent) |
---|
220 | |
---|
221 | def onStatusNotification(self, status): |
---|
222 | print "New status for %s: %s" % (status.getFrom(), status.status) |
---|
223 | self.bee.buddy_status_msg(status.getFrom(), status.status) |
---|
224 | |
---|
225 | @ProtocolEntityCallback("media") |
---|
226 | def onMedia(self, med): |
---|
227 | """Your PC better be MPC3 compliant!""" |
---|
228 | print "YAY MEDIA! %r" % med |
---|
229 | print med |
---|
230 | |
---|
231 | #@ProtocolEntityCallback("chatstate") |
---|
232 | #def onChatstate(self, entity): |
---|
233 | # print(entity) |
---|
234 | |
---|
235 | |
---|
236 | class YowsupDaemon(threading.Thread): |
---|
237 | daemon = True |
---|
238 | stack = None |
---|
239 | |
---|
240 | class Terminate(Exception): |
---|
241 | pass |
---|
242 | |
---|
243 | def run(self): |
---|
244 | try: |
---|
245 | self.stack.loop(timeout=0.2, discrete=0.2, count=1) |
---|
246 | except YowsupDaemon.Terminate: |
---|
247 | print "Exiting loop!" |
---|
248 | pass |
---|
249 | |
---|
250 | def StopDaemon(self): |
---|
251 | # Ugly, but yowsup offers no "run single iteration" version |
---|
252 | # of their event loop :-( |
---|
253 | raise YowsupDaemon.Terminate |
---|
254 | |
---|
255 | |
---|
256 | class YowsupIMPlugin(implugin.BitlBeeIMPlugin): |
---|
257 | NAME = "wa" |
---|
258 | SETTINGS = { |
---|
259 | "cc": { |
---|
260 | # Country code. Seems to be required for registration only. |
---|
261 | "type": "int", |
---|
262 | }, |
---|
263 | "name": { |
---|
264 | "flags": 0x100, # NULL_OK |
---|
265 | }, |
---|
266 | } |
---|
267 | AWAY_STATES = ["Away"] |
---|
268 | ACCOUNT_FLAGS = 14 # HANDLE_DOMAINS + STATUS_MESSAGE + LOCAL_CONTACTS |
---|
269 | # TODO: LOCAL LIST CAUSES CRASH! |
---|
270 | # TODO: HANDLE_DOMAIN in right place (add ... ... nick bug) |
---|
271 | # TODO? Allow set_away (for status msg) even if AWAY_STATES not set? |
---|
272 | # and/or, see why with the current value set_away state is None. |
---|
273 | |
---|
274 | def login(self, account): |
---|
275 | self.stack = self.build_stack(account) |
---|
276 | self.daemon = YowsupDaemon(name="yowsup") |
---|
277 | self.daemon.stack = self.stack |
---|
278 | self.daemon.start() |
---|
279 | self.bee.log("Started yowsup thread") |
---|
280 | |
---|
281 | self.groups = {} |
---|
282 | self.groups_by_id = {} |
---|
283 | |
---|
284 | def keepalive(self): |
---|
285 | # Too noisy while debugging |
---|
286 | pass |
---|
287 | #self.yow.Ship(PingIqProtocolEntity(to="s.whatsapp.net")) |
---|
288 | |
---|
289 | def logout(self): |
---|
290 | self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_DISCONNECT)) |
---|
291 | self.stack.execDetached(self.daemon.StopDaemon) |
---|
292 | |
---|
293 | def buddy_msg(self, to, text, flags): |
---|
294 | msg = TextMessageProtocolEntity(text, to=to) |
---|
295 | self.yow.Ship(msg) |
---|
296 | |
---|
297 | def add_buddy(self, handle, _group): |
---|
298 | self.yow.Ship(GetSyncIqProtocolEntity( |
---|
299 | ["+" + handle.split("@")[0]], mode=GetSyncIqProtocolEntity.MODE_DELTA)) |
---|
300 | |
---|
301 | def remove_buddy(self, handle, _group): |
---|
302 | self.yow.Ship(UnsubscribePresenceProtocolEntity(handle)) |
---|
303 | |
---|
304 | def set_away(self, state, status): |
---|
305 | print "Trying to set status to %r, %r" % (state, status) |
---|
306 | if state: |
---|
307 | # Only one option offered so None = available, not None = away. |
---|
308 | self.yow.Ship(AvailablePresenceProtocolEntity()) |
---|
309 | else: |
---|
310 | self.yow.Ship(UnavailablePresenceProtocolEntity()) |
---|
311 | if status: |
---|
312 | self.yow.Ship(SetStatusIqProtocolEntity(status)) |
---|
313 | |
---|
314 | def set_set_name(self, _key, value): |
---|
315 | self.yow.Ship(PresenceProtocolEntity(name=value)) |
---|
316 | |
---|
317 | def chat_join(self, id, name, _nick, _password, settings): |
---|
318 | print "New chat created with id: %d" % id |
---|
319 | self.groups.setdefault(name, {}).update({"id": id, "name": name}) |
---|
320 | group = self.groups[name] |
---|
321 | self.groups_by_id[id] = group |
---|
322 | |
---|
323 | gi = group.get("info", None) |
---|
324 | if gi: |
---|
325 | self.bee.chat_topic(id, gi.getSubjectOwner(), |
---|
326 | gi.getSubject(), gi.getSubjectTime()) |
---|
327 | |
---|
328 | # WA doesn't really have a concept of joined or not, just |
---|
329 | # long-term membership. Let's just get a list of members and |
---|
330 | # pretend we've "joined" then. |
---|
331 | self.yow.Ship(ParticipantsGroupsIqProtocolEntity(name)) |
---|
332 | |
---|
333 | def chat_join_participants(self, entity): |
---|
334 | group = self.groups[entity.getFrom()] |
---|
335 | id = group["id"] |
---|
336 | for p in entity.getParticipants(): |
---|
337 | if p != self.account["user"]: |
---|
338 | self.bee.chat_add_buddy(id, p) |
---|
339 | |
---|
340 | # Add the user themselves last to avoid a visible join flood. |
---|
341 | self.bee.chat_add_buddy(id, self.account["user"]) |
---|
342 | for msg in group.setdefault("queue", []): |
---|
343 | self.bee.chat_msg(group["id"], msg.getParticipant(), msg.getBody(), 0, msg.getTimestamp()) |
---|
344 | del group["queue"] |
---|
345 | |
---|
346 | def chat_msg(self, id, text, flags): |
---|
347 | msg = TextMessageProtocolEntity(text, to=self.groups_by_id[id]["name"]) |
---|
348 | self.yow.Ship(msg) |
---|
349 | |
---|
350 | def chat_leave(self, id): |
---|
351 | # WA never really let us leave, so just disconnect id and jid. |
---|
352 | group = self.groups_by_id[id] |
---|
353 | del self.groups_by_id[id] |
---|
354 | del group["id"] |
---|
355 | |
---|
356 | def build_stack(self, account): |
---|
357 | self.account = account |
---|
358 | creds = (account["user"].split("@")[0], account["pass"]) |
---|
359 | |
---|
360 | stack = (YowStackBuilder() |
---|
361 | .pushDefaultLayers(False) |
---|
362 | .push(BitlBeeLayer) |
---|
363 | .build()) |
---|
364 | stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, creds) |
---|
365 | stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) |
---|
366 | stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) |
---|
367 | stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) |
---|
368 | stack.setProp("org.bitlbee.Bijtje", self) |
---|
369 | |
---|
370 | stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) |
---|
371 | |
---|
372 | return stack |
---|
373 | |
---|
374 | implugin.RunPlugin(YowsupIMPlugin, debug=True) |
---|