Add plain text message bridging

This commit is contained in:
Tulir Asokan
2018-01-20 23:59:51 +02:00
parent ad6a9ebae3
commit 899f491707
14 changed files with 1102 additions and 24 deletions
+4
View File
@@ -0,0 +1,4 @@
from .appservice import AppService
__version__ = "0.1.0"
__author__ = "Tulir Asokan <tulir@maunium.net>"
+156
View File
@@ -0,0 +1,156 @@
# matrix-appservice-python - A Matrix Application Service framework written in Python.
# Copyright (C) 2018 Tulir Asokan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Partly based on github.com/Cadair/python-appservice-framework (MIT license)
import asyncio
import logging
import aiohttp
from aiohttp import web
from functools import partial
from contextlib import contextmanager
from .intent_api import HTTPAPI
class AppService:
def __init__(self, server, domain, as_token, hs_token, bot_localpart, loop=None, log=None,
query_user=None, query_alias=None):
self.server = server
self.domain = domain
self.as_token = as_token
self.hs_token = hs_token
self.bot_mxid = f"@{bot_localpart}:{domain}"
self.transactions = []
self._http_session = None
self._intent = None
self.loop = loop or asyncio.get_event_loop()
self.log = log or logging.getLogger("mautrix_appservice")
self.query_user = query_user or (lambda: None)
self.query_alias = query_alias or (lambda: None)
self.event_handlers = []
self.app = web.Application(loop=self.loop)
self.app.router.add_route("PUT", "/transactions/{transaction_id}",
self._http_handle_transaction)
self.app.router.add_route("GET", "/rooms/{alias}", self._http_query_alias)
self.app.router.add_route("GET", "/users/{user_id}", self._http_query_user)
@property
def http_session(self):
if self._http_session is None:
raise AttributeError("the http_session attribute can only be used "
"from within the `AppService.run` context manager")
else:
return self._http_session
@property
def intent(self):
if self._intent is None:
raise AttributeError("the intent attribute can only be used from "
"within the `AppService.run` context manager")
else:
return self._intent
@contextmanager
def run(self, host="127.0.0.1", port=8080):
self._http_session = aiohttp.ClientSession(loop=self.loop)
self._intent = HTTPAPI(base_url=self.server, bot_mxid=self.bot_mxid, token=self.as_token, log=self.log).bot_intent()
yield partial(aiohttp.web.run_app, self.app, host=host, port=port)
self._intent = None
self._http_session.close()
self._http_session = None
def _check_token(self, request):
try:
token = request.rel_url.query["access_token"]
except KeyError:
return False
if token != self.hs_token:
return False
return True
async def _http_query_user(self, request):
if not self._check_token(request):
return web.Response(status=401)
user_id = request.match_info["userId"]
try:
response = self.query_user(user_id)
except:
self.log.exception("Exception in user query handler")
return web.Response(status=500)
if not response:
return web.Response(status=404)
return web.json_response(response)
async def _http_query_alias(self, request):
if not self._check_token(request):
return web.Response(status=401)
alias = request.match_info["alias"]
try:
response = self.query_alias(alias)
except:
self.log.exception("Exception in alias query handler")
return web.Response(status=500)
if not response:
return web.Response(status=404)
return web.json_response(response)
async def _http_handle_transaction(self, request):
if not self._check_token(request):
return web.Response(status=401)
transaction_id = request.match_info["transaction_id"]
if transaction_id in self.transactions:
return web.Response(status=200)
json = await request.json()
try:
events = json["events"]
except KeyError:
return web.Response(status=400)
for event in events:
self.handle_matrix_event(event)
self.transactions.append(transaction_id)
return web.json_response({})
def handle_matrix_event(self, event):
for handler in self.event_handlers:
try:
handler(event)
except:
self.log.exception("Exception in Matrix event handler")
def matrix_event_handler(self, func):
self.event_handlers.append(func)
return func
+201
View File
@@ -0,0 +1,201 @@
# mautrix-telegram - A Matrix-Telegram puppeting bridge
# Copyright (C) 2018 Tulir Asokan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import json
from matrix_client.api import MatrixHttpApi
from matrix_client.errors import MatrixRequestError
class HTTPAPI(MatrixHttpApi):
def __init__(self, base_url, bot_mxid=None, token=None, identity=None, log=None):
self.base_url = base_url
self.token = token
self.identity = identity
self.txn_id = 0
self.bot_mxid = bot_mxid
self.log = log
self.validate_cert = True
self.children = {}
def user(self, user):
try:
return self.children[user]
except KeyError:
child = ChildHTTPAPI(user, self)
self.children[user] = child
return child
def bot_intent(self):
return IntentAPI(self.bot_mxid, self, log=self.log)
def intent(self, user):
return IntentAPI(user, self.user(user), self, log=self.log)
def _send(self, method, path, content=None, query_params={}, headers={}):
if not query_params:
query_params = {}
query_params["user_id"] = self.identity
self.log.debug("%s %s %s", method, path, content)
return super()._send(method, path, content, query_params, headers)
def create_room(self, alias=None, is_public=False, name=None, topic=None, is_direct=False, invitees=()):
"""Perform /createRoom.
Args:
alias (str): Optional. The room alias name to set for this room.
is_public (bool): Optional. The public/private visibility.
name (str): Optional. The name for the room.
topic (str): Optional. The topic for the room.
invitees (list<str>): Optional. The list of user IDs to invite.
"""
content = {
"visibility": "public" if is_public else "private"
}
if alias:
content["room_alias_name"] = alias
if invitees:
content["invite"] = invitees
if name:
content["name"] = name
if topic:
content["topic"] = topic
content["is_direct"] = is_direct
return self._send("POST", "/createRoom", content)
class ChildHTTPAPI(HTTPAPI):
def __init__(self, user, parent):
self.identity = user
self.token = parent.token
self.base_url = parent.base_url
self.validate_cert = parent.validate_cert
self.log = parent.log
self.parent = parent
@property
def txn_id(self):
return self.parent.txn_id
@txn_id.setter
def txn_id(self, value):
self.parent.txn_id = value
class IntentError(Exception):
def __init__(self, message, source):
super().__init__(message)
self.source = source
def matrix_error_code(err):
try:
data = json.loads(err.content)
return data["errcode"]
except:
return err.content
class IntentAPI:
mxid_regex = re.compile("@(.+):(.+)")
def __init__(self, mxid, client, bot=None, log=None):
self.client = client
self.bot = bot
self.mxid = mxid
self.log = log
results = self.mxid_regex.search(mxid)
if not results:
raise ValueError("invalid MXID")
self.localpart = results.group(1)
self.memberships = {}
self.power_levels = {}
self.registered = False
def user(self, user):
if not self.bot:
return self.client.intent(user)
else:
raise ValueError("IntentAPI#user() is only available for base intent objects.")
def set_display_name(self, name):
self._ensure_registered()
return self.client.set_display_name(self.mxid, name)
def create_room(self, alias=None, is_public=False, name=None, topic=None, is_direct=False, invitees=()):
self._ensure_registered()
return self.client.create_room(alias, is_public, name, topic, is_direct, invitees)
def send_text(self, room_id, text, html=False, unformatted_text=None, notice=False):
if html:
return self.send_message(room_id, {
"body": unformatted_text or text,
"msgtype": "m.notice" if notice else "m.text",
"format": "org.matrix.custom.html",
"formatted_body": text,
})
else:
return self.send_message(room_id, {
"body": text,
"msgtype": "m.notice" if notice else "m.text",
})
def send_message(self, room_id, body):
return self.send_event(room_id, "m.room.message", body)
def send_event(self, room_id, type, body, txn_id=None, timestamp=None):
self._ensure_joined(room_id)
self._ensure_has_power_level_for(room_id, type)
return self.client.send_message_event(room_id, type, body, txn_id, timestamp)
def send_state_event(self, room_id, type, body, state_key="", timestamp=None):
self._ensure_joined(room_id)
self._ensure_has_power_level_for(room_id, type)
return self.client.send_state_event(room_id, type, body, state_key, timestamp)
def join_room(self, room_id):
return self._ensure_joined(room_id, ignore_cache=True)
def _ensure_joined(self, room_id, ignore_cache=False):
if ignore_cache and self.memberships.get(room_id, "") == "join":
return
self._ensure_registered()
try:
self.client.join_room(room_id)
self.memberships[room_id] = "join"
except MatrixRequestError as e:
if matrix_error_code(e) != "M_FORBIDDEN" and not self.bot:
raise IntentError(f"Failed to join room {room_id} as {self.mxid}", e)
try:
self.bot.invite_user(room_id, self.mxid)
self.client.join_room(room_id)
self.memberships[room_id] = "join"
except MatrixRequestError as e2:
raise IntentError(f"Failed to join room {room_id} as {self.mxid}", e2)
def _ensure_registered(self):
if self.registered:
return
try:
self.client.register({"username": self.localpart})
except MatrixRequestError as e:
if matrix_error_code(e) != "M_USER_IN_USE":
raise IntentError(f"Failed to register {self.mxid}", e)
self.registered = True
def _ensure_has_power_level_for(self, room_id, event_type):
pass