Exemplo n.º 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)
Exemplo n.º 2
0
def set_nsfw(chat_id):
    with INSERTION_LOCK:
        nsfwchat = SESSION.query(NSFWChats).get(str(chat_id))
        if not nsfwchat:
            nsfwchat = NSFWChats(str(chat_id))
        SESSION.add(nsfwchat)
        SESSION.commit()
Exemplo n.º 3
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()
Exemplo n.º 4
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
Exemplo n.º 5
0
def add_history_conn(user_id, chat_id, chat_name):
    global HISTORY_CONNECT
    with CONNECTION_HISTORY_LOCK:
        conn_time = int(time.time())
        if HISTORY_CONNECT.get(int(user_id)):
            counting = SESSION.query(ConnectionHistory.user_id).filter(ConnectionHistory.user_id == str(user_id)).count()
            getchat_id = {}
            for x in HISTORY_CONNECT[int(user_id)]:
                getchat_id[HISTORY_CONNECT[int(user_id)][x]['chat_id']] = x
            if chat_id in getchat_id:
                todeltime = getchat_id[str(chat_id)]
                delold = SESSION.query(ConnectionHistory).get((int(user_id), str(chat_id)))
                if delold:
                    SESSION.delete(delold)
                    HISTORY_CONNECT[int(user_id)].pop(todeltime)
            elif counting >= 5:
                todel = list(HISTORY_CONNECT[int(user_id)])
                todel.reverse()
                todel = todel[4:]
                for x in todel:
                    chat_old = HISTORY_CONNECT[int(user_id)][x]['chat_id']
                    delold = SESSION.query(ConnectionHistory).get((int(user_id), str(chat_old)))
                    if delold:
                        SESSION.delete(delold)
                        HISTORY_CONNECT[int(user_id)].pop(x)
        else:
            HISTORY_CONNECT[int(user_id)] = {}
        delold = SESSION.query(ConnectionHistory).get((int(user_id), str(chat_id)))
        if delold:
            SESSION.delete(delold)
        history = ConnectionHistory(int(user_id), str(chat_id), chat_name, conn_time)
        SESSION.add(history)
        SESSION.commit()
        HISTORY_CONNECT[int(user_id)][conn_time] = {'chat_name': chat_name, 'chat_id': str(chat_id)}
Exemplo n.º 6
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()
Exemplo n.º 7
0
def update_lock(chat_id, lock_type, locked):
    with PERM_LOCK:
        curr_perm = SESSION.query(Permissions).get(str(chat_id))
        if not curr_perm:
            curr_perm = init_permissions(chat_id)

        if lock_type == "audio":
            curr_perm.audio = locked
        elif lock_type == "voice":
            curr_perm.voice = locked
        elif lock_type == "contact":
            curr_perm.contact = locked
        elif lock_type == "video":
            curr_perm.video = locked
        elif lock_type == "document":
            curr_perm.document = locked
        elif lock_type == "photo":
            curr_perm.photo = locked
        elif lock_type == "sticker":
            curr_perm.sticker = locked
        elif lock_type == "gif":
            curr_perm.gif = locked
        elif lock_type == 'url':
            curr_perm.url = locked
        elif lock_type == 'bots':
            curr_perm.bots = locked
        elif lock_type == 'forward':
            curr_perm.forward = locked
        elif lock_type == 'game':
            curr_perm.game = locked
        elif lock_type == 'location':
            curr_perm.location = locked

        SESSION.add(curr_perm)
        SESSION.commit()
Exemplo n.º 8
0
def set_blacklist_strength(chat_id, blacklist_type, value):
    # for blacklist_type
    # 0 = nothing
    # 1 = delete
    # 2 = warn
    # 3 = mute
    # 4 = kick
    # 5 = ban
    # 6 = tban
    # 7 = tmute
    with STICKSET_FILTER_INSERTION_LOCK:
        global CHAT_BLSTICK_BLACKLISTS
        curr_setting = SESSION.query(StickerSettings).get(str(chat_id))
        if not curr_setting:
            curr_setting = StickerSettings(
                chat_id,
                blacklist_type=int(blacklist_type),
                value=value,
            )

        curr_setting.blacklist_type = int(blacklist_type)
        curr_setting.value = str(value)
        CHAT_BLSTICK_BLACKLISTS[str(chat_id)] = {
            "blacklist_type": int(blacklist_type),
            "value": value,
        }

        SESSION.add(curr_setting)
        SESSION.commit()
Exemplo n.º 9
0
def __migrate_filters():
    try:
        all_filters = SESSION.query(CustomFilters).distinct().all()
        for x in all_filters:
            if x.is_document:
                file_type = Types.DOCUMENT
            elif x.is_image:
                file_type = Types.PHOTO
            elif x.is_video:
                file_type = Types.VIDEO
            elif x.is_sticker:
                file_type = Types.STICKER
            elif x.is_audio:
                file_type = Types.AUDIO
            elif x.is_voice:
                file_type = Types.VOICE
            else:
                file_type = Types.TEXT

            print(str(x.chat_id), x.keyword, x.reply, file_type.value)
            if file_type == Types.TEXT:
                filt = CustomFilters(
                    str(x.chat_id), x.keyword, x.reply, file_type.value, None)
            else:
                filt = CustomFilters(
                    str(x.chat_id), x.keyword, None, file_type.value, x.reply)

            SESSION.add(filt)
            SESSION.commit()

    finally:
        SESSION.close()
Exemplo n.º 10
0
def fban_user(fed_id, user_id, first_name, last_name, user_name, reason, time):
    with FEDS_LOCK:
        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)
        try:
            SESSION.commit()
        except:
            SESSION.rollback()
            return False
        finally:
            SESSION.commit()
        __load_all_feds_banned()
        return r
Exemplo n.º 11
0
def migrate_chat(old_chat_id, new_chat_id):
    with ANTICHANNEL_SETTING_LOCK:
        chat = SESSION.query(AntiChannelSettings).get(str(old_chat_id))
        if chat:
            chat.chat_id = new_chat_id
            SESSION.add(chat)

        SESSION.commit()
Exemplo n.º 12
0
def switch_to_locale(chat_id, locale_name):
    with LOCALES_INSERTION_LOCK:
        prev = SESSION.query(Locales).get((str(chat_id)))
        if prev:
            SESSION.delete(prev)
        switch_locale = Locales(str(chat_id), locale_name)
        SESSION.add(switch_locale)
        SESSION.commit()
Exemplo n.º 13
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()
def add_channel(chat_id, channel):
    adder = SESSION.query(forceSubscribe).get(chat_id)
    if adder:
        adder.channel = channel
    else:
        adder = forceSubscribe(chat_id, channel)
    SESSION.add(adder)
    SESSION.commit()
Exemplo n.º 15
0
def migrate_chat(old_chat_id, new_chat_id):
    with GBAN_SETTING_LOCK:
        chat = SESSION.query(GbanSettings).get(str(old_chat_id))
        if chat:
            chat.chat_id = new_chat_id
            SESSION.add(chat)

        SESSION.commit()
Exemplo n.º 16
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
Exemplo n.º 17
0
def set_user_me_info(user_id, info):
    with INSERTION_LOCK:
        userinfo = SESSION.query(UserInfo).get(user_id)
        if userinfo:
            userinfo.info = info
        else:
            userinfo = UserInfo(user_id, info)
        SESSION.add(userinfo)
        SESSION.commit()
Exemplo n.º 18
0
def disable_antichannel(chat_id: int):
    with ANTICHANNEL_SETTING_LOCK:
        chat = SESSION.query(AntiChannelSettings).get(str(chat_id))
        if not chat:
            chat = AntiChannelSettings(chat_id, False)

        chat.setting = False
        SESSION.add(chat)
        SESSION.commit()
Exemplo n.º 19
0
def set_clean_service(chat_id: Union[int, str], setting: bool):
    with CS_LOCK:
        chat_setting = SESSION.query(CleanServiceSetting).get(str(chat_id))
        if not chat_setting:
            chat_setting = CleanServiceSetting(chat_id)

        chat_setting.clean_service = setting
        SESSION.add(chat_setting)
        SESSION.commit()
def set_chat_setting(chat_id: Union[int, str], setting: bool):
    with CHAT_LOCK:
        chat_setting = SESSION.query(AntiArabicChatSettings).get(str(chat_id))
        if not chat_setting:
            chat_setting = AntiArabicChatSettings(chat_id)

        chat_setting.antiarabic = setting
        SESSION.add(chat_setting)
        SESSION.commit()
Exemplo n.º 21
0
def set_rules(chat_id, rules_text):
    with INSERTION_LOCK:
        rules = SESSION.query(Rules).get(str(chat_id))
        if not rules:
            rules = Rules(str(chat_id))
        rules.rules = rules_text

        SESSION.add(rules)
        SESSION.commit()
Exemplo n.º 22
0
def set_allow_connect_to_chat(chat_id: Union[int, str], setting: bool):
    with CHAT_ACCESS_LOCK:
        chat_setting = SESSION.query(ChatAccessConnectionSettings).get(str(chat_id))
        if not chat_setting:
            chat_setting = ChatAccessConnectionSettings(chat_id, setting)

        chat_setting.allow_connect_to_chat = setting
        SESSION.add(chat_setting)
        SESSION.commit()
Exemplo n.º 23
0
def connect(user_id, chat_id):
    with CONNECTION_INSERTION_LOCK:
        prev = SESSION.query(Connection).get((int(user_id)))
        if prev:
            SESSION.delete(prev)
        connect_to_chat = Connection(int(user_id), chat_id)
        SESSION.add(connect_to_chat)
        SESSION.commit()
        return True
Exemplo n.º 24
0
def new_fed(owner_id, fed_name, fed_id):
    with FEDS_LOCK:
        global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME
        fed = Federations(str(owner_id), fed_name, str(fed_id),
                          'Rules is not set in this federation.', None,
                          str({
                              'owner': str(owner_id),
                              'members': '[]'
                          }))
        SESSION.add(fed)
        SESSION.commit()
        FEDERATION_BYOWNER[str(owner_id)] = ({
            'fid':
            str(fed_id),
            'fname':
            fed_name,
            'frules':
            'Rules is not set in this federation.',
            'flog':
            None,
            'fusers':
            str({
                'owner': str(owner_id),
                'members': '[]'
            })
        })
        FEDERATION_BYFEDID[str(fed_id)] = ({
            'owner':
            str(owner_id),
            'fname':
            fed_name,
            'frules':
            'Rules is not set in this federation.',
            'flog':
            None,
            'fusers':
            str({
                'owner': str(owner_id),
                'members': '[]'
            })
        })
        FEDERATION_BYNAME[fed_name] = ({
            'fid':
            str(fed_id),
            'owner':
            str(owner_id),
            'frules':
            'Rules is not set in this federation.',
            'flog':
            None,
            'fusers':
            str({
                'owner': str(owner_id),
                'members': '[]'
            })
        })
        return fed
Exemplo n.º 25
0
def set_chat_setting(chat_id: Union[int, str], setting: bool):
    with CHAT_LOCK:
        chat_setting = SESSION.query(ReportingChatSettings).get(str(chat_id))
        if not chat_setting:
            chat_setting = ReportingChatSettings(chat_id)

        chat_setting.should_report = setting
        SESSION.add(chat_setting)
        SESSION.commit()
Exemplo n.º 26
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
Exemplo n.º 27
0
def set_user_setting(user_id: int, setting: bool):
    with USER_LOCK:
        user_setting = SESSION.query(ReportingUserSettings).get(user_id)
        if not user_setting:
            user_setting = ReportingUserSettings(user_id)

        user_setting.should_report = setting
        SESSION.add(user_setting)
        SESSION.commit()
Exemplo n.º 28
0
def set_false(chat_id):
    with INSERTION_LOCK:
        lewdfalse = SESSION.query(Lewds).get(str(chat_id))
        if not lewdfalse:
            lewdfalse = Lewds(str(chat_id))
        else:
            pass
        SESSION.add(lewdfalse)
        SESSION.commit()
Exemplo n.º 29
0
def set_warn_strength(chat_id, soft_warn):
    with WARN_SETTINGS_LOCK:
        curr_setting = SESSION.query(WarnSettings).get(str(chat_id))
        if not curr_setting:
            curr_setting = WarnSettings(chat_id, soft_warn=soft_warn)

        curr_setting.soft_warn = soft_warn

        SESSION.add(curr_setting)
        SESSION.commit()
Exemplo n.º 30
0
def set_warn_limit(chat_id, warn_limit):
    with WARN_SETTINGS_LOCK:
        curr_setting = SESSION.query(WarnSettings).get(str(chat_id))
        if not curr_setting:
            curr_setting = WarnSettings(chat_id, warn_limit=warn_limit)

        curr_setting.warn_limit = warn_limit

        SESSION.add(curr_setting)
        SESSION.commit()