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 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()
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()
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)
def migrate_chat(old_chat_id, new_chat_id): with INSERTION_LOCK: chat = SESSION.query(Welcome).get(str(old_chat_id)) if chat: chat.chat_id = str(new_chat_id) with WELC_BTN_LOCK: chat_buttons = ( SESSION.query(WelcomeButtons) .filter(WelcomeButtons.chat_id == str(old_chat_id)) .all() ) for btn in chat_buttons: btn.chat_id = str(new_chat_id) with LEAVE_BTN_LOCK: chat_buttons = ( SESSION.query(GoodbyeButtons) .filter(GoodbyeButtons.chat_id == str(old_chat_id)) .all() ) for btn in chat_buttons: btn.chat_id = str(new_chat_id) SESSION.commit()
def warn_user(user_id, chat_id, reason=None): 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 += 1 if reason == "": reason = "No reason given." if reason: if warned_user.reasons is None: warned_user.reasons = [reason] else: # TODO:: double check this Daan: Not really wizardry, it adds a # new entry to a list/array which can be done this way, # basically append equivalent warned_user.reasons = warned_user.reasons + [reason] reasons = warned_user.reasons num = warned_user.num_warns SESSION.add(warned_user) SESSION.commit() return num, reasons
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_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 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
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
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
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 reset_warns(user_id, chat_id): with WARN_INSERTION_LOCK: warned_user = SESSION.query(Warns).get((user_id, str(chat_id))) if warned_user: warned_user.num_warns = 0 warned_user.reasons = [] SESSION.add(warned_user) SESSION.commit() SESSION.close()
def disconnect(user_id): with CONNECTION_INSERTION_LOCK: disconnect = SESSION.query(Connection).get((int(user_id))) if disconnect: SESSION.delete(disconnect) SESSION.commit() return True else: SESSION.close() return False
def set_gdbye_preference(chat_id, should_goodbye): with INSERTION_LOCK: curr = SESSION.query(Welcome).get(str(chat_id)) if not curr: curr = Welcome(str(chat_id), should_goodbye=should_goodbye) else: curr.should_goodbye = should_goodbye SESSION.add(curr) SESSION.commit()
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()
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()
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()
def set_clean_welcome(chat_id, clean_welcome): with INSERTION_LOCK: curr = SESSION.query(Welcome).get(str(chat_id)) if not curr: curr = Welcome(str(chat_id)) curr.clean_welcome = int(clean_welcome) SESSION.add(curr) SESSION.commit()
def migrate_chat(old_chat_id, new_chat_id): with INSERTION_FLOOD_LOCK: flood = SESSION.query(FloodControl).get(str(old_chat_id)) if flood: CHAT_FLOOD[str(new_chat_id)] = CHAT_FLOOD.get( str(old_chat_id), DEF_OBJ ) flood.chat_id = str(new_chat_id) SESSION.commit() SESSION.close()
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 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 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 add_warn_filter(chat_id, keyword, reply): with WARN_FILTER_INSERTION_LOCK: warn_filt = WarnFilters(str(chat_id), keyword, reply) if keyword not in WARN_FILTERS.get(str(chat_id), []): WARN_FILTERS[str(chat_id)] = sorted( WARN_FILTERS.get(str(chat_id), []) + [keyword], key=lambda x: (-len(x), x), ) SESSION.merge(warn_filt) # merge to avoid duplicate key issues SESSION.commit()
def set_flood(chat_id, amount): with INSERTION_FLOOD_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 migrate_chat(old_chat_id, new_chat_id): with NOTES_INSERTION_LOCK: chat_notes = (SESSION.query(Notes).filter( Notes.chat_id == str(old_chat_id)).all()) for note in chat_notes: note.chat_id = str(new_chat_id) with BUTTONS_INSERTION_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 set_human_checks(user_id, chat_id): with INSERTION_LOCK: human_check = SESSION.query(WelcomeMuteUsers).get( (user_id, str(chat_id)) ) if not human_check: human_check = WelcomeMuteUsers(user_id, str(chat_id), True) else: human_check.human_check = True SESSION.add(human_check) SESSION.commit() return human_check
def set_flood_strength(chat_id, flood_type, value): # for flood_type # 1 = ban # 2 = kick # 3 = mute # 4 = tban # 5 = tmute with INSERTION_FLOOD_SETTINGS_LOCK: curr_setting = SESSION.query(FloodSettings).get(str(chat_id)) if not curr_setting: curr_setting = FloodSettings( chat_id, flood_type=int(flood_type), value=value ) curr_setting.flood_type = int(flood_type) curr_setting.value = str(value) SESSION.add(curr_setting) SESSION.commit()
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 elif lock_type == "rtl": curr_perm.rtl = locked elif lock_type == "button": curr_perm.button = locked elif lock_type == "egame": curr_perm.egame = locked elif lock_type == "inline": curr_perm.inline = locked SESSION.add(curr_perm) SESSION.commit()
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), }