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()
def add_welcome_setting(chat_id, custom_welcome_message, should_clean_welcome, previous_welcome, media_file_id): # adder = SESSION.query(Welcome).get(chat_id) adder = Welcome(chat_id, custom_welcome_message, should_clean_welcome, previous_welcome, media_file_id) SESSION.add(adder) SESSION.commit()
def addgvar(variable, value): if SESSION.query(Globals).filter( Globals.variable == str(variable)).one_or_none(): delgvar(variable) adder = Globals(str(variable), value) SESSION.add(adder) SESSION.commit()
def add_to_blacklist(chat_id, trigger): with BLACKLIST_FILTER_INSERTION_LOCK: blacklist_filt = BlackListFilters(str(chat_id), trigger) SESSION.merge(blacklist_filt) # merge to avoid duplicate key issues SESSION.commit() CHAT_BLACKLISTS.setdefault(str(chat_id), set()).add(trigger)
def add_channel(chat_id, channel): adder = SESSION.query(forceSubscribe).get(chat_id) if adder: adder.channel = channel else: adder = forceSubscribe(chat_id, channel) SESSION.add(adder) SESSION.commit()
def delglobal(variable): rem = ( SESSION.query(Global) .filter(Global.variable == str(variable)) .delete(synchronize_session="fetch") ) if rem: SESSION.commit()
def rmwafu_setting(chat_id): try: rem = SESSION.query(Joinwafu).get(str(chat_id)) if rem: SESSION.delete(rem) SESSION.commit() return True except BaseException: return False
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 migrate_chat(old_chat_id, new_chat_id): with INSERTION_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 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 add_sub(my_fed, fed_id): #LEGENDX22 IS BEST mime = FedSubs(my_fed, fed_id) SESSION.merge(mime) # merge to avoid duplicate key issues SESSION.commit() global MYFEDS_SUBSCRIBER if MYFEDS_SUBSCRIBER.get(my_fed, set()) == set(): MYFEDS_SUBSCRIBER[my_fed] = {fed_id} else: MYFEDS_SUBSCRIBER.get(my_fed, set()).add(fed_id) return True
def rem_sub(my_fed, fed_id): sox = SESSION.query(FedSubs).get((my_fed, fed_id)) if sox: if fed_id in MYFEDS_SUBSCRIBER.get(my_fed, set()): # sanity check MYFEDS_SUBSCRIBER.get(my_fed, set()).remove(fed_id) SESSION.delete(sox) 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 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 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 add_filter(chat_id, keyword, reply, snip_type, media_id, media_access_hash, media_file_reference): adder = SESSION.query(Filters).get((str(chat_id), keyword)) if adder: adder.reply = reply adder.snip_type = snip_type adder.media_id = media_id adder.media_access_hash = media_access_hash adder.media_file_reference = media_file_reference else: adder = Filters(chat_id, keyword, reply, snip_type, media_id, media_access_hash, media_file_reference) SESSION.add(adder) SESSION.commit()
def chat_join_fed(fed_id, chat_name, chat_id): with FEDS_LOCK: global FEDERATION_CHATS, FEDERATION_CHATS_BYID r = ChatF(chat_id, chat_name, fed_id) SESSION.add(r) FEDERATION_CHATS[str(chat_id)] = { "chat_name": chat_name, "fid": fed_id } checkid = FEDERATION_CHATS_BYID.get(fed_id) if checkid is None: FEDERATION_CHATS_BYID[fed_id] = [] FEDERATION_CHATS_BYID[fed_id].append(str(chat_id)) SESSION.commit() return r
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 subs_fed(fed_id, my_fed): check = get_spec_subs(fed_id, my_fed) if check: return False with FEDS_SUBSCRIBER_LOCK: subsfed = FedSubs(fed_id, my_fed) SESSION.merge(subsfed) # merge to avoid duplicate key issues SESSION.commit() global FEDS_SUBSCRIBER if FEDS_SUBSCRIBER.get(fed_id, set()) == set(): FEDS_SUBSCRIBER[fed_id] = {my_fed} else: FEDS_SUBSCRIBER.get(fed_id, set()).add(my_fed) return True
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 new_fed(owner_id, fed_name, fed_id): with FEDS_LOCK: global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME fed = Federations( str(owner_id), fed_name, str(fed_id), "Rules is not set in this federation.", None, str({ "owner": str(owner_id), "members": "[]" }), ) SESSION.add(fed) SESSION.commit() FEDERATION_BYOWNER[str(owner_id)] = { "fid": str(fed_id), "fname": fed_name, "frules": "Rules is not set in this federation.", "flog": None, "fusers": str({ "owner": str(owner_id), "members": "[]" }), } FEDERATION_BYFEDID[str(fed_id)] = { "owner": str(owner_id), "fname": fed_name, "frules": "Rules is not set in this federation.", "flog": None, "fusers": str({ "owner": str(owner_id), "members": "[]" }), } FEDERATION_BYNAME[fed_name] = { "fid": str(fed_id), "owner": str(owner_id), "frules": "Rules is not set in this federation.", "flog": None, "fusers": str({ "owner": str(owner_id), "members": "[]" }), } return fed
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 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 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 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 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 set_frules(fed_id, rules): 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_members = getfed["fusers"] fed_rules = str(rules) fed_log = getfed["flog"] # Set user FEDERATION_BYOWNER[str(owner_id)]["frules"] = fed_rules FEDERATION_BYFEDID[str(fed_id)]["frules"] = fed_rules FEDERATION_BYNAME[fed_name]["frules"] = fed_rules # Set on database fed = Federations(str(owner_id), fed_name, str(fed_id), fed_rules, fed_log, str(fed_members)) SESSION.merge(fed) SESSION.commit() return True
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 addwafu_setting(chat_id, previous_wafu, reply, f_mesg_id): to_check = getwafu(chat_id) if not to_check: adder = Joinwafu(chat_id, previous_wafu, reply, f_mesg_id) SESSION.add(adder) SESSION.commit() return True rem = SESSION.query(Joinwafu).get(str(chat_id)) SESSION.delete(rem) SESSION.commit() adder = Joinwafu(chat_id, previous_wafu, reply, f_mesg_id) SESSION.commit() return False
def ungmute(sender): rem = SESSION.query(GMute).get((str(sender))) if rem: SESSION.delete(rem) SESSION.commit()
def gmute(sender): adder = GMute(str(sender)) SESSION.add(adder) SESSION.commit()