Example #1
0
def rm_repo(chat_id, name):
    with GIT_LOCK:
        if repo := SESSION.query(GitHub).get((str(chat_id), name)):
            SESSION.delete(repo)
            SESSION.commit()
            return True
        else:
Example #2
0
def add_repo_to_db(chat_id, name, value, backoffset):
    with GIT_LOCK:
        if prev := SESSION.query(GitHub).get((str(chat_id), name)):
            SESSION.delete(prev)
        repo = GitHub(str(chat_id), name, value, backoffset)
        SESSION.add(repo)
        SESSION.commit()
Example #3
0
def disconnect(user_id):
    with CONNECTION_INSERTION_LOCK:
        if disconnect := SESSION.query(Connection).get((int(user_id))):
            SESSION.delete(disconnect)
            SESSION.commit()
            return True
        else:
Example #4
0
def del_fed(fed_id):
    with FEDS_LOCK:
        global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME, FEDERATION_CHATS, FEDERATION_CHATS_BYID, FEDERATION_BANNED_USERID, FEDERATION_BANNED_FULL
        getcache = FEDERATION_BYFEDID.get(fed_id)
        if getcache is None:
            return False
        # Variables
        getfed = FEDERATION_BYFEDID.get(fed_id)
        owner_id = getfed['owner']
        fed_name = getfed['fname']
        # Delete from cache
        FEDERATION_BYOWNER.pop(owner_id)
        FEDERATION_BYFEDID.pop(fed_id)
        FEDERATION_BYNAME.pop(fed_name)
        if FEDERATION_CHATS_BYID.get(fed_id):
            for x in FEDERATION_CHATS_BYID[fed_id]:
                if delchats := SESSION.query(ChatF).get(str(x)):
                    SESSION.delete(delchats)
                    SESSION.commit()
                FEDERATION_CHATS.pop(x)
            FEDERATION_CHATS_BYID.pop(fed_id)
        if getall := FEDERATION_BANNED_USERID.get(fed_id):
            for x in getall:
                if banlist := SESSION.query(BansF).get((fed_id, str(x))):
                    SESSION.delete(banlist)
                    SESSION.commit()
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 unwhitelistChat(chat_id):
    with ALLOWCHATLOCK:
        chat = SESSION.query(AllowedChat).get(chat_id)
        if chat:
            SESSION.delete(chat)
        SESSION.commit()
        __load_whitelisted_chats_list()
Example #7
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 #8
0
def __load_feds_subscriber():
    global FEDS_SUBSCRIBER
    global MYFEDS_SUBSCRIBER
    try:
        feds = SESSION.query(FedSubs.fed_id).distinct().all()
        for (fed_id, ) in feds:  # remove tuple by ( ,)
            FEDS_SUBSCRIBER[fed_id] = []
            MYFEDS_SUBSCRIBER[fed_id] = []

        all_fedsubs = SESSION.query(FedSubs).all()
        for x in all_fedsubs:
            FEDS_SUBSCRIBER[x.fed_id] += [x.fed_subs]
            try:
                MYFEDS_SUBSCRIBER[x.fed_subs] += [x.fed_id]
            except KeyError:
                getsubs = SESSION.query(FedSubs).get((x.fed_id, x.fed_subs))
                if getsubs:
                    SESSION.delete(getsubs)
                    SESSION.commit()

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

    finally:
        SESSION.close()
Example #9
0
def set_custom_gdbye(chat_id,
                     custom_content_leave,
                     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 or custom_content_leave:
            welcome_settings.custom_content_leave = custom_content_leave
            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 #10
0
def switch_to_locale(chat_id, locale_name):
    with LOCALES_INSERTION_LOCK:
        if prev := SESSION.query(Locales).get((str(chat_id))):
            SESSION.delete(prev)
        switch_locale = Locales(str(chat_id), locale_name)
        SESSION.add(switch_locale)
        SESSION.commit()
Example #11
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 #12
0
def new_add_filter(chat_id, keyword, reply_text, file_type, file_id, buttons):
	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="there is should be a new reply", is_sticker=False, is_document=False, is_image=False, is_audio=False, is_voice=False, is_video=False, has_buttons=bool(buttons), reply_text=reply_text, file_type=file_type.value, file_id=file_id)

		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 #13
0
def ungmute_user(user_id):
    with GMUTED_USERS_LOCK:
        user = SESSION.query(GloballyMutedUsers).get(user_id)
        if user:
            SESSION.delete(user)

        SESSION.commit()
        __load_gmuted_userid_list()
Example #14
0
def add_history(user_id, chat_id1, chat_id2, chat_id3, updated):
    with HISTORY_LOCK:
        if prev := SESSION.query(ConnectionHistory).get((int(user_id))):
            SESSION.delete(prev)
        history = ConnectionHistory(user_id, chat_id1, chat_id2, chat_id3,
                                    updated)
        SESSION.add(history)
        SESSION.commit()
Example #15
0
def connect(user_id, chat_id):
    with CONNECTION_INSERTION_LOCK:
        if prev := SESSION.query(Connection).get((int(user_id))):
            SESSION.delete(prev)
        connect_to_chat = Connection(int(user_id), chat_id)
        SESSION.add(connect_to_chat)
        SESSION.commit()
        return True
Example #16
0
def remove_url(tg_chat_id, tg_feed_link):
    with INSERTION_LOCK:
        # this loops to delete any possible duplicates for the same TG User ID, TG Chat ID and link
        for row in check_url_availability(tg_chat_id, tg_feed_link):
            # add the action to the DB query
            SESSION.delete(row)

        SESSION.commit()
Example #17
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 #18
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 #19
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 #20
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 #21
0
def stop_chat_logging(chat_id):
    with LOGS_INSERTION_LOCK:
        if res := SESSION.query(GroupLogs).get(str(chat_id)):
            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 #22
0
def private_rules(chat_id, is_private):
    with PR_INSERTION_LOCK:
        curr = SESSION.query(PrivateRules).get(str(chat_id))
        if curr:
            SESSION.delete(curr)

        curr = PrivateRules(str(chat_id), is_private)

        SESSION.add(curr)
        SESSION.commit()
Example #23
0
def set_cas_status(chat_id, status):
    with CAS_LOCK:
        ban = False
        prevObj = SESSION.query(CombotCASStatus).get(str(chat_id))
        if prevObj:
            ban = prevObj.autoban
            SESSION.delete(prevObj)
        newObj = CombotCASStatus(str(chat_id), status, ban)
        SESSION.add(newObj)
        SESSION.commit()
Example #24
0
def private_note(chat_id, is_private, is_delete):
    with PMNOTE_INSERTION_LOCK:
        curr = SESSION.query(PrivateNote).get(str(chat_id))
        if curr:
            SESSION.delete(curr)

        curr = PrivateNote(str(chat_id), is_private, is_delete)

        SESSION.add(curr)
        SESSION.commit()
Example #25
0
def set_cas_autoban(chat_id, autoban):
    with CAS_LOCK:
        status = True
        prevObj = SESSION.query(CombotCASStatus).get(str(chat_id))
        if prevObj:
            status = prevObj.status
            SESSION.delete(prevObj)
        newObj = CombotCASStatus(str(chat_id), status, autoban)
        SESSION.add(newObj)
        SESSION.commit()
Example #26
0
def clear_user_info(user_id):
    with INSERTION_LOCK:
        curr = SESSION.query(UserInfo).get(user_id)
        if curr:
            SESSION.delete(curr)
            SESSION.commit()
            return True

        SESSION.close()
    return False
Example #27
0
def rm_from_timeout(chat_id, user_id):
    with TO_LOCK:
        user_filt = SESSION.query(WelcomeTimeout).get((str(chat_id), user_id))
        if user_filt:
            SESSION.delete(user_filt)
            SESSION.commit()
            return True

        SESSION.close()
        return False
Example #28
0
def clear_history_conn(user_id):
    global HISTORY_CONNECT
    todel = list(HISTORY_CONNECT[int(user_id)])
    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)
    SESSION.commit()
    return True
Example #29
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 #30
0
def rm_afk(user_id):
    with INSERTION_LOCK:
        if curr := SESSION.query(AFK).get(user_id):
            if user_id in AFK_USERS:  # sanity check
                del AFK_USERS[user_id]

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

        SESSION.close()
        return False