def _handle_comment(comment, extra_markers=frozenset()):
    logging.info("Handling comment " + comment.id)

    if (comment not in CHECKED_COMMENTS) or ("force" in extra_markers):
        markers = parse_context_markers(comment.body)
        markers |= extra_markers
        if "ignore" in markers:
            return
        else:
            logging.info("Found new comment: " + comment.id)
        r.use_oauth = False
        submission = comment.submission
        submission.refresh()
        submission_markers = parse_context_markers(submission.selftext)
        if "disable" in submission_markers:
            return

        if MOD_COMMANDS.handle_moderation(comment, markers):
            make_reply(comment.body, comment.id, comment.reply, markers)
Beispiel #2
0
def _handle_comment(comment, extra_markers=frozenset()):
    logging.info("Handling comment " + comment.id)

    if (comment not in CHECKED_COMMENTS) or ("force" in extra_markers):
        markers = parse_context_markers(comment.body)
        markers |= extra_markers
        if "ignore" in markers:
            return
        else:
            logging.info("Found new comment: " + comment.id)
        r.use_oauth = False
        submission = comment.submission
        submission.refresh()
        submission_markers = parse_context_markers(submission.selftext)
        if "disable" in submission_markers:
            return

        if MOD_COMMANDS.handle_moderation(comment, markers):
            make_reply(comment.body, comment.id, comment.reply, markers)
Beispiel #3
0
def handle_message(message, markers=set()):
    global COUNT_REPLIES, TIME_SINCE_RESET, TIME_TO_RESET, COUNT_REPLIES_LIMIT
    """What we're using to handle direct messages."""

    logging.info("Handling new message: {0}".format(message.id))

    markers |= parse_context_markers(message.body)
    # Mark message as read here so we don't loop over it in case of error.
    message.mark_read()

    # Check for message validity.
    try:
        if message.submission is not None:
            logging.info("Parsing message belonging to a submission!")
            return
    except AttributeError:
        pass

    # If enough time has elapsed, reset COUNT_REPLIES to an empty dict.
    if time.time() - TIME_SINCE_RESET >= TIME_TO_RESET:
        COUNT_REPLIES = {}

    # Count the number of requests in the body of the message, of format link...(...;...;...)
    request_count = message.body.count('link') + message.body.count(';')
    body = message.body

    sub_recs = None
    if 'linksub(' in body:
        sub_recs = get_submission_recommendations(body)
        markers.add('slim')

    # If the message author can not be found in the dict, add them.
    COUNT_REPLIES.setdefault(message.author.name, request_count)

    # Print a summary of the user's statistics.
    logging.info("{0} has requested {1} fics with {2} remaining requests for the next {3} seconds.".format(
        message.author.name, request_count, COUNT_REPLIES_LIMIT - COUNT_REPLIES[message.author.name],
                                            TIME_TO_RESET - (time.time() - TIME_SINCE_RESET)))

    # Block the request if the user has exceeded their quota of replies.
    if COUNT_REPLIES[message.author.name] + request_count > COUNT_REPLIES_LIMIT:
        logging.error("{0} has exceeded their available replies.", message.author.name)
        return

    # Otherwise, add the number of requests to the user's total number of requests.
    COUNT_REPLIES[message.author.name] += request_count

    # Print the current state of COUNT_REPLIES.
    logging.debug("The current state of DM requests: {0}".format(COUNT_REPLIES))

    # Make the reply and return.
    make_reply(body, message, markers=markers, sub_recs=sub_recs)
    return
Beispiel #4
0
def parse_submission_text(submission, markers):
    body = submission.selftext

    markers |= parse_context_markers(body)

    additions = []

    sub_recs = None
    if 'linksub(' in body:
        sub_recs = get_submission_recommendations(body)
        markers.add('slim')

    make_reply(
        body, submission,
        markers, additions, sub_recs=sub_recs)
Beispiel #5
0
def parse_submission_text(submission, extra_markers=frozenset()):
    body = submission.selftext

    markers = parse_context_markers(body)
    markers |= extra_markers

    # Since the bot would start downloading the stories
    # here, we add the ignore option here
    if "ignore" in markers:
        return

    additions = []
    if "submissionlink" in markers:
        additions.extend(get_direct_links(submission.url, markers))

    make_reply(submission.selftext, submission.id, submission.add_comment,
               markers, additions)
Beispiel #6
0
def parse_submission_text(submission, extra_markers=frozenset()):
    body = submission.selftext

    markers = parse_context_markers(body)
    markers |= extra_markers

    # Since the bot would start downloading the stories
    # here, we add the ignore option here
    if "ignore" in markers:
        return

    additions = []
    if "submissionlink" in markers:
        additions.extend(get_direct_links(submission.url, markers))

    make_reply(
        submission.selftext, submission.id, submission.add_comment,
        markers, additions)
Beispiel #7
0
def handle_comment(comment, extra_markers=frozenset()):
    logging.debug("Handling comment: " + comment.id)
    if (str(comment.id) not in CHECKED_COMMENTS
       ) or ("force" in extra_markers):

        logging.info("Found new comment: " + comment.id)
        markers = parse_context_markers(comment.body)
        markers |= extra_markers
        if "ignore" in markers:
            logging.info("Comment forcefully ignored: " + comment.id)
            return

        if "parent" in markers:
            if comment.is_root:
                item = comment.submission
            else:
                item = r.get_info(thing_id=comment.parent_id)
            handle(item, {"directlinks", "submissionlink", "force"})

        try:
            make_reply(comment.body, comment.id, comment.reply, markers)
        finally:
            CHECKED_COMMENTS.add(str(comment.id))
Beispiel #8
0
def handle_comment(comment, extra_markers=set()):
    # comment.permalink
    logging.info("Handling new comment: {0}".format(comment.permalink))

    markers = parse_context_markers(comment.body)
    markers |= extra_markers

    if "ignore" in markers:
        logging.info("Ignoring {0}".format(comment.id))
        return

    if "parent" in markers or "refresh" in markers:
        refresh_handler(comment)

    body = comment.body
    sub_recs = None
    if 'linksub(' in body:
        sub_recs = get_submission_recommendations(body)
        markers.add('slim')

    try:
        make_reply(body, comment, markers, sub_recs=sub_recs)
    except Exception as e:
        logging.error(e)
Beispiel #9
0
def handle_comment(comment, extra_markers=frozenset()):
    logging.debug("Handling comment: " + comment.id)
    if (str(comment.id) not in CHECKED_COMMENTS
            ) or ("force" in extra_markers):

        markers = parse_context_markers(comment.body)
        markers |= extra_markers
        if "ignore" in markers:
            # logging.info("Comment forcefully ignored: " + comment.id)
            return
        else:
            logging.info("Found new comment: " + comment.id)

        if "parent" in markers:
            if comment.is_root:
                item = comment.submission
            else:
                item = r.get_info(thing_id=comment.parent_id)
            handle(item, {"directlinks", "submissionlink", "force"})

        if "delete" in markers and (comment.id not in CHECKED_COMMENTS):
            CHECKED_COMMENTS.add(str(comment.id))
            logging.info("Delete requested by " + comment.id)
            if not (comment.is_root):
                parent_comment = r.get_info(thing_id=comment.parent_id)
                if parent_comment.author is not None:
                    if (parent_comment.author.name == "FanfictionBot"):
                        logging.info("Deleting comment " + parent_comment.id)
                        parent_comment.delete()
                    else:
                        logging.error("Delete requested on non-bot comment!")
                else:
                    logging.error("Delete requested on null comment.")
            else:
                logging.error("Delete requested by invalid comment!")

        if "refresh" in markers and (str(comment.id) not in CHECKED_COMMENTS):
            CHECKED_COMMENTS.add(str(comment.id))
            logging.info("(Refresh) Refresh requested by " + comment.id)

            # Get the full comment or submission
            comment_with_requests = get_full(comment.parent_id)
            logging.info("(Refresh) Refreshing on " + type(
                comment_with_requests).__name__ + " with id " + comment_with_requests.id)

            # TODO: Make it so FanfictionBot does not have to be hardcoded
            # If ffnbot!refresh is called on an actual bot reply, then go up
            # one level to find the requesting comment
            if comment_with_requests.author.name == "FanfictionBot":
                logging.info(
                    "(Refresh) Refresh requested on a bot comment (" + comment_with_requests.id + ").")
                # Retrieve the requesting parent submission or comment
                comment_with_requests = get_full(
                    comment_with_requests.parent_id)

                # If the requesting comment has been deleted, abort
                if not valid_comment(comment_with_requests):
                    logging.error(
                        "(Refresh) Parent of bot comment is invalid.")
                    return

                logging.info(
                    "          Refresh request being pushed to parent " + comment_with_requests.id)

            if isinstance(comment_with_requests, praw.objects.Comment):
                logging.info(
                    "(Refresh) Running refresh on COMMENT " + str(comment_with_requests.id))
                logging.info("(Refresh) Appending replies to deletion check list: " +
                             ", ".join(str(c.id) for c in comment_with_requests.replies))
                delete_list = comment_with_requests.replies

            elif isinstance(comment_with_requests, praw.objects.Submission):
                logging.info(
                    "(Refresh) Running refresh on SUBMISSION " + str(comment_with_requests.id))

                unfiltered_delete_list = comment_with_requests.comments
                delete_list = []
                for comment in unfiltered_delete_list:
                    if comment.author is not None:
                        if (comment.author.name == "FanfictionBot"):
                            delete_list.append(comment)
                            print("(Refresh) Found root-level bot comment " + comment.id)
            else:
                logging.error("(Refresh) Can't refresh " + comment_with_requests.type(
                ).__name__ + " with ID " + comment_with_requests.id)
                bot_tools.pause(5, 0)
                return

            if delete_list is not None:
                logging.info("(Refresh) Finding replies to delete.")
                for reply in delete_list:
                    if valid_comment(reply):
                        if (reply.author.name == "FanfictionBot"):
                            logging.error(
                                "(Refresh) Deleting bot comment " + reply.id)
                            reply.delete()
            else:
                logging.info(
                    "(Refresh) No bot replies have been made. Continuing...")
            CHECKED_COMMENTS.add(str(comment.id))

            if isinstance(comment_with_requests, praw.objects.Comment):
                logging.info(
                    "(Refresh) Re-handling comment " + comment_with_requests.id)
                handle_comment(comment_with_requests, frozenset(["force"]))
            elif isinstance(comment_with_requests, praw.objects.Submission):
                logging.info(
                    "(Refresh) Re-handling submission " + comment_with_requests.id)
                handle_submission(comment_with_requests, frozenset(["force"]))
            return

        try:
            make_reply(comment.body, comment.id, comment.reply, markers)
        finally:
            CHECKED_COMMENTS.add(str(comment.id))