def set_chat_log_channel(chat_id, log_channel): with LOGS_INSERTION_LOCK: res = SESSION.query(GroupLogs).get(str(chat_id)) if res: res.log_channel = log_channel else: res = GroupLogs(chat_id, log_channel) SESSION.add(res) CHANNELS[str(chat_id)] = log_channel SESSION.commit()
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 enable_antispam(chat_id): with ASPAM_SETTING_LOCK: chat = SESSION.query(AntispamSettings).get(str(chat_id)) if not chat: chat = AntispamSettings(chat_id, True) chat.setting = True SESSION.add(chat) SESSION.commit() if str(chat_id) in GBANSTAT_LIST: GBANSTAT_LIST.remove(str(chat_id))
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
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 toggle_afk(user_id, reason=""): with INSERTION_LOCK: curr = SESSION.query(AFK).get(user_id) if not curr: curr = AFK(user_id, reason, True) elif curr.is_afk: curr.is_afk = False elif not curr.is_afk: curr.is_afk = True SESSION.add(curr) SESSION.commit()
def set_ses(chat_id, ses_id, expires): with INSERTION_LOCK: autochat = SESSION.query(ChatbotChats).get(str(chat_id)) if not autochat: autochat = ChatbotChats(str(chat_id), str(ses_id), str(expires)) else: autochat.ses_id = str(ses_id) autochat.expires = str(expires) SESSION.add(autochat) SESSION.commit()
def migrate_chat(old_chat_id, new_chat_id): with DISABLE_INSERTION_LOCK: chats = SESSION.query(Disable).filter(Disable.chat_id == str(old_chat_id)).all() for chat in chats: chat.chat_id = str(new_chat_id) SESSION.add(chat) if str(old_chat_id) in DISABLED: DISABLED[str(new_chat_id)] = DISABLED.get(str(old_chat_id), set()) SESSION.commit()
def update_gmute_reason(user_id, name, reason=None): with GMUTED_USERS_LOCK: user = SESSION.query(GloballyMutedUsers).get(user_id) if not user: return False user.name = name user.reason = reason SESSION.merge(user) SESSION.commit() return True
def set_feds_setting(user_id: int, setting: bool): with FEDS_SETTINGS_LOCK: global FEDERATION_NOTIFICATION user_setting = SESSION.query(FedsUserSettings).get(user_id) if not user_setting: user_setting = FedsUserSettings(user_id) user_setting.should_report = setting FEDERATION_NOTIFICATION[str(user_id)] = setting SESSION.add(user_setting) SESSION.commit()
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
def __load_chat_settings_blacklists(): global CHAT_SETTINGS_BLACKLISTS try: chats_settings = SESSION.query(BlacklistSettings).all() for x in chats_settings: # remove tuple by ( ,) CHAT_SETTINGS_BLACKLISTS[x.chat_id] = { 'blacklist_type': x.blacklist_type, 'value': x.value } finally: SESSION.close()
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 not in FEDERATION_BANNED_USERID[fed_id]: return False, None r = SESSION.query(BansF).all() reason = None for I in r: if I.fed_id == fed_id and int(I.user_id) == int(user_id): reason = I.reason return True, reason
def __load_user_history(): global HISTORY_CONNECT try: qall = SESSION.query(ConnectionHistory).all() HISTORY_CONNECT = {} for x in qall: check = HISTORY_CONNECT.get(x.user_id) if check == None: HISTORY_CONNECT[x.user_id] = {} HISTORY_CONNECT[x.user_id][x.conn_time] = {'chat_name': x.chat_name, 'chat_id': x.chat_id} finally: SESSION.close()
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
def gban_user(user_id, name, reason=None): with GBANNED_USERS_LOCK: user = SESSION.query(GloballyBannedUsers).get(user_id) if not user: user = GloballyBannedUsers(user_id, name, reason) else: user.name = name user.reason = reason SESSION.merge(user) SESSION.commit() __load_gbanned_userid_list()
def update_gban_reason(user_id, name, reason=None): with GBANNED_USERS_LOCK: user = SESSION.query(GloballyBannedUsers).get(user_id) if not user: return None old_reason = user.reason user.name = name user.reason = reason SESSION.merge(user) SESSION.commit() return old_reason
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 set_afk(user_id, reason=""): with INSERTION_LOCK: curr = SESSION.query(AFK).get(user_id) if not curr: curr = AFK(user_id, reason, True) else: curr.is_afk = True AFK_USERS[user_id] = reason SESSION.add(curr) SESSION.commit()
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]: 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 from database curr = SESSION.query(Federations).get(fed_id) if curr: SESSION.delete(curr) SESSION.commit() return True
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)
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 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 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
def import_warns(user_id, chat_id, warns, reasons): with WARN_INSERTION_LOCK: warned_user = SESSION.query(Warns).get((user_id, str(chat_id))) if not warned_user: warned_user = Warns(user_id, str(chat_id)) warned_user.num_warns = warns warned_user.reasons = reasons SESSION.add(warned_user) SESSION.commit() return
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 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 set_flood(chat_id, amount): with INSERTION_LOCK: flood = SESSION.query(FloodControl).get(str(chat_id)) if not flood: flood = FloodControl(str(chat_id)) flood.user_id = None flood.limit = amount CHAT_FLOOD[str(chat_id)] = (None, DEF_COUNT, amount) SESSION.add(flood) SESSION.commit()
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