Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Undo1 committed Oct 28, 2015
2 parents 37c954b + 2d32569 commit 1f902c4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
16 changes: 9 additions & 7 deletions chatcommunicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ def watcher(ev, wrap2):
ev_room = str(ev.data["room_id"])
ev_user_id = str(ev.data["user_id"])
content_source = ev.message.content_source
message_id = ev.message.id
if is_smokedetector_message(ev_user_id, ev_room):
GlobalVars.latest_smokedetector_messages[ev_room].append(ev.message.id)
GlobalVars.latest_smokedetector_messages[ev_room].append(message_id)
message_parts = content_source.split(" ")

ev_user_name = ev.data["user_name"].encode('utf-8')
Expand Down Expand Up @@ -81,7 +82,7 @@ def watcher(ev, wrap2):
reply += str(i + 1) + ". "
reply += "[" + current_message.split(" ")[0] + "] "
if current_message.split(" ")[1] != "-":
result = handle_commands(current_message.lower(), current_message.split(" "), ev_room, ev_user_id, ev_user_name, wrap2, current_message)
result = handle_commands(current_message.lower(), current_message.split(" "), ev_room, ev_user_id, ev_user_name, wrap2, current_message, message_id)
if result is not None:
reply += result + os.linesep
else:
Expand All @@ -97,12 +98,13 @@ def watcher(ev, wrap2):
if reply != "":
ev.message.reply(reply)
else:
r = handle_commands(content_source.lower(), message_parts, ev_room, ev_user_id, ev_user_name, wrap2, content_source)
r = handle_commands(content_source.lower(), message_parts, ev_room, ev_user_id, ev_user_name, wrap2, content_source, message_id)
if r is not None:
ev.message.reply(r)


def handle_commands(content_lower, message_parts, ev_room, ev_user_id, ev_user_name, wrap2, content):
def handle_commands(content_lower, message_parts, ev_room, ev_user_id, ev_user_name, wrap2, content, message_id):
message_url = "//chat." + wrap2.host + "/transcript/message/" + str(message_id)
second_part_lower = "" if len(message_parts) < 2 else message_parts[1].lower()
if re.compile("^:[0-9]+$").search(message_parts[0]):
msg_id = int(message_parts[0][1:])
Expand Down Expand Up @@ -169,7 +171,7 @@ def handle_commands(content_lower, message_parts, ev_room, ev_user_id, ev_user_n
if url_from_msg is not None:
user = get_user_from_url(url_from_msg)
if user is not None:
add_blacklisted_user(user)
add_blacklisted_user(user, message_url, post_url)
user_added = True
if post_type == "question":
learned = bayesian_learn_title(fetch_title_from_msg_content(msg_content), "bad")
Expand Down Expand Up @@ -210,7 +212,7 @@ def handle_commands(content_lower, message_parts, ev_room, ev_user_id, ev_user_n
and is_privileged(ev_room, ev_user_id, wrap2):
uid, val = get_user_from_list_command(content_lower)
if uid > -1 and val != "":
add_blacklisted_user((uid, val))
add_blacklisted_user((uid, val), message_url, "")
return "User blacklisted (`{}` on `{}`).".format(uid, val)
elif uid == -2:
return "Error: {}".format(val)
Expand Down Expand Up @@ -284,7 +286,7 @@ def handle_commands(content_lower, message_parts, ev_room, ev_user_id, ev_user_n
return "Could not find data for this post in the API. Check whether the post is not deleted yet."
user = get_user_from_url(post_data.owner_url)
if user is not None:
add_blacklisted_user(user)
add_blacklisted_user(user, message_url, post_data.post_url)
bayesian_learn_title(post_data.title, "bad")
handle_spam(post_data.title, post_data.body, post_data.owner_name, post_data.site, post_data.post_url,
post_data.owner_url, post_data.post_id, ["Manually reported " + post_data.post_type],
Expand Down
23 changes: 17 additions & 6 deletions datahandling.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ def is_whitelisted_user(user):


def is_blacklisted_user(user):
return user in GlobalVars.blacklisted_users
for blacklisted_user in GlobalVars.blacklisted_users:
if user == blacklisted_user[0]:
return True
return False


def get_blacklisted_user_data(user):
for blacklisted_user in GlobalVars.blacklisted_users:
if user == blacklisted_user[0]:
return blacklisted_user
return ()


def is_ignored_post(postid_site_tuple):
Expand Down Expand Up @@ -90,10 +100,10 @@ def add_whitelisted_user(user):
pickle.dump(GlobalVars.whitelisted_users, f)


def add_blacklisted_user(user):
if user in GlobalVars.blacklisted_users or user is None:
def add_blacklisted_user(user, message_url, post_url):
if is_blacklisted_user(user) or user is None:
return
GlobalVars.blacklisted_users.append(user)
GlobalVars.blacklisted_users.append((user, message_url, post_url))
with open("blacklistedUsers.txt", "w") as f:
pickle.dump(GlobalVars.blacklisted_users, f)

Expand Down Expand Up @@ -123,9 +133,10 @@ def add_ignored_post(postid_site_tuple):


def remove_blacklisted_user(user):
if user not in GlobalVars.blacklisted_users:
blacklisted_user_data = get_blacklisted_user_data(user)
if not blacklisted_user_data:
return False
GlobalVars.blacklisted_users.remove(user)
GlobalVars.blacklisted_users.remove(blacklisted_user_data)
with open("blacklistedUsers.txt", "w") as f:
pickle.dump(GlobalVars.blacklisted_users, f)
return True
Expand Down
8 changes: 8 additions & 0 deletions spamhandling.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def check_if_spam(title, body, user_name, user_url, post_site, post_id, is_answe
test, why = 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")
blacklisted_user_data = get_blacklisted_user_data(get_user_from_url(user_url))
if len(blacklisted_user_data) > 1:
message_url = blacklisted_user_data[1]
blacklisted_post_url = blacklisted_user_data[2]
if blacklisted_post_url:
why += u"Blacklisted user - blacklisted for {} by {}\n".format(blacklisted_post_url, message_url)
else:
why += u"Blacklisted user - blacklisted by {}\n".format(message_url)
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) \
Expand Down
2 changes: 1 addition & 1 deletion test/test_spamhandling.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_check_if_spam_json(data, match):
def test_blacklisted_user():
user_url = 'http://stackoverflow.com/users/1/jeff-atwood'
user = get_user_from_url(user_url)
add_blacklisted_user(user)
add_blacklisted_user(user, "", "")
is_spam, reason, _ = check_if_spam("", "", "", user_url, "stackoverflow.com", "1", False, False)
assert is_spam is True
# cleanup
Expand Down

0 comments on commit 1f902c4

Please sign in to comment.