Example #1
0
def reset_warns(user_id, chat_id):
    with WARN_INSERTION_LOCK:
        warned_user = SESSION.query(Warns).get((user_id, str(chat_id)))
        if warned_user:
            warned_user.num_warns = 0
            warned_user.reasons = ""

            SESSION.add(warned_user)
            SESSION.commit()
        SESSION.close()
Example #2
0
def set_frules(fed_id, rules):
    with FEDS_LOCK:
        global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME
        # Variables
        getfed = FEDERATION_BYFEDID.get(str(fed_id))
        owner_id = getfed["owner"]
        fed_name = getfed["fname"]
        fed_members = getfed["fusers"]
        fed_rules = str(rules)
        fed_log = getfed["flog"]
        # Set user
        FEDERATION_BYOWNER[str(owner_id)]["frules"] = fed_rules
        FEDERATION_BYFEDID[str(fed_id)]["frules"] = fed_rules
        FEDERATION_BYNAME[fed_name]["frules"] = fed_rules
        # Set on database
        fed = Federations(
            str(owner_id), fed_name, str(fed_id), fed_rules, fed_log, str(fed_members)
        )
        SESSION.merge(fed)
        SESSION.commit()
        return True
Example #3
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()
Example #4
0
def get_fban_user(fed_id, user_id):
    list_fbanned = FEDERATION_BANNED_USERID.get(fed_id)
    if list_fbanned is None:
        FEDERATION_BANNED_USERID[fed_id] = []
    if user_id in FEDERATION_BANNED_USERID[fed_id]:
        r = SESSION.query(BansF).all()
        reason = None
        for I in r:
            if I.fed_id == fed_id:
                if int(I.user_id) == int(user_id):
                    reason = I.reason
                    time = I.time
        return True, reason, time
    return False, None, None
Example #5
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
Example #6
0
def rm_url_from_blacklist(chat_id, domain):
    with URL_BLACKLIST_FILTER_INSERTION_LOCK:
        domain_filt = SESSION.query(URLBlackListFilters).get(
            (str(chat_id), domain))
        if domain_filt:
            if domain in CHAT_URL_BLACKLISTS.get(str(chat_id), set()):
                CHAT_URL_BLACKLISTS.get(str(chat_id), set()).remove(domain)
            SESSION.delete(domain_filt)
            SESSION.commit()
            return True

        SESSION.close()
        return False
Example #7
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]
                del AFK_USERSS[user_id]
            SESSION.delete(curr)
            SESSION.commit()
            return True

        SESSION.close()
        return False
Example #8
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()
Example #9
0
def remove_warn(user_id, chat_id):
    with WARN_INSERTION_LOCK:
        removed = False
        warned_user = SESSION.query(Warns).get((user_id, str(chat_id)))

        if warned_user and warned_user.num_warns > 0:
            warned_user.num_warns -= 1

            SESSION.add(warned_user)
            SESSION.commit()
            removed = True

        SESSION.close()
        return removed
Example #10
0
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
Example #11
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:
            # sanity check
            if trigger in CHAT_BLACKLISTS.get(str(chat_id), set()):
                CHAT_BLACKLISTS.get(str(chat_id), set()).remove(trigger)

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

        SESSION.close()
        return False
Example #12
0
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
Example #13
0
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()
Example #14
0
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
Example #15
0
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
Example #16
0
def rm_goodbye_setting(chat_id):
    rem = SESSION.query(Goodbye).get(str(chat_id))
    if rem:
        SESSION.delete(rem)
        SESSION.commit()
Example #17
0
def all_gmuted():
    rem = SESSION.query(GMute).all()
    SESSION.close()
    return rem
Example #18
0
def ungmute(sender):
    rem = SESSION.query(GMute).get((str(sender)))
    if rem:
        SESSION.delete(rem)
        SESSION.commit()
Example #19
0
def gmute(sender):
    adder = GMute(str(sender))
    SESSION.add(adder)
    SESSION.commit()
Example #20
0
def update_previous_welcome(chat_id, previous_welcome):
    row = SESSION.query(Welcome).get(str(chat_id))
    row.previous_welcome = previous_welcome
    # commit the changes to the DB
    SESSION.commit()
Example #21
0
def remove_all_notes(chat_id):
    saved_note = SESSION.query(NOTES).filter(NOTES.chat_id == str(chat_id))
    if saved_note:
        saved_note.delete()
        SESSION.commit()
Example #22
0
def num_chats():
    try:
        return SESSION.query(func.count(distinct(Rules.chat_id))).scalar()
    finally:
        SESSION.close()
Example #23
0
def multi_fban_user(
    multi_fed_id,
    multi_user_id,
    multi_first_name,
    multi_last_name,
    multi_user_name,
    multi_reason,
):
    if True:  # with FEDS_LOCK:
        counter = 0
        time = 0
        for x in range(len(multi_fed_id)):
            fed_id = multi_fed_id[x]
            user_id = multi_user_id[x]
            first_name = multi_first_name[x]
            last_name = multi_last_name[x]
            user_name = multi_user_name[x]
            reason = multi_reason[x]
            r = SESSION.query(BansF).all()
            for I in r:
                if I.fed_id == fed_id:
                    if int(I.user_id) == int(user_id):
                        SESSION.delete(I)

            r = BansF(
                str(fed_id),
                str(user_id),
                first_name,
                last_name,
                user_name,
                reason,
                time,
            )

            SESSION.add(r)
            counter += 1
            if str(str(counter)[-2:]) == "00":
                print(user_id)
                print(first_name)
                print(reason)
                print(counter)
        try:
            SESSION.commit()
        except:
            SESSION.rollback()
            return False
        finally:
            SESSION.commit()
        __load_all_feds_banned()
        print("Done")
        return counter
Example #24
0
def update_previous_goodbye(chat_id, previous_goodbye):
    row = SESSION.query(Goodbye).get(str(chat_id))
    row.previous_goodbye = previous_goodbye
    # commit the changes to the DB
    SESSION.commit()
Example #25
0
def rm_welcome_setting(chat_id):
    rem = SESSION.query(Welcome).get(str(chat_id))
    if rem:
        SESSION.delete(rem)
        SESSION.commit()
Example #26
0
def remove_filter(chat_id, keyword):
    saved_filter = SESSION.query(Filters).get((str(chat_id), keyword))
    if saved_filter:
        SESSION.delete(saved_filter)
        SESSION.commit()
Example #27
0
def get_all_chats():
    try:
        return SESSION.query(ChatbotChats.chat_id).all()
    finally:
        SESSION.close()
Example #28
0
def remove_all_filters(chat_id):
    saved_filter = SESSION.query(Filters).filter(
        Filters.chat_id == str(chat_id))
    if saved_filter:
        saved_filter.delete()
        SESSION.commit()
Example #29
0
def migrate_chat(old_chat_id, new_chat_id):
    with INSERTION_LOCK:
        chat = SESSION.query(Rules).get(str(old_chat_id))
        if chat:
            chat.chat_id = str(new_chat_id)
        SESSION.commit()
Example #30
0
def remove_note(chat_id, keyword):
    saved_note = SESSION.query(NOTES).get((str(chat_id), keyword))
    if saved_note:
        SESSION.delete(saved_note)
        SESSION.commit()