From e90221d1b6a9ae9bc61477069d2e7f023fc870d2 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 30 Dec 2019 02:51:55 +0100 Subject: [PATCH] idk stavo guardando anacapri the dream e non so bene cos'ho fatto forse ho implementato la scopa https://www.youtube.com/watch?v=NFclBewnVmY --- royalpack/commands/__init__.py | 4 ++++ royalpack/commands/eval.py | 23 +++++++++++++++++++++++ royalpack/commands/exec.py | 23 +++++++++++++++++++++++ royalpack/commands/userinfo.py | 6 +++++- royalpack/stars/__init__.py | 4 ++++ royalpack/stars/api_user_list.py | 1 - royalpack/stars/api_wiki_get.py | 22 ++++++++++++++++++++++ royalpack/stars/api_wiki_list.py | 15 +++++++++++++++ royalpack/tables/__init__.py | 4 +++- royalpack/tables/fiorygi.py | 25 +++++++++++++++++++++++++ royalpack/tables/wikipages.py | 19 ++++++++++++++++++- 11 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 royalpack/commands/eval.py create mode 100644 royalpack/commands/exec.py create mode 100644 royalpack/stars/api_wiki_get.py create mode 100644 royalpack/stars/api_wiki_list.py create mode 100644 royalpack/tables/fiorygi.py diff --git a/royalpack/commands/__init__.py b/royalpack/commands/__init__.py index bade3564..6921a8e5 100644 --- a/royalpack/commands/__init__.py +++ b/royalpack/commands/__init__.py @@ -27,6 +27,8 @@ from .ahnonlosoio import AhnonlosoioCommand from .eat import EatCommand from .pmots import PmotsCommand from .peertube import PeertubeCommand +from .eval import EvalCommand +from .exec import ExecCommand # Enter the commands of your Pack here! available_commands = [ @@ -58,6 +60,8 @@ available_commands = [ EatCommand, PmotsCommand, PeertubeCommand, + EvalCommand, + ExecCommand, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/commands/eval.py b/royalpack/commands/eval.py new file mode 100644 index 00000000..dfd6a481 --- /dev/null +++ b/royalpack/commands/eval.py @@ -0,0 +1,23 @@ +import royalnet +from royalnet.commands import * +from royalnet.backpack.tables import * + + +class EvalCommand(Command): + # oh god if there is a security vulnerability + name: str = "eval" + + description: str = "Esegui una espressione Python... se sei Steffo." + + syntax: str = "{espressione}" + + async def run(self, args: CommandArgs, data: CommandData) -> None: + user: User = await data.get_author(error_if_none=True) + if user.role != "Admin": + raise CommandError("Non sei autorizzato a eseguire codice arbitrario!\n" + "(Sarebbe un po' pericoloso se te lo lasciassi eseguire, non trovi?)") + try: + result = eval(args.joined(require_at_least=1)) + except Exception as e: + raise CommandError(f"Eval fallito: {e}") + await data.reply(repr(result)) diff --git a/royalpack/commands/exec.py b/royalpack/commands/exec.py new file mode 100644 index 00000000..9823da51 --- /dev/null +++ b/royalpack/commands/exec.py @@ -0,0 +1,23 @@ +import royalnet +from royalnet.commands import * +from royalnet.backpack.tables import * + + +class ExecCommand(Command): + # oh god if there is a security vulnerability + name: str = "exec" + + description: str = "Esegui uno script Python... se sei Steffo." + + syntax: str = "{script}" + + async def run(self, args: CommandArgs, data: CommandData) -> None: + user: User = await data.get_author(error_if_none=True) + if user.role != "Admin": + raise CommandError("Non sei autorizzato a eseguire codice arbitrario!\n" + "(Sarebbe un po' pericoloso se te lo lasciassi eseguire, non trovi?)") + try: + exec(args.joined(require_at_least=1)) + except Exception as e: + raise CommandError(f"Esecuzione fallita: {e}") + await data.reply(f"✅ Fatto!") diff --git a/royalpack/commands/userinfo.py b/royalpack/commands/userinfo.py index 685542e3..125ae951 100644 --- a/royalpack/commands/userinfo.py +++ b/royalpack/commands/userinfo.py @@ -36,6 +36,10 @@ class UserinfoCommand(Command): "", ] + if user.fiorygi: + r.append(f"{user.fiorygi}") + r.append("") + # Bios are a bit too long # if user.bio: # r.append(f"{user.bio}") @@ -58,6 +62,6 @@ class UserinfoCommand(Command): if user.trivia_score: r.append(f"Trivia: [b]{user.trivia_score.correct_answers}[/b] risposte corrette / " - f"{user.trivia_score.total_answers}) totali") + f"{user.trivia_score.total_answers} totali") await data.reply("\n".join(r)) diff --git a/royalpack/stars/__init__.py b/royalpack/stars/__init__.py index cf2041b8..209efcfa 100644 --- a/royalpack/stars/__init__.py +++ b/royalpack/stars/__init__.py @@ -4,6 +4,8 @@ from .api_user_get import ApiUserGetStar from .api_diario_list import ApiDiarioListStar from .api_diario_get import ApiDiarioGetStar from .api_discord_cv import ApiDiscordCvStar +from .api_wiki_get import ApiWikiGetStar +from .api_wiki_list import ApiUserListStar # Enter the PageStars of your Pack here! available_page_stars = [ @@ -12,6 +14,8 @@ available_page_stars = [ ApiDiarioListStar, ApiDiarioGetStar, ApiDiscordCvStar, + ApiWikiGetStar, + ApiUserListStar, ] # Enter the ExceptionStars of your Pack here! diff --git a/royalpack/stars/api_user_list.py b/royalpack/stars/api_user_list.py index 969b9a37..f520472c 100644 --- a/royalpack/stars/api_user_list.py +++ b/royalpack/stars/api_user_list.py @@ -7,7 +7,6 @@ from royalnet.backpack.tables import * class ApiUserListStar(PageStar): path = "/api/user/list" - tables = {User} async def page(self, request: Request) -> JSONResponse: async with self.alchemy.session_acm() as session: diff --git a/royalpack/stars/api_wiki_get.py b/royalpack/stars/api_wiki_get.py new file mode 100644 index 00000000..72f5d3c6 --- /dev/null +++ b/royalpack/stars/api_wiki_get.py @@ -0,0 +1,22 @@ +from starlette.requests import Request +from starlette.responses import * +from royalnet.constellation import * +from royalnet.utils import * +from ..tables import * +import uuid + + +class ApiWikiGetStar(PageStar): + path = "/api/wiki/get/{wiki_page_uuid}" + + async def page(self, request: Request) -> JSONResponse: + wiki_page_uuid_str = request.path_params.get("wiki_page_uuid", "") + try: + wiki_page_uuid = uuid.UUID(wiki_page_uuid_str) + except (ValueError, AttributeError, TypeError): + return shoot(400, "Invalid wiki_page_uuid") + async with self.alchemy.session_acm() as session: + wikipage: WikiPage = await asyncify(session.query(self.alchemy.get(WikiPage)).get, wiki_page_uuid) + if wikipage is None: + return shoot(404, "No such page") + return JSONResponse(wikipage.json_full()) diff --git a/royalpack/stars/api_wiki_list.py b/royalpack/stars/api_wiki_list.py new file mode 100644 index 00000000..a0789947 --- /dev/null +++ b/royalpack/stars/api_wiki_list.py @@ -0,0 +1,15 @@ +from starlette.requests import Request +from starlette.responses import * +from royalnet.constellation import * +from royalnet.utils import * +from royalnet.backpack.tables import * +from ..tables import * + + +class ApiUserListStar(PageStar): + path = "/api/wiki/list" + + async def page(self, request: Request) -> JSONResponse: + async with self.alchemy.session_acm() as session: + pages: typing.List[WikiPage] = await asyncify(session.query(self.alchemy.get(WikiPage)).all) + return JSONResponse([page.json_list() for page in pages]) diff --git a/royalpack/tables/__init__.py b/royalpack/tables/__init__.py index b9b4924d..5a201872 100644 --- a/royalpack/tables/__init__.py +++ b/royalpack/tables/__init__.py @@ -9,6 +9,7 @@ from .triviascores import TriviaScore from .mmevents import MMEvent from .mmresponse import MMResponse from .leagueoflegends import LeagueOfLegends +from .fiorygi import Fiorygi # Enter the tables of your Pack here! available_tables = [ @@ -21,7 +22,8 @@ available_tables = [ TriviaScore, MMEvent, MMResponse, - LeagueOfLegends + LeagueOfLegends, + Fiorygi, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/tables/fiorygi.py b/royalpack/tables/fiorygi.py new file mode 100644 index 00000000..65392d2f --- /dev/null +++ b/royalpack/tables/fiorygi.py @@ -0,0 +1,25 @@ +from sqlalchemy import * +from sqlalchemy.orm import relationship, backref +from sqlalchemy.ext.declarative import declared_attr + + +class Fiorygi: + __tablename__ = "fiorygi" + + @declared_attr + def user_id(self): + return Column(Integer, ForeignKey("users.uid"), primary_key=True) + + @declared_attr + def user(self): + return relationship("User", backref=backref("fiorygi", uselist=False)) + + @declared_attr + def fiorygi(self): + return Column(Integer, nullable=False, default=0) + + def __repr__(self): + return f"" + + def __str__(self): + return f"{self.fiorygi} fioryg" + ("i" if self.fiorygi != 1 else "") diff --git a/royalpack/tables/wikipages.py b/royalpack/tables/wikipages.py index 91719fc8..c1fadfc9 100644 --- a/royalpack/tables/wikipages.py +++ b/royalpack/tables/wikipages.py @@ -30,9 +30,26 @@ class WikiPage: return Column(String, nullable=False, default="markdown") @declared_attr - def css(self): + def theme(self): return Column(String) @property def page_short_id(self): return to_urluuid(self.page_id) + + def json_list(self) -> dict: + return { + "page_id": str(self.page_id), + "page_short_id": self.page_short_id, + "title": self.title + } + + def json_full(self) -> dict: + return { + "page_id": str(self.page_id), + "page_short_id": self.page_short_id, + "title": self.title, + "contents": self.contents, + "format": self.format, + "theme": self.theme, + }