def set_welcome_security(chat_id, security, extra_verify, mute_time, timeout, timeout_mode, custom_text): with WS_LOCK: curr_setting = SESSION.query(WelcomeSecurity).get((str(chat_id))) if not curr_setting: curr_setting = WelcomeSecurity(chat_id, security=security, extra_verify=extra_verify, mute_time=mute_time, timeout=timeout, timeout_mode=timeout_mode, custom_text=custom_text) curr_setting.security = bool(security) curr_setting.extra_verify = bool(extra_verify) curr_setting.mute_time = str(mute_time) curr_setting.timeout = str(timeout) curr_setting.timeout_mode = int(timeout_mode) curr_setting.custom_text = str(custom_text) SESSION.add(curr_setting) SESSION.commit()
def user_join_fed(fed_id, user_id): 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_rules = getfed['frules'] fed_log = getfed['flog'] # Temp set members = eval(eval(getfed['fusers'])['members']) members.append(user_id) # Set user FEDERATION_BYOWNER[str(owner_id)]['fusers'] = str({ 'owner': str(owner_id), 'members': str(members) }) FEDERATION_BYFEDID[str(fed_id)]['fusers'] = str({ 'owner': str(owner_id), 'members': str(members) }) FEDERATION_BYNAME[fed_name]['fusers'] = str({ 'owner': str(owner_id), 'members': str(members) }) # Set on database fed = Federations( str(owner_id), fed_name, str(fed_id), fed_rules, fed_log, str({ 'owner': str(owner_id), 'members': str(members) })) SESSION.merge(fed) SESSION.commit() __load_all_feds_chats() return True
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
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
def rm_from_userlist(chat_id, user_id): with UR_LOCK: user_filt = SESSION.query(UserRestrict).get((str(chat_id), user_id)) if user_filt: if user_id in CHAT_USERRESTRICT.get(str(chat_id)): # sanity check CHAT_USERRESTRICT.get(str(chat_id)).pop(user_id) SESSION.delete(user_filt) SESSION.commit() return True SESSION.close() return False
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 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
def get_fban_user(fed_id, user_id): list_fbanned = FEDERATION_BANNED_USERID.get(fed_id) if list_fbanned == 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 else: return False, None, None
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 == 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 == 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 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 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 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
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 warned_user.reasons = warned_user.reasons[:-1] SESSION.add(warned_user) SESSION.commit() removed = True SESSION.close() return removed
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
def migrate_chat(old_chat_id, new_chat_id): with PERM_LOCK: perms = SESSION.query(Permissions).get(str(old_chat_id)) if perms: perms.chat_id = str(new_chat_id) SESSION.commit() with RESTR_LOCK: rest = SESSION.query(Restrictions).get(str(old_chat_id)) if rest: rest.chat_id = str(new_chat_id) SESSION.commit()
def migrate_chat(old_chat_id, new_chat_id): with WARN_INSERTION_LOCK: chat_notes = (SESSION.query(Warns).filter( Warns.chat_id == str(old_chat_id)).all()) for note in chat_notes: note.chat_id = str(new_chat_id) SESSION.commit() with WARN_FILTER_INSERTION_LOCK: chat_filters = (SESSION.query(WarnFilters).filter( WarnFilters.chat_id == str(old_chat_id)).all()) for filt in chat_filters: filt.chat_id = str(new_chat_id) SESSION.commit() WARN_FILTERS[str(new_chat_id)] = WARN_FILTERS[str(old_chat_id)] del WARN_FILTERS[str(old_chat_id)] with WARN_SETTINGS_LOCK: chat_settings = (SESSION.query(WarnSettings).filter( WarnSettings.chat_id == str(old_chat_id)).all()) for setting in chat_settings: setting.chat_id = str(new_chat_id) SESSION.commit()
def set_cleanbt(chat_id, is_enable): with INSERTION_LOCK: curr = SESSION.query(CleanerBlueText).get(str(chat_id)) if curr: SESSION.delete(curr) curr = CleanerBlueText(str(chat_id), is_enable) if is_enable: if str(chat_id) not in CLEANER_BT_CHATS: CLEANER_BT_CHATS.append(str(chat_id)) else: if str(chat_id) in CLEANER_BT_CHATS: CLEANER_BT_CHATS.remove(str(chat_id)) SESSION.add(curr) SESSION.commit()
def disabledel_set(chat_id, is_enable): with DISABLEDEL_INSERTION_LOCK: curr = SESSION.query(DisableDelete).get(str(chat_id)) if curr: SESSION.delete(curr) curr = DisableDelete(str(chat_id), is_enable) if is_enable: if str(chat_id) not in DISABLEDEL: DISABLEDEL.append(str(chat_id)) else: if str(chat_id) in DISABLEDEL: DISABLEDEL.remove(str(chat_id)) SESSION.add(curr) SESSION.commit()
def migrate_chat(old_chat_id, new_chat_id): with CUST_FILT_LOCK: chat_filters = SESSION.query(CustomFilters).filter( CustomFilters.chat_id == str(old_chat_id)).all() for filt in chat_filters: filt.chat_id = str(new_chat_id) SESSION.commit() CHAT_FILTERS[str(new_chat_id)] = CHAT_FILTERS[str(old_chat_id)] del CHAT_FILTERS[str(old_chat_id)] with BUTTON_LOCK: chat_buttons = SESSION.query(Buttons).filter( Buttons.chat_id == str(old_chat_id)).all() for btn in chat_buttons: btn.chat_id = str(new_chat_id) SESSION.commit()
def remove_warn(user_id, chat_id): with WARN_INSERTION_LOCK: removed = False warned_user = SESSION.query(Warns).get((user_id, str(chat_id))) temp_reason = [] if warned_user and warned_user.num_warns > 0: warned_user.num_warns -= 1 if warned_user and warned_user.reasons is not None: len(warned_user.reasons) for reason in warned_user.reasons: temp_reason.append(reason) del temp_reason[-1] warned_user.reasons = temp_reason SESSION.add(warned_user) SESSION.commit() removed = True SESSION.close() return removed
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 == 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]: delchats = SESSION.query(ChatF).get(str(x)) if delchats: SESSION.delete(delchats) SESSION.commit() FEDERATION_CHATS.pop(x) FEDERATION_CHATS_BYID.pop(fed_id) # Delete fedban users getall = FEDERATION_BANNED_USERID.get(fed_id) if getall: for x in getall: banlist = SESSION.query(BansF).get((fed_id, str(x))) if banlist: SESSION.delete(banlist) SESSION.commit() if FEDERATION_BANNED_USERID.get(fed_id): FEDERATION_BANNED_USERID.pop(fed_id) if FEDERATION_BANNED_FULL.get(fed_id): FEDERATION_BANNED_FULL.pop(fed_id) # Delete fedsubs getall = MYFEDS_SUBSCRIBER.get(fed_id) if getall: for x in getall: getsubs = SESSION.query(FedSubs).get((fed_id, str(x))) if getsubs: SESSION.delete(getsubs) SESSION.commit() if FEDS_SUBSCRIBER.get(fed_id): FEDS_SUBSCRIBER.pop(fed_id) if MYFEDS_SUBSCRIBER.get(fed_id): MYFEDS_SUBSCRIBER.pop(fed_id) # Delete from database curr = SESSION.query(Federations).get(fed_id) if curr: SESSION.delete(curr) SESSION.commit() return True
def num_chats(): try: return SESSION.query(func.count(distinct(Notes.chat_id))).scalar() finally: SESSION.close()
def num_notes(): try: return SESSION.query(Notes).count() finally: SESSION.close()
def get_private_note(chat_id): curr = SESSION.query(PrivateNote).get(str(chat_id)) if curr: return curr.is_private, curr.is_delete else: return False, False
def add_note_button_to_db(chat_id, note_name, b_name, url, same_line): with BUTTONS_INSERTION_LOCK: button = Buttons(chat_id, note_name, b_name, url, same_line) SESSION.add(button) SESSION.commit()
def get_all_chat_notes(chat_id): try: return SESSION.query(Notes).filter( Notes.chat_id == str(chat_id)).order_by(Notes.name.asc()).all() finally: SESSION.close()
def get_note(chat_id, note_name): try: return SESSION.query(Notes).get((str(chat_id), note_name)) finally: SESSION.close()
def user_demote_fed(fed_id, user_id): 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_rules = getfed['frules'] fed_log = getfed['flog'] # Temp set try: members = eval(eval(getfed['fusers'])['members']) except ValueError: return False members.remove(user_id) # Set user FEDERATION_BYOWNER[str(owner_id)]['fusers'] = str({ 'owner': str(owner_id), 'members': str(members) }) FEDERATION_BYFEDID[str(fed_id)]['fusers'] = str({ 'owner': str(owner_id), 'members': str(members) }) FEDERATION_BYNAME[fed_name]['fusers'] = str({ 'owner': str(owner_id), 'members': str(members) }) # Set on database fed = Federations( str(owner_id), fed_name, str(fed_id), fed_rules, fed_log, str({ 'owner': str(owner_id), 'members': str(members) })) SESSION.merge(fed) 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.fed_id == fed_id: SESSION.delete(r) SESSION.commit() result = True SESSION.close() return result
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
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