Compare commits
18 Commits
v0.2.0-rc1
...
v0.2.0-rc5
| Author | SHA1 | Date | |
|---|---|---|---|
| 844cf14bcd | |||
| fe32475e10 | |||
| f28f5915a4 | |||
| 1aa80c1a8f | |||
| 5d9b94fa5f | |||
| 6ef31599e9 | |||
| e961e0bcc6 | |||
| dc85754b1e | |||
| 04e2c03dae | |||
| 42d54dac5b | |||
| 767a51f994 | |||
| 313b5e5d07 | |||
| 961707dd30 | |||
| 90197f1a40 | |||
| 53a7111550 | |||
| 78d1f92c13 | |||
| 37b13fe31b | |||
| 39c9548983 |
+30
@@ -0,0 +1,30 @@
|
||||
FROM docker.io/alpine:3.7
|
||||
|
||||
ENV UID=1337 \
|
||||
GID=1337
|
||||
|
||||
COPY . /opt/mautrixtelegram
|
||||
RUN apk add --no-cache \
|
||||
python3-dev \
|
||||
py3-virtualenv \
|
||||
py3-pillow \
|
||||
py3-aiohttp \
|
||||
py3-lxml \
|
||||
py3-magic \
|
||||
py3-numpy \
|
||||
py3-asn1crypto \
|
||||
py3-sqlalchemy \
|
||||
build-base \
|
||||
ffmpeg \
|
||||
bash \
|
||||
ca-certificates \
|
||||
su-exec \
|
||||
s6 \
|
||||
&& cd /opt/mautrixtelegram \
|
||||
&& cp -r docker/root/* / \
|
||||
&& rm docker -rf \
|
||||
&& pip3 install -r requirements.txt -r optional-requirements.txt
|
||||
|
||||
VOLUME /data
|
||||
|
||||
CMD ["/bin/s6-svscan", "/etc/s6.d"]
|
||||
@@ -3,7 +3,7 @@ A Matrix-Telegram hybrid puppeting/relaybot bridge.
|
||||
|
||||
### [Wiki](https://github.com/tulir/mautrix-telegram/wiki)
|
||||
|
||||
### [Features & Roadmap](ROADMAP.md)
|
||||
### [Features & Roadmap](https://github.com/tulir/mautrix-telegram/blob/master/ROADMAP.md)
|
||||
|
||||
## Discussion
|
||||
Matrix room: [`#telegram:maunium.net`](https://matrix.to/#/#telegram:maunium.net)
|
||||
@@ -11,4 +11,4 @@ Matrix room: [`#telegram:maunium.net`](https://matrix.to/#/#telegram:maunium.net
|
||||
Telegram chat: [`mautrix_telegram`](https://t.me/mautrix_telegram) (bridged to Matrix room)
|
||||
|
||||
## Preview
|
||||

|
||||

|
||||
|
||||
@@ -35,9 +35,6 @@ script_location = alembic
|
||||
# are written from script.py.mako
|
||||
# output_encoding = utf-8
|
||||
|
||||
sqlalchemy.url = sqlite:///mautrix-telegram.db
|
||||
|
||||
|
||||
# Logging configuration
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic
|
||||
|
||||
+7
-1
@@ -1,4 +1,3 @@
|
||||
from __future__ import with_statement
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
from logging.config import fileConfig
|
||||
@@ -8,12 +7,19 @@ from os.path import abspath, dirname
|
||||
sys.path.insert(0, dirname(dirname(abspath(__file__))))
|
||||
|
||||
from mautrix_telegram.base import Base
|
||||
from mautrix_telegram.config import Config
|
||||
import mautrix_telegram.db
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
mxtg_config_path = context.get_x_argument(as_dictionary=True).get("config", "config.yaml")
|
||||
mxtg_config = Config(mxtg_config_path, None, None)
|
||||
mxtg_config.load()
|
||||
config.set_main_option("sqlalchemy.url",
|
||||
mxtg_config.get("appservice.database", "sqlite:///mautrix-telegram.db"))
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
"""Add cascade rules to UserPortal
|
||||
|
||||
Revision ID: 2228d49c383f
|
||||
Revises: bcfefa1f1299
|
||||
Create Date: 2018-05-31 11:11:59.482112
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '2228d49c383f'
|
||||
down_revision = 'bcfefa1f1299'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.drop_constraint('user_portal_user_fkey', 'user_portal', type_='foreignkey')
|
||||
op.drop_constraint('user_portal_portal_fkey', 'user_portal', type_='foreignkey')
|
||||
op.create_foreign_key('user_portal_user_fkey', 'user_portal', 'user', ['user'], ['tgid'], onupdate='CASCADE', ondelete='CASCADE')
|
||||
op.create_foreign_key('user_portal_portal_fkey', 'user_portal', 'portal', ['portal', 'portal_receiver'], ['tgid', 'tg_receiver'], onupdate='CASCADE', ondelete='CASCADE')
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_constraint('user_portal_portal_fkey', 'user_portal', type_='foreignkey')
|
||||
op.drop_constraint('user_portal_user_fkey', 'user_portal', type_='foreignkey')
|
||||
op.create_foreign_key('user_portal_portal_fkey', 'user_portal', 'portal', ['portal', 'portal_receiver'], ['tgid', 'tg_receiver'])
|
||||
op.create_foreign_key('user_portal_user_fkey', 'user_portal', 'user', ['user'], ['tgid'])
|
||||
@@ -19,31 +19,31 @@ depends_on = None
|
||||
|
||||
def upgrade():
|
||||
Session = op.create_table('telethon_sessions',
|
||||
sa.Column('session_id', sa.VARCHAR(), nullable=False),
|
||||
sa.Column('dc_id', sa.INTEGER(), nullable=False),
|
||||
sa.Column('server_address', sa.VARCHAR(), nullable=True),
|
||||
sa.Column('port', sa.INTEGER(), nullable=True),
|
||||
sa.Column('auth_key', sa.BLOB(), nullable=True),
|
||||
sa.Column('session_id', sa.String, nullable=False),
|
||||
sa.Column('dc_id', sa.Integer, nullable=False),
|
||||
sa.Column('server_address', sa.String, nullable=True),
|
||||
sa.Column('port', sa.Integer, nullable=True),
|
||||
sa.Column('auth_key', sa.LargeBinary, nullable=True),
|
||||
sa.PrimaryKeyConstraint('session_id', 'dc_id'))
|
||||
SentFile = op.create_table('telethon_sent_files',
|
||||
sa.Column('session_id', sa.VARCHAR(), nullable=False),
|
||||
sa.Column('md5_digest', sa.BLOB(), nullable=False),
|
||||
sa.Column('file_size', sa.INTEGER(), nullable=False),
|
||||
sa.Column('type', sa.INTEGER(), nullable=False),
|
||||
sa.Column('id', sa.INTEGER(), nullable=True),
|
||||
sa.Column('hash', sa.INTEGER(), nullable=True),
|
||||
sa.Column('session_id', sa.String, nullable=False),
|
||||
sa.Column('md5_digest', sa.LargeBinary, nullable=False),
|
||||
sa.Column('file_size', sa.Integer, nullable=False),
|
||||
sa.Column('type', sa.Integer, nullable=False),
|
||||
sa.Column('id', sa.BigInteger, nullable=True),
|
||||
sa.Column('hash', sa.BigInteger, nullable=True),
|
||||
sa.PrimaryKeyConstraint('session_id', 'md5_digest', 'file_size',
|
||||
'type'))
|
||||
Entity = op.create_table('telethon_entities',
|
||||
sa.Column('session_id', sa.VARCHAR(), nullable=False),
|
||||
sa.Column('id', sa.INTEGER(), nullable=False),
|
||||
sa.Column('hash', sa.INTEGER(), nullable=False),
|
||||
sa.Column('username', sa.VARCHAR(), nullable=True),
|
||||
sa.Column('phone', sa.INTEGER(), nullable=True),
|
||||
sa.Column('name', sa.VARCHAR(), nullable=True),
|
||||
sa.Column('session_id', sa.String, nullable=False),
|
||||
sa.Column('id', sa.Integer, nullable=False),
|
||||
sa.Column('hash', sa.Integer, nullable=False),
|
||||
sa.Column('username', sa.String, nullable=True),
|
||||
sa.Column('phone', sa.Integer, nullable=True),
|
||||
sa.Column('name', sa.String, nullable=True),
|
||||
sa.PrimaryKeyConstraint('session_id', 'id'))
|
||||
Version = op.create_table('telethon_version',
|
||||
sa.Column('version', sa.INTEGER(), nullable=False),
|
||||
sa.Column('version', sa.Integer, nullable=False),
|
||||
sa.PrimaryKeyConstraint('version'))
|
||||
conn = op.get_bind()
|
||||
sessions = [os.path.basename(f) for f in os.listdir(".") if f.endswith(".session")]
|
||||
|
||||
@@ -18,7 +18,7 @@ depends_on = None
|
||||
def upgrade():
|
||||
op.add_column('telegram_file',
|
||||
sa.Column('timestamp', sa.BigInteger(), nullable=False, default=0,
|
||||
server_default="true"))
|
||||
server_default="0"))
|
||||
|
||||
|
||||
def downgrade():
|
||||
|
||||
@@ -29,7 +29,7 @@ def upgrade():
|
||||
sa.UniqueConstraint('mxid'))
|
||||
op.create_table('user',
|
||||
sa.Column('mxid', sa.String),
|
||||
sa.Column('tgid', sa.Integer, nullable=True),
|
||||
sa.Column('tgid', sa.Integer, nullable=True, unique=True),
|
||||
sa.Column('tg_username', sa.String, nullable=True),
|
||||
sa.Column('saved_contacts', sa.Integer, nullable=False, default=0),
|
||||
sa.PrimaryKeyConstraint('mxid'))
|
||||
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
#!/bin/sh
|
||||
Executable
+2
@@ -0,0 +1,2 @@
|
||||
#!/bin/bash
|
||||
s6-svscanctl -t /etc/s6.d
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Define functions
|
||||
function fixperms {
|
||||
chown -R ${UID}:${GID} /data /opt/mautrixtelegram
|
||||
}
|
||||
|
||||
|
||||
# Go into env
|
||||
cd /opt/mautrixtelegram
|
||||
export FFMPEG_BINARY=/usr/bin/ffmpeg
|
||||
|
||||
# Replace database path in config.
|
||||
sed -i "s#sqlite:///mautrix-telegram.db#sqlite:////data/mautrix-telegram.db#" /data/config.yaml
|
||||
|
||||
# Check that database is in the right state
|
||||
alembic -x config=/data/config.yaml upgrade head
|
||||
|
||||
if [[ ! -f /data/config.yaml ]]; then
|
||||
cp example-config.yaml /data/config.yaml
|
||||
echo "Didn't find a config file."
|
||||
echo "Copied default config file to /data/config.yaml"
|
||||
echo "Modify that config file to your liking."
|
||||
echo "Start the container again after that to generate the registration file."
|
||||
fixperms
|
||||
exit
|
||||
fi
|
||||
|
||||
if [[ ! -f /data/registration.yaml ]]; then
|
||||
python3 -m mautrix_telegram -g -c /data/config.yaml -r /data/registration.yaml
|
||||
echo "Didn't find a registration file."
|
||||
echo "Generated ode for you."
|
||||
echo "Copy that over to synapses app service directory."
|
||||
fixperms
|
||||
exit
|
||||
fi
|
||||
|
||||
fixperms
|
||||
exec su-exec ${UID}:${GID} python3 -m mautrix_telegram -c /data/config.yaml
|
||||
@@ -1,2 +1,2 @@
|
||||
__version__ = "0.2.0rc1"
|
||||
__version__ = "0.2.0rc5"
|
||||
__author__ = "Tulir Asokan <tulir@maunium.net>"
|
||||
|
||||
@@ -26,7 +26,7 @@ from alchemysession import AlchemySessionContainer
|
||||
from mautrix_appservice import AppService
|
||||
|
||||
from .base import Base
|
||||
from .config import Config, DictWithRecursion
|
||||
from .config import Config
|
||||
from .matrix import MatrixHandler
|
||||
|
||||
from .db import init as init_db
|
||||
|
||||
@@ -166,7 +166,7 @@ class Bot(AbstractUser):
|
||||
if not self.mxid_regex.match(mxid):
|
||||
return await reply("That doesn't look like a Matrix ID.")
|
||||
user = await u.User.get_by_mxid(mxid).ensure_started()
|
||||
if not user.whitelisted:
|
||||
if not user.relaybot_whitelisted:
|
||||
return await reply("That user is not whitelisted to use the bridge.")
|
||||
elif user.logged_in:
|
||||
displayname = f"@{user.username}" if user.username else user.displayname
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
import asyncio
|
||||
|
||||
from telethon.errors import *
|
||||
from telethon.tl.types import ChatForbidden, ChannelForbidden
|
||||
from mautrix_appservice import MatrixRequestError
|
||||
|
||||
from .. import portal as po
|
||||
@@ -102,7 +103,7 @@ def _get_portal_murder_function(action, room_id, function, command, completed_me
|
||||
}
|
||||
|
||||
|
||||
@command_handler()
|
||||
@command_handler(needs_auth=False)
|
||||
async def delete_portal(evt: CommandEvent):
|
||||
portal, ok = await _get_portal_and_check_permission(evt, "delete_portal")
|
||||
if not ok:
|
||||
@@ -117,7 +118,7 @@ async def delete_portal(evt: CommandEvent):
|
||||
"by typing `$cmdprefix+sp confirm-delete`")
|
||||
|
||||
|
||||
@command_handler()
|
||||
@command_handler(needs_auth=False)
|
||||
async def unbridge(evt: CommandEvent):
|
||||
portal, ok = await _get_portal_and_check_permission(evt, "unbridge_room")
|
||||
if not ok:
|
||||
@@ -131,7 +132,7 @@ async def unbridge(evt: CommandEvent):
|
||||
"by typing `$cmdprefix+sp confirm-unbridge`")
|
||||
|
||||
|
||||
@command_handler()
|
||||
@command_handler(needs_auth=False)
|
||||
async def bridge(evt: CommandEvent):
|
||||
if len(evt.args) == 0:
|
||||
return await evt.reply("**Usage:** "
|
||||
@@ -247,14 +248,31 @@ async def confirm_bridge(evt: CommandEvent):
|
||||
return await evt.reply("Please use `$cmdprefix+sp continue` to confirm the bridging or "
|
||||
"`$cmdprefix+sp cancel` to cancel.")
|
||||
|
||||
user = evt.sender if evt.sender.logged_in else evt.tgbot
|
||||
try:
|
||||
entity = await user.client.get_entity(portal.peer)
|
||||
except Exception:
|
||||
evt.log.exception("Failed to get_entity(%s) for manual bridging.", portal.peer)
|
||||
if evt.sender.logged_in:
|
||||
return await evt.reply("Failed to get info of telegram chat. "
|
||||
"You are logged in, are you in that chat?")
|
||||
else:
|
||||
return await evt.reply("Failed to get info of telegram chat. "
|
||||
"You're not logged in, is the relay bot in the chat?")
|
||||
if isinstance(entity, (ChatForbidden, ChannelForbidden)):
|
||||
if evt.sender.logged_in:
|
||||
return await evt.reply("You don't seem to be in that chat.")
|
||||
else:
|
||||
return await evt.reply("The bot doesn't seem to be in that chat.")
|
||||
|
||||
direct = False
|
||||
|
||||
portal.mxid = bridge_to_mxid
|
||||
portal.title, portal.about, levels = await _get_initial_state(evt)
|
||||
portal.photo_id = ""
|
||||
portal.save()
|
||||
|
||||
entity = await evt.sender.client.get_entity(portal.peer)
|
||||
direct = False
|
||||
asyncio.ensure_future(portal.update_matrix_room(evt.sender, entity, direct, levels=levels),
|
||||
asyncio.ensure_future(portal.update_matrix_room(user, entity, direct, levels=levels),
|
||||
loop=evt.loop)
|
||||
|
||||
return await evt.reply("Bridging complete. Portal synchronization should begin momentarily.")
|
||||
|
||||
@@ -57,12 +57,14 @@ class UserPortal(Base):
|
||||
query = None
|
||||
__tablename__ = "user_portal"
|
||||
|
||||
user = Column(Integer, ForeignKey("user.tgid"), primary_key=True)
|
||||
user = Column(Integer, ForeignKey("user.tgid", onupdate="CASCADE", ondelete="CASCADE"),
|
||||
primary_key=True)
|
||||
portal = Column(Integer, primary_key=True)
|
||||
portal_receiver = Column(Integer, primary_key=True)
|
||||
|
||||
__table_args__ = (ForeignKeyConstraint(("portal", "portal_receiver"),
|
||||
("portal.tgid", "portal.tg_receiver")),)
|
||||
("portal.tgid", "portal.tg_receiver"),
|
||||
onupdate="CASCADE", ondelete="CASCADE"),)
|
||||
|
||||
|
||||
class User(Base):
|
||||
@@ -70,7 +72,7 @@ class User(Base):
|
||||
__tablename__ = "user"
|
||||
|
||||
mxid = Column(String, primary_key=True)
|
||||
tgid = Column(Integer, nullable=True)
|
||||
tgid = Column(Integer, nullable=True, unique=True)
|
||||
tg_username = Column(String, nullable=True)
|
||||
saved_contacts = Column(Integer, default=0)
|
||||
contacts = relationship("Contact", uselist=True,
|
||||
|
||||
@@ -154,7 +154,7 @@ class MatrixHandler:
|
||||
|
||||
puppet = Puppet.get_by_mxid(user)
|
||||
if sender and puppet:
|
||||
await portal.leave_matrix(puppet, sender)
|
||||
await portal.leave_matrix(puppet, sender, event_id)
|
||||
|
||||
user = User.get_by_mxid(user, create=False)
|
||||
if not user:
|
||||
|
||||
@@ -1318,6 +1318,8 @@ class Portal:
|
||||
await intent.redact(self.mxid, mxid)
|
||||
|
||||
async def _create_room_on_action(self, source, action):
|
||||
if source.is_relaybot:
|
||||
return False
|
||||
create_and_exit = (MessageActionChatCreate, MessageActionChannelCreate)
|
||||
create_and_continue = (MessageActionChatAddUser, MessageActionChatJoinedByLink)
|
||||
if isinstance(action, create_and_exit + create_and_continue):
|
||||
@@ -1331,7 +1333,7 @@ class Portal:
|
||||
action = update.action
|
||||
should_ignore = ((not self.mxid and not await self._create_room_on_action(source, action))
|
||||
or self.is_duplicate_action(update))
|
||||
if should_ignore:
|
||||
if should_ignore or not self.mxid:
|
||||
return
|
||||
# TODO figure out how to see changes to about text / channel username
|
||||
if isinstance(action, MessageActionChatEditTitle):
|
||||
|
||||
Reference in New Issue
Block a user