Esempio n. 1
0
def perform_action(subreddit, item, condition, matchobj):
    """Performs the action for the condition(s).
    
    Also delivers the comment (if set) and creates an ActionLog entry.
    """
    
    global r
    disclaimer = ('\n\n*[I am a bot](http://www.reddit.com/r/AutoModerator/'
                    'comments/q11pu/what_is_automoderator/), and this action '
                    'was performed automatically. Please [contact the '
                    'moderators of this subreddit](http://www.reddit.com/'
                    'message/compose?to=%2Fr%2F'+item.subreddit.display_name+
                    ') if you have any questions or concerns.*')

    # build the comment if multiple conditions were matched
    if isinstance(condition, list):
        comment = ''
        if any([c.comment for c in condition]):
            for c in condition:
                if c.comment:
                    comment += '* '+c.comment+'\n'

        # bit of a hack and only logs and uses attributes from first
        # condition matched, should find a better method
        condition = condition[0]
        match = matchobj[0]
    else:
        comment = condition.comment
        match = matchobj

    # abort if it's an alert/report/removal/set_flair that's already triggered
    if condition.action in ['alert', 'report', 'remove', 'set_flair']:
        try:
            session.query(ActionLog).filter(
                and_(ActionLog.permalink == get_permalink(item),
                     ActionLog.matched_condition == condition.id)).one()
            return
        except NoResultFound:
            pass

    # perform replacements with match groups
    comment = replace_placeholders(comment, match)
    flair_text = replace_placeholders(condition.set_flair_text, match)
    flair_class = replace_placeholders(condition.set_flair_class, match)

    # perform the action
    if condition.action == 'remove':
        item.remove(condition.spam)
    elif condition.action == 'approve':
        item.approve()
    elif condition.action == 'set_flair':
        item.set_flair(flair_text, flair_class)
    elif condition.action == 'report':
        item.report()

    log_request(condition.action)

    if comment:
        # put together the comment parts for "public" comments
        if condition.comment_method in ['comment', 'message']:
            if subreddit.comment_header:
                comment = subreddit.comment_header+'\n\n'+comment
            if subreddit.comment_footer:
                comment = comment+'\n\n'+subreddit.comment_footer
            comment += disclaimer

        # deliver the comment
        if condition.comment_method == 'comment':
            post_comment(item, comment)
            log_request('comment')
            log_request('distinguish')
        elif condition.comment_method == 'modmail':
            permalink = get_permalink(item)
            if isinstance(item, praw.objects.Comment):
                permalink += '?context=5'
            r.send_message('/r/'+subreddit.name,
                          'AutoModerator condition matched',
                          permalink+'\n\n'+comment)
            log_request('modmail')
        elif condition.comment_method == 'message':
            if item.author:
                r.send_message(item.author.name,
                              'AutoModerator condition matched',
                              get_permalink(item)+'\n\n'+comment)
                log_request('message')

    # log the action taken
    action_log = ActionLog()
    action_log.subreddit_id = subreddit.id
    if item.author:
        action_log.user = item.author.name
    action_log.permalink = get_permalink(item)
    action_log.created_utc = datetime.utcfromtimestamp(item.created_utc)
    action_log.action_time = datetime.utcnow()
    action_log.action = condition.action
    action_log.matched_condition = condition.id

    if isinstance(item, praw.objects.Submission):
        action_log.title = item.title
        action_log.url = item.url
        action_log.domain = item.domain
        logging.info('  /r/%s: %s submission "%s"',
                        subreddit.name,
                        condition.action,
                        item.title.encode('ascii', 'ignore'))
    elif isinstance(item, praw.objects.Comment):
        if item.author:
            logging.info('  /r/%s: %s comment by user %s',
                            subreddit.name,
                            condition.action,
                            item.author.name)
        else:
            logging.info('  /r/%s: %s comment by deleted user',
                            subreddit.name,
                            condition.action)

    session.add(action_log)
    session.commit()
Esempio n. 2
0
def perform_action(subreddit, item, condition):
    """Performs the action for the condition(s).
    
    Also delivers the comment (if set) and creates an ActionLog entry.
    """
    
    global r
    disclaimer = ('\n\n*I am a bot, and this action was performed '
                    'automatically. Please [contact the moderators of this '
                    'subreddit](http://www.reddit.com/message/compose?'
                    'to=%23'+item.subreddit.display_name+') if you have any '
                    'questions or concerns.*')

    # build the comment if multiple conditions were matched
    if isinstance(condition, list):
        if any([c.comment for c in condition]):
            if condition[0].action == 'alert':
                verb = 'alerted'
            else:
                verb = condition[0].action+'d'

            comment = ('This has been '+verb+' for the following reasons:\n\n')
            for c in condition:
                if c.comment:
                    comment += '* '+c.comment+'\n'
            post_comment(item, comment)

        # bit of a hack and only logs and uses attributes from first
        # condition matched, should find a better method
        condition = condition[0]
    else:
        comment = condition.comment

    # abort if it's an alert and we've already alerted on this item
    if condition.action == 'alert':
        try:
            ActionLog.query.filter(
                and_(ActionLog.permalink == get_permalink(item),
                     ActionLog.action == 'alert')).one()
            return
        except NoResultFound:
            pass

    # perform the action
    if condition.action == 'remove':
        item.remove(condition.spam)
    elif condition.action == 'approve':
        item.approve()
    elif condition.action == 'set_flair':
        item.set_flair(condition.set_flair_text,
                       condition.set_flair_class)

    # deliver the comment if set
    if comment:
        if condition.comment_method == 'comment':
            post_comment(item, comment+disclaimer)
        elif condition.comment_method == 'modmail':
            r.compose_message('#'+subreddit.name,
                              'AutoModerator condition matched',
                              get_permalink(item)+'\n\n'+comment)
        elif condition.comment_method == 'message':
            r.compose_message(item.author.name,
                              'AutoModerator condition matched',
                              get_permalink(item)+'\n\n'+comment+disclaimer)

    # log the action taken
    action_log = ActionLog()
    action_log.subreddit_id = subreddit.id
    action_log.user = item.author.name
    action_log.permalink = get_permalink(item)
    action_log.created_utc = datetime.utcfromtimestamp(item.created_utc)
    action_log.action_time = datetime.utcnow()
    action_log.action = condition.action
    action_log.matched_condition = condition.id

    if isinstance(item, reddit.objects.Submission):
        action_log.title = item.title
        action_log.url = item.url
        action_log.domain = item.domain
        logging.info('  /r/%s: %s submission "%s"',
                        subreddit.name,
                        condition.action,
                        item.title.encode('ascii', 'ignore'))
    elif isinstance(item, reddit.objects.Comment):
        logging.info('  /r/%s: %s comment by user %s',
                        subreddit.name,
                        condition.action,
                        item.author.name)

    db.session.add(action_log)
    db.session.commit()
Esempio n. 3
0
def perform_action(subreddit, item, condition):
    """Performs the action for the condition(s) and creates an ActionLog entry."""
    global r

    # post the comment if one is set
    if isinstance(condition, list):
        if any([c.comment for c in condition]):
            comment = ('This has been '+condition[0].action+'d for the '
                       'following reasons:\n\n')
            for c in condition:
                if c.comment:
                    comment += '* '+c.comment+'\n'
            post_comment(item, comment)

        # bit of a hack and only logs first action matched
        # should find a better method
        condition = condition[0]
    elif condition.comment:
        post_comment(item, condition.comment)

    # perform the action
    if condition.action == 'remove':
        item.remove()
    elif condition.action == 'approve':
        item.approve()
    elif condition.action == 'alert':
        r.compose_message(
            '#'+subreddit.name,
            'Reported Item Alert',
            'The following item has received a large number of reports, '+
            'please investigate:\n\n'+item)

    # log the action taken
    action_log = ActionLog()
    action_log.subreddit_id = subreddit.id
    action_log.action_time = datetime.utcnow()
    action_log.action = condition.action

    if isinstance(item, str) or isinstance(item, unicode):
        # for report threshold alert, we only know permalink to item
        action_log.permalink = item
    else:
        action_log.user = item.author.name
        action_log.created_utc = datetime.utcfromtimestamp(item.created_utc)
        action_log.matched_condition = condition.id

    if isinstance(item, reddit.objects.Submission):
        action_log.title = item.title
        action_log.permalink = item.permalink
        action_log.url = item.url
        action_log.domain = item.domain
        logging.info('  /r/%s: %sd submission "%s"',
                        subreddit.name,
                        condition.action,
                        item.title.encode('ascii', 'ignore'))
    elif isinstance(item, reddit.objects.Comment):
        action_log.permalink = ('http://www.reddit.com/r/'+
                                item.subreddit.display_name+
                                '/comments/'+item.link_id.split('_')[1]+
                                '/a/'+item.id)
        logging.info('        %sd comment by user %s',
                        condition.action,
                        item.author.name)

    db.session.add(action_log)
    db.session.commit()
Esempio n. 4
0
def perform_action(subreddit, item, condition):
    """Performs the action for the condition(s).
    
    Also delivers the comment (if set) and creates an ActionLog entry.
    """

    global r
    comment = None
    disclaimer = ('\n\n*I am a bot, and this action was performed '
                  'automatically. Please [contact the moderators of this '
                  'subreddit](http://www.reddit.com/message/compose?'
                  'to=%23' + item.subreddit.display_name + ') if you have any '
                  'questions or concerns.*')

    # build the comment if multiple conditions were matched
    if isinstance(condition, list):
        # build short reason string for modporn submission later
        short_reason = ' '.join([
            '[' + c.short_reason + ']' for c in condition
            if c.short_reason is not None
        ])

        if any([c.comment for c in condition]):
            if condition[0].action == 'alert':
                verb = 'alerted'
            else:
                verb = condition[0].action + 'd'

            comment = ('Unfortunately, your submission has been ' + verb +
                       ' for the following reasons:\n\n')
            for c in condition:
                if c.comment:
                    comment += '* ' + c.comment + '\n'
            comment += ('\nFor more information regarding these issues please '
                        ' [see the FAQ](http://www.reddit.com/r/' +
                        item.subreddit.display_name + '/faq), and feel '
                        'free to resubmit once they have been resolved. '
                        'Thank you!')

        # bit of a hack and only logs and uses attributes from first
        # condition matched, should find a better method
        condition = condition[0]
    else:
        short_reason = condition.short_reason
        comment = condition.comment

    # abort if it's an alert and we've already alerted on this item
    if condition.action == 'alert':
        try:
            ActionLog.query.filter(
                and_(ActionLog.permalink == get_permalink(item),
                     ActionLog.action == 'alert')).one()
            return
        except NoResultFound:
            pass

    # perform the action
    if condition.action == 'remove':
        item.remove(condition.spam)
    elif condition.action == 'approve':
        item.approve()
    elif condition.action == 'set_flair':
        item.set_flair(condition.set_flair_text, condition.set_flair_class)

    # deliver the comment if set
    if comment:
        if condition.comment_method == 'comment':
            post_comment(item, comment + disclaimer)
        elif condition.comment_method == 'modmail':
            r.compose_message('#' + subreddit.name,
                              'AutoModerator condition matched',
                              get_permalink(item) + '\n\n' + comment)
        elif condition.comment_method == 'message':
            r.compose_message(
                item.author.name, 'AutoModerator condition matched',
                get_permalink(item) + '\n\n' + comment + disclaimer)

    # log the action taken
    action_log = ActionLog()
    action_log.subreddit_id = subreddit.id
    action_log.user = item.author.name
    action_log.permalink = get_permalink(item)
    action_log.created_utc = datetime.utcfromtimestamp(item.created_utc)
    action_log.action_time = datetime.utcnow()
    action_log.action = condition.action
    action_log.matched_condition = condition.id

    if isinstance(item, reddit.objects.Submission):
        action_log.title = item.title
        action_log.url = item.url
        action_log.domain = item.domain
        #         logging.info('  /r/%s: %s submission "%s"',
        #                         subreddit.name,
        #                         condition.action,
        #                         item.title.encode('ascii', 'ignore'))
        logging.info('  /r/%s: %s submission "%s"', subreddit.name,
                     condition.action, item.title)
    elif isinstance(item, reddit.objects.Comment):
        logging.info('  /r/%s: %s comment by user %s', subreddit.name,
                     condition.action, item.author.name)

    db.session.add(action_log)
    db.session.commit()

    # SFWPorn network moderation requirements
    # NO LONGER NEEDED
    #     if (isinstance(item, reddit.objects.Submission) and
    #             condition.action == 'remove' and
    #             short_reason is not None):
    #         # submit to ModerationPorn
    #         r.submit('ModerationPorn',
    #                 '['+subreddit.name+'] '+
    #                 '['+item.author.name+'] '+
    #                 item.title+
    #                 ' '+short_reason,
    #                 url=item.permalink)

    if (isinstance(item, reddit.objects.Submission)
            and condition.action == 'remove' and comment):
        # send them a PM as well
        r.compose_message(item.author.name, 'SFWPorn submission removed',
                          get_permalink(item) + '\n\n' + comment + disclaimer)