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

Code required to have relay mode #72

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7eabcbf
Working in relay mode
Nov 23, 2021
c5f62e9
Working in relay mode - new merged changes
Nov 23, 2021
0150c57
Fixed the import of BaseUser
Nov 23, 2021
fec24bc
Added the relay.py file and the commands set-relay and unset-realy
Nov 23, 2021
61e0a2b
Working in relay mode
Nov 30, 2021
2713dbf
Working in relay mode
Nov 30, 2021
5a67edc
Working in relay mode
Nov 30, 2021
5c1d801
Working in relay mode
Nov 30, 2021
2b207ec
Remove word instagram
Nov 30, 2021
5fae371
Working in relay mode
Nov 30, 2021
cc7b9ee
Working in relay mode
Nov 30, 2021
d517d87
Adding relay mode (#1)
Nov 30, 2021
f830085
Fixed any changes
Dec 6, 2021
af97139
Fixed conflicts
Dec 6, 2021
9ff8094
Fixed conflicts
Dec 6, 2021
7199cfe
Blacken code
Dec 6, 2021
b05da5e
Correcting imports
Dec 6, 2021
a5fd725
Correcting imports
Dec 6, 2021
60133d6
Correcting imports
Dec 6, 2021
809f458
Merge pull request #2 from iKonoTelecomunicaciones/adding-relay-mode
Dec 6, 2021
c63a688
Merge remote-tracking branch 'upstream/master' into adding-relay-mode
Dec 6, 2021
f4d5bed
Merge pull request #3 from iKonoTelecomunicaciones/adding-relay-mode
Dec 6, 2021
5003489
Merge branch 'master' of github.com:mautrix/python
Dec 10, 2021
6e0427f
Merge pull request #4 from iKonoTelecomunicaciones/adding-relay-mode
Dec 10, 2021
0bbdf99
Working in the relay mode with the pr https://github.com/mautrix/inst…
Dec 10, 2021
7f02112
Applying Black
Dec 10, 2021
256b4c7
Applying Black and fixed error with Black
Dec 10, 2021
a530cd5
Merge pull request #5 from iKonoTelecomunicaciones/adding-relay-mode
Dec 10, 2021
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
12 changes: 11 additions & 1 deletion mautrix/bridge/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .handler import (
SECTION_ADMIN,
SECTION_AUTH,
SECTION_CONNECTION,
SECTION_GENERAL,
CommandEvent,
CommandHandler,
Expand All @@ -12,7 +13,15 @@
)
from .meta import cancel, help_cmd, unknown_command

from . import admin, clean_rooms, crypto, delete_portal, login_matrix, manhole # isort: skip
from . import ( # isort: skip
admin,
clean_rooms,
crypto,
delete_portal,
login_matrix,
manhole,
relay,
)

__all__ = [
"HelpSection",
Expand All @@ -25,4 +34,5 @@
"SECTION_GENERAL",
"SECTION_ADMIN",
"SECTION_AUTH",
"SECTION_CONNECTION",
]
1 change: 1 addition & 0 deletions mautrix/bridge/commands/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
SECTION_GENERAL = HelpSection("General", 0, "")
SECTION_AUTH = HelpSection("Authentication", 10, "")
SECTION_ADMIN = HelpSection("Administration", 50, "")
SECTION_CONNECTION = HelpSection("Connection management", 15, "")


def ensure_trailing_newline(s: str) -> str:
Expand Down
45 changes: 45 additions & 0 deletions mautrix/bridge/commands/relay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright (c) 2021 Tulir Asokan
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from mautrix.types import EventID

from .handler import SECTION_CONNECTION, CommandEvent, command_handler


@command_handler(
needs_auth=True,
management_only=False,
name="set-relay",
help_section=SECTION_CONNECTION,
help_text="Relay messages in this room through your account.",
)
async def set_relay(evt: CommandEvent) -> EventID:
if not evt.config["bridge.relay.enabled"]:
return await evt.reply("Relay mode is not enable in this instance of the bridge.")
elif not evt.is_portal:
return await evt.reply("This is not a portal room.")
await evt.portal.set_relay_user(evt.sender)
return await evt.reply(
"Messages from non-logged-in users in this room will now be bridged "
"through your account."
)


@command_handler(
needs_auth=True,
management_only=False,
name="unset-relay",
help_section=SECTION_CONNECTION,
help_text="Stop relaying messages in this room.",
)
async def unset_relay(evt: CommandEvent) -> EventID:
if not evt.config["bridge.relay.enabled"]:
return await evt.reply("Relay mode is not enable in this instance of the bridge.")
elif not evt.is_portal:
return await evt.reply("This is not a portal room.")
elif not evt.portal.has_relay:
return await evt.reply("This room does not have a relay user set.")
await evt.portal.set_relay_user(None)
return await evt.reply("Messages from non-logged-in users will no longer be bridged.")
4 changes: 3 additions & 1 deletion mautrix/bridge/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def forbidden_defaults(self) -> list[ForbiddenDefault]:
)

def do_update(self, helper: ConfigUpdateHelper) -> None:
copy = helper.copy
copy, copy_dict = helper.copy, helper.copy_dict

copy("homeserver.address")
copy("homeserver.domain")
Expand Down Expand Up @@ -100,6 +100,8 @@ def do_update(self, helper: ConfigUpdateHelper) -> None:
copy("bridge.management_room_text.welcome_unconnected")
copy("bridge.management_room_text.additional_help")
copy("bridge.management_room_multiple_messages")
copy("bridge.relay.enabled")
copy_dict("bridge.relay.message_formats")

copy("manhole.enabled")
copy("manhole.path")
Expand Down
30 changes: 29 additions & 1 deletion mautrix/bridge/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations

from typing import Any
from typing import Any, Optional, Tuple
from abc import ABC, abstractmethod
from collections import defaultdict
import asyncio
Expand Down Expand Up @@ -35,6 +35,7 @@ class BasePortal(ABC):
encrypted: bool
is_direct: bool
backfill_lock: SimpleLock
_relay_user: Optional["br.BaseUser"]

@abstractmethod
async def save(self) -> None:
Expand All @@ -46,6 +47,33 @@ async def handle_matrix_message(
) -> None:
pass

@property
@abstractmethod
async def has_relay(self) -> bool:
pass

@property
@abstractmethod
async def get_relay_user(self) -> Optional["br.BaseUser"]:
pass

@abstractmethod
async def set_relay_user(self, user: Optional["br.BaseUser"]) -> None:
pass

@abstractmethod
async def get_relay_sender(
self, sender: br.BaseUser, evt_identifier: str
) -> Tuple[Optional["br.BaseUser"], bool]:
pass

@abstractmethod
async def apply_msg_format(self, sender: br.BaseUser, content: MessageEventContent) -> None:
pass

async def get_displayname(self, user: br.BaseUser) -> str:
return await self.main_intent.get_room_displayname(self.mxid, user.mxid) or user.mxid

async def check_dm_encryption(self) -> bool | None:
try:
evt = await self.main_intent.get_state_event(self.mxid, EventType.ROOM_ENCRYPTION)
Expand Down