Skip to content

Commit

Permalink
Fix and unify post title markdown link sanitizer
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteCommander committed Apr 19, 2018
1 parent c5fbde6 commit d7f8b34
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion chatcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ def checkpost(msg, url, alias_used='scan'): # FIXME: Currently does not support
handle_spam(post=post, reasons=reasons, why=why + "\nManually triggered scan")
return None

return "Post [{}]({}) does not look like spam.".format(post_data.title, url)
return "Post [{}]({}) does not look like spam.".format(sanitize_title(post_data.title), url)


# noinspection PyIncorrectDocstring,PyUnusedLocal
Expand Down
28 changes: 14 additions & 14 deletions classes/_Post.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ class PostParseError(Exception):


class Post:
_body = ""
_body_is_summary = False
_is_answer = False
_owner_rep = 1
_parent = None # If this is not none, then _is_answer should be 'true' because there would then be a parent post.
_post_id = ""
_post_score = 0
_post_site = ""
_post_url = ""
_title = ""
_user_name = ""
_user_url = ""
_votes = {'downvotes': None, 'upvotes': None}

def __init__(self, json_data=None, api_response=None, parent=None):
# type: (AnyStr, dict, Post) -> None

self._body = ""
self._body_is_summary = False
self._is_answer = False
self._owner_rep = 1
self._parent = None # If not None, _is_answer should be 'true' because there would then be a parent post.
self._post_id = ""
self._post_score = 0
self._post_site = ""
self._post_url = ""
self._title = ""
self._user_name = ""
self._user_url = ""
self._votes = {'downvotes': None, 'upvotes': None}

if parent is not None:
if not isinstance(parent, Post):
raise TypeError("Parent object for a Post object must also be a Post object.")
Expand Down
3 changes: 3 additions & 0 deletions parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def unescape_title(title_escaped):
def escape_special_chars_in_title(title_unescaped):
return regex.sub(r"([_*\\`\[\]])", r"\\\1", title_unescaped)

# noinspection PyMissingTypeHints
def sanitize_title(title_unescaped):
return regex.sub('(https?://|\n)', '', escape_special_chars_in_title(title_unescaped).replace('\n', u'\u23CE'))

# noinspection PyMissingTypeHints
def get_user_from_list_command(cmd): # for example, !!/addblu is a list command
Expand Down
22 changes: 7 additions & 15 deletions spamhandling.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,11 @@ def handle_spam(post, reasons, why):
if post.is_answer and post.post_id is not None and post.post_id is not "":
datahandling.add_post_site_id_link((post.post_id, post.post_site, "answer"), post.parent.post_id)
try:
post._title = parsing.escape_special_chars_in_title(post.title)
if post.is_answer:
# If the post is an answer type post, the 'title' is going to be blank, so when posting the
# message contents we need to set the post title to the *parent* title, so the message in the
# chat is properly constructed with parent title instead. This will make things 'print'
# in a proper way in chat messages.
sanitized_title = regex.sub('(https?://|\n)', '', post.parent.title)
else:
sanitized_title = regex.sub('(https?://|\n)', '', post.title)

sanitized_title = regex.sub(r'([\]*`])', r'\\\1', sanitized_title).replace('\n', u'\u23CE')
# If the post is an answer type post, the 'title' is going to be blank, so when posting the
# message contents we need to set the post title to the *parent* title, so the message in the
# chat is properly constructed with parent title instead. This will make things 'print'
# in a proper way in chat messages.
sanitized_title = parsing.sanitize_title(post.title if not post.is_answer else post.parent.title)

prefix = u"[ [SmokeDetector](//goo.gl/eLDYqh) ]"
if GlobalVars.metasmoke_key:
Expand All @@ -111,11 +105,10 @@ def handle_spam(post, reasons, why):

# We'll insert reason list later
if not post.user_name.strip() or (not poster_url or poster_url.strip() == ""):
s = u" {{}}: [{}]({}) by a deleted user on `{}`".format(sanitized_title.strip(), post_url,
shortened_site)
s = u" {{}}: [{}]({}) by a deleted user on `{}`".format(sanitized_title, post_url, shortened_site)
username = ""
else:
s = u" {{}}: [{}]({}) by [{}]({}) on `{}`".format(sanitized_title.strip(), post_url,
s = u" {{}}: [{}]({}) by [{}]({}) on `{}`".format(sanitized_title, post_url,
post.user_name.strip(), poster_url, shortened_site)
username = post.user_name.strip()

Expand All @@ -125,7 +118,6 @@ def handle_spam(post, reasons, why):
post.up_vote_count, post.down_vote_count)

log('debug', GlobalVars.parser.unescape(s).encode('ascii', errors='replace'))
datahandling.append_to_latest_questions(post.post_site, post.post_id, post.title)
GlobalVars.deletion_watcher.subscribe(post_url)

for reason_count in range(5, 2, -1): # Try 5 reasons, then 4, then 3
Expand Down

0 comments on commit d7f8b34

Please sign in to comment.