Compare commits

...

9 Commits

Author SHA1 Message Date
Tulir Asokan 85d38e3db6 Bump version to 0.8.0rc3 2020-05-22 20:49:47 +03:00
Tulir Asokan 3a25ee2c93 Merge pull request #468 from davidmehren/fix-peerchannel-admin
Fix admin detection in _can_use_commands
2020-05-22 20:02:53 +03:00
Tulir Asokan a4d49a41e0 Maybe fix branch condition in CI 2020-05-21 19:35:04 +03:00
David Mehren 7ba9e10f0f Fix admin detection in _can_use_commands 2020-05-21 09:44:27 +02:00
Tulir Asokan 05e966011e Fix error syncing private chat portals with no avatar 2020-05-20 23:29:36 +03:00
Tulir Asokan 9081f6bce4 Bump mautrix-python requirement 2020-05-20 23:17:42 +03:00
Tulir Asokan c126e8b615 Actually ignore ChatForbidden when syncing. Fixes #446 2020-05-20 22:45:22 +03:00
Tulir Asokan f454803ef7 Move private information to trace log level. Fixes #321 2020-05-20 22:40:20 +03:00
Tulir Asokan 40beb8f752 Add private_chat_portal_meta option and fix bugs
* The new option is implicitly enabled when encryption is default
* Private chat metadata is now updated after creating the room too
* The puppet metadata is updated before creating the room, to make sure their
  name is available locally
2020-05-20 21:19:42 +03:00
16 changed files with 87 additions and 53 deletions
+1 -1
View File
@@ -36,6 +36,6 @@ manifest:
script: script:
- docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-amd64 - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-amd64
- docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-arm64 - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-arm64
- if [ $CI_COMMIT_BRANCH == "master" ]; then docker manifest create $CI_REGISTRY_IMAGE:latest $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-amd64 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-arm64 && docker manifest push $CI_REGISTRY_IMAGE:latest; fi - if [ $CI_COMMIT_BRANCH = "master" ]; then docker manifest create $CI_REGISTRY_IMAGE:latest $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-amd64 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-arm64 && docker manifest push $CI_REGISTRY_IMAGE:latest; fi
- if [ $CI_COMMIT_BRANCH != "master" ]; then docker manifest create $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-amd64 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-arm64 && docker manifest push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME; fi - if [ $CI_COMMIT_BRANCH != "master" ]; then docker manifest create $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-amd64 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-arm64 && docker manifest push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME; fi
- docker rmi $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-amd64 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-arm64 - docker rmi $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-amd64 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-arm64
+1 -1
View File
@@ -1,2 +1,2 @@
__version__ = "0.8.0rc2" __version__ = "0.8.0rc3"
__author__ = "Tulir Asokan <tulir@maunium.net>" __author__ = "Tulir Asokan <tulir@maunium.net>"
+8 -9
View File
@@ -35,6 +35,7 @@ from telethon.tl.types import (
from mautrix.types import UserID, PresenceState from mautrix.types import UserID, PresenceState
from mautrix.errors import MatrixError from mautrix.errors import MatrixError
from mautrix.appservice import AppService from mautrix.appservice import AppService
from mautrix.util.logging import TraceLogger
from alchemysession import AlchemySessionContainer from alchemysession import AlchemySessionContainer
from . import portal as po, puppet as pu, __version__ from . import portal as po, puppet as pu, __version__
@@ -68,7 +69,7 @@ except ImportError:
class AbstractUser(ABC): class AbstractUser(ABC):
session_container: AlchemySessionContainer = None session_container: AlchemySessionContainer = None
loop: asyncio.AbstractEventLoop = None loop: asyncio.AbstractEventLoop = None
log: logging.Logger log: TraceLogger
az: AppService az: AppService
relaybot: Optional['Bot'] relaybot: Optional['Bot']
ignore_incoming_bot_events: bool = True ignore_incoming_bot_events: bool = True
@@ -258,7 +259,7 @@ class AbstractUser(ABC):
elif isinstance(update, UpdateReadHistoryOutbox): elif isinstance(update, UpdateReadHistoryOutbox):
await self.update_read_receipt(update) await self.update_read_receipt(update)
else: else:
self.log.debug("Unhandled update: %s", update) self.log.trace("Unhandled update: %s", update)
async def update_pinned_messages(self, update: Union[UpdateChannelPinnedMessage, async def update_pinned_messages(self, update: Union[UpdateChannelPinnedMessage,
UpdateChatPinnedMessage]) -> None: UpdateChatPinnedMessage]) -> None:
@@ -333,7 +334,7 @@ class AbstractUser(ABC):
if await puppet.update_avatar(self, update.photo): if await puppet.update_avatar(self, update.photo):
puppet.save() puppet.save()
else: else:
self.log.warning("Unexpected other user info update: %s", update) self.log.warning(f"Unexpected other user info update: {type(update)}")
async def update_status(self, update: UpdateUserStatus) -> None: async def update_status(self, update: UpdateUserStatus) -> None:
puppet = pu.Puppet.get(TelegramID(update.user_id)) puppet = pu.Puppet.get(TelegramID(update.user_id))
@@ -342,7 +343,7 @@ class AbstractUser(ABC):
elif isinstance(update.status, UserStatusOffline): elif isinstance(update.status, UserStatusOffline):
await puppet.default_mxid_intent.set_presence(PresenceState.OFFLINE) await puppet.default_mxid_intent.set_presence(PresenceState.OFFLINE)
else: else:
self.log.warning("Unexpected user status update: %s", update) self.log.warning(f"Unexpected user status update: type({update})")
return return
def get_message_details(self, update: UpdateMessage) -> Tuple[UpdateMessageContent, def get_message_details(self, update: UpdateMessage) -> Tuple[UpdateMessageContent,
@@ -366,8 +367,7 @@ class AbstractUser(ABC):
portal = po.Portal.get_by_entity(update.to_id, receiver_id=self.tgid) portal = po.Portal.get_by_entity(update.to_id, receiver_id=self.tgid)
sender = pu.Puppet.get(update.from_id) if update.from_id else None sender = pu.Puppet.get(update.from_id) if update.from_id else None
else: else:
self.log.warning( self.log.warning(f"Unexpected message type in User#get_message_details: {type(update)}")
f"Unexpected message type in User#get_message_details: {type(update)}")
return update, None, None return update, None, None
return update, sender, portal return update, sender, portal
@@ -428,11 +428,10 @@ class AbstractUser(ABC):
if isinstance(update, MessageService): if isinstance(update, MessageService):
if isinstance(update.action, MessageActionChannelMigrateFrom): if isinstance(update.action, MessageActionChannelMigrateFrom):
self.log.debug(f"Ignoring action %s to %s by %d", update.action, self.log.trace(f"Ignoring action %s to %s by %d", update.action, portal.tgid_log,
portal.tgid_log,
sender.id) sender.id)
return return
self.log.debug("Handling action %s to %s by %d", update.action, portal.tgid_log, self.log.trace("Handling action %s to %s by %d", update.action, portal.tgid_log,
sender.id) sender.id)
return await portal.handle_telegram_action(self, sender, update) return await portal.handle_telegram_action(self, sender, update)
+1 -1
View File
@@ -147,7 +147,7 @@ class Bot(AbstractUser):
if self.whitelist_group_admins: if self.whitelist_group_admins:
if isinstance(chat, PeerChannel): if isinstance(chat, PeerChannel):
p = await self.client(GetParticipantRequest(chat, tgid)) p = await self.client(GetParticipantRequest(chat, tgid))
return isinstance(p, (ChannelParticipantCreator, ChannelParticipantAdmin)) return isinstance(p.participant, (ChannelParticipantCreator, ChannelParticipantAdmin))
elif isinstance(chat, PeerChat): elif isinstance(chat, PeerChat):
chat = await self.client(GetFullChatRequest(chat.chat_id)) chat = await self.client(GetFullChatRequest(chat.chat_id))
participants = chat.full_chat.participants.participants participants = chat.full_chat.participants.participants
+3 -1
View File
@@ -165,7 +165,9 @@ async def join(evt: CommandEvent) -> Optional[EventID]:
try: try:
await portal.create_matrix_room(evt.sender, chat, [evt.sender.mxid]) await portal.create_matrix_room(evt.sender, chat, [evt.sender.mxid])
except ChatIdInvalidError as e: except ChatIdInvalidError as e:
logging.getLogger("mau.commands").info(updates.stringify()) logging.getLogger("mau.commands").trace("ChatIdInvalidError while creating portal "
"from !tg join command: %s",
updates.stringify())
raise e raise e
return await evt.reply(f"Created room for {portal.title}") return await evt.reply(f"Created room for {portal.title}")
return None return None
+1
View File
@@ -123,6 +123,7 @@ class Config(BaseBridgeConfig):
copy("bridge.animated_sticker.args") copy("bridge.animated_sticker.args")
copy("bridge.encryption.allow") copy("bridge.encryption.allow")
copy("bridge.encryption.default") copy("bridge.encryption.default")
copy("bridge.private_chat_portal_meta")
copy("bridge.initial_power_level_overrides.group") copy("bridge.initial_power_level_overrides.group")
copy("bridge.initial_power_level_overrides.user") copy("bridge.initial_power_level_overrides.user")
+3
View File
@@ -207,6 +207,9 @@ bridge:
# Default to encryption, force-enable encryption in all portals the bridge creates # Default to encryption, force-enable encryption in all portals the bridge creates
# This will cause the bridge bot to be in private chats for the encryption to work properly. # This will cause the bridge bot to be in private chats for the encryption to work properly.
default: false default: false
# Whether or not to explicitly set the avatar and room name for private
# chat portal rooms. This will be implicitly enabled if encryption.default is true.
private_chat_portal_meta: false
# Overrides for base power levels. # Overrides for base power levels.
initial_power_level_overrides: initial_power_level_overrides:
+6 -4
View File
@@ -32,6 +32,7 @@ from mautrix.errors import MatrixRequestError, IntentError
from mautrix.appservice import AppService, IntentAPI from mautrix.appservice import AppService, IntentAPI
from mautrix.types import RoomID, RoomAlias, UserID, EventType, PowerLevelStateEventContent from mautrix.types import RoomID, RoomAlias, UserID, EventType, PowerLevelStateEventContent
from mautrix.util.simple_template import SimpleTemplate from mautrix.util.simple_template import SimpleTemplate
from mautrix.util.logging import TraceLogger
from ..types import TelegramID from ..types import TelegramID
from ..context import Context from ..context import Context
@@ -55,7 +56,7 @@ config: Optional['Config'] = None
class BasePortal(ABC): class BasePortal(ABC):
base_log: logging.Logger = logging.getLogger("mau.portal") base_log: TraceLogger = logging.getLogger("mau.portal")
az: AppService = None az: AppService = None
bot: 'Bot' = None bot: 'Bot' = None
loop: asyncio.AbstractEventLoop = None loop: asyncio.AbstractEventLoop = None
@@ -69,6 +70,7 @@ class BasePortal(ABC):
sync_channel_members: bool = True sync_channel_members: bool = True
sync_matrix_state: bool = True sync_matrix_state: bool = True
public_portals: bool = False public_portals: bool = False
private_chat_portal_meta: bool = False
alias_template: SimpleTemplate[str] alias_template: SimpleTemplate[str]
hs_domain: str hs_domain: str
@@ -91,7 +93,7 @@ class BasePortal(ABC):
deleted: bool deleted: bool
backfilling: bool backfilling: bool
backfill_leave: Optional[Set[IntentAPI]] backfill_leave: Optional[Set[IntentAPI]]
log: logging.Logger log: TraceLogger
alias: Optional[RoomAlias] alias: Optional[RoomAlias]
@@ -242,8 +244,7 @@ class BasePortal(ABC):
return await user.client.get_entity(self.peer) return await user.client.get_entity(self.peer)
except ValueError: except ValueError:
if user.is_bot: if user.is_bot:
self.log.warning(f"Could not find entity with bot {user.tgid}. " self.log.warning(f"Could not find entity with bot {user.tgid}. Failing...")
"Failing...")
raise raise
self.log.warning(f"Could not find entity with user {user.tgid}. " self.log.warning(f"Could not find entity with user {user.tgid}. "
"falling back to get_dialogs.") "falling back to get_dialogs.")
@@ -518,6 +519,7 @@ def init(context: Context) -> None:
BasePortal.sync_channel_members = config["bridge.sync_channel_members"] BasePortal.sync_channel_members = config["bridge.sync_channel_members"]
BasePortal.sync_matrix_state = config["bridge.sync_matrix_state"] BasePortal.sync_matrix_state = config["bridge.sync_matrix_state"]
BasePortal.public_portals = config["bridge.public_portals"] BasePortal.public_portals = config["bridge.public_portals"]
BasePortal.private_chat_portal_meta = config["bridge.private_chat_portal_meta"]
BasePortal.filter_mode = config["bridge.filter.mode"] BasePortal.filter_mode = config["bridge.filter.mode"]
BasePortal.filter_list = config["bridge.filter.list"] BasePortal.filter_list = config["bridge.filter.list"]
BasePortal.hs_domain = config["homeserver.domain"] BasePortal.hs_domain = config["homeserver.domain"]
+2 -2
View File
@@ -342,7 +342,7 @@ class PortalMatrix(BasePortal, MautrixBasePortal, ABC):
def _add_telegram_message_to_db(self, event_id: EventID, space: TelegramID, def _add_telegram_message_to_db(self, event_id: EventID, space: TelegramID,
edit_index: int, response: TypeMessage) -> None: edit_index: int, response: TypeMessage) -> None:
self.log.debug("Handled Matrix message: %s", response) self.log.trace("Handled Matrix message: %s", response)
self.dedup.check(response, (event_id, space), force_hash=edit_index != 0) self.dedup.check(response, (event_id, space), force_hash=edit_index != 0)
if edit_index < 0: if edit_index < 0:
prev_edit = DBMessage.get_one_by_tgid(TelegramID(response.id), space, -1) prev_edit = DBMessage.get_one_by_tgid(TelegramID(response.id), space, -1)
@@ -403,7 +403,7 @@ class PortalMatrix(BasePortal, MautrixBasePortal, ABC):
await self._handle_matrix_file(sender_id, event_id, space, client, content, reply_to, await self._handle_matrix_file(sender_id, event_id, space, client, content, reply_to,
caption_content) caption_content)
else: else:
self.log.debug(f"Unhandled Matrix event: {content}") self.log.trace("Unhandled Matrix event: %s", content)
async def handle_matrix_pin(self, sender: 'u.User', async def handle_matrix_pin(self, sender: 'u.User',
pinned_message: Optional[EventID]) -> None: pinned_message: Optional[EventID]) -> None:
+24 -15
View File
@@ -13,7 +13,7 @@
# #
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import List, Optional, Tuple, Union, Callable, TYPE_CHECKING from typing import List, Optional, Tuple, Union, Callable, Awaitable, TYPE_CHECKING
from abc import ABC from abc import ABC
import asyncio import asyncio
@@ -26,7 +26,7 @@ from telethon.tl.types import (
Channel, ChatBannedRights, ChannelParticipantsRecent, ChannelParticipantsSearch, ChatPhoto, Channel, ChatBannedRights, ChannelParticipantsRecent, ChannelParticipantsSearch, ChatPhoto,
PhotoEmpty, InputChannel, InputUser, ChatPhotoEmpty, PeerUser, Photo, TypeChat, TypeInputPeer, PhotoEmpty, InputChannel, InputUser, ChatPhotoEmpty, PeerUser, Photo, TypeChat, TypeInputPeer,
TypeUser, User, InputPeerPhotoFileLocation, ChatParticipantAdmin, ChannelParticipantAdmin, TypeUser, User, InputPeerPhotoFileLocation, ChatParticipantAdmin, ChannelParticipantAdmin,
ChatParticipantCreator, ChannelParticipantCreator) ChatParticipantCreator, ChannelParticipantCreator, UserProfilePhoto, UserProfilePhotoEmpty)
from mautrix.errors import MForbidden from mautrix.errors import MForbidden
from mautrix.types import (RoomID, UserID, RoomCreatePreset, EventType, Membership, Member, from mautrix.types import (RoomID, UserID, RoomCreatePreset, EventType, Membership, Member,
@@ -218,10 +218,17 @@ class PortalMetadata(BasePortal, ABC):
puppet = p.Puppet.get(self.tgid) puppet = p.Puppet.get(self.tgid)
await puppet.update_info(user, entity) await puppet.update_info(user, entity)
await puppet.intent_for(self).join_room(self.mxid) await puppet.intent_for(self).join_room(self.mxid)
if self.encrypted or self.private_chat_portal_meta:
# The bridge bot needs to join for e2ee, but that messes up the default name
# generation. If/when canonical DMs happen, this might not be necessary anymore.
changed = await self._update_title(puppet.displayname)
changed = await self._update_avatar(user, entity.photo) or changed
if changed:
self.save()
if self.sync_matrix_state: if self.sync_matrix_state:
await self.sync_matrix_members() await self.sync_matrix_members()
async def create_matrix_room(self, user: 'AbstractUser', entity: TypeChat = None, async def create_matrix_room(self, user: 'AbstractUser', entity: Union[TypeChat, User] = None,
invites: InviteList = None, update_if_exists: bool = True, invites: InviteList = None, update_if_exists: bool = True,
synchronous: bool = False) -> Optional[str]: synchronous: bool = False) -> Optional[str]:
if self.mxid: if self.mxid:
@@ -245,8 +252,8 @@ class PortalMetadata(BasePortal, ABC):
except Exception: except Exception:
self.log.exception("Fatal error creating Matrix room") self.log.exception("Fatal error creating Matrix room")
async def _create_matrix_room(self, user: 'AbstractUser', entity: TypeChat, invites: InviteList async def _create_matrix_room(self, user: 'AbstractUser', entity: Union[TypeChat, User],
) -> Optional[RoomID]: invites: InviteList) -> Optional[RoomID]:
direct = self.peer_type == "user" direct = self.peer_type == "user"
if invites is None: if invites is None:
@@ -260,7 +267,7 @@ class PortalMetadata(BasePortal, ABC):
if not entity: if not entity:
entity = await self.get_entity(user) entity = await self.get_entity(user)
self.log.debug(f"Fetched data: {entity}") self.log.trace("Fetched data: %s", entity)
self.log.debug("Creating room") self.log.debug("Creating room")
@@ -274,6 +281,8 @@ class PortalMetadata(BasePortal, ABC):
self.about = "Your Telegram cloud storage chat" self.about = "Your Telegram cloud storage chat"
puppet = p.Puppet.get(self.tgid) if direct else None puppet = p.Puppet.get(self.tgid) if direct else None
if puppet:
await puppet.update_info(user, entity)
self._main_intent = puppet.intent_for(self) if direct else self.az.intent self._main_intent = puppet.intent_for(self) if direct else self.az.intent
if self.peer_type == "channel": if self.peer_type == "channel":
@@ -340,9 +349,8 @@ class PortalMetadata(BasePortal, ABC):
}) })
if direct: if direct:
invites.append(self.az.bot_mxid) invites.append(self.az.bot_mxid)
# The bridge bot needs to join for e2ee, but that messes up the default name if direct and (self.encrypted or self.private_chat_portal_meta):
# generation. If/when canonical DMs happen, this might not be necessary anymore. self.title = puppet.displayname
self.title = puppet.displayname
if config["appservice.community_id"]: if config["appservice.community_id"]:
initial_state.append({ initial_state.append({
"type": "m.room.related_groups", "type": "m.room.related_groups",
@@ -587,12 +595,12 @@ class PortalMetadata(BasePortal, ABC):
self.log.warning("Called update_info() for direct chat portal") self.log.warning("Called update_info() for direct chat portal")
return return
changed = False
self.log.debug("Updating info") self.log.debug("Updating info")
try: try:
if not entity: if not entity:
entity = await self.get_entity(user) entity = await self.get_entity(user)
self.log.debug(f"Fetched data: {entity}") self.log.trace("Fetched data: %s", entity)
changed = False
if self.peer_type == "channel": if self.peer_type == "channel":
changed = self.megagroup != entity.megagroup or changed changed = self.megagroup != entity.megagroup or changed
@@ -631,7 +639,7 @@ class PortalMetadata(BasePortal, ABC):
return True return True
async def _try_use_intent(self, sender: Optional['p.Puppet'], async def _try_use_intent(self, sender: Optional['p.Puppet'],
action: Callable[[IntentAPI], None]) -> None: action: Callable[[IntentAPI], Awaitable[None]]) -> None:
if sender: if sender:
try: try:
await action(sender.intent_for(self)) await action(sender.intent_for(self))
@@ -666,18 +674,19 @@ class PortalMetadata(BasePortal, ABC):
async def _update_avatar(self, user: 'AbstractUser', photo: TypeChatPhoto, async def _update_avatar(self, user: 'AbstractUser', photo: TypeChatPhoto,
sender: Optional['p.Puppet'] = None, save: bool = False) -> bool: sender: Optional['p.Puppet'] = None, save: bool = False) -> bool:
if isinstance(photo, ChatPhoto): if isinstance(photo, (ChatPhoto, UserProfilePhoto)):
loc = InputPeerPhotoFileLocation( loc = InputPeerPhotoFileLocation(
peer=await self.get_input_entity(user), peer=await self.get_input_entity(user),
local_id=photo.photo_big.local_id, local_id=photo.photo_big.local_id,
volume_id=photo.photo_big.volume_id, volume_id=photo.photo_big.volume_id,
big=True big=True
) )
photo_id = f"{loc.volume_id}-{loc.local_id}" photo_id = (f"{loc.volume_id}-{loc.local_id}" if isinstance(photo, ChatPhoto)
else photo.photo_id)
elif isinstance(photo, Photo): elif isinstance(photo, Photo):
loc, largest = self._get_largest_photo_size(photo) loc, largest = self._get_largest_photo_size(photo)
photo_id = f"{largest.location.volume_id}-{largest.location.local_id}" photo_id = f"{largest.location.volume_id}-{largest.location.local_id}"
elif isinstance(photo, (ChatPhotoEmpty, PhotoEmpty)): elif isinstance(photo, (UserProfilePhotoEmpty, ChatPhotoEmpty, PhotoEmpty, type(None))):
photo_id = "" photo_id = ""
loc = None loc = None
else: else:
+19 -8
View File
@@ -80,7 +80,7 @@ class PortalTelegram(BasePortal, ABC):
return await intent.send_message_event(self.mxid, event_type, content, **kwargs) return await intent.send_message_event(self.mxid, event_type, content, **kwargs)
async def handle_telegram_photo(self, source: 'AbstractUser', intent: IntentAPI, evt: Message, async def handle_telegram_photo(self, source: 'AbstractUser', intent: IntentAPI, evt: Message,
relates_to: Dict = None) -> Optional[EventID]: relates_to: RelatesTo = None) -> Optional[EventID]:
loc, largest_size = self._get_largest_photo_size(evt.media.photo) loc, largest_size = self._get_largest_photo_size(evt.media.photo)
file = await util.transfer_file_to_matrix(source.client, intent, loc, file = await util.transfer_file_to_matrix(source.client, intent, loc,
encrypt=self.encrypted) encrypt=self.encrypted)
@@ -227,8 +227,8 @@ class PortalTelegram(BasePortal, ABC):
content.url = file.mxc content.url = file.mxc
return await self._send_message(intent, content, event_type=event_type, timestamp=evt.date) return await self._send_message(intent, content, event_type=event_type, timestamp=evt.date)
def handle_telegram_location(self, _: 'AbstractUser', intent: IntentAPI, evt: Message, def handle_telegram_location(self, source: 'AbstractUser', intent: IntentAPI, evt: Message,
relates_to: dict = None) -> Awaitable[EventID]: relates_to: RelatesTo = None) -> Awaitable[EventID]:
long = evt.media.geo.long long = evt.media.geo.long
lat = evt.media.geo.lat lat = evt.media.geo.lat
long_char = "E" if long > 0 else "W" long_char = "E" if long > 0 else "W"
@@ -257,7 +257,7 @@ class PortalTelegram(BasePortal, ABC):
return await self._send_message(intent, content, timestamp=evt.date) return await self._send_message(intent, content, timestamp=evt.date)
async def handle_telegram_unsupported(self, source: 'AbstractUser', intent: IntentAPI, async def handle_telegram_unsupported(self, source: 'AbstractUser', intent: IntentAPI,
evt: Message, relates_to: dict = None) -> EventID: evt: Message, relates_to: RelatesTo = None) -> EventID:
override_text = ("This message is not supported on your version of Mautrix-Telegram. " override_text = ("This message is not supported on your version of Mautrix-Telegram. "
"Please check https://github.com/tulir/mautrix-telegram or ask your " "Please check https://github.com/tulir/mautrix-telegram or ask your "
"bridge administrator about possible updates.") "bridge administrator about possible updates.")
@@ -312,7 +312,7 @@ class PortalTelegram(BasePortal, ABC):
@staticmethod @staticmethod
def _int_to_bytes(i: int) -> bytes: def _int_to_bytes(i: int) -> bytes:
hex_value = "{0:010x}".format(i) hex_value = "{0:010x}".format(i).encode("utf-8")
return codecs.decode(hex_value, "hex_codec") return codecs.decode(hex_value, "hex_codec")
def _encode_msgid(self, source: 'AbstractUser', evt: Message) -> str: def _encode_msgid(self, source: 'AbstractUser', evt: Message) -> str:
@@ -355,6 +355,7 @@ class PortalTelegram(BasePortal, ABC):
async def handle_telegram_edit(self, source: 'AbstractUser', sender: p.Puppet, evt: Message async def handle_telegram_edit(self, source: 'AbstractUser', sender: p.Puppet, evt: Message
) -> None: ) -> None:
if not self.mxid: if not self.mxid:
self.log.trace("Ignoring edit to %d as chat has no Matrix room", evt.id)
return return
elif hasattr(evt, "media") and isinstance(evt.media, MessageMediaGame): elif hasattr(evt, "media") and isinstance(evt.media, MessageMediaGame):
self.log.debug("Ignoring game message edit event") self.log.debug("Ignoring game message edit event")
@@ -402,17 +403,21 @@ class PortalTelegram(BasePortal, ABC):
DBMessage.update_by_mxid(temporary_identifier, self.mxid, mxid=event_id) DBMessage.update_by_mxid(temporary_identifier, self.mxid, mxid=event_id)
async def backfill(self, source: 'AbstractUser') -> None: async def backfill(self, source: 'AbstractUser') -> None:
self.log.debug("Backfilling history through %s", source.mxid)
last = DBMessage.find_last(self.mxid, (source.tgid if self.peer_type != "channel" last = DBMessage.find_last(self.mxid, (source.tgid if self.peer_type != "channel"
else self.tgid)) else self.tgid))
min_id = last.tgid if last else 0 min_id = last.tgid if last else 0
self.backfilling = True self.backfilling = True
self.backfill_leave = set() self.backfill_leave = set()
if self.peer_type == "user": if self.peer_type == "user":
self.log.debug("Adding %s's default puppet to room for backfilling", source.mxid)
sender = p.Puppet.get(source.tgid) sender = p.Puppet.get(source.tgid)
await self.main_intent.invite_user(self.mxid, sender.default_mxid) await self.main_intent.invite_user(self.mxid, sender.default_mxid)
await sender.default_mxid_intent.join_room_by_id(self.mxid) await sender.default_mxid_intent.join_room_by_id(self.mxid)
self.backfill_leave.add(sender.default_mxid_intent) self.backfill_leave.add(sender.default_mxid_intent)
max_file_size = min(config["bridge.max_document_size"], 1500) * 1024 * 1024 max_file_size = min(config["bridge.max_document_size"], 1500) * 1024 * 1024
self.log.trace("Opening takeout client for %d, message ID %d->", source.tgid, min_id)
count = 0
async with source.client.takeout(files=True, megagroups=self.megagroup, async with source.client.takeout(files=True, megagroups=self.megagroup,
chats=self.peer_type == "chat", chats=self.peer_type == "chat",
users=self.peer_type == "user", users=self.peer_type == "user",
@@ -426,14 +431,18 @@ class PortalTelegram(BasePortal, ABC):
# if isinstance(message, MessageService): # if isinstance(message, MessageService):
# await self.handle_telegram_action(source, sender, message) # await self.handle_telegram_action(source, sender, message)
await self.handle_telegram_message(source, sender, message) await self.handle_telegram_message(source, sender, message)
count += 1
for intent in self.backfill_leave: for intent in self.backfill_leave:
self.log.trace("Leaving room with %s post-backfill", intent.mxid)
await intent.leave_room(self.mxid) await intent.leave_room(self.mxid)
self.backfilling = False self.backfilling = False
self.backfill_leave = None self.backfill_leave = None
self.log.info("Backfilled %d messages through %s", count, source.mxid)
async def handle_telegram_message(self, source: 'AbstractUser', sender: p.Puppet, async def handle_telegram_message(self, source: 'AbstractUser', sender: p.Puppet,
evt: Message) -> None: evt: Message) -> None:
if not self.mxid: if not self.mxid:
self.log.trace("Got telegram message %d, but no room exists, creating...", evt.id)
await self.create_matrix_room(source, invites=[source.mxid], update_if_exists=False) await self.create_matrix_room(source, invites=[source.mxid], update_if_exists=False)
if (self.peer_type == "user" and sender.tgid == self.tg_receiver if (self.peer_type == "user" and sender.tgid == self.tg_receiver
@@ -467,6 +476,8 @@ class PortalTelegram(BasePortal, ABC):
"bridge.deduplication.cache_queue_length in the config.") "bridge.deduplication.cache_queue_length in the config.")
return return
self.log.trace("Handling Telegram message %s", evt)
if sender and not sender.displayname: if sender and not sender.displayname:
self.log.debug(f"Telegram user {sender.tgid} sent a message, but doesn't have a " self.log.debug(f"Telegram user {sender.tgid} sent a message, but doesn't have a "
"displayname, updating info...") "displayname, updating info...")
@@ -500,7 +511,7 @@ class PortalTelegram(BasePortal, ABC):
}[type(media)](source, intent, evt, }[type(media)](source, intent, evt,
relates_to=formatter.telegram_reply_to_matrix(evt, source)) relates_to=formatter.telegram_reply_to_matrix(evt, source))
else: else:
self.log.debug("Unhandled Telegram message: %s", evt) self.log.debug("Unhandled Telegram message %d", evt.id)
return return
if not event_id: if not event_id:
@@ -517,7 +528,7 @@ class PortalTelegram(BasePortal, ABC):
await intent.redact(self.mxid, event_id) await intent.redact(self.mxid, event_id)
return return
self.log.debug("Handled Telegram message: %s", evt) self.log.debug("Handled telegram message %d -> %s", evt.id, event_id)
try: try:
DBMessage(tgid=TelegramID(evt.id), mx_room=self.mxid, mxid=event_id, DBMessage(tgid=TelegramID(evt.id), mx_room=self.mxid, mxid=event_id,
tg_space=tg_space, edit_index=0).insert() tg_space=tg_space, edit_index=0).insert()
@@ -572,7 +583,7 @@ class PortalTelegram(BasePortal, ABC):
# TODO handle game score # TODO handle game score
pass pass
else: else:
self.log.debug("Unhandled Telegram action in %s: %s", self.title, action) self.log.trace("Unhandled Telegram action in %s: %s", self.title, action)
async def set_telegram_admin(self, user_id: TelegramID) -> None: async def set_telegram_admin(self, user_id: TelegramID) -> None:
puppet = p.Puppet.get(user_id) puppet = p.Puppet.get(user_id)
+6 -4
View File
@@ -242,8 +242,7 @@ class Puppet(CustomPuppetMixin):
try: try:
changed = await self.update_displayname(source, info) or changed changed = await self.update_displayname(source, info) or changed
if isinstance(info.photo, UserProfilePhoto): changed = await self.update_avatar(source, info.photo) or changed
changed = await self.update_avatar(source, info.photo) or changed
except Exception: except Exception:
self.log.exception(f"Failed to update info from source {source.tgid}") self.log.exception(f"Failed to update info from source {source.tgid}")
@@ -294,10 +293,13 @@ class Puppet(CustomPuppetMixin):
if self.disable_updates: if self.disable_updates:
return False return False
if isinstance(photo, UserProfilePhotoEmpty): if photo is None or isinstance(photo, UserProfilePhotoEmpty):
photo_id = "" photo_id = ""
else: elif isinstance(photo, UserProfilePhoto):
photo_id = str(photo.photo_id) photo_id = str(photo.photo_id)
else:
self.log.warning(f"Unknown user profile photo type: {type(photo)}")
return False
if self.photo_id != photo_id: if self.photo_id != photo_id:
if not photo_id: if not photo_id:
self.photo_id = "" self.photo_id = ""
+4 -1
View File
@@ -29,6 +29,7 @@ from mautrix.client import Client
from mautrix.errors import MatrixRequestError from mautrix.errors import MatrixRequestError
from mautrix.types import UserID from mautrix.types import UserID
from mautrix.bridge import BaseUser from mautrix.bridge import BaseUser
from mautrix.util.logging import TraceLogger
from .types import TelegramID from .types import TelegramID
from .db import User as DBUser from .db import User as DBUser
@@ -45,7 +46,7 @@ SearchResult = NewType('SearchResult', Tuple['pu.Puppet', int])
class User(AbstractUser, BaseUser): class User(AbstractUser, BaseUser):
log: logging.Logger = logging.getLogger("mau.user") log: TraceLogger = logging.getLogger("mau.user")
by_mxid: Dict[str, 'User'] = {} by_mxid: Dict[str, 'User'] = {}
by_tgid: Dict[int, 'User'] = {} by_tgid: Dict[int, 'User'] = {}
@@ -343,10 +344,12 @@ class User(AbstractUser, BaseUser):
entity = dialog.entity entity = dialog.entity
if isinstance(entity, ChatForbidden): if isinstance(entity, ChatForbidden):
self.log.warning(f"Ignoring forbidden chat {entity} while syncing") self.log.warning(f"Ignoring forbidden chat {entity} while syncing")
continue
elif isinstance(entity, Chat) and (entity.deactivated or entity.left): elif isinstance(entity, Chat) and (entity.deactivated or entity.left):
self.log.warning(f"Ignoring deactivated or left chat {entity} while syncing") self.log.warning(f"Ignoring deactivated or left chat {entity} while syncing")
continue continue
elif isinstance(entity, TLUser) and not config["bridge.sync_direct_chats"]: elif isinstance(entity, TLUser) and not config["bridge.sync_direct_chats"]:
self.log.trace(f"Ignoring user {entity.id} while syncing")
continue continue
portal = po.Portal.get_by_entity(entity, receiver_id=self.tgid) portal = po.Portal.get_by_entity(entity, receiver_id=self.tgid)
self.portals[portal.tgid_full] = portal self.portals[portal.tgid_full] = portal
+2 -1
View File
@@ -13,7 +13,8 @@
# #
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from mautrix.util.color_log import ColorFormatter as BaseColorFormatter, PREFIX, MXID_COLOR, RESET from mautrix.util.logging.color import (ColorFormatter as BaseColorFormatter,
PREFIX, MXID_COLOR, RESET)
TELETHON_COLOR = PREFIX + "35;1m" # magenta TELETHON_COLOR = PREFIX + "35;1m" # magenta
TELETHON_MODULE_COLOR = PREFIX + "35m" TELETHON_MODULE_COLOR = PREFIX + "35m"
@@ -13,7 +13,7 @@
# #
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Optional, List, AsyncGenerator, Union, Awaitable, DefaultDict, Tuple from typing import Optional, List, AsyncGenerator, Union, Awaitable, DefaultDict, Tuple, cast
from collections import defaultdict from collections import defaultdict
import hashlib import hashlib
import asyncio import asyncio
@@ -35,6 +35,7 @@ from telethon import utils, helpers
from mautrix.appservice import IntentAPI from mautrix.appservice import IntentAPI
from mautrix.types import ContentURI, EncryptedFile from mautrix.types import ContentURI, EncryptedFile
from mautrix.util.logging import TraceLogger
from ..tgclient import MautrixTelegramClient from ..tgclient import MautrixTelegramClient
from ..db import TelegramFile as DBTelegramFile from ..db import TelegramFile as DBTelegramFile
@@ -44,7 +45,7 @@ try:
except ImportError: except ImportError:
async_encrypt_attachment = None async_encrypt_attachment = None
log: logging.Logger = logging.getLogger("mau.util") log: TraceLogger = cast(TraceLogger, logging.getLogger("mau.util"))
TypeLocation = Union[Document, InputDocumentFileLocation, InputPeerPhotoFileLocation, TypeLocation = Union[Document, InputDocumentFileLocation, InputPeerPhotoFileLocation,
InputFileLocation, InputPhotoFileLocation] InputFileLocation, InputPhotoFileLocation]
@@ -102,7 +103,7 @@ class UploadSender:
async def _next(self, data: bytes) -> None: async def _next(self, data: bytes) -> None:
self.request.bytes = data self.request.bytes = data
log.debug(f"Sending file part {self.request.file_part}/{self.part_count}" log.trace(f"Sending file part {self.request.file_part}/{self.part_count}"
f" with {len(data)} bytes") f" with {len(data)} bytes")
await self.sender.send(self.request) await self.sender.send(self.request)
self.request.file_part += self.stride self.request.file_part += self.stride
@@ -236,7 +237,7 @@ class ParallelTransferrer:
break break
yield data yield data
part += 1 part += 1
log.debug(f"Part {part} downloaded") log.trace(f"Part {part} downloaded")
log.debug("Parallel download finished, cleaning up connections") log.debug("Parallel download finished, cleaning up connections")
await self._cleanup() await self._cleanup()
+1 -1
View File
@@ -4,6 +4,6 @@ ruamel.yaml>=0.15.35,<0.17
python-magic>=0.4,<0.5 python-magic>=0.4,<0.5
commonmark>=0.8,<0.10 commonmark>=0.8,<0.10
aiohttp>=3,<4 aiohttp>=3,<4
mautrix==0.5.0.beta15 mautrix==0.5.0.beta16
telethon>=1.13,<1.14 telethon>=1.13,<1.14
telethon-session-sqlalchemy>=0.2.14,<0.3 telethon-session-sqlalchemy>=0.2.14,<0.3