From 8820224fe4cd5e5d1d9fe32f85c3c507da59daac Mon Sep 17 00:00:00 2001 From: Philippe Daouadi Date: Sat, 29 Oct 2022 18:47:23 +0200 Subject: [PATCH] Allow quoting full messages when replying --- heisenbridge/__main__.py | 1 + heisenbridge/channel_room.py | 1 + heisenbridge/control_room.py | 19 +++++++++++++++++++ heisenbridge/plumbed_room.py | 1 + heisenbridge/private_room.py | 35 +++++++++++++++++++++++++++++++---- 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/heisenbridge/__main__.py b/heisenbridge/__main__.py index 80e9f14..5957165 100644 --- a/heisenbridge/__main__.py +++ b/heisenbridge/__main__.py @@ -525,6 +525,7 @@ async def run(self, listen_address, listen_port, homeserver_url, owner, safe_mod "member_sync": "half", "max_lines": 0, "use_pastebin": False, + "quote_messages": False, "media_url": None, "namespace": self.puppet_prefix, } diff --git a/heisenbridge/channel_room.py b/heisenbridge/channel_room.py index 119dad0..1fb9a53 100644 --- a/heisenbridge/channel_room.py +++ b/heisenbridge/channel_room.py @@ -193,6 +193,7 @@ def create(network: NetworkRoom, name: str) -> "ChannelRoom": room.max_lines = network.serv.config["max_lines"] room.use_pastebin = network.serv.config["use_pastebin"] + room.quote_messages = network.serv.config["quote_messages"] asyncio.ensure_future(room._create_mx(name)) return room diff --git a/heisenbridge/control_room.py b/heisenbridge/control_room.py index 5cb5889..9cb179d 100644 --- a/heisenbridge/control_room.py +++ b/heisenbridge/control_room.py @@ -191,6 +191,16 @@ def init(self): cmd.set_defaults(enabled=None) self.commands.register(cmd, self.cmd_pastebin) + cmd = CommandParser( + prog="QUOTEMESSAGES", + description="Quote full messages on reply, or only quote the message author", + epilog="Note: Users can override this per room.", + ) + cmd.add_argument("--enable", dest="enabled", action="store_true", help="Quote full messages") + cmd.add_argument("--disable", dest="enabled", action="store_false", help="Quote only message authors") + cmd.set_defaults(enabled=None) + self.commands.register(cmd, self.cmd_quote_messages) + cmd = CommandParser(prog="MEDIAURL", description="configure media URL for links") cmd.add_argument("url", nargs="?", help="new URL override") cmd.add_argument("--remove", help="remove URL override (will retry auto-detection)", action="store_true") @@ -576,6 +586,15 @@ async def cmd_pastebin(self, args): self.send_notice(f"Pastebin is {'enabled' if self.serv.config['use_pastebin'] else 'disabled'} by default") + async def cmd_quote_messages(self, args): + if args.enabled is not None: + self.serv.config["quote_messages"] = args.enabled + await self.serv.save() + + self.send_notice( + f"{'Full messages' if self.serv.config['quote_messages'] else 'Only message authors'} are quoted by default in replies" + ) + async def cmd_open(self, args): networks = self.networks() name = args.name.lower() diff --git a/heisenbridge/plumbed_room.py b/heisenbridge/plumbed_room.py index 143d5aa..b3b79e6 100644 --- a/heisenbridge/plumbed_room.py +++ b/heisenbridge/plumbed_room.py @@ -46,6 +46,7 @@ def sanitize_irc_nick(nick): class PlumbedRoom(ChannelRoom): max_lines = 5 use_pastebin = True + quote_messages = True use_displaynames = True use_disambiguation = True use_zwsp = False diff --git a/heisenbridge/private_room.py b/heisenbridge/private_room.py index d8118e8..c0b1425 100644 --- a/heisenbridge/private_room.py +++ b/heisenbridge/private_room.py @@ -371,6 +371,7 @@ class PrivateRoom(Room): max_lines = 0 use_pastebin = False force_forward = False + quote_messages = False commands: CommandManager @@ -402,6 +403,15 @@ def init(self) -> None: cmd.set_defaults(enabled=None) self.commands.register(cmd, self.cmd_pastebin) + cmd = CommandParser( + prog="QUOTEMESSAGES", + description="Quote full messages on reply, or only quote the message author", + ) + cmd.add_argument("--enable", dest="enabled", action="store_true", help="Quote full messages") + cmd.add_argument("--disable", dest="enabled", action="store_false", help="Quote only message authors") + cmd.set_defaults(enabled=None) + self.commands.register(cmd, self.cmd_quote_messages) + self.mx_register("m.room.message", self.on_mx_message) self.mx_register("m.room.redaction", self.on_mx_redaction) @@ -412,6 +422,9 @@ def from_config(self, config: dict) -> None: if "use_pastebin" in config: self.use_pastebin = config["use_pastebin"] + if "quote_messages" in config: + self.quote_messages = config["quote_messages"] + if "name" not in config: raise Exception("No name key in config for ChatRoom") @@ -438,6 +451,7 @@ def to_config(self) -> dict: "media": self.media[:5], "max_lines": self.max_lines, "use_pastebin": self.use_pastebin, + "quote_messages": self.quote_messages, } @staticmethod @@ -458,6 +472,7 @@ def create(network: NetworkRoom, name: str) -> "PrivateRoom": room.max_lines = network.serv.config["max_lines"] room.use_pastebin = network.serv.config["use_pastebin"] + room.quote_messages = network.serv.config["quote_messages"] asyncio.ensure_future(room._create_mx(name)) return room @@ -693,15 +708,20 @@ async def _process_event_content(self, event, prefix, reply_to=None): lines = [x for x in lines if not re.match(r"^\s*$", x)] # handle replies - if reply_to and reply_to.sender != event.sender: + if reply_to: # resolve displayname sender = reply_to.sender if sender in self.displaynames: sender = self.displaynames[sender] - # prefix first line with nickname of the reply_to source - first_line = sender + ": " + lines.pop(0) - lines.insert(0, first_line) + if self.quote_messages: + # prefix first line with nickname and message of the reply_to source + first_line = "> " + sender + ": " + reply_to.content.body + lines.insert(0, first_line) + elif reply_to.sender != event.sender: + # prefix first line with nickname of the reply_to source + first_line = sender + ": " + lines.pop(0) + lines.insert(0, first_line) messages = [] @@ -886,6 +906,13 @@ async def cmd_pastebin(self, args) -> None: self.send_notice(f"Pastebin is {'enabled' if self.use_pastebin else 'disabled'}") + async def cmd_quote_messages(self, args): + if args.enabled is not None: + self.quote_messages = args.enabled + await self.save() + + self.send_notice(f"{'Full messages' if self.quote_messages else 'Only message authors'} are quoted in replies") + async def _attach_hidden_room_internal(self) -> None: await self.az.intent.send_state_event( self.id,