Skip to content

Commit

Permalink
Allow for reacts to be enabled/disabled (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
9p4 committed Nov 28, 2023
1 parent 06d2d44 commit 53bff43
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions heisenbridge/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ async def run(self, listen_address, listen_port, homeserver_url, owner, safe_mod
"member_sync": "half",
"max_lines": 0,
"use_pastebin": False,
"use_reacts": True,
"media_url": None,
"media_path": 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.use_reacts = network.serv.config["use_reacts"]

asyncio.ensure_future(room._create_mx(name))
return room
Expand Down
17 changes: 17 additions & 0 deletions heisenbridge/control_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ def init(self):
cmd.set_defaults(enabled=None)
self.commands.register(cmd, self.cmd_pastebin)

cmd = CommandParser(
prog="REACTS",
description="enable or disable reacting to messages on splits/linking",
epilog="Note: Users can override this per room.",
)
cmd.add_argument("--enable", dest="enabled", action="store_true", help="Enable reacts")
cmd.add_argument("--disable", dest="enabled", action="store_false", help="Disable reacts")
cmd.set_defaults(enabled=True)
self.commands.register(cmd, self.cmd_reacts)

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 @@ -600,6 +610,13 @@ 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_reacts(self, args):
if args.enabled is not None:
self.serv.config["use_reacts"] = args.enabled
await self.serv.save()

self.send_notice(f"Reacts are {'enabled' if self.serv.config['use_reacts'] else 'disabled'} by default")

async def cmd_open(self, args):
networks = self.networks()
name = args.name.lower()
Expand Down
5 changes: 4 additions & 1 deletion 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
use_reacts = True
use_displaynames = True
use_disambiguation = True
use_zwsp = False
Expand Down Expand Up @@ -137,6 +138,7 @@ async def create(network: "NetworkRoom", id: str, channel: str, key: str) -> "Ch

room.max_lines = network.serv.config["max_lines"]
room.use_pastebin = network.serv.config["use_pastebin"]
room.use_reacts = network.serv.config["use_reacts"]

for user_id, member in joined.items():
if member.displayname is not None:
Expand Down Expand Up @@ -245,7 +247,8 @@ async def on_mx_message(self, event) -> None:
)
await self.relay_message(media_event, self.network.conn.privmsg, sender)

self.react(event.event_id, "\U0001F517") # link
if self.use_reacts:
self.react(event.event_id, "\U0001F517") # link
self.media.append([event.event_id, event.content.url])
await self.save()
elif event.content.msgtype == MessageType.EMOTE:
Expand Down
31 changes: 27 additions & 4 deletions heisenbridge/private_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ class PrivateRoom(Room):

max_lines = 0
use_pastebin = False
use_reacts = False
force_forward = False

commands: CommandManager
Expand Down Expand Up @@ -400,7 +401,14 @@ def init(self) -> None:
"--disable", dest="enabled", action="store_false", help="Disable pastebin (messages will be truncated)"
)
cmd.set_defaults(enabled=None)

self.commands.register(cmd, self.cmd_pastebin)
cmd = CommandParser(prog="REACTS", description="enable or disable reacting to messages on splits/linking")
cmd.add_argument("--enable", dest="enabled", action="store_true", help="Enable reacts")
cmd.add_argument("--disable", dest="enabled", action="store_false", help="Disable reacts")
cmd.set_defaults(enabled=True)
self.commands.register(cmd, self.cmd_reacts)


self.mx_register("m.room.message", self.on_mx_message)
self.mx_register("m.room.redaction", self.on_mx_redaction)
Expand All @@ -412,6 +420,9 @@ def from_config(self, config: dict) -> None:
if "use_pastebin" in config:
self.use_pastebin = config["use_pastebin"]

if "use_reacts" in config:
self.use_reacts = config["use_reacts"]

if "name" not in config:
raise Exception("No name key in config for ChatRoom")

Expand All @@ -438,6 +449,7 @@ def to_config(self) -> dict:
"media": self.media[:5],
"max_lines": self.max_lines,
"use_pastebin": self.use_pastebin,
"use_reacts": self.use_reacts,
}

@staticmethod
Expand All @@ -458,6 +470,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.use_reacts = network.serv.config["use_reacts"]

asyncio.ensure_future(room._create_mx(name))
return room
Expand Down Expand Up @@ -776,7 +789,8 @@ async def _send_message(self, event, func, prefix=""):

for i, message in enumerate(messages):
if self.max_lines > 0 and i == self.max_lines - 1 and len(messages) > self.max_lines:
self.react(event.event_id, "\u2702") # scissors
if self.use_reacts:
self.react(event.event_id, "\u2702") # scissors

if self.use_pastebin:
content_uri = await self.az.intent.upload_media(
Expand All @@ -793,7 +807,8 @@ async def _send_message(self, event, func, prefix=""):
self.name,
f"... long message truncated: {self.serv.mxc_to_url(str(content_uri))} ({len(messages)} lines)",
)
self.react(event.event_id, "\U0001f4dd") # memo
if self.use_reacts:
self.react(event.event_id, "\U0001f4dd") # memo

self.media.append([event.event_id, str(content_uri)])
await self.save()
Expand All @@ -809,7 +824,7 @@ async def _send_message(self, event, func, prefix=""):
func(self.name, message)

# show number of lines sent to IRC
if self.max_lines == 0 and len(messages) > 1:
if self.use_reacts and self.max_lines == 0 and len(messages) > 1:
self.react(event.event_id, f"\u2702 {len(messages)} lines")

async def on_mx_message(self, event) -> None:
Expand All @@ -824,7 +839,8 @@ async def on_mx_message(self, event) -> None:
await self._send_message(event, self.network.conn.action)
elif str(event.content.msgtype) in ["m.image", "m.file", "m.audio", "m.video"]:
self.network.conn.privmsg(self.name, self.serv.mxc_to_url(event.content.url, event.content.body))
self.react(event.event_id, "\U0001F517") # link
if self.use_reacts:
self.react(event.event_id, "\U0001F517") # link
self.media.append([event.event_id, event.content.url])
await self.save()
elif str(event.content.msgtype) == "m.text":
Expand Down Expand Up @@ -886,6 +902,13 @@ async def cmd_pastebin(self, args) -> None:

self.send_notice(f"Pastebin is {'enabled' if self.use_pastebin else 'disabled'}")

async def cmd_reacts(self, args) -> None:
if args.enabled is not None:
self.use_reacts = args.enabled
await self.save()

self.send_notice(f"Reacts are {'enabled' if self.use_reacts else 'disabled'}")

async def _attach_hidden_room_internal(self) -> None:
await self.az.intent.send_state_event(
self.id,
Expand Down

0 comments on commit 53bff43

Please sign in to comment.