Esempio n. 1
0
def add_ban(ban: BanModel) -> BanModel:
    if ban.length > MAX_BAN_TIME:
        raise ArgumentError(MESSAGE_BAN_TOO_LONG)

    if ban.ip4_end is not None and ban.ip4_end <= ban.ip4:
        raise ArgumentError(MESSAGE_IP4_ILLEGAL_RANGE)

    if ban.board:
        board = board_service.find_board(ban.board)
        if not board:
            raise ArgumentError(MESSAGE_BOARD_NOT_FOUND)

    if ban.reason and len(ban.reason) > MAX_REASON_LENGTH:
        raise ArgumentError(MESSAGE_BAN_TEXT_TOO_LONG)

    ban.date = now()

    ban = bans.create_ban(ban)

    for_board_text = ' on {}'.format(ban.board) if ban.board else ''
    ip4_end_text = ip4_to_str(ban.ip4_end) if ban.ip4_end is not None else '-'
    f = 'ban add {} from {} to {}{} for {} hours reason {}'
    text = f.format(ban.id, ip4_to_str(ban.ip4), ip4_end_text, for_board_text,
                    ban.length / 60 / 60 / 1000, ban.reason)
    mod_log(text)

    return ban
Esempio n. 2
0
def add_ban(ban: BanModel) -> BanModel:
    if ban.length > MAX_BAN_TIME:
        raise ArgumentError(MESSAGE_BAN_TOO_LONG)

    if ban.ip4_end is not None and ban.ip4_end <= ban.ip4:
        raise ArgumentError(MESSAGE_IP4_ILLEGAL_RANGE)

    if ban.board:
        board = board_service.find_board(ban.board)
        if not board:
            raise ArgumentError(MESSAGE_BOARD_NOT_FOUND)

    if ban.reason and len(ban.reason) > MAX_REASON_LENGTH:
        raise ArgumentError(MESSAGE_BAN_TEXT_TOO_LONG)

    ban.date = now()

    ban = bans.create_ban(ban)

    for_board_text = ' on {}'.format(ban.board) if ban.board else ''
    ip4_end_text = ip4_to_str(ban.ip4_end) if ban.ip4_end is not None else '-'
    f = 'ban add {} from {} to {}{} for {} hours reason {}'
    text = f.format(ban.id, ip4_to_str(ban.ip4), ip4_end_text, for_board_text, ban.length / 60 / 60 / 1000, ban.reason)
    mod_log(text)

    return ban
Esempio n. 3
0
def _manage_delete(details: ManagePostDetails, moderator: ModeratorModel, post: PostModel):
    try:
        action_authorizer.authorize_post_action(moderator, PostAction.POST_DELETE, post, details)

        message = 'post {} delete'.format(details.post_id)
        mod_log(message, ip4_str=ip4_to_str(details.ip4), moderator=moderator)

        posts.delete_post(post)
    except NoPermissionError as e:
        message = 'post {} delete failed, {}'.format(details.post_id, str(e))
        mod_log(message, ip4_str=ip4_to_str(details.ip4), moderator=moderator)

        raise BadRequestError(MESSAGE_DELETE_NO_PERMISSION)
Esempio n. 4
0
def _manage_locked_toggle(thread: ThreadModel, details: ManagePostDetails, moderator: ModeratorModel):
    action_authorizer.authorize_post_action(moderator, PostAction.THREAD_LOCKED_TOGGLE, board=thread.board)

    posts.update_thread_locked(thread, not thread.locked)

    message = 'lock on /{}/{} {}'.format(thread.board.name, thread.id, 'disabled' if thread.locked else 'enabled')
    mod_log(message, ip4_str=ip4_to_str(details.ip4), moderator=moderator)
Esempio n. 5
0
def _manage_report(details: ManagePostDetails, moderator: ModeratorModel, post: PostModel):
    action_authorizer.authorize_post_action(moderator, PostAction.POST_REPORT, post, details)

    report_service.report_post(post)

    message = 'post {} reported'.format(post.id)
    mod_log(message, ip4_str=ip4_to_str(details.ip4), moderator=moderator)
Esempio n. 6
0
def mod_bans():
    ban_messages = []

    if request.method == 'POST':
        ban_form = BanForm(request.form)
        if ban_form.validate():
            ip4 = parse_ip4(ban_form.ban_ip4.data)
            ip4_end_form = ban_form.ban_ip4_end.data
            ip4_end = parse_ip4(ip4_end_form) if ip4_end_form else None

            ban = BanModel()
            ban.ip4 = ip4
            if ip4_end is not None:
                ban.ip4_end = ip4_end
            ban.reason = ban_form.reason.data
            board_form = ban_form.board.data
            ban.board = board_form if board_form else None
            ban.length = ban_form.duration.data * 60 * 60 * 1000

            try:
                ban_service.add_ban(ban)
                ban_messages.append('Ban added')
            except ArgumentError as e:
                ban_messages.append(e.message)
    else:
        # Searches for the ip4 of the post and fills it in if for_post was set to a post id
        filled_in_ip4 = ''
        for_post_id = request.args.get('for_post', None)
        if for_post_id:
            post = posts_service.find_post(for_post_id)
            if post:
                filled_in_ip4 = ip4_to_str(post.ip4)
        ban_form = BanForm(None, ban_ip4=filled_in_ip4)

    return render_template('mod_bans.html', ban_messages=ban_messages, ban_form=ban_form, paged_bans=PagedBans())
Esempio n. 7
0
def _log_post(post_details: PostDetails, result: PostResultModel, insert_time, cache_time):
    total = insert_time + cache_time + post_details.file_time
    file_time_str = 'file: {}ms, '.format(post_details.file_time) if post_details.file_time else ''
    s = '{}db: {}ms, caches: {}ms, total: {}ms'
    timings = s.format(file_time_str, insert_time, cache_time, total)
    post_type = 'thread' if result.post_refno == 1 else 'reply'
    log = 'new {} /{}/{}#{} ({})'.format(post_type, result.board_name, result.thread_refno, result.post_refno, timings)
    mod_log(log, ip4_str=ip4_to_str(post_details.ip4))
Esempio n. 8
0
    def row(self, ban: BanOrmModel):
        if ban.length > 0:
            expire_time = ban.date + ban.length
            until = formatted_time(expire_time) + ' - '
            if expire_time - now() < 0:
                until += 'Expired, not viewed'
            else:
                until += time_remaining(expire_time) + ' remaining'
        else:
            until = 'Does not expire'

        delete_button = '<button class="confirm-button" name="ban_id" value="' + str(
            ban.id) + '">Lift ban</button>'

        return (ip4_to_str(ban.ip4),
                ip4_to_str(ban.ip4_end) if ban.ip4_end is not None else '',
                formatted_time(ban.date), until, ban.board
                or '', ban.reason, Markup(delete_button))
Esempio n. 9
0
def _log_post(post_details: PostDetails, result: PostResultModel, insert_time,
              cache_time):
    total = insert_time + cache_time + post_details.file_time
    file_time_str = 'file: {}ms, '.format(
        post_details.file_time) if post_details.file_time else ''
    s = '{}db: {}ms, caches: {}ms, total: {}ms'
    timings = s.format(file_time_str, insert_time, cache_time, total)
    post_type = 'thread' if result.post_refno == 1 else 'reply'
    log = 'new {} /{}/{}#{} ({})'.format(post_type, result.board_name,
                                         result.thread_refno,
                                         result.post_refno, timings)
    mod_log(log, ip4_str=ip4_to_str(post_details.ip4))
Esempio n. 10
0
    def row(self, ban: BanOrmModel):
        if ban.length > 0:
            expire_time = ban.date + ban.length
            until = formatted_time(expire_time) + ' - '
            if expire_time - now() < 0:
                until += 'Expired, not viewed'
            else:
                until += time_remaining(expire_time) + ' remaining'
        else:
            until = 'Does not expire'

        delete_button = '<button class="confirm-button" name="ban_id" value="' + str(ban.id) + '">Lift ban</button>'

        return (
            ip4_to_str(ban.ip4),
            ip4_to_str(ban.ip4_end) if ban.ip4_end is not None else '',
            formatted_time(ban.date),
            until,
            ban.board or '',
            ban.reason,
            Markup(delete_button)
        )
Esempio n. 11
0
def mod_bans():
    ban_messages = []

    if request.method == 'POST':
        ban_form = BanForm(request.form)
        if ban_form.validate():
            ip4 = parse_ip4(ban_form.ban_ip4.data)
            ip4_end_form = ban_form.ban_ip4_end.data
            ip4_end = parse_ip4(ip4_end_form) if ip4_end_form else None

            ban = BanModel()
            ban.ip4 = ip4
            if ip4_end is not None:
                ban.ip4_end = ip4_end
            ban.reason = ban_form.reason.data
            board_form = ban_form.board.data
            ban.board = board_form if board_form else None
            ban.length = ban_form.duration.data * 60 * 60 * 1000

            try:
                ban_service.add_ban(ban)
                ban_messages.append('Ban added')
            except ArgumentError as e:
                ban_messages.append(e.message)
    else:
        # Searches for the ip4 of the post and fills it in if for_post was set to a post id
        filled_in_ip4 = ''
        for_post_id = request.args.get('for_post', None)
        if for_post_id:
            post = posts_service.find_post(for_post_id)
            if post:
                filled_in_ip4 = ip4_to_str(post.ip4)
        ban_form = BanForm(None, ban_ip4=filled_in_ip4)

    return render_template('mod_bans.html',
                           ban_messages=ban_messages,
                           ban_form=ban_form,
                           paged_bans=PagedBans())