Skip to content

Commit

Permalink
Allow quoting full messages when replying
Browse files Browse the repository at this point in the history
  • Loading branch information
blastrock committed Oct 29, 2022
1 parent 88b0375 commit 8820224
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions heisenbridge/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
1 change: 1 addition & 0 deletions heisenbridge/channel_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions heisenbridge/control_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions heisenbridge/plumbed_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 31 additions & 4 deletions heisenbridge/private_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ class PrivateRoom(Room):
max_lines = 0
use_pastebin = False
force_forward = False
quote_messages = False

commands: CommandManager

Expand Down Expand Up @@ -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)

Expand All @@ -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")

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 = []

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8820224

Please sign in to comment.