def post_message_if_not_deleted(self, post_site_id, post_url, message_text, room): was_report_deleted = self.check_websocket_for_deletion( post_site_id, post_url, 300) if not was_report_deleted and not datahandling.is_false_positive(post_site_id[0:2]) and not \ datahandling.is_ignored_post(post_site_id[0:2]): room.send_message(message_text)
def check_if_spam(post): # if not post.body: # body = "" # test, why = FindSpam.test_post(title, body, user_name, post_site, # is_answer, body_is_summary, owner_rep, post_score) test, why = FindSpam.test_post(post) if datahandling.is_blacklisted_user(parsing.get_user_from_url(post.user_url)): test.append("blacklisted user") blacklisted_user_data = datahandling.get_blacklisted_user_data(parsing.get_user_from_url(post.user_url)) if len(blacklisted_user_data) > 1: if blacklisted_user_data[1] == "metasmoke": blacklisted_by = "the metasmoke API" else: blacklisted_by = "http:" + blacklisted_user_data[1] blacklisted_post_url = blacklisted_user_data[2] if blacklisted_post_url: rel_url = blacklisted_post_url.replace("http:", "", 1) why += u"\nBlacklisted user - blacklisted for {} (" \ u"https://m.erwaysoftware.com/posts/by-url?url={}) by {}".format(blacklisted_post_url, rel_url, blacklisted_by) else: why += u"\n" + u"Blacklisted user - blacklisted by {}".format(blacklisted_by) if 0 < len(test): if datahandling.has_already_been_posted(post.post_site, post.post_id, post.title) \ or datahandling.is_false_positive((post.post_id, post.post_site)) \ or should_whitelist_prevent_alert(post.user_url, test) \ or datahandling.is_ignored_post((post.post_id, post.post_site)) \ or datahandling.is_auto_ignored_post((post.post_id, post.post_site)): return False, None, "" # Don't repost. Reddit will hate you. return True, test, why return False, None, ""
def check_if_spam(post): # if not post.body: # body = "" # test, why = FindSpam.test_post(title, body, user_name, post_site, # is_answer, body_is_summary, owner_rep, post_score) test, why = FindSpam.test_post(post) if datahandling.is_blacklisted_user( parsing.get_user_from_url(post.user_url)): test.append("blacklisted user") blacklisted_user_data = datahandling.get_blacklisted_user_data( parsing.get_user_from_url(post.user_url)) if len(blacklisted_user_data) > 1: if blacklisted_user_data[1] == "metasmoke": blacklisted_by = "the metasmoke API" else: blacklisted_by = "http:" + blacklisted_user_data[1] blacklisted_post_url = blacklisted_user_data[2] if blacklisted_post_url: rel_url = blacklisted_post_url.replace("http:", "", 1) why += u"\nBlacklisted user - blacklisted for {} (" \ u"https://m.erwaysoftware.com/posts/by-url?url={}) by {}".format(blacklisted_post_url, rel_url, blacklisted_by) else: why += u"\n" + u"Blacklisted user - blacklisted by {}".format( blacklisted_by) if 0 < len(test): if datahandling.has_already_been_posted(post.post_site, post.post_id, post.title) \ or datahandling.is_false_positive((post.post_id, post.post_site)) \ or should_whitelist_prevent_alert(post.user_url, test) \ or datahandling.is_ignored_post((post.post_id, post.post_site)) \ or datahandling.is_auto_ignored_post((post.post_id, post.post_site)): return False, None, "" # Don't repost. Reddit will hate you. return True, test, why return False, None, ""
def callback(room=room, msg=msg_pings): post = fetch_post_id_and_site_from_url( report_data[0])[0:2] if not datahandling.is_false_positive( post) and not datahandling.is_ignored_post( post): _msg_queue.put((room, msg, report_data))
def post_message_if_not_deleted(self, message_text, room, report_data): post_url = report_data[0] post_site_id = fetch_post_id_and_site_from_url(post_url) was_report_deleted = self.check_websocket_for_deletion(post_site_id, post_url, 300) if not was_report_deleted and not datahandling.is_false_positive(post_site_id[0:2]) and not \ datahandling.is_ignored_post(post_site_id[0:2]): chatcommunicate._msg_queue.put((room, message_text, report_data))
def post_message_if_not_deleted(self, message_text, room, report_data): post_url = report_data[0] post_site_id = fetch_post_id_and_site_from_url(post_url) was_report_deleted = self.check_websocket_for_deletion( post_site_id, post_url, 300) if not was_report_deleted and not datahandling.is_false_positive(post_site_id[0:2]) and not \ datahandling.is_ignored_post(post_site_id[0:2]): chatcommunicate._msg_queue.put((room, message_text, report_data))
def check_if_spam(title, body, user_name, user_url, post_site, post_id, is_answer, body_is_summary): if not body: body = "" test = FindSpam.test_post(title, body, user_name, post_site, is_answer, body_is_summary) if is_blacklisted_user(get_user_from_url(user_url)): test.append("Blacklisted user") if 0 < len(test): if has_already_been_posted(post_site, post_id, title) or is_false_positive((post_id, post_site)) \ or should_whitelist_prevent_alert(user_url, test) \ or is_ignored_post((post_id, post_site)) \ or is_auto_ignored_post((post_id, post_site)): return False, None # Don't repost. Reddit will hate you. return True, test return False, None
def check_if_spam(post): test, why = findspam.FindSpam.test_post(post) if datahandling.is_blacklisted_user( parsing.get_user_from_url(post.user_url)): test.append("blacklisted user") blacklisted_user_data = datahandling.get_blacklisted_user_data( parsing.get_user_from_url(post.user_url)) if len(blacklisted_user_data) > 1: if blacklisted_user_data[1] == "metasmoke": blacklisted_by = "the metasmoke API" else: blacklisted_by = blacklisted_user_data[1] blacklisted_post_url = blacklisted_user_data[2] if why and why[-1] == "\n": why = why[:-1] if blacklisted_post_url: rel_url = blacklisted_post_url.replace("http:", "", 1) why += u"\nBlacklisted user - blacklisted for {} ({}) by {}".format( blacklisted_post_url, to_metasmoke_link(rel_url), blacklisted_by) else: why += u"\n" + u"Blacklisted user - blacklisted by {}".format( blacklisted_by) if test: result = None if datahandling.has_already_been_posted(post.post_site, post.post_id, post.title): result = "post has already been reported" elif datahandling.is_false_positive((post.post_id, post.post_site)): result = "post is marked as false positive" elif should_whitelist_prevent_alert(post.user_url, test): result = "user is whitelisted" elif datahandling.is_ignored_post((post.post_id, post.post_site)): result = "post is ignored" elif datahandling.is_auto_ignored_post((post.post_id, post.post_site)): result = "post is automatically ignored" elif datahandling.has_community_bumped_post(post.post_url, post.body): result = "post is bumped by Community \u2666\uFE0F" # Dirty approach if result is None: # Post not ignored return True, test, why else: return False, (test, why), result # XXX: Return an empty string for "why" if the post isn't scanned as spam # Don't touch if unsure, you'll break !!/report return False, None, ""
def check_if_spam(post): test, why = findspam.FindSpam.test_post(post) if datahandling.is_blacklisted_user(parsing.get_user_from_url(post.user_url)): test.append("blacklisted user") blacklisted_user_data = datahandling.get_blacklisted_user_data(parsing.get_user_from_url(post.user_url)) if len(blacklisted_user_data) > 1: if blacklisted_user_data[1] == "metasmoke": blacklisted_by = "the metasmoke API" else: blacklisted_by = blacklisted_user_data[1] blacklisted_post_url = blacklisted_user_data[2] if why and why[-1] == "\n": why = why[:-1] if blacklisted_post_url: rel_url = blacklisted_post_url.replace("http:", "", 1) ms_url = datahandling.resolve_ms_link(rel_url) or to_metasmoke_link(rel_url) why += "\nBlacklisted user - blacklisted for {} ({}) by {}".format( blacklisted_post_url, ms_url, blacklisted_by) else: why += "\n" + u"Blacklisted user - blacklisted by {}".format(blacklisted_by) if test: result = None if datahandling.has_already_been_posted(post.post_site, post.post_id, post.title): result = "post has already been reported" elif datahandling.is_false_positive((post.post_id, post.post_site)): result = "post is marked as false positive" elif should_whitelist_prevent_alert(post.user_url, test): result = "user is whitelisted" elif datahandling.is_ignored_post((post.post_id, post.post_site)): result = "post is ignored" elif datahandling.is_auto_ignored_post((post.post_id, post.post_site)): result = "post is automatically ignored" elif datahandling.has_community_bumped_post(post.post_url, post.body): result = "post is bumped by Community \u2666\uFE0F" # Dirty approach if result is None: # Post not ignored return True, test, why else: return False, (test, why), result return False, None, ""
def _ignore(post_site_id): return datahandling.is_false_positive(post_site_id) or datahandling.is_ignored_post(post_site_id) or \ datahandling.is_auto_ignored_post(post_site_id)
def test_false_positive(): mocked_client = mock_client_get_message(client.Client()) assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") is False event = mock_event(":1234 fp", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({ 1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] All-caps title: [TEST](//stackoverflow.com/questions/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`' }) watcher(event, mocked_client) assert reply_value == "Registered question as false positive." assert is_false_positive(("1000", "stackoverflow.com")) GlobalVars.false_positives = [] assert not is_false_positive(("1000", "stackoverflow.com")) event = mock_event(":1234 fp-", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({ 1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] All-caps title: [TEST](//stackoverflow.com/questions/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`' }) watcher(event, mocked_client) assert reply_value == "" assert is_false_positive(("1000", "stackoverflow.com")) GlobalVars.false_positives = [] assert not is_false_positive(("1000", "stackoverflow.com")) event = mock_event(":1234 fpu", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({ 1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] All-caps title: [TEST](//stackoverflow.com/questions/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`' }) watcher(event, mocked_client) assert reply_value == "Registered question as false positive and whitelisted user." assert is_false_positive(("1000", "stackoverflow.com")) assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") event = mock_event("!!/rmwlu http://stackoverflow.com/users/1", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") watcher(event, client.Client()) assert reply_value == "User removed from whitelist (`1` on `stackoverflow.com`)." assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") is False GlobalVars.false_positives = [] assert not is_false_positive(("1000", "stackoverflow.com")) event = mock_event(":1234 fpu-", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({ 1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] All-caps title: [TEST](//stackoverflow.com/questions/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`' }) watcher(event, mocked_client) assert reply_value == "" assert is_false_positive(("1000", "stackoverflow.com")) assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") event = mock_event("!!/rmwlu http://stackoverflow.com/users/1", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") watcher(event, client.Client()) assert reply_value == "User removed from whitelist (`1` on `stackoverflow.com`)." assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") is False event = mock_event(":1234 fp", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({ 1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] Bad keyword in answer: [TEST](//stackoverflow.com/a/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`' }) watcher(event, mocked_client) assert reply_value == "Registered answer as false positive." assert is_false_positive(("1000", "stackoverflow.com")) GlobalVars.false_positives = [] assert not is_false_positive(("1000", "stackoverflow.com")) event = mock_event(":1234 fp-", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({ 1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] Bad keyword in answer: [TEST](//stackoverflow.com/a/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`' }) watcher(event, mocked_client) assert reply_value == "" assert is_false_positive(("1000", "stackoverflow.com")) GlobalVars.false_positives = [] assert not is_false_positive(("1000", "stackoverflow.com")) event = mock_event(":1234 fpu", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({ 1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] Bad keyword in answer: [TEST](//stackoverflow.com/a/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`' }) watcher(event, mocked_client) assert reply_value == "Registered answer as false positive and whitelisted user." assert is_false_positive(("1000", "stackoverflow.com")) assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") event = mock_event("!!/rmwlu http://stackoverflow.com/users/1", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") watcher(event, client.Client()) assert reply_value == "User removed from whitelist (`1` on `stackoverflow.com`)." assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") is False GlobalVars.false_positives = [] assert not is_false_positive(("1000", "stackoverflow.com")) event = mock_event(":1234 fpu-", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({ 1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] Bad keyword in answer: [TEST](//stackoverflow.com/a/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`' }) watcher(event, mocked_client) assert reply_value == "" assert is_false_positive(("1000", "stackoverflow.com")) assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") event = mock_event("!!/rmwlu http://stackoverflow.com/users/1", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") watcher(event, client.Client()) assert reply_value == "User removed from whitelist (`1` on `stackoverflow.com`)." assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") is False event = mock_event(":1234 fpu-", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({1234: 'Not a report yay for bots'}) watcher(event, mocked_client) assert reply_value == "That message is not a report." # cleanup os.remove("whitelistedUsers.p") os.remove("falsePositives.p")
def handle_websocket_data(data): if "message" not in data: return message = data['message'] if isinstance(message, Iterable): if "message" in message: GlobalVars.charcoal_hq.send_message(message['message']) elif "exit" in message: os._exit(message["exit"]) elif "blacklist" in message: datahandling.add_blacklisted_user( (message['blacklist']['uid'], message['blacklist']['site']), "metasmoke", message['blacklist']['post']) elif "naa" in message: post_site_id = parsing.fetch_post_id_and_site_from_url( message["naa"]["post_link"]) datahandling.add_ignored_post(post_site_id[0:2]) elif "fp" in message: post_site_id = parsing.fetch_post_id_and_site_from_url( message["fp"]["post_link"]) datahandling.add_false_positive(post_site_id[0:2]) elif "report" in message: post_data = apigetpost.api_get_post( message["report"]["post_link"]) if post_data is None or post_data is False: return if datahandling.has_already_been_posted(post_data.site, post_data.post_id, post_data.title) \ and not datahandling.is_false_positive((post_data.post_id, post_data.site)): return user = parsing.get_user_from_url(post_data.owner_url) if user is not None: datahandling.add_blacklisted_user(user, "metasmoke", post_data.post_url) why = u"Post manually reported by user *{}* from metasmoke.\n".format( message["report"]["user"]) postobj = classes.Post( api_response={ 'title': post_data.title, 'body': post_data.body, 'owner': { 'display_name': post_data.owner_name, 'reputation': post_data.owner_rep, 'link': post_data.owner_url }, 'site': post_data.site, 'IsAnswer': (post_data.post_type == "answer"), 'score': post_data.score, 'link': post_data.post_url, 'question_id': post_data.post_id, 'up_vote_count': post_data.up_vote_count, 'down_vote_count': post_data.down_vote_count }) spamhandling.handle_spam( post=postobj, reasons=["Manually reported " + post_data.post_type], why=why) elif "deploy_updated" in message: sha = message["deploy_updated"]["head_commit"]["id"] if sha != os.popen('git log --pretty=format:"%H" -n 1').read(): if "autopull" in message["deploy_updated"]["head_commit"][ "message"]: if only_blacklists_changed( GitManager.get_remote_diff()): commit_md = "[`{0}`](https://github.com/Charcoal-SE/SmokeDetector/commit/{0})" \ .format(sha[:7]) i = [] # Currently no issues with backlists for bl_file in glob('bad_*.txt') + glob( 'blacklisted_*.txt' ): # Check blacklists for issues with open(bl_file, 'r') as lines: seen = dict() for lineno, line in enumerate(lines, 1): if line.endswith('\r\n'): i.append( "DOS line ending at `{0}:{1}` in {2}" .format( bl_file, lineno, commit_md)) if not line.endswith('\n'): i.append( "No newline at end of `{0}` in {1}" .format(bl_file, commit_md)) if line == '\n': i.append( "Blank line at `{0}:{1}` in {2}" .format( bl_file, lineno, commit_md)) if line in seen: i.append( "Duplicate entry of {0} at lines {1} and {2} of {3} in {4}" .format( line.rstrip('\n'), seen[line], lineno, bl_file, commit_md)) seen[line] = lineno if i == []: # No issues GitManager.pull_remote() load_blacklists() GlobalVars.charcoal_hq.send_message( "No code modified in {0}, only blacklists" " reloaded.".format(commit_md)) else: i.append("please fix before pulling.") GlobalVars.charcoal_hq.send_message( ", ".join(i)) elif "commit_status" in message: c = message["commit_status"] sha = c["commit_sha"][:7] if c["commit_sha"] != os.popen( 'git log --pretty=format:"%H" -n 1').read(): if c["status"] == "success": if "autopull" in c["commit_message"]: s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha})"\ " succeeded. Message contains 'autopull', pulling...".format(ci_link=c["ci_url"], commit_sha=sha) GlobalVars.charcoal_hq.send_message(s) time.sleep(2) os._exit(3) else: s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha}) succeeded.".format(ci_link=c["ci_url"], commit_sha=sha) elif c["status"] == "failure": s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha}) failed.".format(ci_link=c["ci_url"], commit_sha=sha) # noinspection PyUnboundLocalVariable GlobalVars.charcoal_hq.send_message(s) elif "everything_is_broken" in message: if message["everything_is_broken"] is True: os._exit(6)
def test_false_positive(): mocked_client = mock_client_get_message(client.Client()) assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") is False event = mock_event(":1234 fp", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] All-caps title: [TEST](//stackoverflow.com/questions/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`'}) watcher(event, mocked_client) assert reply_value == "Registered question as false positive." assert is_false_positive(("1000", "stackoverflow.com")) GlobalVars.false_positives = [] assert not is_false_positive(("1000", "stackoverflow.com")) event = mock_event(":1234 fp-", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] All-caps title: [TEST](//stackoverflow.com/questions/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`'}) watcher(event, mocked_client) assert reply_value == "" assert is_false_positive(("1000", "stackoverflow.com")) GlobalVars.false_positives = [] assert not is_false_positive(("1000", "stackoverflow.com")) event = mock_event(":1234 fpu", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] All-caps title: [TEST](//stackoverflow.com/questions/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`'}) watcher(event, mocked_client) assert reply_value == "Registered question as false positive and whitelisted user." assert is_false_positive(("1000", "stackoverflow.com")) assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") event = mock_event("!!/rmwlu http://stackoverflow.com/users/1", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") watcher(event, client.Client()) assert reply_value == "User removed from whitelist (`1` on `stackoverflow.com`)." assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") is False GlobalVars.false_positives = [] assert not is_false_positive(("1000", "stackoverflow.com")) event = mock_event(":1234 fpu-", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] All-caps title: [TEST](//stackoverflow.com/questions/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`'}) watcher(event, mocked_client) assert reply_value == "" assert is_false_positive(("1000", "stackoverflow.com")) assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") event = mock_event("!!/rmwlu http://stackoverflow.com/users/1", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") watcher(event, client.Client()) assert reply_value == "User removed from whitelist (`1` on `stackoverflow.com`)." assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") is False event = mock_event(":1234 fp", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] Bad keyword in answer: [TEST](//stackoverflow.com/a/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`'}) watcher(event, mocked_client) assert reply_value == "Registered answer as false positive." assert is_false_positive(("1000", "stackoverflow.com")) GlobalVars.false_positives = [] assert not is_false_positive(("1000", "stackoverflow.com")) event = mock_event(":1234 fp-", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] Bad keyword in answer: [TEST](//stackoverflow.com/a/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`'}) watcher(event, mocked_client) assert reply_value == "" assert is_false_positive(("1000", "stackoverflow.com")) GlobalVars.false_positives = [] assert not is_false_positive(("1000", "stackoverflow.com")) event = mock_event(":1234 fpu", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] Bad keyword in answer: [TEST](//stackoverflow.com/a/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`'}) watcher(event, mocked_client) assert reply_value == "Registered answer as false positive and whitelisted user." assert is_false_positive(("1000", "stackoverflow.com")) assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") event = mock_event("!!/rmwlu http://stackoverflow.com/users/1", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") watcher(event, client.Client()) assert reply_value == "User removed from whitelist (`1` on `stackoverflow.com`)." assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") is False GlobalVars.false_positives = [] assert not is_false_positive(("1000", "stackoverflow.com")) event = mock_event(":1234 fpu-", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({1234: '[ [SmokeDetector](https://github.com/Charcoal-SE/SmokeDetector) ] Bad keyword in answer: [TEST](//stackoverflow.com/a/1000) by [Community](//stackoverflow.com/users/1) on `stackoverflow.com`'}) watcher(event, mocked_client) assert reply_value == "" assert is_false_positive(("1000", "stackoverflow.com")) assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") event = mock_event("!!/rmwlu http://stackoverflow.com/users/1", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") watcher(event, client.Client()) assert reply_value == "User removed from whitelist (`1` on `stackoverflow.com`)." assert is_user_currently_whitelisted("http://stackoverflow.com/users/1", "1", "stackoverflow.com") is False event = mock_event(":1234 fpu-", 1, 11540, "Charcoal HQ", 59776, u"Doorknob 冰") mock_previous_messages({1234: 'Not a report yay for bots'}) watcher(event, mocked_client) assert reply_value == "That message is not a report." # cleanup os.remove("whitelistedUsers.p") os.remove("falsePositives.p")
def handle_websocket_data(data): if "message" not in data: return message = data['message'] if not isinstance(message, Iterable): return if "message" in message: chatcommunicate.tell_rooms_with("metasmoke", message['message']) elif "autoflag_fp" in message: event = message["autoflag_fp"] chatcommunicate.tell_rooms(event["message"], ("debug", "site-" + event["site"]), ("no-site-" + event["site"], ), notify_site="/autoflag_fp") elif "exit" in message: os._exit(message["exit"]) elif "blacklist" in message: ids = (message['blacklist']['uid'], message['blacklist']['site']) datahandling.add_blacklisted_user(ids, "metasmoke", message['blacklist']['post']) datahandling.last_feedbacked = (ids, time.time() + 60) elif "unblacklist" in message: ids = (message['unblacklist']['uid'], message['unblacklist']['site']) datahandling.remove_blacklisted_user(ids) elif "naa" in message: post_site_id = parsing.fetch_post_id_and_site_from_url( message["naa"]["post_link"]) datahandling.add_ignored_post(post_site_id[0:2]) elif "fp" in message: post_site_id = parsing.fetch_post_id_and_site_from_url( message["fp"]["post_link"]) datahandling.add_false_positive(post_site_id[0:2]) elif "report" in message: import chatcommands # Do it here chatcommands.report_posts([message["report"]["post_link"]], "the metasmoke API", None, "the metasmoke API") return post_data = apigetpost.api_get_post(message["report"]["post_link"]) if post_data is None or post_data is False: return if datahandling.has_already_been_posted(post_data.site, post_data.post_id, post_data.title) \ and not datahandling.is_false_positive((post_data.post_id, post_data.site)): return user = parsing.get_user_from_url(post_data.owner_url) post = classes.Post(api_response=post_data.as_dict) scan_spam, scan_reasons, scan_why = spamhandling.check_if_spam( post) if scan_spam: why_append = u"This post would have also been caught for: " + \ u", ".join(scan_reasons).capitalize() + "\n" + scan_why else: why_append = u"This post would not have been caught otherwise." # Add user to blacklist *after* post is scanned if user is not None: datahandling.add_blacklisted_user(user, "metasmoke", post_data.post_url) why = u"Post manually reported by user *{}* from metasmoke.\n\n{}".format( message["report"]["user"], why_append) spamhandling.handle_spam( post=post, reasons=["Manually reported " + post_data.post_type], why=why) elif "deploy_updated" in message: return # Disabled sha = message["deploy_updated"]["head_commit"]["id"] if sha != os.popen('git log -1 --pretty="%H"').read(): if "autopull" in message["deploy_updated"]["head_commit"][ "message"]: if only_blacklists_changed(GitManager.get_remote_diff()): commit_md = "[`{0}`](https://github.com/{1}/commit/{0})" \ .format(sha[:7], GlobalVars.bot_repo_slug) integrity = blacklist_integrity_check() if len(integrity) == 0: # No issues GitManager.pull_remote() findspam.reload_blacklists() chatcommunicate.tell_rooms_with( "debug", "No code modified in {0}, only blacklists" " reloaded.".format(commit_md)) else: integrity.append("please fix before pulling.") chatcommunicate.tell_rooms_with( "debug", ", ".join(integrity)) elif "commit_status" in message: c = message["commit_status"] sha = c["commit_sha"][:7] if c["commit_sha"] == sp.check_output( ["git", "log", "-1", "--pretty=%H"]).decode('utf-8').strip(): return if c["status"] == "success": if "autopull" in c["commit_message"]: s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/{repo}/" \ "commit/{commit_sha}) succeeded. Message contains 'autopull', pulling...".format( ci_link=c["ci_url"], repo=GlobalVars.bot_repo_slug, commit_sha=sha) remote_diff = GitManager.get_remote_diff() if only_blacklists_changed(remote_diff): GitManager.pull_remote() if not GlobalVars.on_master: # Restart if HEAD detached log('warning', "Pulling remote with HEAD detached, checkout deploy", f=True) os._exit(8) GlobalVars.reload() findspam.FindSpam.reload_blacklists() chatcommunicate.tell_rooms_with( 'debug', GlobalVars.s_norestart) elif only_modules_changed(remote_diff): GitManager.pull_remote() if not GlobalVars.on_master: # Restart if HEAD detached log('warning', "Pulling remote with HEAD detached, checkout deploy", f=True) os._exit(8) GlobalVars.reload() reload_modules() chatcommunicate.tell_rooms_with( 'debug', GlobalVars.s_norestart2) else: chatcommunicate.tell_rooms_with('debug', s, notify_site="/ci") os._exit(3) else: s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/{repo}/commit/{commit_sha}) " \ "succeeded.".format(ci_link=c["ci_url"], repo=GlobalVars.bot_repo_slug, commit_sha=sha) chatcommunicate.tell_rooms_with("debug", s, notify_site="/ci") elif c["status"] == "failure": s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/{repo}/commit/{commit_sha}) " \ "failed.".format(ci_link=c["ci_url"], repo=GlobalVars.bot_repo_slug, commit_sha=sha) chatcommunicate.tell_rooms_with("debug", s, notify_site="/ci") elif "everything_is_broken" in message: if message["everything_is_broken"] is True: os._exit(6)
def post_message_if_not_deleted(self, post_site_id, message_text, room): was_report_deleted = self.check_websocket_for_deletion(post_site_id, 300) if not was_report_deleted and not is_false_positive(post_site_id[0:2]) and not is_ignored_post(post_site_id[0:2]): room.send_message(message_text)
def handle_websocket_data(data): if "message" not in data: return message = data['message'] if isinstance(message, Iterable): if "message" in message: GlobalVars.charcoal_hq.send_message(message['message']) elif "exit" in message: os._exit(message["exit"]) elif "blacklist" in message: datahandling.add_blacklisted_user((message['blacklist']['uid'], message['blacklist']['site']), "metasmoke", message['blacklist']['post']) elif "naa" in message: post_site_id = parsing.fetch_post_id_and_site_from_url(message["naa"]["post_link"]) datahandling.add_ignored_post(post_site_id[0:2]) elif "fp" in message: post_site_id = parsing.fetch_post_id_and_site_from_url(message["fp"]["post_link"]) datahandling.add_false_positive(post_site_id[0:2]) elif "report" in message: post_data = apigetpost.api_get_post(message["report"]["post_link"]) if post_data is None or post_data is False: return if datahandling.has_already_been_posted(post_data.site, post_data.post_id, post_data.title) \ and not datahandling.is_false_positive((post_data.post_id, post_data.site)): return user = parsing.get_user_from_url(post_data.owner_url) if user is not None: datahandling.add_blacklisted_user(user, "metasmoke", post_data.post_url) why = u"Post manually reported by user *{}* from metasmoke.\n".format(message["report"]["user"]) postobj = classes.Post(api_response={'title': post_data.title, 'body': post_data.body, 'owner': {'display_name': post_data.owner_name, 'reputation': post_data.owner_rep, 'link': post_data.owner_url}, 'site': post_data.site, 'IsAnswer': (post_data.post_type == "answer"), 'score': post_data.score, 'link': post_data.post_url, 'question_id': post_data.post_id, 'up_vote_count': post_data.up_vote_count, 'down_vote_count': post_data.down_vote_count}) spamhandling.handle_spam(post=postobj, reasons=["Manually reported " + post_data.post_type], why=why) elif "commit_status" in message: c = message["commit_status"] sha = c["commit_sha"][:7] if c["commit_sha"] != os.popen('git log --pretty=format:"%H" -n 1').read(): if c["status"] == "success": if "autopull" in c["commit_message"]: s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha})"\ " succeeded. Message contains 'autopull', pulling...".format(ci_link=c["ci_url"], commit_sha=sha) GlobalVars.charcoal_hq.send_message(s) time.sleep(2) os._exit(3) else: s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha}) succeeded.".format(ci_link=c["ci_url"], commit_sha=sha) elif c["status"] == "failure": s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha}) failed.".format(ci_link=c["ci_url"], commit_sha=sha) # noinspection PyUnboundLocalVariable GlobalVars.charcoal_hq.send_message(s)
def init_websocket(self): try: GlobalVars.metasmoke_ws = websocket.create_connection( GlobalVars.metasmoke_ws_host, origin=GlobalVars.metasmoke_host) GlobalVars.metasmoke_ws.send( json.dumps({ "command": "subscribe", "identifier": "{\"channel\":\"SmokeDetectorChannel\",\"key\":\"" + GlobalVars.metasmoke_key + "\"}" })) while True: a = GlobalVars.metasmoke_ws.recv() print(a) try: data = json.loads(a) if "message" in data: message = data['message'] if isinstance(message, Iterable): if "message" in message: GlobalVars.charcoal_hq.send_message( message['message']) elif "blacklist" in message: datahandling.add_blacklisted_user( (message['blacklist']['uid'], message['blacklist']['site']), "metasmoke", message['blacklist']['post']) elif "naa" in message: post_site_id = parsing.fetch_post_id_and_site_from_url( message["naa"]["post_link"]) datahandling.add_ignored_post( post_site_id[0:2]) elif "fp" in message: post_site_id = parsing.fetch_post_id_and_site_from_url( message["fp"]["post_link"]) datahandling.add_false_positive( post_site_id[0:2]) elif "report" in message: post_data = apigetpost.api_get_post( message["report"]["post_link"]) if post_data is None or post_data is False: continue if datahandling.has_already_been_posted( post_data.site, post_data.post_id, post_data.title ) and not datahandling.is_false_positive( (post_data.post_id, post_data.site)): continue user = parsing.get_user_from_url( post_data.owner_url) if user is not None: datahandling.add_blacklisted_user( user, "metasmoke", post_data.post_url) why = u"Post manually reported by user *{}* from metasmoke.\n".format( message["report"]["user"]) spamhandling.handle_spam( title=post_data.title, body=post_data.body, poster=post_data.owner_name, site=post_data.site, post_url=post_data.post_url, poster_url=post_data.owner_url, post_id=post_data.post_id, reasons=[ "Manually reported " + post_data.post_type ], is_answer=post_data.post_type == "answer", why=why, owner_rep=post_data.owner_rep, post_score=post_data.score, up_vote_count=post_data.up_vote_count, down_vote_count=post_data.down_vote_count, question_id=post_data.question_id) elif "commit_status" in message: c = message["commit_status"] sha = c["commit_sha"][:7] if c["commit_sha"] != os.popen( 'git log --pretty=format:"%H" -n 1' ).read(): if c["status"] == "success": if "autopull" in c["commit_message"]: GlobalVars.charcoal_hq.send_message( "[CI]({ci_link}) on {commit_sha} succeeded. Message contains 'autopull', pulling..." .format(ci_link=c["ci_url"], commit_sha=sha)) time.sleep(2) os._exit(3) else: GlobalVars.charcoal_hq.send_message( "[CI]({ci_link}) on {commit_sha} succeeded." .format(ci_link=c["ci_url"], commit_sha=sha)) elif c["status"] == "failure": GlobalVars.charcoal_hq.send_message( "[CI]({ci_link}) on {commit_sha} failed." .format(ci_link=c["ci_url"], commit_sha=sha)) except Exception, e: GlobalVars.metasmoke_ws = websocket.create_connection( GlobalVars.metasmoke_ws_host, origin=GlobalVars.metasmoke_host) GlobalVars.metasmoke_ws.send( json.dumps({ "command": "subscribe", "identifier": "{\"channel\":\"SmokeDetectorChannel\"}" })) print e try: exc_info = sys.exc_info() traceback.print_exception(*exc_info) except: print "meh" except: print "Couldn't bind to MS websocket"
def handle_websocket_data(data): if "message" not in data: return message = data['message'] if isinstance(message, Iterable): if "message" in message: GlobalVars.charcoal_hq.send_message(message['message']) elif "blacklist" in message: datahandling.add_blacklisted_user((message['blacklist']['uid'], message['blacklist']['site']), "metasmoke", message['blacklist']['post']) elif "naa" in message: post_site_id = parsing.fetch_post_id_and_site_from_url(message["naa"]["post_link"]) datahandling.add_ignored_post(post_site_id[0:2]) elif "fp" in message: post_site_id = parsing.fetch_post_id_and_site_from_url(message["fp"]["post_link"]) datahandling.add_false_positive(post_site_id[0:2]) elif "report" in message: post_data = apigetpost.api_get_post(message["report"]["post_link"]) if post_data is None or post_data is False: return if datahandling.has_already_been_posted(post_data.site, post_data.post_id, post_data.title) \ and not datahandling.is_false_positive((post_data.post_id, post_data.site)): return user = parsing.get_user_from_url(post_data.owner_url) if user is not None: datahandling.add_blacklisted_user(user, "metasmoke", post_data.post_url) why = u"Post manually reported by user *{}* from metasmoke.\n".format(message["report"]["user"]) spamhandling.handle_spam(title=post_data.title, body=post_data.body, poster=post_data.owner_name, site=post_data.site, post_url=post_data.post_url, poster_url=post_data.owner_url, post_id=post_data.post_id, reasons=["Manually reported " + post_data.post_type], is_answer=post_data.post_type == "answer", why=why, owner_rep=post_data.owner_rep, post_score=post_data.score, up_vote_count=post_data.up_vote_count, down_vote_count=post_data.down_vote_count, question_id=post_data.question_id) elif "commit_status" in message: c = message["commit_status"] sha = c["commit_sha"][:7] if c["commit_sha"] != os.popen('git log --pretty=format:"%H" -n 1').read(): if c["status"] == "success": if "autopull" in c["commit_message"]: s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha})"\ " succeeded. Message contains 'autopull', pulling...".format(ci_link=c["ci_url"], commit_sha=sha) GlobalVars.charcoal_hq.send_message(s) time.sleep(2) os._exit(3) else: s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha}) succeeded.".format(ci_link=c["ci_url"], commit_sha=sha) elif c["status"] == "failure": s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha}) failed.".format(ci_link=c["ci_url"], commit_sha=sha) # noinspection PyUnboundLocalVariable GlobalVars.charcoal_hq.send_message(s)
def handle_websocket_data(data): if "message" not in data: return message = data["message"] if isinstance(message, Iterable): if "message" in message: GlobalVars.charcoal_hq.send_message(message["message"]) elif "blacklist" in message: datahandling.add_blacklisted_user( (message["blacklist"]["uid"], message["blacklist"]["site"]), "metasmoke", message["blacklist"]["post"], ) elif "naa" in message: post_site_id = parsing.fetch_post_id_and_site_from_url(message["naa"]["post_link"]) datahandling.add_ignored_post(post_site_id[0:2]) elif "fp" in message: post_site_id = parsing.fetch_post_id_and_site_from_url(message["fp"]["post_link"]) datahandling.add_false_positive(post_site_id[0:2]) elif "report" in message: post_data = apigetpost.api_get_post(message["report"]["post_link"]) if post_data is None or post_data is False: return if datahandling.has_already_been_posted( post_data.site, post_data.post_id, post_data.title ) and not datahandling.is_false_positive((post_data.post_id, post_data.site)): return user = parsing.get_user_from_url(post_data.owner_url) if user is not None: datahandling.add_blacklisted_user(user, "metasmoke", post_data.post_url) why = u"Post manually reported by user *{}* from metasmoke.\n".format(message["report"]["user"]) spamhandling.handle_spam( title=post_data.title, body=post_data.body, poster=post_data.owner_name, site=post_data.site, post_url=post_data.post_url, poster_url=post_data.owner_url, post_id=post_data.post_id, reasons=["Manually reported " + post_data.post_type], is_answer=post_data.post_type == "answer", why=why, owner_rep=post_data.owner_rep, post_score=post_data.score, up_vote_count=post_data.up_vote_count, down_vote_count=post_data.down_vote_count, question_id=post_data.question_id, ) elif "commit_status" in message: c = message["commit_status"] sha = c["commit_sha"][:7] if c["commit_sha"] != os.popen('git log --pretty=format:"%H" -n 1').read(): if c["status"] == "success": if "autopull" in c["commit_message"]: s = "[CI]({ci_link}) on {commit_sha} succeeded. Message contains 'autopull', pulling...".format( ci_link=c["ci_url"], commit_sha=sha ) GlobalVars.charcoal_hq.send_message(s) time.sleep(2) os._exit(3) else: s = "[CI]({ci_link}) on {commit_sha} succeeded.".format(ci_link=c["ci_url"], commit_sha=sha) elif c["status"] == "failure": s = "[CI]({ci_link}) on {commit_sha} failed.".format(ci_link=c["ci_url"], commit_sha=sha) # noinspection PyUnboundLocalVariable GlobalVars.charcoal_hq.send_message(s)
def handle_websocket_data(data): if "message" not in data: return message = data['message'] if isinstance(message, Iterable): if "message" in message: chatcommunicate.tell_rooms_with("debug", message['message']) elif "exit" in message: os._exit(message["exit"]) elif "blacklist" in message: datahandling.add_blacklisted_user((message['blacklist']['uid'], message['blacklist']['site']), "metasmoke", message['blacklist']['post']) elif "naa" in message: post_site_id = parsing.fetch_post_id_and_site_from_url(message["naa"]["post_link"]) datahandling.add_ignored_post(post_site_id[0:2]) elif "fp" in message: post_site_id = parsing.fetch_post_id_and_site_from_url(message["fp"]["post_link"]) datahandling.add_false_positive(post_site_id[0:2]) elif "report" in message: post_data = apigetpost.api_get_post(message["report"]["post_link"]) if post_data is None or post_data is False: return if datahandling.has_already_been_posted(post_data.site, post_data.post_id, post_data.title) \ and not datahandling.is_false_positive((post_data.post_id, post_data.site)): return user = parsing.get_user_from_url(post_data.owner_url) if user is not None: datahandling.add_blacklisted_user(user, "metasmoke", post_data.post_url) why = u"Post manually reported by user *{}* from metasmoke.\n".format(message["report"]["user"]) postobj = classes.Post(api_response={'title': post_data.title, 'body': post_data.body, 'owner': {'display_name': post_data.owner_name, 'reputation': post_data.owner_rep, 'link': post_data.owner_url}, 'site': post_data.site, 'is_answer': (post_data.post_type == "answer"), 'score': post_data.score, 'link': post_data.post_url, 'question_id': post_data.post_id, 'up_vote_count': post_data.up_vote_count, 'down_vote_count': post_data.down_vote_count}) spamhandling.handle_spam(post=postobj, reasons=["Manually reported " + post_data.post_type], why=why) elif "deploy_updated" in message: sha = message["deploy_updated"]["head_commit"]["id"] if sha != os.popen('git log --pretty=format:"%H" -n 1').read(): if "autopull" in message["deploy_updated"]["head_commit"]["message"]: if only_blacklists_changed(GitManager.get_remote_diff()): commit_md = "[`{0}`](https://github.com/Charcoal-SE/SmokeDetector/commit/{0})" \ .format(sha[:7]) i = [] # Currently no issues with backlists for bl_file in glob('bad_*.txt') + glob('blacklisted_*.txt'): # Check blacklists for issues with open(bl_file, 'r') as lines: seen = dict() for lineno, line in enumerate(lines, 1): if line.endswith('\r\n'): i.append("DOS line ending at `{0}:{1}` in {2}".format(bl_file, lineno, commit_md)) if not line.endswith('\n'): i.append("No newline at end of `{0}` in {1}".format(bl_file, commit_md)) if line == '\n': i.append("Blank line at `{0}:{1}` in {2}".format(bl_file, lineno, commit_md)) if line in seen: i.append("Duplicate entry of {0} at lines {1} and {2} of {3} in {4}" .format(line.rstrip('\n'), seen[line], lineno, bl_file, commit_md)) seen[line] = lineno if i == []: # No issues GitManager.pull_remote() load_blacklists() chatcommunicate.tell_rooms_with("debug", "No code modified in {0}, only blacklists" " reloaded.".format(commit_md)) else: i.append("please fix before pulling.") chatcommunicate.tell_rooms_with("debug", ", ".join(i)) elif "commit_status" in message: c = message["commit_status"] sha = c["commit_sha"][:7] if c["commit_sha"] != os.popen('git log --pretty=format:"%H" -n 1').read(): if c["status"] == "success": if "autopull" in c["commit_message"]: s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha})"\ " succeeded. Message contains 'autopull', pulling...".format(ci_link=c["ci_url"], commit_sha=sha) chatcommunicate.tell_rooms_with("debug", s) time.sleep(2) os._exit(3) else: s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha}) succeeded.".format(ci_link=c["ci_url"], commit_sha=sha) chatcommunicate.tell_rooms_with("debug", s) elif c["status"] == "failure": s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha}) failed.".format(ci_link=c["ci_url"], commit_sha=sha) chatcommunicate.tell_rooms_with("debug", s) elif "everything_is_broken" in message: if message["everything_is_broken"] is True: os._exit(6)
def handle_websocket_data(data): if "message" not in data: return message = data['message'] if isinstance(message, Iterable): if "message" in message: chatcommunicate.tell_rooms_with("metasmoke", message['message']) elif "autoflag_fp" in message: event = message["autoflag_fp"] chatcommunicate.tell_rooms(event["message"], ("debug", "site-" + event["site"]), ("no-site-" + event["site"],), notify_site="/autoflag_fp") elif "exit" in message: os._exit(message["exit"]) elif "blacklist" in message: ids = (message['blacklist']['uid'], message['blacklist']['site']) datahandling.add_blacklisted_user(ids, "metasmoke", message['blacklist']['post']) datahandling.last_feedbacked = (ids, time.time() + 60) elif "unblacklist" in message: ids = (message['unblacklist']['uid'], message['unblacklist']['site']) datahandling.remove_blacklisted_user(ids) elif "naa" in message: post_site_id = parsing.fetch_post_id_and_site_from_url(message["naa"]["post_link"]) datahandling.add_ignored_post(post_site_id[0:2]) elif "fp" in message: post_site_id = parsing.fetch_post_id_and_site_from_url(message["fp"]["post_link"]) datahandling.add_false_positive(post_site_id[0:2]) elif "report" in message: post_data = apigetpost.api_get_post(message["report"]["post_link"]) if post_data is None or post_data is False: return if datahandling.has_already_been_posted(post_data.site, post_data.post_id, post_data.title) \ and not datahandling.is_false_positive((post_data.post_id, post_data.site)): return user = parsing.get_user_from_url(post_data.owner_url) post = classes.Post(api_response=post_data.as_dict) scan_spam, scan_reasons, scan_why = spamhandling.check_if_spam(post) if scan_spam: why_append = u"This post would have also been caught for: " + \ u", ".join(scan_reasons).capitalize() + "\n" + scan_why else: why_append = u"This post would not have been caught otherwise." # Add user to blacklist *after* post is scanned if user is not None: datahandling.add_blacklisted_user(user, "metasmoke", post_data.post_url) why = u"Post manually reported by user *{}* from metasmoke.\n\n{}".format( message["report"]["user"], why_append) spamhandling.handle_spam(post=post, reasons=["Manually reported " + post_data.post_type], why=why) elif "deploy_updated" in message: sha = message["deploy_updated"]["head_commit"]["id"] if sha != os.popen('git log -1 --pretty="%H"').read(): if "autopull" in message["deploy_updated"]["head_commit"]["message"]: if only_blacklists_changed(GitManager.get_remote_diff()): commit_md = "[`{0}`](https://github.com/Charcoal-SE/SmokeDetector/commit/{0})" \ .format(sha[:7]) i = [] # Currently no issues with backlists for bl_file in glob('bad_*.txt') + glob('blacklisted_*.txt'): # Check blacklists for issues with open(bl_file, 'r') as lines: seen = dict() for lineno, line in enumerate(lines, 1): if line.endswith('\r\n'): i.append("DOS line ending at `{0}:{1}` in {2}".format(bl_file, lineno, commit_md)) if not line.endswith('\n'): i.append("No newline at end of `{0}` in {1}".format(bl_file, commit_md)) if line == '\n': i.append("Blank line at `{0}:{1}` in {2}".format(bl_file, lineno, commit_md)) if line in seen: i.append("Duplicate entry of {0} at lines {1} and {2} of {3} in {4}" .format(line.rstrip('\n'), seen[line], lineno, bl_file, commit_md)) seen[line] = lineno if i == []: # No issues GitManager.pull_remote() load_blacklists() chatcommunicate.tell_rooms_with("debug", "No code modified in {0}, only blacklists" " reloaded.".format(commit_md)) else: i.append("please fix before pulling.") chatcommunicate.tell_rooms_with("debug", ", ".join(i)) elif "commit_status" in message: c = message["commit_status"] sha = c["commit_sha"][:7] if c["commit_sha"] != os.popen('git log -1 --pretty="%H"').read(): if c["status"] == "success": if "autopull" in c["commit_message"]: s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha})"\ " succeeded. Message contains 'autopull', pulling...".format(ci_link=c["ci_url"], commit_sha=sha) chatcommunicate.tell_rooms_with("debug", s, notify_site="/ci") time.sleep(2) os._exit(3) else: s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha}) succeeded.".format(ci_link=c["ci_url"], commit_sha=sha) chatcommunicate.tell_rooms_with("debug", s, notify_site="/ci") elif c["status"] == "failure": s = "[CI]({ci_link}) on [`{commit_sha}`](https://github.com/Charcoal-SE/SmokeDetector/" \ "commit/{commit_sha}) failed.".format(ci_link=c["ci_url"], commit_sha=sha) chatcommunicate.tell_rooms_with("debug", s, notify_site="/ci") elif "everything_is_broken" in message: if message["everything_is_broken"] is True: os._exit(6)
def callback(room=room, msg=msg_pings): post = fetch_post_id_and_site_from_url(report_data[0])[0:2] if not datahandling.is_false_positive(post) and not datahandling.is_ignored_post(post): _msg_queue.put((room, msg, report_data))
def init_websocket(self): try: GlobalVars.metasmoke_ws = websocket.create_connection(GlobalVars.metasmoke_ws_host, origin=GlobalVars.metasmoke_host) GlobalVars.metasmoke_ws.send(json.dumps({"command": "subscribe", "identifier": "{\"channel\":\"SmokeDetectorChannel\",\"key\":\"" + GlobalVars.metasmoke_key + "\"}"})) while True: a = GlobalVars.metasmoke_ws.recv() print(a) try: data = json.loads(a) if "message" in data: message = data['message'] if isinstance(message, Iterable): if "message" in message: GlobalVars.charcoal_hq.send_message(message['message']) elif "blacklist" in message: datahandling.add_blacklisted_user((message['blacklist']['uid'], message['blacklist']['site']), "metasmoke", message['blacklist']['post']) elif "naa" in message: post_site_id = parsing.fetch_post_id_and_site_from_url(message["naa"]["post_link"]) datahandling.add_ignored_post(post_site_id[0:2]) elif "fp" in message: post_site_id = parsing.fetch_post_id_and_site_from_url(message["fp"]["post_link"]) datahandling.add_false_positive(post_site_id[0:2]) elif "report" in message: post_data = apigetpost.api_get_post(message["report"]["post_link"]) if post_data is None or post_data is False: continue if datahandling.has_already_been_posted(post_data.site, post_data.post_id, post_data.title) and not datahandling.is_false_positive((post_data.post_id, post_data.site)): continue user = parsing.get_user_from_url(post_data.owner_url) if user is not None: datahandling.add_blacklisted_user(user, "metasmoke", post_data.post_url) why = u"Post manually reported by user *{}* from metasmoke.\n".format(message["report"]["user"]) spamhandling.handle_spam(title=post_data.title, body=post_data.body, poster=post_data.owner_name, site=post_data.site, post_url=post_data.post_url, poster_url=post_data.owner_url, post_id=post_data.post_id, reasons=["Manually reported " + post_data.post_type], is_answer=post_data.post_type == "answer", why=why, owner_rep=post_data.owner_rep, post_score=post_data.score, up_vote_count=post_data.up_vote_count, down_vote_count=post_data.down_vote_count, question_id=post_data.question_id) elif "commit_status" in message: c = message["commit_status"] sha = c["commit_sha"][:7] if c["commit_sha"] != os.popen('git log --pretty=format:"%H" -n 1').read(): if c["status"] == "success": if "autopull" in c["commit_message"]: GlobalVars.charcoal_hq.send_message("[CI]({ci_link}) on {commit_sha} succeeded. Message contains 'autopull', pulling...".format(ci_link=c["ci_url"], commit_sha=sha)) time.sleep(2) os._exit(3) else: GlobalVars.charcoal_hq.send_message("[CI]({ci_link}) on {commit_sha} succeeded.".format(ci_link=c["ci_url"], commit_sha=sha)) elif c["status"] == "failure": GlobalVars.charcoal_hq.send_message("[CI]({ci_link}) on {commit_sha} failed.".format(ci_link=c["ci_url"], commit_sha=sha)) except Exception, e: GlobalVars.metasmoke_ws = websocket.create_connection(GlobalVars.metasmoke_ws_host, origin=GlobalVars.metasmoke_host) GlobalVars.metasmoke_ws.send(json.dumps({"command": "subscribe", "identifier": "{\"channel\":\"SmokeDetectorChannel\"}"})) print e try: exc_info = sys.exc_info() traceback.print_exception(*exc_info) except: print "meh" except: print "Couldn't bind to MS websocket"