def accept(msg: catbot.Message):
    operator = bot.get_chat_member(config['group'], msg.from_.id)
    if not (operator.status == 'creator' or operator.status == 'administrator'):
        return

    if msg.reply:
        accepted_id = msg.reply_to_message.from_.id
    else:
        user_input_token = msg.text.split()
        if len(user_input_token) < 2:
            bot.send_message(msg.chat.id, text=config['messages']['general_prompt'], reply_to_message_id=msg.id)
            return
        try:
            accepted_id = int(user_input_token[1])
        except ValueError:
            bot.send_message(msg.chat.id, text=config['messages']['telegram_id_error'], reply_to_message_id=msg.id)
            return

    with t_lock:
        ac_list, rec = bot.secure_record_fetch('ac', list)
        for i in range(len(ac_list)):
            entry = Ac.from_dict(ac_list[i])
            if entry.telegram_id == accepted_id:
                accepted_index = i
                break
        else:
            entry = Ac(accepted_id)
            ac_list.append(entry)
            accepted_index = -1
        entry.refused = False
        ac_list[accepted_index] = entry.to_dict()
        rec['ac'] = ac_list
        json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False)

    log(config['messages']['accept_log'].format(tg_id=accepted_id, acceptor=html_refer(operator.name)))
def refuse(msg: catbot.Message):
    try:
        operator = bot.get_chat_member(config['group'], msg.from_.id)
    except catbot.UserNotFoundError:
        return
    if not (operator.status == 'creator' or operator.status == 'administrator'):
        return

    if msg.reply:
        refused_id = msg.reply_to_message.from_.id
    else:
        user_input_token = msg.text.split()
        if len(user_input_token) < 2:
            bot.send_message(msg.chat.id, text=config['messages']['general_prompt'], reply_to_message_id=msg.id)
            return
        try:
            refused_id = int(user_input_token[1])
        except ValueError:
            bot.send_message(msg.chat.id, text=config['messages']['telegram_id_error'], reply_to_message_id=msg.id)
            return

    try:
        refused_user = bot.get_chat_member(config['group'], refused_id)
    except catbot.UserNotFoundError:
        restricted_until = 0
    else:
        if refused_user.status == 'restricted':
            restricted_until = refused_user.until_date
            if restricted_until == 0:
                restricted_until = -1
        else:
            restricted_until = 0

    with t_lock:
        ac_list, rec = bot.secure_record_fetch('ac', list)
        for i in range(len(ac_list)):
            entry = Ac.from_dict(ac_list[i])
            if entry.telegram_id == refused_id:
                refused_index = i
                break
        else:
            entry = Ac(refused_id)
            ac_list.append(entry)
            refused_index = -1

        if restricted_until != -1:
            entry.restricted_until = restricted_until
        entry.confirmed = False
        entry.confirming = False
        entry.refused = True
        ac_list[refused_index] = entry.to_dict()
        rec['ac'] = ac_list
        json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False)

    log(config['messages']['refuse_log'].format(tg_id=refused_id, refuser=html_refer(operator.name)))

    silence_trial(entry)
def new_member(msg: catbot.ChatMemberUpdate):
    if msg.new_chat_member.status == 'restricted':
        restricted_until = msg.new_chat_member.until_date
        if restricted_until == 0:
            restricted_until = -1  # Restricted by bot, keep entry.restricted_until unchanged later
    elif msg.new_chat_member.status == 'creator' or \
            msg.new_chat_member.status == 'administrator' or \
            msg.new_chat_member.status == 'kicked':
        return
    else:
        restricted_until = 0

    try:
        bot.silence_chat_member(config['group'], msg.new_chat_member.id)
    except catbot.InsufficientRightError:
        bot.send_message(config['group'], text=config['messages']['insufficient_right'])
        return

    with t_lock:
        ac_list, rec = bot.secure_record_fetch('ac', list)
        for i in range(len(ac_list)):
            entry = Ac.from_dict(ac_list[i])
            if entry.telegram_id == msg.new_chat_member.id:
                user_index = i
                break
        else:
            entry = Ac(msg.from_.id)
            ac_list.append(entry)
            user_index = -1

        if restricted_until != -1:
            entry.restricted_until = restricted_until
        ac_list[user_index] = entry.to_dict()
        rec['ac'] = ac_list
        json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False)

    if entry.confirmed or entry.whitelist_reason:
        lift_restriction_trial(entry, config['group'])
    else:
        with t_lock:
            last_id, rec = bot.secure_record_fetch('last_welcome', int)
            cur = bot.send_message(config['group'],
                                   text=config['messages']['new_member_hint'].format(
                                       tg_id=msg.new_chat_member.id,
                                       tg_name=html_refer(msg.new_chat_member.name)),
                                   parse_mode='HTML')
            rec['last_welcome'] = cur.id
            json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False)
            try:
                bot.delete_message(config['group'], last_id)
            except catbot.DeleteMessageError:
                pass
def confirm(msg: catbot.Message):
    with t_lock:
        ac_list, rec = bot.secure_record_fetch('ac', list)

        entry_index = -1
        for i in range(len(ac_list)):
            entry = Ac.from_dict(ac_list[i])
            if entry.telegram_id == msg.from_.id:
                if entry.confirmed:
                    bot.send_message(msg.chat.id, text=config['messages']['confirm_already'].format(
                        wp_name=get_mw_username(entry.mw_id)
                    ))
                    return
                elif entry.confirming:
                    bot.send_message(msg.chat.id, text=config['messages']['confirm_confirming'])
                    return
                elif entry.refused:
                    bot.send_message(msg.chat.id, text=config['messages']['confirm_ineligible'])
                    return
                else:
                    entry_index = i

        else:
            if entry_index == -1:
                entry = Ac(msg.from_.id)
                ac_list.append(entry.to_dict())
            entry = Ac.from_dict(ac_list[entry_index])
            entry.confirming = True
            ac_list[entry_index] = entry.to_dict()

        rec['ac'] = ac_list
        json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False)

    button = catbot.InlineKeyboardButton(config['messages']['confirm_button'], callback_data=f'confirm')
    keyboard = catbot.InlineKeyboard([[button]])
    bot.send_message(msg.chat.id, text=config['messages']['confirm_wait'].format(
        link=config['oauth_auth_url'].format(telegram_id=msg.from_.id),
    ),
                     parse_mode='HTML', disable_web_page_preview=True, reply_markup=keyboard)
def add_whitelist(msg: catbot.Message):
    adder = bot.get_chat_member(config['group'], msg.from_.id)
    if not (adder.status == 'creator' or adder.status == 'administrator'):
        return

    user_input_token = msg.text.split()
    if msg.reply:
        whitelist_id = msg.reply_to_message.from_.id
        if len(user_input_token) > 1:
            reason = ' '.join(user_input_token[1:])
        else:
            reason = 'whitelisted'
    else:
        if len(user_input_token) < 2:
            bot.send_message(msg.chat.id, text=config['messages']['add_whitelist_prompt'], reply_to_message_id=msg.id)
            return
        try:
            whitelist_id = int(user_input_token[1])
        except ValueError:
            bot.send_message(msg.chat.id, text=config['messages']['telegram_id_error'], reply_to_message_id=msg.id)
            return
        if len(user_input_token) > 2:
            reason = ' '.join(user_input_token[2:])
        else:
            reason = 'whitelisted'

    with t_lock:
        ac_list, rec = bot.secure_record_fetch('ac', list)
        for i in range(len(ac_list)):
            entry = Ac.from_dict(ac_list[i])
            if entry.telegram_id == whitelist_id:
                entry.whitelist_reason = reason
                ac_list[i] = entry.to_dict()
                break
        else:
            entry = Ac(whitelist_id)
            entry.whitelist_reason = reason
            ac_list.append(entry.to_dict())

        rec['ac'] = ac_list
        json.dump(rec, open(config['record'], 'w', encoding='utf-8'), indent=2, ensure_ascii=False)

    log(config['messages']['add_whitelist_log'].format(adder=html_refer(adder.name), tg_id=whitelist_id, reason=reason))
    bot.send_message(msg.chat.id, text=config['messages']['add_whitelist_succ'].format(tg_id=whitelist_id),
                     reply_to_message_id=msg.id)

    lift_restriction_trial(entry, msg.chat.id)