Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow quoting full messages when replying #245

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions heisenbridge/network_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from heisenbridge.plumbed_room import PlumbedRoom
from heisenbridge.private_room import parse_irc_formatting
from heisenbridge.private_room import PrivateRoom
from heisenbridge.private_room import ReplyMode
from heisenbridge.private_room import unix_to_local
from heisenbridge.room import Room
from heisenbridge.space_room import SpaceRoom
Expand Down Expand Up @@ -100,6 +101,7 @@ class NetworkRoom(Room):
rejoin_kick: bool
caps: list
forward: bool
reply_mode: ReplyMode

# state
commands: CommandManager
Expand Down Expand Up @@ -144,6 +146,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 +501,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 +597,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 +622,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 +1188,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