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 31, 2022
1 parent 88b0375 commit c75c281
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
38 changes: 37 additions & 1 deletion heisenbridge/network_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from heisenbridge.command_parse import CommandParserError
from heisenbridge.irc import HeisenReactor
from heisenbridge.plumbed_room import PlumbedRoom
from heisenbridge.private_room import parse_irc_formatting
from heisenbridge.private_room import ReplyMode, parse_irc_formatting
from heisenbridge.private_room import PrivateRoom
from heisenbridge.private_room import unix_to_local
from heisenbridge.room import Room
Expand Down Expand Up @@ -100,6 +100,7 @@ class NetworkRoom(Room):
rejoin_kick: bool
caps: list
forward: bool
reply_mode: ReplyMode

# state
commands: CommandManager
Expand Down Expand Up @@ -144,6 +145,7 @@ def init(self):
self.backoff_task = None
self.next_server = 0
self.connected_at = 0
self.reply_mode = ReplyMode.MENTION

self.commands = CommandManager()
self.conn = None
Expand Down Expand Up @@ -498,6 +500,27 @@ def init(self):
cmd.set_defaults(enabled=True)
self.commands.register(cmd, self.cmd_color)

cmd = CommandParser(
prog="REPLYMODE",
description="Select a behavior when replying to a message in Matrix",
)
cmd.add_argument(
"--mention",
dest="reply_mode",
action="store_const",
const=ReplyMode.MENTION,
help="Mention only message authors",
)
cmd.add_argument(
"--quote",
dest="reply_mode",
action="store_const",
const=ReplyMode.QUOTE,
help="Quote full message",
)
cmd.set_defaults(reply_mode=None)
self.commands.register(cmd, self.cmd_reply_mode)

self.mx_register("m.room.message", self.on_mx_message)

@staticmethod
Expand Down Expand Up @@ -573,6 +596,9 @@ def from_config(self, config: dict):
if "forward" in config:
self.forward = config["forward"]

if "reply_mode" in config:
self.reply_mode = ReplyMode(config["reply_mode"])

def to_config(self) -> dict:
return {
"name": self.name,
Expand All @@ -595,6 +621,7 @@ def to_config(self) -> dict:
"rejoin_kick": self.rejoin_kick,
"caps": self.caps,
"forward": self.forward,
"reply_mode": self.reply_mode.value,
}

def is_valid(self) -> bool:
Expand Down Expand Up @@ -1160,6 +1187,15 @@ async def cmd_color(self, args) -> None:

self.send_notice(f"Color is {'enabled' if self.color else 'disabled'}")

async def cmd_reply_mode(self, args):
if args.reply_mode is not None:
self.reply_mode = args.reply_mode
await self.save()

self.send_notice(
f"{'Full messages' if self.reply_mode == ReplyMode.QUOTE else 'Only message authors'} are quoted in replies"
)

def kickban(self, channel: str, nick: str, reason: str) -> None:
self.pending_kickbans[nick].append((channel, reason))
self.conn.whois(f"{nick}")
Expand Down
20 changes: 16 additions & 4 deletions heisenbridge/private_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import unicodedata
from datetime import datetime
from datetime import timezone
from enum import Enum
from html import escape
from typing import List
from typing import Optional
Expand All @@ -31,6 +32,11 @@ class NetworkRoom:
pass


class ReplyMode(Enum):
MENTION = 1
QUOTE = 2


def unix_to_local(timestamp: Optional[str]):
try:
dt = datetime.fromtimestamp(int(timestamp), timezone.utc)
Expand Down Expand Up @@ -693,15 +699,21 @@ 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.network.reply_mode == ReplyMode.QUOTE:
# prefix first line with nickname and last line of the body of the reply_to source
quoted_body = reply_to.content.body.strip().split("\n")[-1]
first_line = f"<{sender}> {quoted_body}"
lines.insert(0, first_line)
elif self.network.reply_mode == ReplyMode.MENTION and reply_to.sender != event.sender:
# prefix first line with nickname of the reply_to source
first_line = f"{sender}: {lines.pop(0)}"
lines.insert(0, first_line)

messages = []

Expand Down

0 comments on commit c75c281

Please sign in to comment.