53 lines
2.3 KiB
Python
53 lines
2.3 KiB
Python
|
# mautrix-amp - A very hacky Matrix-SMS bridge based on using Android Messages for Web in Puppeteer
|
||
|
# Copyright (C) 2020 Tulir Asokan
|
||
|
#
|
||
|
# This program is free software: you can redistribute it and/or modify
|
||
|
# it under the terms of the GNU Affero General Public License as published by
|
||
|
# the Free Software Foundation, either version 3 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU Affero General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU Affero General Public License
|
||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
from typing import TYPE_CHECKING
|
||
|
|
||
|
from mautrix.bridge import BaseMatrixHandler
|
||
|
from mautrix.types import (Event, ReactionEvent, MessageEvent, StateEvent, EncryptedEvent, RoomID,
|
||
|
RedactionEvent)
|
||
|
|
||
|
from . import commands as com, puppet as pu, user as u
|
||
|
|
||
|
if TYPE_CHECKING:
|
||
|
from .__main__ import MessagesBridge
|
||
|
|
||
|
|
||
|
class MatrixHandler(BaseMatrixHandler):
|
||
|
commands: 'com.CommandProcessor'
|
||
|
|
||
|
def __init__(self, bridge: 'MessagesBridge') -> None:
|
||
|
prefix, suffix = bridge.config["bridge.username_template"].format(userid=":").split(":")
|
||
|
homeserver = bridge.config["homeserver.domain"]
|
||
|
self.user_id_prefix = f"@{prefix}"
|
||
|
self.user_id_suffix = f"{suffix}:{homeserver}"
|
||
|
|
||
|
super().__init__(command_processor=com.CommandProcessor(bridge), bridge=bridge)
|
||
|
|
||
|
def filter_matrix_event(self, evt: Event) -> bool:
|
||
|
if not isinstance(evt, (ReactionEvent, MessageEvent, StateEvent, EncryptedEvent,
|
||
|
RedactionEvent)):
|
||
|
return True
|
||
|
return (evt.sender == self.az.bot_mxid
|
||
|
or pu.Puppet.get_id_from_mxid(evt.sender) is not None)
|
||
|
|
||
|
async def send_welcome_message(self, room_id: RoomID, inviter: 'u.User') -> None:
|
||
|
await super().send_welcome_message(room_id, inviter)
|
||
|
if not inviter.notice_room:
|
||
|
inviter.notice_room = room_id
|
||
|
await inviter.update()
|
||
|
await self.az.intent.send_notice(room_id, "This room has been marked as your "
|
||
|
"Android Messages bridge notice room.")
|