示例#1
0
async def export_chat_data(message, chat, strings):
    chat_id = chat["chat_id"]
    key = "export_lock:" + str(chat_id)
    if redis.get(key) and message.from_user.id not in OPERATORS:
        ttl = format_timedelta(timedelta(seconds=redis.ttl(key)),
                               strings["language_info"]["babel"])
        await message.reply(strings["exports_locked"] % ttl)
        return

    redis.set(key, 1)
    redis.expire(key, 7200)

    msg = await message.reply(strings["started_exporting"])
    data = {
        "general": {
            "chat_name": chat["chat_title"],
            "chat_id": chat_id,
            "date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "version": VERSION,
        }
    }

    for module in [m for m in LOADED_MODULES if hasattr(m, "__export__")]:
        await asyncio.sleep(0)  # Switch to other events before continue
        if k := await module.__export__(chat_id):
            data.update(k)
示例#2
0
async def importfbans_cmd(message, fed, strings):
    fed_id = fed["fed_id"]
    key = "importfbans_lock:" + str(fed_id)
    if redis.get(key) and message.from_user.id not in OPERATORS:
        ttl = format_timedelta(
            timedelta(seconds=redis.ttl(key)), strings["language_info"]["babel"]
        )
        await message.reply(strings["importfbans_locked"] % ttl)
        return

    redis.set(key, 1)
    redis.expire(key, 600)

    if "document" in message:
        document = message.document
    else:
        if "reply_to_message" not in message:
            await ImportFbansFileWait.waiting.set()
            await message.reply(strings["send_import_file"])
            return

        elif "document" not in message.reply_to_message:
            await message.reply(strings["rpl_to_file"])
            return
        document = message.reply_to_message.document

    await importfbans_func(message, fed, document=document)
示例#3
0
async def kick_user_cmd(message, chat, user, args, strings):
    chat_id = chat["chat_id"]
    user_id = user["user_id"]

    if user_id == BOT_ID:
        await message.reply(strings["kick_RaVaN"])
        return

    elif user_id == message.from_user.id:
        await message.reply(strings["kick_self"])
        return

    elif await is_user_admin(chat_id, user_id):
        await message.reply(strings["kick_admin"])
        return

    text = strings["user_kicked"].format(
        user=await get_user_link(user_id),
        admin=await get_user_link(message.from_user.id),
        chat_name=chat["chat_title"],
    )

    # Add reason
    if args:
        text += strings["reason"] % args

    # Check if silent
    silent = False
    if get_cmd(message) == "skick":
        silent = True
        key = "leave_silent:" + str(chat_id)
        redis.set(key, user_id)
        redis.expire(key, 30)
        text += strings["purge"]

    await kick_user(chat_id, user_id)

    msg = await message.reply(text)

    # Del msgs if silent
    if silent:
        to_del = [msg.message_id, message.message_id]
        if ("reply_to_message" in message
                and message.reply_to_message.from_user.id == user_id):
            to_del.append(message.reply_to_message.message_id)
        await asyncio.sleep(5)
        await tbot.delete_messages(chat_id, to_del)
示例#4
0
async def import_fun(message, document, chat, strings):
    chat_id = chat["chat_id"]
    key = "import_lock:" + str(chat_id)
    if redis.get(key) and message.from_user.id not in OPERATORS:
        ttl = format_timedelta(timedelta(seconds=redis.ttl(key)),
                               strings["language_info"]["babel"])
        await message.reply(strings["imports_locked"] % ttl)
        return

    redis.set(key, 1)
    redis.expire(key, 7200)

    msg = await message.reply(strings["started_importing"])
    if document["file_size"] > 52428800:
        await message.reply(strings["big_file"])
        return
    data = await bot.download_file_by_id(document.file_id, io.BytesIO())
    try:
        data = rapidjson.load(data)
    except ValueError:
        return await message.reply(strings["invalid_file"])

    if "general" not in data:
        await message.reply(strings["bad_file"])
        return

    file_version = data["general"]["version"]

    if file_version > VERSION:
        await message.reply(strings["file_version_so_new"])
        return

    imported = []
    for module in [m for m in LOADED_MODULES if hasattr(m, "__import__")]:
        module_name = module.__name__.replace("RaVaN.modules.", "")
        if module_name not in data:
            continue
        if not data[module_name]:
            continue

        imported.append(module_name)
        await asyncio.sleep(0)  # Switch to other events before continue
        await module.__import__(chat_id, data[module_name])

    await msg.edit_text(strings["import_done"])
示例#5
0
async def fban_export(message, fed, strings):
    fed_id = fed["fed_id"]
    key = "fbanlist_lock:" + str(fed_id)
    if redis.get(key) and message.from_user.id not in OPERATORS:
        ttl = format_timedelta(
            timedelta(seconds=redis.ttl(key)), strings["language_info"]["babel"]
        )
        await message.reply(strings["fbanlist_locked"] % ttl)
        return

    redis.set(key, 1)
    redis.expire(key, 600)

    msg = await message.reply(strings["creating_fbanlist"])
    fields = ["user_id", "reason", "by", "time", "banned_chats"]
    with io.StringIO() as f:
        writer = csv.DictWriter(f, fields)
        writer.writeheader()
        async for banned_data in db.fed_bans.find({"fed_id": fed_id}):
            await asyncio.sleep(0)

            data = {"user_id": banned_data["user_id"]}

            if "reason" in banned_data:
                data["reason"] = banned_data["reason"]

            if "time" in banned_data:
                data["time"] = int(time.mktime(banned_data["time"].timetuple()))

            if "by" in banned_data:
                data["by"] = banned_data["by"]

            if "banned_chats" in banned_data:
                data["banned_chats"] = banned_data["banned_chats"]

            writer.writerow(data)

        text = strings["fbanlist_done"] % html.escape(fed["fed_name"], False)
        f.seek(0)
        await message.answer_document(InputFile(f, filename="fban_export.csv"), text)
    await msg.delete()
示例#6
0
        user_id=user_id,
        by=await get_user_link(message.from_user.id),
        chat_count=len(banned_chats),
        all_chats=num,
    )

    if reason:
        channel_text += strings["fban_reason_fed_log"].format(reason=reason)

    # Check if silent
    silent = False
    if get_cmd(message) == "sfban":
        silent = True
        key = "leave_silent:" + str(message.chat.id)
        redis.set(key, user_id)
        redis.expire(key, 30)
        text += strings["fbanned_silence"]

    # SubsFeds process
    if len(sfeds_list := await get_all_subs_feds_r(fed["fed_id"], [])) > 1:
        sfeds_list.remove(fed["fed_id"])
        this_fed_banned_count = len(banned_chats)

        await msg.edit_text(
            text + strings["fbanned_subs_process"].format(feds=len(sfeds_list))
        )

        all_banned_chats_count = 0
        for s_fed_id in sfeds_list:
            if (
                await db.fed_bans.find_one({"fed_id": s_fed_id, "user_id": user_id})
示例#7
0
    # Check on /allowusersconnect enabled
    if settings := await db.chat_connection_settings.find_one(
        {"chat_id": chat_id}):
        if ("allow_users_connect" in settings
                and settings["allow_users_connect"] is False
                and not user_admin):
            return {"status": None, "err_msg": "conn_not_allowed"}

    data = {"status": True, "chat_id": chat_id, "chat_title": chat_title}

    # Cache connection status for 15 minutes
    cached = data
    cached["status"] = 1
    redis.hmset(key, cached)
    redis.expire(key, 900)

    return data


def chat_connection(**dec_kwargs):
    def wrapped(func):
        async def wrapped_1(*args, **kwargs):

            message = args[0]
            from_id = None
            if hasattr(message, "message"):
                from_id = message.from_user.id
                message = message.message

            if (check := await get_connected_chat(