def test_receive(content): def handle_ping(data): assert 'location' in data assert 'timestamp' in data SocketScience.register('ping', handle_ping) SocketScience.receive(content)
def test_receive(content): def handle_ping(data): assert 'location' in data assert 'timestamp' in data SocketScience.register('ping', handle_ping) SocketScience.receive(content)
def send_status_ping(): if GlobalVars.metasmoke_host is None: log( 'info', 'Attempted to send status ping but metasmoke_host is undefined. Not sent.' ) return elif GlobalVars.metasmoke_down: payload = { "location": GlobalVars.location, "timestamp": time.time() } SocketScience.send(payload) metasmoke_key = GlobalVars.metasmoke_key try: payload = { 'location': GlobalVars.location, 'key': metasmoke_key, 'standby': GlobalVars.standby_mode } headers = {'content-type': 'application/json'} response = Metasmoke.post("/status-update.json", data=json.dumps(payload), headers=headers) try: response = response.json() GlobalVars.metasmoke_last_ping_time = datetime.now( ) # Otherwise the ping watcher will exit(10) if 'failover' in response and GlobalVars.standby_mode: if response['failover']: GlobalVars.standby_mode = False chatcommunicate.tell_rooms_with( "debug", GlobalVars.location + " received failover signal.", notify_site="/failover") if response['standby']: chatcommunicate.tell_rooms_with( "debug", GlobalVars.location + " entering metasmoke-forced standby.") time.sleep(2) os._exit(7) if 'shutdown' in response: if response['shutdown']: os._exit(6) except Exception: # TODO: What could happen here? pass except Exception as e: log('error', e)
def on_msg(msg, client): global _room_roles if not isinstance(msg, events.MessagePosted) and not isinstance( msg, events.MessageEdited): return message = msg.message room_ident = (client.host, message.room.id) with _room_roles_lock: if message.owner.id == client._br.user_id: if 'direct' in _room_roles and room_ident in _room_roles['direct']: SocketScience.receive( message.content_source.replace("\u200B", "").replace("\u200C", "")) return if message.content.startswith("<div class='partial'>"): message.content = message.content[21:] if message.content.endswith("</div>"): message.content = message.content[:-6] if message.parent: try: if message.parent.owner.id == client._br.user_id: strip_mention = regex.sub( "^(<span class=(\"|')mention(\"|')>)?@.*?(</span>)? ", "", message.content) cmd = GlobalVars.parser.unescape(strip_mention) result = dispatch_reply_command(message.parent, message, cmd) send_reply_if_not_blank(room_ident, message.id, result) except ValueError: pass elif message.content.lower().startswith("sd "): result = dispatch_shorthand_command(message) send_reply_if_not_blank(room_ident, message.id, result) elif message.content.startswith( "!!/") or message.content.lower().startswith("sdc "): result = dispatch_command(message) send_reply_if_not_blank(room_ident, message.id, result) elif classes.feedback.FEEDBACK_REGEX.search(message.content) \ and is_privileged(message.owner, message.room) and datahandling.last_feedbacked: ids, expires_in = datahandling.last_feedbacked if time.time() < expires_in: Tasks.do(metasmoke.Metasmoke.post_auto_comment, message.content_source, message.owner, ids=ids) else: with _room_roles_lock: if 'direct' in _room_roles and room_ident in _room_roles['direct']: SocketScience.receive( message.content_source.replace("\u200B", "").replace("\u200C", ""))
def on_msg(msg, client): global _room_roles if not isinstance(msg, events.MessagePosted) and not isinstance(msg, events.MessageEdited): return message = msg.message room_ident = (client.host, message.room.id) room_data = _rooms[room_ident] if message.owner.id == client._br.user_id: if 'direct' in _room_roles and room_ident in _room_roles['direct']: SocketScience.receive(message.content_source.replace("\u200B", "").replace("\u200C", "")) return if message.content.startswith("<div class='partial'>"): message.content = message.content[21:] if message.content.endswith("</div>"): message.content = message.content[:-6] if message.parent: try: if message.parent.owner.id == client._br.user_id: strip_mention = regex.sub("^(<span class=(\"|')mention(\"|')>)?@.*?(</span>)? ", "", message.content) cmd = GlobalVars.parser.unescape(strip_mention) result = dispatch_reply_command(message.parent, message, cmd) if result: s = ":{}\n{}" if "\n" not in result and len(result) >= 488 else ":{} {}" _msg_queue.put((room_data, s.format(message.id, result), None)) except ValueError: pass elif message.content.lower().startswith("sd "): result = dispatch_shorthand_command(message) if result: s = ":{}\n{}" if "\n" not in result and len(result) >= 488 else ":{} {}" _msg_queue.put((room_data, s.format(message.id, result), None)) elif message.content.startswith("!!/"): result = dispatch_command(message) if result: s = ":{}\n{}" if "\n" not in result and len(result) >= 488 else ":{} {}" _msg_queue.put((room_data, s.format(message.id, result), None)) elif classes.feedback.FEEDBACK_REGEX.search(message.content) \ and is_privileged(message.owner, message.room) and datahandling.last_feedbacked: ids, expires_in = datahandling.last_feedbacked if time.time() < expires_in: Tasks.do(metasmoke.Metasmoke.post_auto_comment, message.content_source, message.owner, ids=ids) elif 'direct' in _room_roles and room_ident in _room_roles['direct']: SocketScience.receive(message.content_source.replace("\u200B", "").replace("\u200C", ""))
def send_status_ping(): if GlobalVars.metasmoke_host is None: log('info', 'Attempted to send status ping but metasmoke_host is undefined. Not sent.') return elif GlobalVars.metasmoke_down: payload = { "location": GlobalVars.location, "timestamp": time.time() } SocketScience.send(payload) metasmoke_key = GlobalVars.metasmoke_key try: payload = { 'location': GlobalVars.location, 'key': metasmoke_key, 'standby': GlobalVars.standby_mode } headers = {'content-type': 'application/json'} response = Metasmoke.post("/status-update.json", data=json.dumps(payload), headers=headers, ignore_down=True) try: response = response.json() GlobalVars.metasmoke_last_ping_time = datetime.now() # Otherwise the ping watcher will exit(10) if response.get('pull_update', False): log('info', "Received pull command from MS ping response") exit_mode("pull_update") if 'failover' in response and GlobalVars.standby_mode: if response['failover']: GlobalVars.standby_mode = False chatcommunicate.tell_rooms_with("debug", GlobalVars.location + " received failover signal.", notify_site="/failover") if response.get('standby', False): chatcommunicate.tell_rooms_with("debug", GlobalVars.location + " entering metasmoke-forced standby.") time.sleep(2) exit_mode("standby") if response.get('shutdown', False): exit_mode("shutdown") except Exception: # TODO: What could happen here? pass except Exception as e: log('error', e)
def send_status_ping(): if GlobalVars.metasmoke_host is None: log( 'info', 'Attempted to send status ping but metasmoke_host is undefined. Not sent.' ) return elif GlobalVars.metasmoke_down: payload = { "location": GlobalVars.location, "timestamp": time.time() } SocketScience.send(payload) metasmoke_key = GlobalVars.metasmoke_key try: payload = { 'location': GlobalVars.location, 'key': metasmoke_key, 'standby': GlobalVars.standby_mode or GlobalVars.no_se_activity_scan } headers = {'content-type': 'application/json'} response = Metasmoke.post("/status-update.json", data=json.dumps(payload), headers=headers, ignore_down=True) try: response = response.json() GlobalVars.metasmoke_last_ping_time = datetime.utcnow( ) # Otherwise the ping watcher will exit(10) if response.get('pull_update', False): log('info', "Received pull command from MS ping response") exit_mode("pull_update") if ('failover' in response and GlobalVars.standby_mode and not GlobalVars.no_se_activity_scan): # If we're not scanning, then we don't want to become officially active due to failover. if response['failover']: GlobalVars.standby_mode = False chatcommunicate.tell_rooms_with( "debug", GlobalVars.location + " received failover signal.", notify_site="/failover") if response.get('standby', False): chatcommunicate.tell_rooms_with( "debug", GlobalVars.location + " entering metasmoke-forced standby.") time.sleep(2) exit_mode("standby") if response.get('shutdown', False): exit_mode("shutdown") except Exception: # TODO: What could happen here? pass except Exception as e: log('error', e)