updater.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. # Friendly Telegram (telegram userbot)
  2. # Copyright (C) 2018-2021 The Authors
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU Affero General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU Affero General Public License for more details.
  11. # You should have received a copy of the GNU Affero General Public License
  12. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. # █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
  14. # █▀█ █ █ █ █▀█ █▀▄ █
  15. # © Copyright 2022
  16. # https://t.me/hikariatama
  17. #
  18. # 🔒 Licensed under the GNU AGPLv3
  19. # 🌐 https://www.gnu.org/licenses/agpl-3.0.html
  20. import asyncio
  21. import atexit
  22. import contextlib
  23. import logging
  24. import os
  25. import subprocess
  26. import sys
  27. from typing import Union
  28. import time
  29. import git
  30. from git import GitCommandError, Repo
  31. from telethon.tl.functions.messages import (
  32. GetDialogFiltersRequest,
  33. UpdateDialogFilterRequest,
  34. )
  35. from telethon.tl.types import DialogFilter, Message
  36. from .. import loader, utils, heroku, main
  37. from ..inline.types import InlineCall
  38. logger = logging.getLogger(__name__)
  39. @loader.tds
  40. class UpdaterMod(loader.Module):
  41. """Updates itself"""
  42. strings = {
  43. "name": "Updater",
  44. "source": "ℹ️ <b>Read the source code from</b> <a href='{}'>here</a>",
  45. "restarting_caption": "🔄 <b>Restarting...</b>",
  46. "downloading": "🕐 <b>Downloading updates...</b>",
  47. "installing": "🕐 <b>Installing updates...</b>",
  48. "success": (
  49. "⏳ <b>Restart successful! {}</b>\n<i>But still loading"
  50. " modules...</i>\n<i>Restart took {}s</i>"
  51. ),
  52. "origin_cfg_doc": "Git origin URL, for where to update from",
  53. "btn_restart": "🔄 Restart",
  54. "btn_update": "🧭 Update",
  55. "restart_confirm": "🔄 <b>Are you sure you want to restart?</b>",
  56. "secure_boot_confirm": (
  57. "🔄 <b>Are you sure you want to restart in secure boot mode?</b>"
  58. ),
  59. "update_confirm": (
  60. "🧭 <b>Are you sure you want to update?\n\n"
  61. '<a href="https://github.com/hikariatama/Hikka/commit/{}">{}</a> ⤑ '
  62. '<a href="https://github.com/hikariatama/Hikka/commit/{}">{}</a></b>'
  63. ),
  64. "no_update": "🚸 <b>You are on the latest version, pull updates anyway?</b>",
  65. "cancel": "🚫 Cancel",
  66. "lavhost_restart": "✌️ <b>Your lavHost is restarting...\n&gt;///&lt;</b>",
  67. "lavhost_update": "✌️ <b>Your lavHost is updating...\n&gt;///&lt;</b>",
  68. "heroku_update": (
  69. "♓️ <b>Deploying new version to Heroku...\nThis might take some time</b>"
  70. ),
  71. "heroku_update_done_nothing_to_push": (
  72. "😔 <b>Update complete. Nothing to push...</b>"
  73. ),
  74. "full_success": (
  75. "✅ <b>Userbot is fully loaded! {}</b>\n<i>Full restart took {}s</i>"
  76. ),
  77. "secure_boot_complete": (
  78. "🔒 <b>Secure boot completed! {}</b>\n<i>Restart took {}s</i>"
  79. ),
  80. }
  81. strings_ru = {
  82. "source": "ℹ️ <b>Исходный код можно прочитать</b> <a href='{}'>здесь</a>",
  83. "restarting_caption": "🔄 <b>Перезагрузка...</b>",
  84. "downloading": "🕐 <b>Скачивание обновлений...</b>",
  85. "installing": "🕐 <b>Установка обновлений...</b>",
  86. "success": (
  87. "⏳ <b>Перезагрузка успешна! {}</b>\n<i>Но модули еще"
  88. " загружаются...</i>\n<i>Перезагрузка заняла {} сек</i>"
  89. ),
  90. "full_success": (
  91. "✅ <b>Юзербот полностью загружен! {}</b>\n<i>Полная перезагрузка заняла {}"
  92. " сек</i>"
  93. ),
  94. "secure_boot_complete": (
  95. "🔒 <b>Безопасная загрузка завершена! {}</b>\n<i>Перезагрузка заняла {}"
  96. " сек</i>"
  97. ),
  98. "origin_cfg_doc": "Ссылка, из которой будут загружаться обновления",
  99. "btn_restart": "🔄 Перезагрузиться",
  100. "btn_update": "🧭 Обновиться",
  101. "restart_confirm": "🔄 <b>Ты уверен, что хочешь перезагрузиться?</b>",
  102. "secure_boot_confirm": (
  103. "🔄 <b>Ты уверен, что хочешь перезагрузиться в режиме безопасной"
  104. " загрузки?</b>"
  105. ),
  106. "update_confirm": (
  107. "🧭 <b>Ты уверен, что хочешь обновиться??\n\n"
  108. '<a href="https://github.com/hikariatama/Hikka/commit/{}">{}</a> ⤑ '
  109. '<a href="https://github.com/hikariatama/Hikka/commit/{}">{}</a></b>'
  110. ),
  111. "no_update": "🚸 <b>У тебя последняя версия. Обновиться принудительно?</b>",
  112. "cancel": "🚫 Отмена",
  113. "_cmd_doc_restart": "Перезагружает юзербот",
  114. "_cmd_doc_download": "Скачивает обновления",
  115. "_cmd_doc_update": "Обновляет юзербот",
  116. "_cmd_doc_source": "Ссылка на исходный код проекта",
  117. "_cls_doc": "Обновляет юзербот",
  118. "lavhost_restart": "✌️ <b>Твой lavHost перезагружается...\n&gt;///&lt;</b>",
  119. "lavhost_update": "✌️ <b>Твой lavHost обновляется...\n&gt;///&lt;</b>",
  120. "heroku_update": (
  121. "♓️ <b>Обновляю Heroku...\nЭто может занять некоторое время</b>"
  122. ),
  123. "heroku_update_done_nothing_to_push": (
  124. "😔 <b>Обновление завершено. Ничего не изменилось, нечего обновлять...</b>"
  125. ),
  126. }
  127. def __init__(self):
  128. self.config = loader.ModuleConfig(
  129. loader.ConfigValue(
  130. "GIT_ORIGIN_URL",
  131. "https://github.com/hikariatama/Hikka",
  132. lambda: self.strings("origin_cfg_doc"),
  133. validator=loader.validators.Link(),
  134. )
  135. )
  136. @loader.owner
  137. async def restartcmd(self, message: Message):
  138. """Restarts the userbot"""
  139. secure_boot = "--secure-boot" in utils.get_args_raw(message)
  140. try:
  141. if (
  142. "--force" in (utils.get_args_raw(message) or "")
  143. or not self.inline.init_complete
  144. or not await self.inline.form(
  145. message=message,
  146. text=self.strings(
  147. "secure_boot_confirm" if secure_boot else "restart_confirm"
  148. ),
  149. reply_markup=[
  150. {
  151. "text": self.strings("btn_restart"),
  152. "callback": self.inline_restart,
  153. "args": (secure_boot,),
  154. },
  155. {"text": self.strings("cancel"), "action": "close"},
  156. ],
  157. )
  158. ):
  159. raise
  160. except Exception:
  161. await self.restart_common(message, secure_boot)
  162. async def inline_restart(self, call: InlineCall, secure_boot: bool = False):
  163. await self.restart_common(call, secure_boot=secure_boot)
  164. async def process_restart_message(self, msg_obj: Union[InlineCall, Message]):
  165. self.set(
  166. "selfupdatemsg",
  167. msg_obj.inline_message_id
  168. if hasattr(msg_obj, "inline_message_id")
  169. else f"{utils.get_chat_id(msg_obj)}:{msg_obj.id}",
  170. )
  171. async def restart_common(
  172. self,
  173. msg_obj: Union[InlineCall, Message],
  174. secure_boot: bool = False,
  175. ):
  176. if (
  177. hasattr(msg_obj, "form")
  178. and isinstance(msg_obj.form, dict)
  179. and "uid" in msg_obj.form
  180. and msg_obj.form["uid"] in self.inline._units
  181. and "message" in self.inline._units[msg_obj.form["uid"]]
  182. ):
  183. message = self.inline._units[msg_obj.form["uid"]]["message"]
  184. else:
  185. message = msg_obj
  186. if secure_boot:
  187. self._db.set(loader.__name__, "secure_boot", True)
  188. msg_obj = await utils.answer(
  189. msg_obj,
  190. self.strings(
  191. "restarting_caption"
  192. if "LAVHOST" not in os.environ
  193. else "lavhost_restart"
  194. ),
  195. )
  196. await self.process_restart_message(msg_obj)
  197. self.set("restart_ts", time.time())
  198. await self._db.remote_force_save()
  199. if "LAVHOST" in os.environ:
  200. os.system("lavhost restart")
  201. return
  202. if "DYNO" in os.environ:
  203. app = heroku.get_app(api_token=main.hikka.api_token)[0]
  204. app.restart()
  205. return
  206. with contextlib.suppress(Exception):
  207. await main.hikka.web.stop()
  208. atexit.register(restart, *sys.argv[1:])
  209. handler = logging.getLogger().handlers[0]
  210. handler.setLevel(logging.CRITICAL)
  211. for client in self.allclients:
  212. # Terminate main loop of all running clients
  213. # Won't work if not all clients are ready
  214. if client is not message.client:
  215. await client.disconnect()
  216. await message.client.disconnect()
  217. sys.exit(0)
  218. async def download_common(self):
  219. try:
  220. repo = Repo(os.path.dirname(utils.get_base_dir()))
  221. origin = repo.remote("origin")
  222. r = origin.pull()
  223. new_commit = repo.head.commit
  224. for info in r:
  225. if info.old_commit:
  226. for d in new_commit.diff(info.old_commit):
  227. if d.b_path == "requirements.txt":
  228. return True
  229. return False
  230. except git.exc.InvalidGitRepositoryError:
  231. repo = Repo.init(os.path.dirname(utils.get_base_dir()))
  232. origin = repo.create_remote("origin", self.config["GIT_ORIGIN_URL"])
  233. origin.fetch()
  234. repo.create_head("master", origin.refs.master)
  235. repo.heads.master.set_tracking_branch(origin.refs.master)
  236. repo.heads.master.checkout(True)
  237. return False
  238. @staticmethod
  239. def req_common():
  240. # Now we have downloaded new code, install requirements
  241. logger.debug("Installing new requirements...")
  242. try:
  243. subprocess.run(
  244. [
  245. sys.executable,
  246. "-m",
  247. "pip",
  248. "install",
  249. "-r",
  250. os.path.join(
  251. os.path.dirname(utils.get_base_dir()),
  252. "requirements.txt",
  253. ),
  254. "--user",
  255. ],
  256. check=True,
  257. )
  258. except subprocess.CalledProcessError:
  259. logger.exception("Req install failed")
  260. @loader.owner
  261. async def updatecmd(self, message: Message):
  262. """Downloads userbot updates"""
  263. try:
  264. current = utils.get_git_hash()
  265. upcoming = next(
  266. git.Repo().iter_commits("origin/master", max_count=1)
  267. ).hexsha
  268. if (
  269. "--force" in (utils.get_args_raw(message) or "")
  270. or not self.inline.init_complete
  271. or not await self.inline.form(
  272. message=message,
  273. text=self.strings("update_confirm").format(
  274. *(
  275. [current, current[:8], upcoming, upcoming[:8]]
  276. if "DYNO" not in os.environ
  277. else ["", "", "", ""]
  278. )
  279. )
  280. if upcoming != current or "DYNO" in os.environ
  281. else self.strings("no_update"),
  282. reply_markup=[
  283. {
  284. "text": self.strings("btn_update"),
  285. "callback": self.inline_update,
  286. },
  287. {"text": self.strings("cancel"), "action": "close"},
  288. ],
  289. )
  290. ):
  291. raise
  292. except Exception:
  293. await self.inline_update(message)
  294. async def inline_update(
  295. self,
  296. msg_obj: Union[InlineCall, Message],
  297. hard: bool = False,
  298. ):
  299. # We don't really care about asyncio at this point, as we are shutting down
  300. if hard:
  301. os.system(f"cd {utils.get_base_dir()} && cd .. && git reset --hard HEAD")
  302. try:
  303. if "LAVHOST" in os.environ:
  304. msg_obj = await utils.answer(msg_obj, self.strings("lavhost_update"))
  305. await self.process_restart_message(msg_obj)
  306. os.system("lavhost update")
  307. return
  308. if "DYNO" in os.environ:
  309. msg_obj = await utils.answer(msg_obj, self.strings("heroku_update"))
  310. await self.process_restart_message(msg_obj)
  311. try:
  312. nosave = "--no-save" in utils.get_args_raw(msg_obj)
  313. except Exception:
  314. nosave = False
  315. if not nosave:
  316. await self._db.remote_force_save()
  317. app, _ = heroku.get_app(
  318. api_token=main.hikka.api_token,
  319. create_new=False,
  320. )
  321. repo = heroku.get_repo()
  322. url = app.git_url.replace(
  323. "https://",
  324. f"https://api:{os.environ.get('heroku_api_token')}@",
  325. )
  326. if "heroku" in repo.remotes:
  327. remote = repo.remote("heroku")
  328. remote.set_url(url)
  329. else:
  330. remote = repo.create_remote("heroku", url)
  331. await utils.run_sync(remote.push, refspec="HEAD:refs/heads/master")
  332. await utils.answer(
  333. msg_obj,
  334. self.strings("heroku_update_done_nothing_to_push"),
  335. )
  336. return
  337. with contextlib.suppress(Exception):
  338. msg_obj = await utils.answer(msg_obj, self.strings("downloading"))
  339. req_update = await self.download_common()
  340. with contextlib.suppress(Exception):
  341. msg_obj = await utils.answer(msg_obj, self.strings("installing"))
  342. if req_update:
  343. self.req_common()
  344. await self.restart_common(msg_obj)
  345. except GitCommandError:
  346. if not hard:
  347. await self.inline_update(msg_obj, True)
  348. return
  349. logger.critical("Got update loop. Update manually via .terminal")
  350. return
  351. @loader.unrestricted
  352. async def sourcecmd(self, message: Message):
  353. """Links the source code of this project"""
  354. await utils.answer(
  355. message,
  356. self.strings("source").format(self.config["GIT_ORIGIN_URL"]),
  357. )
  358. async def client_ready(self, client, _):
  359. if self.get("selfupdatemsg") is not None:
  360. try:
  361. await self.update_complete(client)
  362. except Exception:
  363. logger.exception("Failed to complete update!")
  364. if self.get("do_not_create", False):
  365. return
  366. try:
  367. await self._add_folder()
  368. except Exception:
  369. logger.exception("Failed to add folder!")
  370. finally:
  371. self.set("do_not_create", True)
  372. async def _add_folder(self):
  373. folders = await self._client(GetDialogFiltersRequest())
  374. if any(getattr(folder, "title", None) == "hikka" for folder in folders):
  375. return
  376. try:
  377. folder_id = (
  378. max(
  379. folders,
  380. key=lambda x: x.id,
  381. ).id
  382. + 1
  383. )
  384. except ValueError:
  385. folder_id = 2
  386. try:
  387. await self._client(
  388. UpdateDialogFilterRequest(
  389. folder_id,
  390. DialogFilter(
  391. folder_id,
  392. title="hikka",
  393. pinned_peers=(
  394. [
  395. await self._client.get_input_entity(
  396. self._client.loader.inline.bot_id
  397. )
  398. ]
  399. if self._client.loader.inline.init_complete
  400. else []
  401. ),
  402. include_peers=[
  403. await self._client.get_input_entity(dialog.entity)
  404. async for dialog in self._client.iter_dialogs(
  405. None,
  406. ignore_migrated=True,
  407. )
  408. if dialog.name
  409. in {
  410. "hikka-logs",
  411. "hikka-onload",
  412. "hikka-assets",
  413. "hikka-backups",
  414. "hikka-acc-switcher",
  415. "silent-tags",
  416. }
  417. and dialog.is_channel
  418. and (
  419. dialog.entity.participants_count == 1
  420. or dialog.entity.participants_count == 2
  421. and dialog.name in {"hikka-logs", "silent-tags"}
  422. )
  423. or (
  424. self._client.loader.inline.init_complete
  425. and dialog.entity.id
  426. == self._client.loader.inline.bot_id
  427. )
  428. or dialog.entity.id
  429. in [
  430. 1554874075,
  431. 1697279580,
  432. 1679998924,
  433. ] # official hikka chats
  434. ],
  435. emoticon="🐱",
  436. exclude_peers=[],
  437. contacts=False,
  438. non_contacts=False,
  439. groups=False,
  440. broadcasts=False,
  441. bots=False,
  442. exclude_muted=False,
  443. exclude_read=False,
  444. exclude_archived=False,
  445. ),
  446. )
  447. )
  448. except Exception:
  449. logger.critical(
  450. "Can't create Hikka folder. Possible reasons are:\n"
  451. "- User reached the limit of folders in Telegram\n"
  452. "- User got floodwait\n"
  453. "Ignoring error and adding folder addition to ignore list"
  454. )
  455. async def update_complete(self, client: "TelegramClient"): # type: ignore
  456. logger.debug("Self update successful! Edit message")
  457. start = self.get("restart_ts")
  458. try:
  459. took = round(time.time() - start)
  460. except Exception:
  461. took = "n/a"
  462. msg = self.strings("success").format(utils.ascii_face(), took)
  463. ms = self.get("selfupdatemsg")
  464. if ":" in str(ms):
  465. chat_id, message_id = ms.split(":")
  466. chat_id, message_id = int(chat_id), int(message_id)
  467. await self._client.edit_message(chat_id, message_id, msg)
  468. return
  469. await self.inline.bot.edit_message_text(
  470. inline_message_id=ms,
  471. text=msg,
  472. )
  473. async def full_restart_complete(self, secure_boot: bool = False):
  474. start = self.get("restart_ts")
  475. try:
  476. took = round(time.time() - start)
  477. except Exception:
  478. took = "n/a"
  479. self.set("restart_ts", None)
  480. ms = self.get("selfupdatemsg")
  481. msg = self.strings(
  482. "secure_boot_complete" if secure_boot else "full_success"
  483. ).format(utils.ascii_face(), took)
  484. if ms is None:
  485. return
  486. self.set("selfupdatemsg", None)
  487. if ":" in str(ms):
  488. chat_id, message_id = ms.split(":")
  489. chat_id, message_id = int(chat_id), int(message_id)
  490. await self._client.edit_message(chat_id, message_id, msg)
  491. await asyncio.sleep(60)
  492. await self._client.delete_messages(chat_id, message_id)
  493. return
  494. await self.inline.bot.edit_message_text(
  495. inline_message_id=ms,
  496. text=msg,
  497. )
  498. def restart(*argv):
  499. os.execl(
  500. sys.executable,
  501. sys.executable,
  502. "-m",
  503. os.path.relpath(utils.get_base_dir()),
  504. *argv,
  505. )