Example #1
0
def add_note_to_db(chat_id,
                   note_name,
                   note_data,
                   msgtype,
                   buttons=None,
                   file=None):
    if not buttons:
        buttons = []

    with NOTES_INSERTION_LOCK:
        prev = SESSION.query(Notes).get((str(chat_id), note_name))
        if prev:
            with BUTTONS_INSERTION_LOCK:
                prev_buttons = SESSION.query(Buttons).filter(
                    Buttons.chat_id == str(chat_id),
                    Buttons.note_name == note_name).all()
                for btn in prev_buttons:
                    SESSION.delete(btn)
            SESSION.delete(prev)
        note = Notes(str(chat_id),
                     note_name,
                     note_data or "",
                     msgtype=msgtype.value,
                     file=file)
        SESSION.add(note)
        SESSION.commit()

    for b_name, url, same_line in buttons:
        add_note_button_to_db(chat_id, note_name, b_name, url, same_line)
Example #2
0
def set_custom_gdbye(chat_id, custom_goodbye, goodbye_type, buttons=None):
    if buttons is None:
        buttons = []

    with INSERTION_LOCK:
        welcome_settings = SESSION.query(Welcome).get(str(chat_id))
        if not welcome_settings:
            welcome_settings = Welcome(str(chat_id), True)

        if custom_goodbye:
            welcome_settings.custom_leave = custom_goodbye
            welcome_settings.leave_type = goodbye_type.value

        else:
            welcome_settings.custom_leave = DEFAULT_GOODBYE
            welcome_settings.leave_type = Types.TEXT.value

        SESSION.add(welcome_settings)

        with LEAVE_BTN_LOCK:
            prev_buttons = SESSION.query(GoodbyeButtons).filter(
                GoodbyeButtons.chat_id == str(chat_id)).all()
            for btn in prev_buttons:
                SESSION.delete(btn)

            for b_name, url, same_line in buttons:
                button = GoodbyeButtons(chat_id, b_name, url, same_line)
                SESSION.add(button)

        SESSION.commit()
Example #3
0
def removedChat(chat_id):
    with ALLOWCHATLOCK:
        chat = SESSION.query(AllowedChat).get(chat_id)
        if chat:
            SESSION.delete(chat)
        SESSION.commit()
        __load_added_chats_list()
Example #4
0
def __load_dynasties_subscriber():
    global DYNASTY_SUBSCRIBER
    global MYDYNASTY_SUBSCRIBER
    try:
        dynasties = SESSION.query(DynastySubs.dynasty_id).distinct().all()
        for (dynasty_id, ) in dynasties:  # remove tuple by ( ,)
            DYNASTY_SUBSCRIBER[dynasty_id] = []
            MYDYNASTY_SUBSCRIBER[dynasty_id] = []

        all_dynastiesubs = SESSION.query(DynastySubs).all()
        for x in all_dynastiesubs:
            DYNASTY_SUBSCRIBER[x.dynasty_id] += [x.dynasty_subs]
            try:
                MYDYNASTY_SUBSCRIBER[x.dynasty_subs] += [x.dynasty_id]
            except KeyError:
                getsubs = SESSION.query(DynastySubs).get(
                    (x.dynasty_id, x.dynasty_subs))
                if getsubs:
                    SESSION.delete(getsubs)
                    SESSION.commit()

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

    finally:
        SESSION.close()
Example #5
0
def set_custom_welcome(chat_id,
                       custom_content,
                       custom_welcome,
                       welcome_type,
                       buttons=None):
    if buttons is None:
        buttons = []

    with INSERTION_LOCK:
        welcome_settings = SESSION.query(Welcome).get(str(chat_id))
        if not welcome_settings:
            welcome_settings = Welcome(str(chat_id), True)

        if custom_welcome or custom_content:
            welcome_settings.custom_content = custom_content
            welcome_settings.custom_welcome = custom_welcome
            welcome_settings.welcome_type = welcome_type.value

        else:
            welcome_settings.custom_welcome = DEFAULT_WELCOME
            welcome_settings.welcome_type = Types.TEXT.value

        SESSION.add(welcome_settings)

        with WELC_BTN_LOCK:
            prev_buttons = SESSION.query(WelcomeButtons).filter(
                WelcomeButtons.chat_id == str(chat_id)).all()
            for btn in prev_buttons:
                SESSION.delete(btn)

            for b_name, url, same_line in buttons:
                button = WelcomeButtons(chat_id, b_name, url, same_line)
                SESSION.add(button)

        SESSION.commit()
Example #6
0
def rem_chat(chat_id):
    with INSERTION_LOCK:
        autochat = SESSION.query(ChatbotChats).get(str(chat_id))
        if autochat:
            SESSION.delete(autochat)

        SESSION.commit()
Example #7
0
def rem_chat(chat_id):
    with INSERTION_LOCK:
        chat = SESSION.query(Chats).get(str(chat_id))
        if chat:
            SESSION.delete(chat)
            SESSION.commit()
        else:
            SESSION.close()
Example #8
0
def set_welcome_mutes(chat_id, welcomemutes):
    with WM_LOCK:
        prev = SESSION.query(WelcomeMute).get((str(chat_id)))
        if prev:
            SESSION.delete(prev)
        welcome_m = WelcomeMute(str(chat_id), welcomemutes)
        SESSION.add(welcome_m)
        SESSION.commit()
Example #9
0
def ungban_user(user_id):
    with GBANNED_USERS_LOCK:
        user = SESSION.query(GloballyBannedUsers).get(user_id)
        if user:
            SESSION.delete(user)

        SESSION.commit()
        __load_gbanned_userid_list()
Example #10
0
def init_permissions(chat_id, reset=False):
    curr_perm = SESSION.query(Permissions).get(str(chat_id))
    if reset:
        SESSION.delete(curr_perm)
        SESSION.flush()
    perm = Permissions(str(chat_id))
    SESSION.add(perm)
    SESSION.commit()
    return perm
Example #11
0
def init_restrictions(chat_id, reset=False):
    curr_restr = SESSION.query(Restrictions).get(str(chat_id))
    if reset:
        SESSION.delete(curr_restr)
        SESSION.flush()
    restr = Restrictions(str(chat_id))
    SESSION.add(restr)
    SESSION.commit()
    return restr
Example #12
0
def disapprove(chat_id, user_id):
    with APPROVE_INSERTION_LOCK:
        note = SESSION.query(Approvals).get((str(chat_id), user_id))
        if note:
            SESSION.delete(note)
            SESSION.commit()
            return True
        else:
            SESSION.close()
            return False
def stop_chat_logging(chat_id):
    with LOGS_INSERTION_LOCK:
        res = SESSION.query(GroupLogs).get(str(chat_id))
        if res:
            if str(chat_id) in CHANNELS:
                del CHANNELS[str(chat_id)]

            log_channel = res.log_channel
            SESSION.delete(res)
            SESSION.commit()
            return log_channel
Example #14
0
def remove_warn_filter(chat_id, keyword):
    with WARN_FILTER_INSERTION_LOCK:
        warn_filt = SESSION.query(WarnFilters).get((str(chat_id), keyword))
        if warn_filt:
            if keyword in WARN_FILTERS.get(str(chat_id), []):  # sanity check
                WARN_FILTERS.get(str(chat_id), []).remove(keyword)

            SESSION.delete(warn_filt)
            SESSION.commit()
            return True
        SESSION.close()
        return False
Example #15
0
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
Example #16
0
def user_demote_dynasty(dynasty_id, user_id):
    with DYNASTY_LOCK:
        global DYNASTY_BYOWNER, DYNASTY_BYDYNASTYID, DYNASTY_BYNAME
        # Variables
        getdynasty = DYNASTY_BYDYNASTYID.get(str(dynasty_id))
        owner_id = getdynasty['owner']
        dynasty_name = getdynasty['dname']
        dynasty_rules = getdynasty['drules']
        dynasty_log = getdynasty['dlog']
        # Temp set
        try:
            members = eval(eval(getdynasty['dusers'])['members'])
        except ValueError:
            return False
        members.remove(user_id)
        # Set user
        DYNASTY_BYOWNER[str(owner_id)]['dusers'] = str({
            'owner': str(owner_id),
            'members': str(members)
        })
        DYNASTY_BYDYNASTYID[str(dynasty_id)]['dusers'] = str({
            'owner':
            str(owner_id),
            'members':
            str(members)
        })
        DYNASTY_BYNAME[dynasty_name]['dusers'] = str({
            'owner': str(owner_id),
            'members': str(members)
        })
        # Set on database
        dynasty = Dynasties(
            str(owner_id), dynasty_name, str(dynasty_id), dynasty_rules,
            dynasty_log, str({
                'owner': str(owner_id),
                'members': str(members)
            }))
        SESSION.merge(dynasty)
        SESSION.commit()
        return True

        curr = SESSION.query(UserF).all()
        result = False
        for r in curr:
            if int(r.user_id) == int(user_id):
                if r.dynasty_id == dynasty_id:
                    SESSION.delete(r)
                    SESSION.commit()
                    result = True

        SESSION.close()
        return result
def add_filter(
    chat_id,
    keyword,
    reply,
    is_sticker=False,
    is_document=False,
    is_image=False,
    is_audio=False,
    is_voice=False,
    is_video=False,
    buttons=None,
):
    global CHAT_FILTERS

    if buttons is None:
        buttons = []

    with CUST_FILT_LOCK:
        prev = SESSION.query(CustomFilters).get((str(chat_id), keyword))
        if prev:
            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(prev)

        filt = CustomFilters(
            str(chat_id),
            keyword,
            reply,
            is_sticker,
            is_document,
            is_image,
            is_audio,
            is_voice,
            is_video,
            bool(buttons),
        )

        if keyword not in CHAT_FILTERS.get(str(chat_id), []):
            CHAT_FILTERS[str(chat_id)] = sorted(
                CHAT_FILTERS.get(str(chat_id), []) + [keyword],
                key=lambda x: (-len(x), x),
            )

        SESSION.add(filt)
        SESSION.commit()

    for b_name, url, same_line in buttons:
        add_note_button_to_db(chat_id, keyword, b_name, url, same_line)
Example #18
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
Example #19
0
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
Example #20
0
def unsubs_dynasty(dynasty_id, my_dynasty):
    with DYNASTY_SUBSCRIBER_LOCK:
        getsubs = SESSION.query(DynastySubs).get((dynasty_id, my_dynasty))
        if getsubs:
            if my_dynasty in DYNASTY_SUBSCRIBER.get(dynasty_id,
                                                    set()):  # sanity check
                DYNASTY_SUBSCRIBER.get(dynasty_id, set()).remove(my_dynasty)

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

        SESSION.close()
        return False
Example #21
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 #22
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
Example #23
0
def rm_from_stickers(chat_id, trigger):
    with STICKERS_FILTER_INSERTION_LOCK:
        stickers_filt = SESSION.query(StickersFilters).get(
            (str(chat_id), trigger))
        if stickers_filt:
            if trigger in CHAT_STICKERS.get(str(chat_id),
                                            set()):  # sanity check
                CHAT_STICKERS.get(str(chat_id), set()).remove(trigger)

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

        SESSION.close()
        return False
Example #24
0
def un_dban_user(dynasty_id, user_id):
    with DYNASTY_LOCK:
        r = SESSION.query(BansD).all()
        for I in r:
            if I.dynasty_id == dynasty_id:
                if int(I.user_id) == int(user_id):
                    SESSION.delete(I)
        try:
            SESSION.commit()
        except:
            SESSION.rollback()
            return False
        finally:
            SESSION.commit()
        __load_all_dynasties_banned()
        return I
Example #25
0
def chat_leave_dynasty(chat_id):
    with DYNASTY_LOCK:
        global DYNASTY_CHATS, DYNASTY_CHATS_BYID
        # Set variables
        dynasty_info = DYNASTY_CHATS.get(str(chat_id))
        if dynasty_info is None:
            return False
        dynasty_id = dynasty_info['fid']
        # Delete from cache
        DYNASTY_CHATS.pop(str(chat_id))
        DYNASTY_CHATS_BYID[str(dynasty_id)].remove(str(chat_id))
        # Delete from db
        curr = SESSION.query(ChatD).all()
        for U in curr:
            if int(U.chat_id) == int(chat_id):
                SESSION.delete(U)
                SESSION.commit()
        return True
Example #26
0
def del_dynasty(dynasty_id):
    with DYNASTY_LOCK:
        global DYNASTY_BYOWNER, DYNASTY_BYDYNASTYID, DYNASTY_BYNAME, DYNASTY_CHATS, DYNASTY_CHATS_BYID, DYNASTY_BANNED_USERID, DYNASTY_BANNED_FULL
        getcache = DYNASTY_BYDYNASTYID.get(dynasty_id)
        if getcache is None:
            return False
        # Variables
        getdynasty = DYNASTY_BYDYNASTYID.get(dynasty_id)
        owner_id = getdynasty['owner']
        dynasty_name = getdynasty['dname']
        # Delete from cache
        DYNASTY_BYOWNER.pop(owner_id)
        DYNASTY_BYDYNASTYID.pop(dynasty_id)
        DYNASTY_BYNAME.pop(dynasty_name)
        if DYNASTY_CHATS_BYID.get(dynasty_id):
            for x in DYNASTY_CHATS_BYID[dynasty_id]:
                delchats = SESSION.query(ChatD).get(str(x))
                if delchats:
                    SESSION.delete(delchats)
                    SESSION.commit()
                DYNASTY_CHATS.pop(x)
            DYNASTY_CHATS_BYID.pop(dynasty_id)
        # Delete dynastyban users
        getall = DYNASTY_BANNED_USERID.get(dynasty_id)
        if getall:
            for x in getall:
                banlist = SESSION.query(BansD).get((dynasty_id, str(x)))
                if banlist:
                    SESSION.delete(banlist)
                    SESSION.commit()
        if DYNASTY_BANNED_USERID.get(dynasty_id):
            DYNASTY_BANNED_USERID.pop(dynasty_id)
        if DYNASTY_BANNED_FULL.get(dynasty_id):
            DYNASTY_BANNED_FULL.pop(dynasty_id)
        # Delete dynastiesubs
        getall = MYDYNASTY_SUBSCRIBER.get(dynasty_id)
        if getall:
            for x in getall:
                getsubs = SESSION.query(DynastySubs).get((dynasty_id, str(x)))
                if getsubs:
                    SESSION.delete(getsubs)
                    SESSION.commit()
        if DYNASTY_SUBSCRIBER.get(dynasty_id):
            DYNASTY_SUBSCRIBER.pop(dynasty_id)
        if MYDYNASTY_SUBSCRIBER.get(dynasty_id):
            MYDYNASTY_SUBSCRIBER.pop(dynasty_id)
        # Delete from database
        curr = SESSION.query(Dynasties).get(dynasty_id)
        if curr:
            SESSION.delete(curr)
            SESSION.commit()
        return True
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 #28
0
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 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
Example #30
0
def dban_user(dynasty_id, user_id, first_name, last_name, user_name, reason,
              time):
    with DYNASTY_LOCK:
        r = SESSION.query(BansD).all()
        for I in r:
            if I.dynasty_id == dynasty_id:
                if int(I.user_id) == int(user_id):
                    SESSION.delete(I)

        r = BansD(str(dynasty_id), str(user_id), first_name, last_name,
                  user_name, reason, time)

        SESSION.add(r)
        try:
            SESSION.commit()
        except:
            SESSION.rollback()
            return False
        finally:
            SESSION.commit()
        __load_all_dynasties_banned()
        return r