Beispiel #1
0
def get_blacklist_setting(chat_id):
    try:
        setting = CHAT_SETTINGS_BLACKLISTS.get(str(chat_id))
        if setting:
            return setting["blacklist_type"], setting["value"]
        else:
            return 1, "0"

    finally:
        SESSION.close()
def get_ses(chat_id):
    autochat = SESSION.query(ChatbotChats).get(str(chat_id))
    sesh = ""
    exp = ""
    if autochat:
        sesh = str(autochat.ses_id)
        exp = str(autochat.expires)

    SESSION.close()
    return sesh, exp
def get_welc_buttons(chat_id):
    try:
        return (
            SESSION.query(WelcomeButtons)
            .filter(WelcomeButtons.chat_id == str(chat_id))
            .order_by(WelcomeButtons.id)
            .all()
        )
    finally:
        SESSION.close()
def get_gdbye_buttons(chat_id):
    try:
        return (
            SESSION.query(GoodbyeButtons)
            .filter(GoodbyeButtons.chat_id == str(chat_id))
            .order_by(GoodbyeButtons.id)
            .all()
        )
    finally:
        SESSION.close()
Beispiel #5
0
def disapprove(chat_id, user_id):
    with APPROVE_INSERTION_LOCK:
        disapprove_user = SESSION.query(Approvals).get((str(chat_id), user_id))
        if disapprove_user:
            SESSION.delete(disapprove_user)
            SESSION.commit()
            return True
        else:
            SESSION.close()
            return False
Beispiel #6
0
def fs_settings(chat_id):
    try:
        return (
            SESSION.query(forceSubscribe)
            .filter(forceSubscribe.chat_id == chat_id)
            .one()
        )
    except:
        return None
    finally:
        SESSION.close()
Beispiel #7
0
def __load_chat_settings_blacklists():
    global CHAT_SETTINGS_BLACKLISTS
    try:
        chats_settings = SESSION.query(BlacklistSettings).all()
        for x in chats_settings:  # remove tuple by ( ,)
            CHAT_SETTINGS_BLACKLISTS[x.chat_id] = {
                "blacklist_type": x.blacklist_type,
                "value": x.value,
            }

    finally:
        SESSION.close()
def del_user(user_id):
    with INSERTION_LOCK:
        curr = SESSION.query(Users).get(user_id)
        if curr:
            SESSION.delete(curr)
            SESSION.commit()
            return True

        ChatMembers.query.filter(ChatMembers.user == user_id).delete()
        SESSION.commit()
        SESSION.close()
    return False
Beispiel #9
0
def rm_afk(user_id):
    with INSERTION_LOCK:
        curr = SESSION.query(AFK).get(user_id)
        if curr:
            if user_id in AFK_USERS:  # sanity check
                del AFK_USERS[user_id]

            SESSION.delete(curr)
            SESSION.commit()
            return True

        SESSION.close()
        return False
Beispiel #10
0
def rm_from_blacklist(chat_id, trigger):
    with BLACKLIST_FILTER_INSERTION_LOCK:
        blacklist_filt = SESSION.query(BlackListFilters).get((str(chat_id), trigger))
        if blacklist_filt:
            if trigger in CHAT_BLACKLISTS.get(str(chat_id), set()):  # sanity check
                CHAT_BLACKLISTS.get(str(chat_id), set()).remove(trigger)

            SESSION.delete(blacklist_filt)
            SESSION.commit()
            return True

        SESSION.close()
        return False
Beispiel #11
0
def unsubs_fed(fed_id, my_fed):
    with FEDS_SUBSCRIBER_LOCK:
        getsubs = SESSION.query(FedSubs).get((fed_id, my_fed))
        if getsubs:
            if my_fed in FEDS_SUBSCRIBER.get(fed_id, set()):  # sanity check
                FEDS_SUBSCRIBER.get(fed_id, set()).remove(my_fed)

            SESSION.delete(getsubs)
            SESSION.commit()
            return True

        SESSION.close()
        return False
def enable_command(chat_id, enable):
    with DISABLE_INSERTION_LOCK:
        disabled = SESSION.query(Disable).get((str(chat_id), enable))

        if disabled:
            if enable in DISABLED.get(str(chat_id)):  # sanity check
                DISABLED.setdefault(str(chat_id), set()).remove(enable)

            SESSION.delete(disabled)
            SESSION.commit()
            return True

        SESSION.close()
        return False
def disable_command(chat_id, disable):
    with DISABLE_INSERTION_LOCK:
        disabled = SESSION.query(Disable).get((str(chat_id), disable))

        if not disabled:
            DISABLED.setdefault(str(chat_id), set()).add(disable)

            disabled = Disable(str(chat_id), disable)
            SESSION.add(disabled)
            SESSION.commit()
            return True

        SESSION.close()
        return False
def get_welc_pref(chat_id):
    welc = SESSION.query(Welcome).get(str(chat_id))
    SESSION.close()
    if welc:
        return (
            welc.should_welcome,
            welc.custom_welcome,
            welc.custom_content,
            welc.welcome_type,
        )

    else:
        # Welcome by default.
        return True, DEFAULT_WELCOME, None, Types.TEXT
Beispiel #15
0
def __load_chat_blacklists():
    global CHAT_BLACKLISTS
    try:
        chats = SESSION.query(BlackListFilters.chat_id).distinct().all()
        for (chat_id,) in chats:  # remove tuple by ( ,)
            CHAT_BLACKLISTS[chat_id] = []

        all_filters = SESSION.query(BlackListFilters).all()
        for x in all_filters:
            CHAT_BLACKLISTS[x.chat_id] += [x.trigger]

        CHAT_BLACKLISTS = {x: set(y) for x, y in CHAT_BLACKLISTS.items()}

    finally:
        SESSION.close()
def global_unignore_command(command):
    command = command.lower()
    with CLEANER_GLOBAL_LOCK:
        unignored = SESSION.query(CleanerBlueTextGlobal).get(str(command))

        if unignored:
            if command in GLOBAL_IGNORE_COMMANDS:
                GLOBAL_IGNORE_COMMANDS.remove(command)

            SESSION.delete(command)
            SESSION.commit()
            return True

        SESSION.close()
        return False
def global_ignore_command(command):
    command = command.lower()
    with CLEANER_GLOBAL_LOCK:
        ignored = SESSION.query(CleanerBlueTextGlobal).get(str(command))

        if not ignored:
            GLOBAL_IGNORE_COMMANDS.add(command)

            ignored = CleanerBlueTextGlobal(str(command))
            SESSION.add(ignored)
            SESSION.commit()
            return True

        SESSION.close()
        return False
def __load_chat_filters():
    global CHAT_FILTERS
    try:
        chats = SESSION.query(CustomFilters.chat_id).distinct().all()
        for (chat_id, ) in chats:  # remove tuple by ( ,)
            CHAT_FILTERS[chat_id] = []

        all_filters = SESSION.query(CustomFilters).all()
        for x in all_filters:
            CHAT_FILTERS[x.chat_id] += [x.keyword]

        CHAT_FILTERS = {
            x: sorted(set(y), key=lambda i: (-len(i), i))
            for x, y in CHAT_FILTERS.items()
        }

    finally:
        SESSION.close()
def is_restr_locked(chat_id, lock_type):
    curr_restr = SESSION.query(Restrictions).get(str(chat_id))
    SESSION.close()

    if not curr_restr:
        return False

    if lock_type == "messages":
        return curr_restr.messages
    elif lock_type == "media":
        return curr_restr.media
    elif lock_type == "other":
        return curr_restr.other
    elif lock_type == "previews":
        return curr_restr.preview
    elif lock_type == "all":
        return (curr_restr.messages and curr_restr.media and curr_restr.other
                and curr_restr.preview)
def chat_unignore_command(chat_id, unignore):
    unignore = unignore.lower()
    with CLEANER_CHAT_LOCK:
        unignored = SESSION.query(CleanerBlueTextChat).get((str(chat_id), unignore))

        if unignored:

            if str(chat_id) not in CLEANER_CHATS:
                CLEANER_CHATS.setdefault(
                    str(chat_id), {"setting": False, "commands": set()}
                )
            if unignore in CLEANER_CHATS.get(str(chat_id)).get("commands"):
                CLEANER_CHATS[str(chat_id)]["commands"].remove(unignore)

            SESSION.delete(unignored)
            SESSION.commit()
            return True

        SESSION.close()
        return False
def chat_ignore_command(chat_id, ignore):
    ignore = ignore.lower()
    with CLEANER_CHAT_LOCK:
        ignored = SESSION.query(CleanerBlueTextChat).get((str(chat_id), ignore))

        if not ignored:

            if str(chat_id) not in CLEANER_CHATS:
                CLEANER_CHATS.setdefault(
                    str(chat_id), {"setting": False, "commands": set()}
                )

            CLEANER_CHATS[str(chat_id)]["commands"].add(ignore)

            ignored = CleanerBlueTextChat(str(chat_id), ignore)
            SESSION.add(ignored)
            SESSION.commit()
            return True
        SESSION.close()
        return False
def rm_note(chat_id, note_name):
    with NOTES_INSERTION_LOCK:
        note = (SESSION.query(Notes).filter(
            func.lower(Notes.name) == note_name,
            Notes.chat_id == str(chat_id)).first())
        if note:
            with BUTTONS_INSERTION_LOCK:
                buttons = (SESSION.query(Buttons).filter(
                    Buttons.chat_id == str(chat_id),
                    Buttons.note_name == note_name).all())
                for btn in buttons:
                    SESSION.delete(btn)

            SESSION.delete(note)
            SESSION.commit()
            return True

        else:
            SESSION.close()
            return False
def is_locked(chat_id, lock_type):
    curr_perm = SESSION.query(Permissions).get(str(chat_id))
    SESSION.close()

    if not curr_perm:
        return False

    elif lock_type == "sticker":
        return curr_perm.sticker
    elif lock_type == "photo":
        return curr_perm.photo
    elif lock_type == "audio":
        return curr_perm.audio
    elif lock_type == "voice":
        return curr_perm.voice
    elif lock_type == "contact":
        return curr_perm.contact
    elif lock_type == "video":
        return curr_perm.video
    elif lock_type == "document":
        return curr_perm.document
    elif lock_type == "gif":
        return curr_perm.gif
    elif lock_type == "url":
        return curr_perm.url
    elif lock_type == "bots":
        return curr_perm.bots
    elif lock_type == "forward":
        return curr_perm.forward
    elif lock_type == "game":
        return curr_perm.game
    elif lock_type == "location":
        return curr_perm.location
    elif lock_type == "rtl":
        return curr_perm.rtl
    elif lock_type == "button":
        return curr_perm.button
    elif lock_type == "egame":
        return curr_perm.egame
    elif lock_type == "inline":
        return curr_perm.inline
def remove_filter(chat_id, keyword):
    global CHAT_FILTERS
    with CUST_FILT_LOCK:
        filt = SESSION.query(CustomFilters).get((str(chat_id), keyword))
        if filt:
            if keyword in CHAT_FILTERS.get(str(chat_id), []):  # Sanity check
                CHAT_FILTERS.get(str(chat_id), []).remove(keyword)

            with BUTTON_LOCK:
                prev_buttons = (SESSION.query(Buttons).filter(
                    Buttons.chat_id == str(chat_id),
                    Buttons.keyword == keyword).all())
                for btn in prev_buttons:
                    SESSION.delete(btn)

            SESSION.delete(filt)
            SESSION.commit()
            return True

        SESSION.close()
        return False
Beispiel #25
0
def __load_all_feds():
    global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME
    try:
        feds = SESSION.query(Federations).all()
        for x in feds:  # remove tuple by ( ,)
            # Fed by Owner
            check = FEDERATION_BYOWNER.get(x.owner_id)
            if check is None:
                FEDERATION_BYOWNER[x.owner_id] = []
            FEDERATION_BYOWNER[str(x.owner_id)] = {
                "fid": str(x.fed_id),
                "fname": x.fed_name,
                "frules": x.fed_rules,
                "flog": x.fed_log,
                "fusers": str(x.fed_users),
            }
            # Fed By FedId
            check = FEDERATION_BYFEDID.get(x.fed_id)
            if check is None:
                FEDERATION_BYFEDID[x.fed_id] = []
            FEDERATION_BYFEDID[str(x.fed_id)] = {
                "owner": str(x.owner_id),
                "fname": x.fed_name,
                "frules": x.fed_rules,
                "flog": x.fed_log,
                "fusers": str(x.fed_users),
            }
            # Fed By Name
            check = FEDERATION_BYNAME.get(x.fed_name)
            if check is None:
                FEDERATION_BYNAME[x.fed_name] = []
            FEDERATION_BYNAME[x.fed_name] = {
                "fid": str(x.fed_id),
                "owner": str(x.owner_id),
                "frules": x.fed_rules,
                "flog": x.fed_log,
                "fusers": str(x.fed_users),
            }
    finally:
        SESSION.close()
Beispiel #26
0
def __load_all_feds_chats():
    global FEDERATION_CHATS, FEDERATION_CHATS_BYID
    try:
        qall = SESSION.query(ChatF).all()
        FEDERATION_CHATS = {}
        FEDERATION_CHATS_BYID = {}
        for x in qall:
            # Federation Chats
            check = FEDERATION_CHATS.get(x.chat_id)
            if check is None:
                FEDERATION_CHATS[x.chat_id] = {}
            FEDERATION_CHATS[x.chat_id] = {
                "chat_name": x.chat_name,
                "fid": x.fed_id
            }
            # Federation Chats By ID
            check = FEDERATION_CHATS_BYID.get(x.fed_id)
            if check is None:
                FEDERATION_CHATS_BYID[x.fed_id] = []
            FEDERATION_CHATS_BYID[x.fed_id].append(x.chat_id)
    finally:
        SESSION.close()
Beispiel #27
0
def __load_all_feds_banned():
    global FEDERATION_BANNED_USERID, FEDERATION_BANNED_FULL
    try:
        FEDERATION_BANNED_USERID = {}
        FEDERATION_BANNED_FULL = {}
        qall = SESSION.query(BansF).all()
        for x in qall:
            check = FEDERATION_BANNED_USERID.get(x.fed_id)
            if check is None:
                FEDERATION_BANNED_USERID[x.fed_id] = []
            if int(x.user_id) not in FEDERATION_BANNED_USERID[x.fed_id]:
                FEDERATION_BANNED_USERID[x.fed_id].append(int(x.user_id))
            check = FEDERATION_BANNED_FULL.get(x.fed_id)
            if check is None:
                FEDERATION_BANNED_FULL[x.fed_id] = {}
            FEDERATION_BANNED_FULL[x.fed_id][x.user_id] = {
                "first_name": x.first_name,
                "last_name": x.last_name,
                "user_name": x.user_name,
                "reason": x.reason,
                "time": x.time,
            }
    finally:
        SESSION.close()
def __load_cleaner_list():
    global GLOBAL_IGNORE_COMMANDS
    global CLEANER_CHATS

    try:
        GLOBAL_IGNORE_COMMANDS = {
            int(x.command) for x in SESSION.query(CleanerBlueTextGlobal).all()
        }
    finally:
        SESSION.close()

    try:
        for x in SESSION.query(CleanerBlueTextChatSettings).all():
            CLEANER_CHATS.setdefault(x.chat_id, {"setting": False, "commands": set()})
            CLEANER_CHATS[x.chat_id]["setting"] = x.is_enable
    finally:
        SESSION.close()

    try:
        for x in SESSION.query(CleanerBlueTextChat).all():
            CLEANER_CHATS.setdefault(x.chat_id, {"setting": False, "commands": set()})
            CLEANER_CHATS[x.chat_id]["commands"].add(x.command)
    finally:
        SESSION.close()
def num_disabled():
    try:
        return SESSION.query(Disable).count()
    finally:
        SESSION.close()
def num_chats():
    try:
        return SESSION.query(func.count(distinct(Disable.chat_id))).scalar()
    finally:
        SESSION.close()