(stream)
| 250 | return stream |
| 251 | |
| 252 | def client_message_loop(stream): |
| 253 | try: |
| 254 | # Read server responses |
| 255 | for msg in stream: |
| 256 | log(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3], "in:", to_json(msg)) |
| 257 | |
| 258 | if msg.HasField("ctrl"): |
| 259 | # Run code on command completion |
| 260 | exec_future(msg.ctrl.id, msg.ctrl.code, msg.ctrl.text, msg.ctrl.params) |
| 261 | |
| 262 | elif msg.HasField("data"): |
| 263 | # log("message from:", msg.data.from_user_id) |
| 264 | |
| 265 | # Protection against the bot talking to self from another session. |
| 266 | if msg.data.from_user_id != botUID: |
| 267 | # Respond to message. |
| 268 | # Mark received message as read |
| 269 | client_post(note_read(msg.data.topic, msg.data.seq_id)) |
| 270 | # Insert a small delay to prevent accidental DoS self-attack. |
| 271 | time.sleep(0.1) |
| 272 | # Respond with a witty quote |
| 273 | client_post(publish(msg.data.topic, next_quote())) |
| 274 | |
| 275 | elif msg.HasField("pres"): |
| 276 | # log("presence:", msg.pres.topic, msg.pres.what) |
| 277 | # Wait for peers to appear online and subscribe to their topics |
| 278 | if msg.pres.topic == 'me': |
| 279 | if (msg.pres.what == pb.ServerPres.ON or msg.pres.what == pb.ServerPres.MSG) \ |
| 280 | and subscriptions.get(msg.pres.src) == None: |
| 281 | client_post(subscribe(msg.pres.src)) |
| 282 | elif msg.pres.what == pb.ServerPres.OFF and subscriptions.get(msg.pres.src) != None: |
| 283 | client_post(leave(msg.pres.src)) |
| 284 | |
| 285 | else: |
| 286 | # Ignore everything else |
| 287 | pass |
| 288 | |
| 289 | except grpc._channel._Rendezvous as err: |
| 290 | log("Disconnected:", err) |
| 291 | |
| 292 | def read_auth_cookie(cookie_file_name): |
| 293 | """Read authentication token from a file""" |
no test coverage detected
searching dependent graphs…