Fix bugs and switch to commonmark for command replies

This commit is contained in:
Tulir Asokan
2018-09-25 00:26:02 +03:00
parent f758884c75
commit 5ccd1bc2fe
5 changed files with 32 additions and 24 deletions
+3 -2
View File
@@ -15,7 +15,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Awaitable, Callable, Dict, List, NamedTuple, Optional from typing import Awaitable, Callable, Dict, List, NamedTuple, Optional
import markdown import commonmark
import logging import logging
from telethon.errors import FloodWaitError from telethon.errors import FloodWaitError
@@ -60,7 +60,8 @@ class CommandEvent:
message = message.replace("$cmdprefix", self.command_prefix) message = message.replace("$cmdprefix", self.command_prefix)
html = None html = None
if render_markdown: if render_markdown:
html = markdown.markdown(message, safe_mode="escape" if allow_html else False) html = commonmark.commonmark(message if allow_html else
message.replace("<", "&lt;").replace(">", "&gt;"))
elif allow_html: elif allow_html:
html = message html = message
return self.az.intent.send_notice(self.room_id, message, html=html) return self.az.intent.send_notice(self.room_id, message, html=html)
+25 -19
View File
@@ -393,7 +393,6 @@ async def upgrade(evt: CommandEvent) -> Dict:
return await evt.reply(e.args[0]) return await evt.reply(e.args[0])
@command_handler(help_section=SECTION_PORTAL_MANAGEMENT, @command_handler(help_section=SECTION_PORTAL_MANAGEMENT,
help_text="View or change per-portal settings.", help_text="View or change per-portal settings.",
help_args="<`help`|_subcommand_> [...]") help_args="<`help`|_subcommand_> [...]")
@@ -402,37 +401,43 @@ async def config(evt: CommandEvent) -> Dict:
portal = po.Portal.get_by_mxid(evt.room_id) portal = po.Portal.get_by_mxid(evt.room_id)
if not portal and cmd != "help": if not portal and cmd != "help":
return await evt.reply("This is not a portal room.") return await evt.reply("This is not a portal room.")
if cmd == "help": if cmd not in ("view", "defaults", "set", "unset", "add", "del"):
return await evt.reply("""`$cmdprefix config`: return await evt.reply("""**Usage:** `$cmdprefix config <subcommand> [...]`. Subcommands:
* `help` - View this help text.
* `view` - View the current config data. * **help** - View this help text.
* `defaults` - View the default config values. * **view** - View the current config data.
* `set` <_key_> <_value_> - Set a config value. * **defaults** - View the default config values.
* `unset` <_key_> - Remove a config value. * **set** <_key_> <_value_> - Set a config value.
* `add` <_key_> <_value_> - Add a value to an array. * **unset** <_key_> - Remove a config value.
* `del` <_key_> <_value_> - Remove a value from an array. * **add** <_key_> <_value_> - Add a value to an array.
* **del** <_key_> <_value_> - Remove a value from an array.
""") """)
elif cmd == "view": elif cmd == "view":
stream = StringIO() stream = StringIO()
yaml.dump(portal.local_config, stream) yaml.dump(portal.local_config, stream)
return await evt.reply(f"Room-specific config:\n```yaml\n{stream.getvalue()}\n```") return await evt.reply(f"Room-specific config:\n\n```yaml\n{stream.getvalue()}\n```",
allow_html=True)
elif cmd == "defaults": elif cmd == "defaults":
stream = StringIO() stream = StringIO()
yaml.dump({ yaml.dump({
"edits_as_replies": evt.config["bridge.edits_as_replies"], "edits_as_replies": evt.config["bridge.edits_as_replies"],
"bridge_notices": evt.config["bridge.bridge_notices"], "bridge_notices": {
"default": evt.config["bridge.bridge_notices.default"],
"exceptions": evt.config["bridge.bridge_notices.exceptions"],
},
"bot_messages_as_notices": evt.config["bridge.bot_messages_as_notices"], "bot_messages_as_notices": evt.config["bridge.bot_messages_as_notices"],
"inline_images": evt.config["bridge.inline_images"], "inline_images": evt.config["bridge.inline_images"],
"native_stickers": evt.config["native_stickers"], "native_stickers": evt.config["bridge.native_stickers"],
"message_formats": evt.config["message_formats"], "message_formats": evt.config["bridge.message_formats"],
"state_event_formats": evt.config["state_event_formats"], "state_event_formats": evt.config["bridge.state_event_formats"],
}, stream) }, stream)
return await evt.reply(f"Bridge instance wide config:\n```yaml\n{stream.getvalue()}\n```") return await evt.reply(f"Bridge instance wide config:\n\n```yaml\n{stream.getvalue()}```",
allow_html=True)
key = evt.args[1] if len(evt.args) > 1 else None key = evt.args[1] if len(evt.args) > 1 else None
value = yaml.load(evt.args[2:]) if len(evt.args) > 2 else None value = yaml.load(" ".join(evt.args[2:])) if len(evt.args) > 2 else None
if cmd == "set": if cmd == "set":
if not key or not value: if not key or value is None:
return await evt.reply(f"**Usage:** `$cmdprefix+sp config {cmd} <key> <value>`") return await evt.reply(f"**Usage:** `$cmdprefix+sp config {cmd} <key> <value>`")
elif util.recursive_set(portal.local_config, key, value): elif util.recursive_set(portal.local_config, key, value):
return await evt.reply(f"Successfully set the value of `{key}` to `{value}`.") return await evt.reply(f"Successfully set the value of `{key}` to `{value}`.")
@@ -447,7 +452,7 @@ async def config(evt: CommandEvent) -> Dict:
else: else:
return await evt.reply(f"`{key}` not found in config.") return await evt.reply(f"`{key}` not found in config.")
elif cmd == "add" or cmd == "del": elif cmd == "add" or cmd == "del":
if not key or not value: if not key or value is None:
return await evt.reply(f"**Usage:** `$cmdprefix+sp config {cmd} <key> <value>`") return await evt.reply(f"**Usage:** `$cmdprefix+sp config {cmd} <key> <value>`")
arr = util.recursive_get(portal.local_config, key) arr = util.recursive_get(portal.local_config, key)
@@ -466,6 +471,7 @@ async def config(evt: CommandEvent) -> Dict:
return await evt.reply(f"The array at `{key}` does not contain `{value}`.") return await evt.reply(f"The array at `{key}` does not contain `{value}`.")
arr.remove(value) arr.remove(value)
return await evt.reply(f"Successfully removed `{value}` from the array at `{key}`") return await evt.reply(f"Successfully removed `{value}` from the array at `{key}`")
portal.save()
@command_handler(help_section=SECTION_PORTAL_MANAGEMENT, @command_handler(help_section=SECTION_PORTAL_MANAGEMENT,
+2 -1
View File
@@ -17,6 +17,7 @@
from typing import Dict, Any from typing import Dict, Any
from ..config import DictWithRecursion from ..config import DictWithRecursion
def recursive_set(data: Dict[str, Any], key: str, value: Any) -> bool: def recursive_set(data: Dict[str, Any], key: str, value: Any) -> bool:
key, next_key = DictWithRecursion._parse_key(key) key, next_key = DictWithRecursion._parse_key(key)
if next_key is not None: if next_key is not None:
@@ -46,7 +47,7 @@ def recursive_del(data: Dict[str, any], key: str) -> bool:
if key not in data: if key not in data:
return False return False
next_data = data.get(key, {}) next_data = data.get(key, {})
recursive_del(next_data, next_key) return recursive_del(next_data, next_key)
if key in data: if key in data:
del data[key] del data[key]
return True return True
+1 -1
View File
@@ -4,7 +4,7 @@ ruamel.yaml
python-magic python-magic
SQLAlchemy SQLAlchemy
alembic alembic
Markdown commonmark
future-fstrings future-fstrings
telethon telethon
telethon-session-sqlalchemy telethon-session-sqlalchemy
+1 -1
View File
@@ -30,7 +30,7 @@ setuptools.setup(
"mautrix-appservice>=0.3.6,<0.4.0", "mautrix-appservice>=0.3.6,<0.4.0",
"SQLAlchemy>=1.2.3,<2", "SQLAlchemy>=1.2.3,<2",
"alembic>=1.0.0,<2", "alembic>=1.0.0,<2",
"Markdown>=2.6.11,<3", "commonmark>=0.8.1,<1",
"ruamel.yaml>=0.15.35,<0.16", "ruamel.yaml>=0.15.35,<0.16",
"future-fstrings>=0.4.2", "future-fstrings>=0.4.2",
"python-magic>=0.4.15,<0.5", "python-magic>=0.4.15,<0.5",