async def update_user(chat_id, new_user): old_user = await db.user_list.find_one({'user_id': new_user.id}) new_chat = [chat_id] if old_user and 'chats' in old_user: if old_user['chats']: new_chat = old_user['chats'] if not new_chat or chat_id not in new_chat: new_chat.append(chat_id) if old_user and 'first_detected_date' in old_user: first_detected_date = old_user['first_detected_date'] else: first_detected_date = datetime.datetime.now() if new_user.username: username = new_user.username.lower() else: username = None if hasattr(new_user, 'last_name') and new_user.last_name: last_name = html.escape(new_user.last_name, quote=False) else: last_name = None first_name = html.escape(new_user.first_name, quote=False) user_new = { 'user_id': new_user.id, 'first_name': first_name, 'last_name': last_name, 'username': username, 'user_lang': new_user.language_code, 'chats': new_chat, 'first_detected_date': first_detected_date } # Check on old user in DB with same username find_old_user = { 'username': user_new['username'], 'user_id': {'$ne': user_new['user_id']} } if user_new['username'] and (check := await db.user_list.find_one(find_old_user)): await db.user_list.delete_one({'_id': check['_id']}) log.info( f"Found user ({check['user_id']}) with same username as ({user_new['user_id']}), old user was deleted.")
async def update_users_handler(message): chat_id = message.chat.id # Update chat new_chat = message.chat if not new_chat.type == 'private': old_chat = await db.chat_list.find_one({'chat_id': chat_id}) if not hasattr(new_chat, 'username'): chatnick = None else: chatnick = new_chat.username if old_chat and 'first_detected_date' in old_chat: first_detected_date = old_chat['first_detected_date'] else: first_detected_date = datetime.datetime.now() chat_new = { "chat_id": chat_id, "chat_title": html.escape(new_chat.title, quote=False), "chat_nick": chatnick, "type": new_chat.type, "first_detected_date": first_detected_date } # Check on old chat in DB with same username find_old_chat = { 'chat_nick': chat_new['chat_nick'], 'chat_id': {'$ne': chat_new['chat_id']} } if chat_new['chat_nick'] and (check := await db.chat_list.find_one(find_old_chat)): await db.chat_list.delete_one({'_id': check['_id']}) log.info( f"Found chat ({check['chat_id']}) with same username as ({chat_new['chat_id']}), old chat was deleted.") await db.chat_list.update_one({'chat_id': chat_id}, {"$set": chat_new}, upsert=True) log.debug(f"Users: Chat {chat_id} updated")
# You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from apscheduler.executors.asyncio import AsyncIOExecutor from apscheduler.jobstores.redis import RedisJobStore from apscheduler.schedulers.asyncio import AsyncIOScheduler from pytz import utc from utah.config import get_str_key, get_int_key from utah.utils.logger import log DEFAULT = "default" jobstores = { DEFAULT: RedisJobStore(host=get_str_key("REDIS_URI"), port=get_str_key("REDIS_PORT"), password=get_str_key("REDIS_PASS")) } executors = {DEFAULT: AsyncIOExecutor()} job_defaults = {"coalesce": False, "max_instances": 3} scheduler = AsyncIOScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults, timezone=utc) log.info("Starting apscheduller...") scheduler.start()
if get_bool_key("DEBUG_MODE"): log.debug("Enabling logging middleware.") dp.middleware.setup(LoggingMiddleware()) LOAD = get_list_key("LOAD") DONT_LOAD = get_list_key("DONT_LOAD") if get_bool_key('LOAD_MODULES'): if len(LOAD) > 0: modules = LOAD else: modules = ALL_MODULES modules = [x for x in modules if x not in DONT_LOAD] log.info("Modules to load: %s", str(modules)) for module_name in modules: # Load pm_menu at last if module_name == 'pm_menu': continue log.debug(f"Importing <d><n>{module_name}</></>") imported_module = import_module("utah.modules." + module_name) if hasattr(imported_module, '__help__'): if hasattr(imported_module, '__mod_name__'): MOD_HELP[ imported_module.__mod_name__] = imported_module.__help__ else: MOD_HELP[imported_module.__name__] = imported_module.__help__ LOADED_MODULES.append(imported_module) log.info("Modules loaded!") else:
# GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from pymongo import UpdateOne from utah.services.mongo import mongodb from utah.utils.logger import log changelog = \ """ Utah database v8 Warns: Change serialization method of warnmodes (time based) """ log.info(changelog) log.info("Fetching all documents needed to update (in 'warnmode' collection)!") data = mongodb['warnmode'].find({'time': {"$exists": True}}) count = data.count() changed, deleted = 0, 0 updated_list = [] def _convert_time(__t: dict) -> str: from datetime import timedelta sec = timedelta(**__t).total_seconds() # this works on basis that days, hours, minutes are whole numbers! # check days first if sec % 86400 == 0:
from utah import BOT_USERNAME, dp from utah.config import get_bool_key from utah.modules.error import parse_update from utah.utils.filters import ALL_FILTERS from utah.utils.logger import log DEBUG_MODE = get_bool_key('DEBUG_MODE') ALLOW_F_COMMANDS = get_bool_key("ALLOW_FORWARDS_COMMANDS") ALLOW_COMMANDS_FROM_EXC = get_bool_key("ALLOW_COMMANDS_WITH_!") CMD_NOT_MONO = get_bool_key("DISALLOW_MONO_CMDS") REGISTRED_COMMANDS = [] COMMANDS_ALIASES = {} # Import filters log.info("Filters to load: %s", str(ALL_FILTERS)) for module_name in ALL_FILTERS: log.debug("Importing " + module_name) imported_module = import_module("utah.utils.filters." + module_name) log.info("Filters loaded!") def register(*args, cmds=None, f=None, allow_edited=True, allow_kwargs=False, **kwargs): if cmds and type(cmds) == str: cmds = [cmds]
async def channel_log(msg, info_log=True): chat_id = get_int_key('LOGS_CHANNEL_ID') if info_log: log.info(msg) await bot.send_message(chat_id, html.escape(msg, quote=False))
# You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import asyncio import logging import spamwatch from aiogram import Bot, Dispatcher, types from aiogram.bot.api import TelegramAPIServer, TELEGRAM_PRODUCTION from aiogram.contrib.fsm_storage.redis import RedisStorage2 from utah.config import get_str_key, get_int_key, get_list_key, get_bool_key from utah.utils.logger import log from utah.versions import UTAH_VERSION log.info("----------------------") log.info("| Utah |") log.info("----------------------") log.info("Version: " + UTAH_VERSION) if get_bool_key("DEBUG_MODE") is True: UTAH_VERSION += "-debug" log.setLevel(logging.DEBUG) log.warn( "! Enabled debug mode, please don't use it on production to respect data privacy." ) TOKEN = get_str_key("TOKEN", required=True) OWNER_ID = get_int_key("OWNER_ID", required=True) OPERATORS = list(get_list_key("OPERATORS"))
# it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from utah.services.mongo import mongodb from utah.utils.logger import log log.info('Utah Database v3') log.info('Support notes aliases') log.info('Starting updating all notes...') all_notes = mongodb.notes_v2.find({}) all_notes_count = all_notes.count() counter = 0 changed_notes = 0 for note in all_notes: counter += 1 log.info(f'Updating {counter} of {all_notes_count}...') if 'name' in note: changed_notes += 1 names = [note['name']] del note['name']
# License, or (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from pymongo import UpdateOne from utah.services.mongo import mongodb from utah.utils.logger import log log.info('Utah Database v6') log.info("Filters: migrate 'reply_message'") log.info('Starting to updating all filters...') all_filters = mongodb.filters.find({'action': 'reply_message'}) count = all_filters.count() changed = 0 updated_list = [] for i in all_filters: if not isinstance(i['reply_text'], dict): changed += 1 log.info(f'Updated {changed} filters of {count}') updated_list.append( UpdateOne({'_id': i['_id']}, { '$set': {
# License, or (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os import signal from utah.services.redis import redis from utah.utils.logger import log def exit_gracefully(signum, frame): log.warning("Bye!") try: redis.save() except Exception: log.error("Exiting immediately!") os.kill(os.getpid(), signal.SIGUSR1) # Signal exit log.info("Setting exit_gracefully task...") signal.signal(signal.SIGINT, exit_gracefully)
'REDIS_HOST': 'localhost', 'REDIS_PORT': 6379, 'REDIS_PASS': "******", 'MONGODB_URI': 'localhost', 'MONGO_DB': 'utah', 'API_PORT': 8080, 'JOIN_CONFIRM_DURATION': '30m', } CONFIG_PATH = 'data/bot_conf.yaml' if os.name == 'nt': log.debug("Detected Windows, changing config path...") CONFIG_PATH = os.getcwd() + "\\data\\bot_conf.yaml" if os.path.isfile(CONFIG_PATH): log.info(CONFIG_PATH) for item in (data := yaml.load(open('data/bot_conf.yaml', "r"), Loader=yaml.CLoader)): DEFAULTS[item.upper()] = data[item] else: log.info("Using env vars") def get_str_key(name, required=False): if name in DEFAULTS: default = DEFAULTS[name] else: default = None if not (data := env.str(name, default=default)) and not required: log.warn('No str key: ' + name) return None
# License, or (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from pymongo import InsertOne from utah.services.mongo import mongodb from utah.utils.logger import log log.info('Utah Database v5') log.info("Feds: migrate to old feds database structure") log.info('Starting updating all feds...') all_feds = mongodb.feds.find({}) all_feds_count = all_feds.count() counter = 0 changed_feds = 0 for fed in all_feds: counter += 1 log.info(f'Updating {counter} of {all_feds_count}...') queue = [] if 'banned' not in fed: continue
# it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from utah.services.mongo import mongodb from utah.utils.logger import log log.info('Utah Database v4') log.info("Filters: move 'note' to 'note_name'") log.info('Starting updating all filters...') all_filters = mongodb.filters.find({}) all_filters_count = all_filters.count() counter = 0 changed_filters = 0 for item in all_filters: counter += 1 log.info(f'Updating {counter} of {all_filters_count}...') if 'note' in item: changed_filters += 1 item['note_name'] = item['note'] del item['note']
async def notify_bot_owner(old_ver, new_ver): await bot.send_message( OWNER_ID, f"Texas database structure was updated from <code>{old_ver}</code> to <code>{new_ver}</code>" ) # TODO: Logs channel log.debug("Checking on database structure update...") if not (data := mongodb.db_structure.find_one({'db_ver': {"$exists": True}})): log.info("Your database is empty! Creating database structure key...") mongodb.db_structure.insert_one({'db_ver': DB_STRUCTURE_VER}) log.info("Database structure version is: " + str(DB_STRUCTURE_VER)) else: curr_ver = data['db_ver'] log.info("Your database structure version is: " + str(curr_ver)) if DB_STRUCTURE_VER > curr_ver: log.error("Your database is old! Waiting 20 seconds till update...") log.info("Press CTRL + C to cancel!") time.sleep(20) log.debug("Trying to update database structure...") log.info("--------------------------------") log.info("Your current database structure version: " + str(curr_ver)) log.info("New database structure version: " + str(DB_STRUCTURE_VER)) log.info("--------------------------------") old_ver = curr_ver
# You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os import yaml from babel.core import Locale from utah.services.mongo import db from utah.services.redis import redis from utah.utils.logger import log LANGUAGES = {} log.info("Loading localizations...") for filename in os.listdir('utah/localization'): log.debug('Loading language file ' + filename) with open('utah/localization/' + filename, "r", encoding='utf8') as f: lang = yaml.load(f, Loader=yaml.CLoader) lang_code = lang['language_info']['code'] lang['language_info']['babel'] = Locale(lang_code) LANGUAGES[lang_code] = lang log.info("Languages loaded: {}".format( [language['language_info']['babel'].display_name for language in LANGUAGES.values()]))