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 | class BitlBeeLayer(YowInterfaceLayer): |
---|
58 | |
---|
59 | def __init__(self, *a, **kwa): |
---|
60 | super(BitlBeeLayer, self).__init__(*a, **kwa) |
---|
61 | |
---|
62 | def receive(self, entity): |
---|
63 | print "Received: %r" % entity |
---|
64 | #print entity |
---|
65 | super(BitlBeeLayer, self).receive(entity) |
---|
66 | |
---|
67 | def Ship(self, entity): |
---|
68 | """Send an entity into Yowsup, but through the correct thread.""" |
---|
69 | print "Queueing: %s" % entity.getTag() |
---|
70 | #print entity |
---|
71 | def doit(): |
---|
72 | self.toLower(entity) |
---|
73 | self.getStack().execDetached(doit) |
---|
74 | |
---|
75 | @ProtocolEntityCallback("success") |
---|
76 | def onSuccess(self, entity): |
---|
77 | self.b = self.getStack().getProp("org.bitlbee.Bijtje") |
---|
78 | self.cb = self.b.bee |
---|
79 | self.b.yow = self |
---|
80 | self.cb.connected() |
---|
81 | try: |
---|
82 | self.toLower(PresenceProtocolEntity(name=self.b.setting("name"))) |
---|
83 | except KeyError: |
---|
84 | pass |
---|
85 | # Should send the contact list now, but BitlBee hasn't given |
---|
86 | # it yet. See set_away() and send_initial_contacts() below. |
---|
87 | |
---|
88 | @ProtocolEntityCallback("failure") |
---|
89 | def onFailure(self, entity): |
---|
90 | self.b = self.getStack().getProp("org.bitlbee.Bijtje") |
---|
91 | self.cb = self.b.bee |
---|
92 | self.cb.error(entity.getReason()) |
---|
93 | self.cb.logout(False) |
---|
94 | |
---|
95 | def onEvent(self, event): |
---|
96 | print "Received event: %s name %s" % (event, event.getName()) |
---|
97 | if event.getName() == "disconnect": |
---|
98 | self.getStack().execDetached(self.daemon.StopDaemon) |
---|
99 | |
---|
100 | @ProtocolEntityCallback("presence") |
---|
101 | def onPresence(self, pres): |
---|
102 | status = 8 # MOBILE |
---|
103 | if pres.getType() != "unavailable": |
---|
104 | status |= 1 # ONLINE |
---|
105 | self.cb.buddy_status(pres.getFrom(), status, None, None) |
---|
106 | |
---|
107 | @ProtocolEntityCallback("message") |
---|
108 | def onMessage(self, msg): |
---|
109 | receipt = OutgoingReceiptProtocolEntity(msg.getId(), msg.getFrom()) |
---|
110 | self.toLower(receipt) |
---|
111 | |
---|
112 | if msg.getParticipant(): |
---|
113 | group = self.b.groups.get(msg.getFrom(), None) |
---|
114 | if not group: |
---|
115 | self.cb.log("Warning: Activity in room %s" % msg.getFrom()) |
---|
116 | return |
---|
117 | self.cb.chat_msg(group["id"], msg.getParticipant(), msg.getBody(), 0, msg.getTimestamp()) |
---|
118 | else: |
---|
119 | self.cb.buddy_msg(msg.getFrom(), msg.getBody(), 0, msg.getTimestamp()) |
---|
120 | |
---|
121 | @ProtocolEntityCallback("receipt") |
---|
122 | def onReceipt(self, entity): |
---|
123 | print "ACK THE ACK!" |
---|
124 | ack = OutgoingAckProtocolEntity(entity.getId(), entity.getTag(), |
---|
125 | entity.getType(), entity.getFrom()) |
---|
126 | self.toLower(ack) |
---|
127 | |
---|
128 | @ProtocolEntityCallback("iq") |
---|
129 | def onIq(self, entity): |
---|
130 | if isinstance(entity, ResultSyncIqProtocolEntity): |
---|
131 | return self.onSyncResult(entity) |
---|
132 | |
---|
133 | def onSyncResult(self, entity): |
---|
134 | # TODO HERE AND ELSEWHERE: Threat idiocy happens when going |
---|
135 | # from here to the IMPlugin. Check how bjsonrpc lets me solve that. |
---|
136 | ok = set(num.lstrip("+") for num in entity.inNumbers) |
---|
137 | for handle in self.b.contacts: |
---|
138 | if handle.split("@")[0] in ok: |
---|
139 | self.toLower(SubscribePresenceProtocolEntity(handle)) |
---|
140 | self.cb.add_buddy(handle, "") |
---|
141 | if entity.outNumbers: |
---|
142 | self.cb.error("Not on WhatsApp: %s" % ", ".join(entity.outNumbers)) |
---|
143 | if entity.invalidNumbers: |
---|
144 | self.cb.error("Invalid numbers: %s" % ", ".join(entity.invalidNumbers)) |
---|
145 | |
---|
146 | @ProtocolEntityCallback("notification") |
---|
147 | def onNotification(self, ent): |
---|
148 | if isinstance(ent, StatusNotificationProtocolEntity): |
---|
149 | return self.onStatusNotification(ent) |
---|
150 | |
---|
151 | def onStatusNotification(self, status): |
---|
152 | print "New status for %s: %s" % (status.getFrom(), status.status) |
---|
153 | |
---|
154 | #@ProtocolEntityCallback("chatstate") |
---|
155 | #def onChatstate(self, entity): |
---|
156 | # print(entity) |
---|
157 | |
---|
158 | |
---|
159 | class YowsupDaemon(threading.Thread): |
---|
160 | daemon = True |
---|
161 | stack = None |
---|
162 | |
---|
163 | class Terminate(Exception): |
---|
164 | pass |
---|
165 | |
---|
166 | def run(self): |
---|
167 | try: |
---|
168 | self.stack.loop(timeout=0.2, discrete=0.2, count=1) |
---|
169 | except YowsupDaemon.Terminate: |
---|
170 | print "Exiting loop!" |
---|
171 | pass |
---|
172 | |
---|
173 | def StopDaemon(self): |
---|
174 | # Ugly, but yowsup offers no "run single iteration" version |
---|
175 | # of their event loop :-( |
---|
176 | raise YowsupDaemon.Terminate |
---|
177 | |
---|
178 | |
---|
179 | class YowsupIMPlugin(implugin.BitlBeeIMPlugin): |
---|
180 | NAME = "wa" |
---|
181 | SETTINGS = { |
---|
182 | "cc": { |
---|
183 | "type": "int", |
---|
184 | }, |
---|
185 | "name": { |
---|
186 | "flags": 0x100, # NULL_OK |
---|
187 | }, |
---|
188 | } |
---|
189 | AWAY_STATES = ["Away"] |
---|
190 | ACCOUNT_FLAGS = 14 # HANDLE_DOMAINS + STATUS_MESSAGE + LOCAL_CONTACTS |
---|
191 | # TODO: LOCAL LIST CAUSES CRASH! |
---|
192 | # TODO: HANDLE_DOMAIN in right place (add ... ... nick bug) |
---|
193 | # TODO? Allow set_away (for status msg) even if AWAY_STATES not set? |
---|
194 | # and/or, see why with the current value set_away state is None. |
---|
195 | |
---|
196 | def login(self, account): |
---|
197 | self.stack = self.build_stack(account) |
---|
198 | self.daemon = YowsupDaemon(name="yowsup") |
---|
199 | self.daemon.stack = self.stack |
---|
200 | self.daemon.start() |
---|
201 | self.bee.log("Started yowsup thread") |
---|
202 | |
---|
203 | self.logging_in = True |
---|
204 | self.contacts = set() |
---|
205 | self.groups = {} |
---|
206 | self.groups_by_id = {} |
---|
207 | |
---|
208 | def keepalive(self): |
---|
209 | # Too noisy while debugging |
---|
210 | pass |
---|
211 | #self.yow.Ship(PingIqProtocolEntity(to="s.whatsapp.net")) |
---|
212 | |
---|
213 | def logout(self): |
---|
214 | self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_DISCONNECT)) |
---|
215 | self.stack.execDetached(self.daemon.StopDaemon) |
---|
216 | |
---|
217 | def buddy_msg(self, to, text, flags): |
---|
218 | msg = TextMessageProtocolEntity(text, to=to) |
---|
219 | self.yow.Ship(msg) |
---|
220 | |
---|
221 | def add_buddy(self, handle, _group): |
---|
222 | if self.logging_in: |
---|
223 | # Need to batch up the initial adds. This is a "little" ugly. |
---|
224 | self.contacts.add(handle) |
---|
225 | else: |
---|
226 | self.yow.Ship(GetSyncIqProtocolEntity( |
---|
227 | ["+" + handle.split("@")[0]], mode=GetSyncIqProtocolEntity.MODE_DELTA)) |
---|
228 | self.yow.Ship(SubscribePresenceProtocolEntity(handle)) |
---|
229 | |
---|
230 | def remove_buddy(self, handle, _group): |
---|
231 | self.yow.Ship(UnsubscribePresenceProtocolEntity(handle)) |
---|
232 | |
---|
233 | def set_away(self, state, status): |
---|
234 | # When our first status is set, we've finalised login. |
---|
235 | # Which means sync the full contact list now. |
---|
236 | if self.logging_in: |
---|
237 | self.logging_in = False |
---|
238 | self.send_initial_contacts() |
---|
239 | |
---|
240 | print "Trying to set status to %r, %r" % (state, status) |
---|
241 | if state: |
---|
242 | # Only one option offered so None = available, not None = away. |
---|
243 | self.yow.Ship(AvailablePresenceProtocolEntity()) |
---|
244 | else: |
---|
245 | self.yow.Ship(UnavailablePresenceProtocolEntity()) |
---|
246 | if status: |
---|
247 | self.yow.Ship(SetStatusIqProtocolEntity(status)) |
---|
248 | |
---|
249 | def send_initial_contacts(self): |
---|
250 | if not self.contacts: |
---|
251 | return |
---|
252 | numbers = [("+" + x.split("@")[0]) for x in self.contacts] |
---|
253 | self.yow.Ship(GetSyncIqProtocolEntity(numbers)) |
---|
254 | |
---|
255 | def set_set_name(self, _key, value): |
---|
256 | self.yow.Ship(PresenceProtocolEntity(name=value)) |
---|
257 | |
---|
258 | def chat_join(self, id, name, _nick, _password, settings): |
---|
259 | print "New chat created with id: %d" % id |
---|
260 | self.groups[name] = {"id": id, "name": name} |
---|
261 | self.groups_by_id[id] = self.groups[name] |
---|
262 | self.bee.chat_add_buddy(id, self.account["user"]) |
---|
263 | |
---|
264 | def chat_msg(self, id, text, flags): |
---|
265 | msg = TextMessageProtocolEntity(text, to=self.groups_by_id[id]["name"]) |
---|
266 | self.yow.Ship(msg) |
---|
267 | |
---|
268 | def build_stack(self, account): |
---|
269 | self.account = account |
---|
270 | creds = (account["user"].split("@")[0], account["pass"]) |
---|
271 | |
---|
272 | stack = (YowStackBuilder() |
---|
273 | .pushDefaultLayers(False) |
---|
274 | .push(BitlBeeLayer) |
---|
275 | .build()) |
---|
276 | stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, creds) |
---|
277 | stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0]) |
---|
278 | stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN) |
---|
279 | stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource()) |
---|
280 | stack.setProp("org.bitlbee.Bijtje", self) |
---|
281 | |
---|
282 | stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) |
---|
283 | |
---|
284 | return stack |
---|
285 | |
---|
286 | implugin.RunPlugin(YowsupIMPlugin, debug=True) |
---|