def command_delete(self, args, msg, event): if len(args) == 0: return "Not enough arguments." try: message_id = int(args[0]) except: return "Invalid arguments." message_to_delete = Message(message_id, self.client) try: message_to_delete.delete() except: pass
def command_reply(self, args, msg, event): if len(args) < 1: return "Not enough arguments." try: msg_id_to_reply_to = int(args[0]) except ValueError: if args[0] == "recent": msg_id_to_reply_to = self.msg_id_no_reply_found else: return "Invalid arguments." if msg_id_to_reply_to == -1: return "'recent' has a value of -1, which is not a valid message ID. Please provide an explicit ID." msg_to_reply_to = Message(msg_id_to_reply_to, self.client) content = msg_to_reply_to.content_source content = re.sub(r"([:;][-']?[)/(DPdpoO\[\]\\|])", "", content) # strip smilies content = re.sub(r"\[(.+?)\]\(.+?\)", r"\1", content) content = re.sub(r"\(.+?\)", "", content) content = re.sub(r"\s+", " ", content) content = content.strip() parts = content.split(" ") msg_does_not_qualify = "Message does not qualify as a message that belongs to the word association game." if len(parts) != 2: return msg_does_not_qualify if not re.compile("^:([0-9]+)$").search(parts[0]): return msg_does_not_qualify if re.compile("[^a-zA-Z0-9-]").search(parts[1]): return "Word contains invalid characters." self.reply_word(msg_to_reply_to, False, parts[1]) return None
def on_event(self, event, client): if ((not self.enabled or self.suspended_until > time.time()) and event.user.id not in self.owner_ids) \ or not self.running: return watchers = self.modules.get_event_watchers() for w in watchers: w(event, client, self) if not (isinstance(event, MessagePosted) or isinstance(event, MessageEdited) or str(type(event)).find('Chatbot.MockEvent') > -1): return if event.user.id == self.client.get_me().id: return if isinstance(event, MessageEdited): message = Message(event.message.id, client) else: message = event.message content_source = message.content_source content = content_source fixed_font = is_fixed_font(content) if fixed_font: fixed_font = True content = fixed_font_to_normal(content) content = re.sub(r"^%s\s+" % self.prefix, self.prefix, content) content = re.sub("(^[^ \r\n]+)(\r?\n)", r"\1 ", content) if not fixed_font: stripped_content = re.sub(r"\s+", " ", content) stripped_content = stripped_content.strip() else: stripped_content = content parts = stripped_content.split(" ") if not parts[0].startswith(self.prefix): return cmd_args = stripped_content[len(self.prefix):] if self.requires_special_arg_parsing(cmd_args.split(" ")[0]): cmd_args = content[len(self.prefix):] output = self.command(cmd_args, message, event, 0) if output is not False and output is not None: output_with_reply = ":%i %s" % (message.id, output) if len(output_with_reply) > 500 and "\n" not in output_with_reply: message.reply("Output would be longer than 500 characters (the limit for single-line messages), so only the first 500 characters are posted now.") message.reply(output[:500 - (len(message.id) + 2)]) else: message.reply(output, False)
def watcher(ev, wrap2): try: if ev.type_id != 1 and ev.type_id != 2: return if ev.type_id == 2 and (wrap2.host + str(ev.message.id)) not in GlobalVars.listen_to_these_if_edited: return print_chat_message(ev) ev_room = str(ev.data["room_id"]) ev_user_id = str(ev.data["user_id"]) ev_room_name = ev.data["room_name"].encode('utf-8') if ev.type_id == 2: ev.message = Message(ev.message.id, wrap2) content_source = ev.message.content_source message_id = ev.message.id if is_smokedetector_message(ev_user_id, ev_room): add_latest_message_lock.acquire() add_latest_smokedetector_message(ev_room, message_id) add_latest_message_lock.release() post_site_id = fetch_post_id_and_site_from_msg_content(content_source) post_url = fetch_post_url_from_msg_content(content_source) if post_site_id is not None and (ev_room == GlobalVars.meta_tavern_room_id or ev_room == GlobalVars.socvr_room_id): t_check_websocket = Thread(name="DeletionWatcher check", target=DeletionWatcher.check_if_report_was_deleted, args=(post_site_id, post_url, ev.message)) t_check_websocket.daemon = True t_check_websocket.start() message_parts = re.split('[ ,]+', content_source) ev_user_name = ev.data["user_name"] ev_user_link = "//chat.{host}/users/{user_id}".format(host=wrap2.host, user_id=ev.user.id) if ev_user_name != "SmokeDetector": GlobalVars.users_chatting[ev_room].append((ev_user_name, ev_user_link)) shortcut_messages = [] if message_parts[0].lower() == "sd": message_parts = preprocess_shortcut_command(content_source).split(" ") latest_smokedetector_messages = GlobalVars.latest_smokedetector_messages[ev_room] commands = message_parts[1:] if len(latest_smokedetector_messages) == 0: ev.message.reply("I don't have a record of any messages posted.") return if len(commands) > len(latest_smokedetector_messages): ev.message.reply("I only have a record of {} of my messages; that's not enough to execute all " "commands. No commands were executed.".format(len(latest_smokedetector_messages))) return for i in range(0, len(commands)): shortcut_messages.append(u":{message} {command_name}".format( message=latest_smokedetector_messages[-(i + 1)], command_name=commands[i])) reply = "" amount_none = 0 amount_skipped = 0 amount_unrecognized = 0 length = len(shortcut_messages) for i in range(0, length): current_message = shortcut_messages[i] if length > 1: reply += str(i + 1) + ". " reply += u"[{0}] ".format(current_message.split(" ")[0]) if current_message.split(" ")[1] != "-": result = handle_commands(content_lower=current_message.lower(), message_parts=current_message.split(" "), ev_room=ev_room, ev_room_name=ev_room_name, ev_user_id=ev_user_id, ev_user_name=ev_user_name, wrap2=wrap2, content=current_message, message_id=message_id) if not result: # avoiding errors due to unprivileged commands result = Response(command_status=True, message=None) if result.command_status and result.message: reply += result.message + os.linesep if result.command_status is False: reply += "<unrecognized command>" + os.linesep amount_unrecognized += 1 if result.message is None and result.command_status is not False: reply += "<processed without return value>" + os.linesep amount_none += 1 else: reply += "<skipped>" + os.linesep amount_skipped += 1 if amount_unrecognized == length: add_to_listen_if_edited(wrap2.host, message_id) if amount_none + amount_skipped + amount_unrecognized == length: reply = "" reply = reply.strip() if reply != "": message_with_reply = u":{} {}".format(message_id, reply) if len(message_with_reply) <= 500 or "\n" in reply: ev.message.reply(reply, False) else: try: result = handle_commands(content_lower=content_source.lower(), message_parts=message_parts, ev_room=ev_room, ev_room_name=ev_room_name, ev_user_id=ev_user_id, ev_user_name=ev_user_name, wrap2=wrap2, content=content_source, message_id=message_id) except requests.exceptions.HTTPError as e: if "404 Client Error: Not Found for url:" in e.message and "/history" in e.message: return # Return and do nothing. else: traceback.print_exc() raise e # Raise an error if it's not the 'expected' nonexistent link error. if result.message: if wrap2.host + str(message_id) in GlobalVars.listen_to_these_if_edited: GlobalVars.listen_to_these_if_edited.remove(wrap2.host + str(message_id)) message_with_reply = u":{} {}".format(message_id, result.message) if len(message_with_reply) <= 500 or "\n" in result.message: ev.message.reply(result.message, False) if result.command_status is False: add_to_listen_if_edited(wrap2.host, message_id) except: try: # log the error log_exception(*sys.exc_info()) ev.message.reply("I hit an error while trying to run that command; run `!!/errorlogs` for details.") return except: print("An exception was thrown while handling an exception: ") traceback.print_exc()
def watcher(ev, wrap2): if ev.type_id != 1 and ev.type_id != 2: return if ev.type_id == 2 and (wrap2.host + str(ev.message.id)) not in GlobalVars.listen_to_these_if_edited: return print_chat_message(ev) ev_room = str(ev.data["room_id"]) ev_user_id = str(ev.data["user_id"]) ev_room_name = ev.data["room_name"].encode('utf-8') if ev.type_id == 2: ev.message = Message(ev.message.id, wrap2) content_source = ev.message.content_source message_id = ev.message.id if is_smokedetector_message(ev_user_id, ev_room): add_latest_message_lock.acquire() add_latest_smokedetector_message(ev_room, message_id) add_latest_message_lock.release() post_site_id = fetch_post_id_and_site_from_msg_content(content_source) post_url = fetch_post_url_from_msg_content(content_source) if post_site_id is not None and (ev_room == GlobalVars.meta_tavern_room_id or ev_room == GlobalVars.socvr_room_id): t_check_websocket = Thread(target=DeletionWatcher.check_if_report_was_deleted, args=(post_site_id, post_url, ev.message)) t_check_websocket.daemon = True t_check_websocket.start() message_parts = re.split('[ ,]+', content_source) ev_user_name = ev.data["user_name"] ev_user_link = "//chat.{host}/users/{user_id}".format(host=wrap2.host, user_id=ev.user.id) if ev_user_name != "SmokeDetector": GlobalVars.users_chatting[ev_room].append((ev_user_name, ev_user_link)) shortcut_messages = [] if message_parts[0].lower() == "sd": message_parts = preprocess_shortcut_command(content_source).split(" ") latest_smokedetector_messages = GlobalVars.latest_smokedetector_messages[ev_room] commands = message_parts[1:] if len(latest_smokedetector_messages) == 0: ev.message.reply("I don't have any messages posted after the latest reboot.") return if len(commands) > len(latest_smokedetector_messages): ev.message.reply("I've only posted {} messages since the latest reboot; that's not enough to execute all commands. No commands were executed.".format(len(latest_smokedetector_messages))) return for i in xrange(0, len(commands)): shortcut_messages.append(u":{message} {command_name}".format(message=latest_smokedetector_messages[-(i + 1)], command_name=commands[i])) reply = "" amount_none = 0 amount_skipped = 0 amount_unrecognized = 0 length = len(shortcut_messages) for i in xrange(0, length): current_message = shortcut_messages[i] if length > 1: reply += str(i + 1) + ". " reply += u"[{0}] ".format(current_message.split(" ")[0]) if current_message.split(" ")[1] != "-": result = handle_commands(current_message.lower(), current_message.split(" "), ev_room, ev_room_name, ev_user_id, ev_user_name, wrap2, current_message, message_id) r = result if type(result) == tuple: result = result[1] if result is not None and result is not False: reply += result + os.linesep elif result is None: reply += "<processed without return value>" + os.linesep amount_none += 1 elif result is False or r[0] is False: reply += "<unrecognized command>" + os.linesep amount_unrecognized += 1 else: reply += "<skipped>" + os.linesep amount_skipped += 1 if amount_unrecognized == length: add_to_listen_if_edited(wrap2.host, message_id) if amount_none + amount_skipped + amount_unrecognized == length: reply = "" reply = reply.strip() if reply != "": message_with_reply = u":{} {}".format(message_id, reply) if len(message_with_reply) <= 500 or "\n" in reply: ev.message.reply(reply, False) else: result = handle_commands(content_source.lower(), message_parts, ev_room, ev_room_name, ev_user_id, ev_user_name, wrap2, content_source, message_id) if type(result) != tuple: result = (True, result) if result[1] is not None: if wrap2.host + str(message_id) in GlobalVars.listen_to_these_if_edited: GlobalVars.listen_to_these_if_edited.remove(wrap2.host + str(message_id)) message_with_reply = u":{} {}".format(message_id, result[1]) if len(message_with_reply) <= 500 or "\n" in result[1]: ev.message.reply(result[1], False) if result[0] is False: add_to_listen_if_edited(wrap2.host, message_id)
def watcher(ev, wrap2): if ev.type_id != 1 and ev.type_id != 2: return if ev.type_id == 2 and (wrap2.host + str( ev.message.id)) not in GlobalVars.listen_to_these_if_edited: return print_chat_message(ev) ev_room = str(ev.data["room_id"]) ev_user_id = str(ev.data["user_id"]) ev_room_name = ev.data["room_name"].encode('utf-8') if ev.type_id == 2: ev.message = Message(ev.message.id, wrap2) content_source = ev.message.content_source message_id = ev.message.id if is_smokedetector_message(ev_user_id, ev_room): add_latest_smokedetector_message(ev_room, message_id) message_parts = content_source.split(" ") ev_user_name = ev.data["user_name"] ev_user_link = "//chat." + wrap2.host + "/users/" + str(ev.user.id) if ev_user_name != "SmokeDetector": GlobalVars.users_chatting[ev_room].append((ev_user_name, ev_user_link)) shortcut_messages = [] if message_parts[0].lower() == "sd": message_parts = preprocess_shortcut_command(content_source).split(" ") latest_smokedetector_messages = GlobalVars.latest_smokedetector_messages[ ev_room] commands = message_parts[1:] if len(latest_smokedetector_messages) == 0: ev.message.reply( "I don't have any messages posted after the latest reboot.") return if len(commands) > len(latest_smokedetector_messages): ev.message.reply( "I've only posted {} messages since the latest reboot; that's not enough to execute all commands. No commands were executed." .format(len(latest_smokedetector_messages))) return for i in range(0, len(commands)): shortcut_messages.append( ":" + str(latest_smokedetector_messages[-(i + 1)]) + " " + commands[i]) reply = "" amount_none = 0 amount_skipped = 0 amount_unrecognized = 0 length = len(shortcut_messages) for i in range(0, length): current_message = shortcut_messages[i] if length > 1: reply += str(i + 1) + ". " reply += "[" + current_message.split(" ")[0] + "] " if current_message.split(" ")[1] != "-": r = handle_commands(current_message.lower(), current_message.split(" "), ev_room, ev_room_name, ev_user_id, ev_user_name, wrap2, current_message, message_id) result = r if type(result) == tuple: result = result[1] if result is not None and result is not False: reply += result + os.linesep elif result is None: reply += "<processed without return value>" + os.linesep amount_none += 1 elif result is False or r[0] is False: reply += "<unrecognized command>" + os.linesep amount_unrecognized += 1 else: reply += "<skipped>" + os.linesep amount_skipped += 1 if amount_unrecognized == length: add_to_listen_if_edited(wrap2.host, message_id) if amount_none + amount_skipped + amount_unrecognized == length: reply = "" reply = reply.strip() if reply != "": message_with_reply = u":{} {}".format(message_id, reply) if len(message_with_reply) <= 500 or "\n" in reply: ev.message.reply(reply, False) else: r = handle_commands(content_source.lower(), message_parts, ev_room, ev_room_name, ev_user_id, ev_user_name, wrap2, content_source, message_id) if type(r) != tuple: r = (True, r) if r[1] is not None: if wrap2.host + str( message_id) in GlobalVars.listen_to_these_if_edited: GlobalVars.listen_to_these_if_edited.remove(wrap2.host + str(message_id)) message_with_reply = u":{} {}".format(message_id, r[1]) if len(message_with_reply) <= 500 or "\n" in r[1]: ev.message.reply(r[1], False) if r[0] is False: add_to_listen_if_edited(wrap2.host, message_id)