def global_ignore_command(command): command = command.lower() with CLEANER_GLOBAL_LOCK: ignored = SESSION.query(CleanerBlueTextGlobal).get(str(command)) if not ignored: GLOBAL_IGNORE_COMMANDS.add(command) ignored = CleanerBlueTextGlobal(str(command)) SESSION.add(ignored) SESSION.commit() return True SESSION.close() return False
def global_unignore_command(command): command = command.lower() with CLEANER_GLOBAL_LOCK: unignored = SESSION.query(CleanerBlueTextGlobal).get(str(command)) if unignored: if command in GLOBAL_IGNORE_COMMANDS: GLOBAL_IGNORE_COMMANDS.remove(command) SESSION.delete(command) SESSION.commit() return True SESSION.close() return False
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 is 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 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 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 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 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) if fed_id in MYFEDS_SUBSCRIBER.get(my_fed, set()): # sanity check MYFEDS_SUBSCRIBER.get(my_fed, set()).remove(fed_id) SESSION.delete(getsubs) SESSION.commit() return True SESSION.close() return False
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 un_fban_user(fed_id, user_id): 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) try: SESSION.commit() except: SESSION.rollback() return False finally: SESSION.commit() __load_all_feds_banned() return I
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 elif lock_type == 'anonchannel': curr_perm.anonchannel = locked SESSION.add(curr_perm) SESSION.commit()
def chat_leave_fed(chat_id): with FEDS_LOCK: global FEDERATION_CHATS, FEDERATION_CHATS_BYID # Set variables fed_info = FEDERATION_CHATS.get(str(chat_id)) if fed_info is None: return False fed_id = fed_info['fid'] # Delete from cache FEDERATION_CHATS.pop(str(chat_id)) FEDERATION_CHATS_BYID[str(fed_id)].remove(str(chat_id)) # Delete from db curr = SESSION.query(ChatF).all() for U in curr: if int(U.chat_id) == int(chat_id): SESSION.delete(U) SESSION.commit() return True
def is_restr_locked(chat_id, lock_type): curr_restr = SESSION.query(Restrictions).get(str(chat_id)) SESSION.close() if not curr_restr: return False if lock_type == "messages": return curr_restr.messages elif lock_type == "media": return curr_restr.media elif lock_type == "other": return curr_restr.other elif lock_type == "previews": return curr_restr.preview elif lock_type == "all": return (curr_restr.messages and curr_restr.media and curr_restr.other and curr_restr.preview)
def rename_fed(fed_id, owner_id, newname): with FEDS_LOCK: global FEDERATION_BYFEDID, FEDERATION_BYOWNER, FEDERATION_BYNAME fed = SESSION.query(Federations).get(fed_id) if not fed: return False fed.fed_name = newname SESSION.commit() # Update the dicts oldname = FEDERATION_BYFEDID[str(fed_id)]["fname"] tempdata = FEDERATION_BYNAME[oldname] FEDERATION_BYNAME.pop(oldname) FEDERATION_BYOWNER[str(owner_id)]["fname"] = newname FEDERATION_BYFEDID[str(fed_id)]["fname"] = newname FEDERATION_BYNAME[newname] = tempdata return True
def set_cleanbt(chat_id, is_enable): with CLEANER_CHAT_SETTINGS: curr = SESSION.query(CleanerBlueTextChatSettings).get(str(chat_id)) if not curr: curr = CleanerBlueTextChatSettings(str(chat_id), is_enable) else: curr.is_enabled = is_enable if str(chat_id) not in CLEANER_CHATS: CLEANER_CHATS.setdefault(str(chat_id), { "setting": False, "commands": set() }) CLEANER_CHATS[str(chat_id)]["setting"] = is_enable SESSION.add(curr) 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: warned_user.reasons = warned_user.reasons + [ reason ] # TODO:: double check this wizardry reasons = warned_user.reasons num = warned_user.num_warns SESSION.add(warned_user) SESSION.commit() return num, reasons
def __load_all_feds_chats(): global FEDERATION_CHATS, FEDERATION_CHATS_BYID try: qall = SESSION.query(ChatF).all() FEDERATION_CHATS = {} FEDERATION_CHATS_BYID = {} for x in qall: # Federation Chats check = FEDERATION_CHATS.get(x.chat_id) if check is None: FEDERATION_CHATS[x.chat_id] = {} FEDERATION_CHATS[x.chat_id] = {"chat_name": x.chat_name, "fid": x.fed_id} # Federation Chats By ID check = FEDERATION_CHATS_BYID.get(x.fed_id) if check is None: FEDERATION_CHATS_BYID[x.fed_id] = [] FEDERATION_CHATS_BYID[x.fed_id].append(x.chat_id) finally: SESSION.close()
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 is_locked(chat_id, lock_type): curr_perm = SESSION.query(Permissions).get(str(chat_id)) SESSION.close() if not curr_perm: return False elif lock_type == "sticker": return curr_perm.sticker elif lock_type == "photo": return curr_perm.photo elif lock_type == "audio": return curr_perm.audio elif lock_type == "voice": return curr_perm.voice elif lock_type == "contact": return curr_perm.contact elif lock_type == "video": return curr_perm.video elif lock_type == "document": return curr_perm.document elif lock_type == "gif": return curr_perm.gif elif lock_type == "url": return curr_perm.url elif lock_type == "bots": return curr_perm.bots elif lock_type == "forward": return curr_perm.forward elif lock_type == "game": return curr_perm.game elif lock_type == "location": return curr_perm.location elif lock_type == "rtl": return curr_perm.rtl elif lock_type == "button": return curr_perm.button elif lock_type == "egame": return curr_perm.egame elif lock_type == "inline": return curr_perm.inline elif lock_type == "anonchannel": return curr_perm.anonchannel
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
def update_restriction(chat_id, restr_type, locked): with RESTR_LOCK: curr_restr = SESSION.query(Restrictions).get(str(chat_id)) if not curr_restr: curr_restr = init_restrictions(chat_id) if restr_type == "messages": curr_restr.messages = locked elif restr_type == "media": curr_restr.media = locked elif restr_type == "other": curr_restr.other = locked elif restr_type == "previews": curr_restr.preview = locked elif restr_type == "all": curr_restr.messages = locked curr_restr.media = locked curr_restr.other = locked curr_restr.preview = locked SESSION.add(curr_restr) SESSION.commit()
def __load_all_feds(): global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME try: feds = SESSION.query(Federations).all() for x in feds: # remove tuple by ( ,) # Fed by Owner check = FEDERATION_BYOWNER.get(x.owner_id) if check is None: FEDERATION_BYOWNER[x.owner_id] = [] FEDERATION_BYOWNER[str(x.owner_id)] = { 'fid': str(x.fed_id), 'fname': x.fed_name, 'frules': x.fed_rules, 'flog': x.fed_log, 'fusers': str(x.fed_users) } # Fed By FedId check = FEDERATION_BYFEDID.get(x.fed_id) if check is None: FEDERATION_BYFEDID[x.fed_id] = [] FEDERATION_BYFEDID[str(x.fed_id)] = { 'owner': str(x.owner_id), 'fname': x.fed_name, 'frules': x.fed_rules, 'flog': x.fed_log, 'fusers': str(x.fed_users) } # Fed By Name check = FEDERATION_BYNAME.get(x.fed_name) if check is None: FEDERATION_BYNAME[x.fed_name] = [] FEDERATION_BYNAME[x.fed_name] = { 'fid': str(x.fed_id), 'owner': str(x.owner_id), 'frules': x.fed_rules, 'flog': x.fed_log, 'fusers': str(x.fed_users) } finally: SESSION.close()
def chat_unignore_command(chat_id, unignore): unignore = unignore.lower() with CLEANER_CHAT_LOCK: unignored = SESSION.query(CleanerBlueTextChat).get( (str(chat_id), unignore)) if unignored: if str(chat_id) not in CLEANER_CHATS: CLEANER_CHATS.setdefault(str(chat_id), { "setting": False, "commands": set() }) if unignore in CLEANER_CHATS.get(str(chat_id)).get("commands"): CLEANER_CHATS[str(chat_id)]["commands"].remove(unignore) SESSION.delete(unignored) SESSION.commit() return True SESSION.close() return False
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
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 __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 is 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 is 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 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_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_all_repos(chat_id): try: return SESSION.query(GitHub).filter( GitHub.chat_id == str(chat_id)).order_by(GitHub.name.asc()).all() finally: SESSION.close()
def get_repo(chat_id, name): try: return SESSION.query(GitHub).get((str(chat_id), name)) finally: SESSION.close()
def get_user_bio(user_id): userbio = SESSION.query(UserBio).get(user_id) SESSION.close() if userbio: return userbio.bio return None