matridge.session

Module Contents

Classes

Session

The session of a registered User.

Attributes

matridge.session.Sender[source]
matridge.session.Recipient[source]
class matridge.session.Session(*a)[source]

Bases: slidge.BaseSession[str, Recipient]

The session of a registered User.

Represents a gateway user logged in to the legacy network and performing actions.

Will be instantiated automatically on slidge startup for each registered user, or upon registration for new (validated) users.

Must be subclassed for a functional Legacy Module.

bookmarks: matridge.group.Bookmarks[source]
contacts: matridge.contact.Roster[source]
matrix: matridge.matrix.Client[source]
MESSAGE_IDS_ARE_THREAD_IDS = True[source]
async login()[source]

Logs in the gateway user to the legacy network.

Triggered when the gateway start and on user registration. It is recommended that this function returns once the user is logged in, so if you need to await forever (for instance to listen to incoming events), it’s a good idea to wrap your listener in an asyncio.Task.

Returns:

Optionally, a text to use as the gateway status, e.g., “Connected as ‘dude@legacy.network’”

async logout()[source]

Logs out the gateway user from the legacy network.

Called on gateway shutdown.

async __relates_to(room_id, content, reply_to_msg_id, thread)[source]
Parameters:
  • room_id (str)

  • content (dict[str, Any])

  • reply_to_msg_id (Optional[str])

  • thread (Optional[str])

async __handle_response(response)[source]
Parameters:

response (nio.Response)

async __room_send(chat, content, message_type='m.room.message')[source]
Parameters:
async on_text(chat, text, *, reply_to_msg_id=None, reply_to_fallback_text=None, reply_to=None, thread=None, link_previews=(), mentions=None)[source]

Triggered when the user sends a text message from XMPP to a bridged entity, e.g. to translated_user_name@slidge.example.com, or translated_group_name@slidge.example.com

Override this and implement sending a message to the legacy network in this method.

Parameters:
  • text (str) – Content of the message

  • chat (Recipient) – Recipient of the message. LegacyContact instance for 1:1 chat, MUC instance for groups.

  • reply_to_msg_id (Optional[str]) – A legacy message ID if the message references (quotes) another message (XEP-0461)

  • reply_to_fallback_text (Optional[str]) – Content of the quoted text. Not necessarily set by XMPP clients

  • reply_to (Optional[Sender]) – Author of the quoted message. LegacyContact instance for 1:1 chat, LegacyParticipant instance for groups. If None, should be interpreted as a self-reply if reply_to_msg_id is not None.

  • link_previews (Iterable[slidge.util.types.LinkPreview]) – A list of sender-generated link previews. At the time of writing, only Cheogram supports it.

  • mentions (Optional[list[slidge.util.types.Mention]]) – (only for groups) A list of Contacts mentioned by their nicknames.

  • thread (Optional[str])

Returns:

An ID of some sort that can be used later to ack and mark the message as read by the user

Return type:

Optional[slidge.util.types.LegacyMessageType]

async on_file(chat, url, *, http_response, reply_to_msg_id=None, reply_to_fallback_text=None, reply_to=None, thread=None)[source]

Triggered when the user sends a file using HTTP Upload (XEP-0363)

Parameters:
  • url (str) – URL of the file

  • chat (Recipient) – See BaseSession.on_text()

  • http_response (aiohttp.ClientResponse) – The HTTP GET response object on the URL

  • reply_to_msg_id (Optional[str]) – See BaseSession.on_text()

  • reply_to_fallback_text (Optional[str]) – See BaseSession.on_text()

  • reply_to (Optional[Sender]) – See BaseSession.on_text()

  • thread (Optional[str])

Returns:

An ID of some sort that can be used later to ack and mark the message as read by the user

Return type:

Optional[slidge.util.types.LegacyMessageType]

async on_composing(chat, thread=None)[source]

Triggered when the user starts typing in a legacy chat (XEP-0085)

Parameters:
  • chat (Recipient) – See BaseSession.on_text()

  • thread (Optional[slidge.util.types.LegacyThreadType])

async on_paused(chat, thread=None)[source]

Triggered when the user pauses typing in a legacy chat (XEP-0085)

Parameters:
  • chat (Recipient) – See BaseSession.on_text()

  • thread (Optional[slidge.util.types.LegacyThreadType])

async on_displayed(chat, legacy_msg_id, thread=None)[source]

Triggered when the user reads a message in a legacy chat. (XEP-0333)

This is only possible if a valid legacy_msg_id was passed when transmitting a message from a legacy chat to the user, eg in slidge.contact.LegacyContact.send_text() or slidge.group.LegacyParticipant.send_text().

Parameters:
  • chat (Recipient) – See BaseSession.on_text()

  • legacy_msg_id (slidge.util.types.LegacyMessageType) – Identifier of the message/

  • thread (Optional[slidge.util.types.LegacyThreadType])

async on_correct(chat, text, legacy_msg_id, thread=None, link_previews=(), mentions=None)[source]

Triggered when the user corrects a message using XEP-0308

This is only possible if a valid legacy_msg_id was returned by on_text().

Parameters:
  • chat (Recipient) – See BaseSession.on_text()

  • text (str) – The new text

  • legacy_msg_id (str) – Identifier of the edited message

  • thread (Optional[str])

  • link_previews (Iterable[slidge.util.types.LinkPreview]) –

    A list of sender-generated link previews. At the time of writing, only Cheogram supports it.

  • mentions (Optional[list[slidge.util.types.Mention]]) – (only for groups) A list of Contacts mentioned by their nicknames.

Return type:

Optional[str]

async on_react(chat, legacy_msg_id, emojis, thread=None)[source]

Triggered when the user sends message reactions (XEP-0444).

Parameters:
  • chat (Recipient) – See BaseSession.on_text()

  • thread (Optional[slidge.util.types.LegacyThreadType])

  • legacy_msg_id (str) – ID of the message the user reacts to

  • emojis (list[str]) – Unicode characters representing reactions to the message legacy_msg_id. An empty string means “no reaction”, ie, remove all reactions if any were present before

async on_retract(chat, legacy_msg_id, thread=None)[source]

Triggered when the user retracts (XEP-0424) a message.

Parameters:
  • chat (Recipient) – See BaseSession.on_text()

  • thread (Optional[str])

  • legacy_msg_id (str) – Legacy ID of the retracted message

async on_presence(resource, show, status, resources, merged_resource)[source]

Called when the gateway component receives a presence, ie, when one of the user’s clients goes online of offline, or changes its status.

Parameters:
  • resource (str) – The XMPP client identifier, arbitrary string.

  • show (slidge.util.types.PseudoPresenceShow) – The presence <show>, if available. If the resource is just ‘available’ without any <show> element, this is an empty str.

  • status (str) – A status message, like a deeply profound quote, eg, “Roses are red, violets are blue, [INSERT JOKE]”.

  • resources (dict[str, slidge.util.types.ResourceDict]) – A summary of all the resources for this user.

  • merged_resource (Optional[slidge.util.types.ResourceDict]) – A global presence for the user account, following rules described in merge_resources()

async on_avatar(bytes_, hash_, type_, width, height)[source]

Triggered when the user uses modifies their avatar via XEP-0084.

Parameters:
  • bytes – The data of the avatar. According to the spec, this should always be a PNG, but some implementations do not respect that. If None it means the user has unpublished their avatar.

  • hash – The SHA1 hash of the avatar data. This is an identifier of the avatar.

  • type – The MIME type of the avatar.

  • width (Optional[int]) – The width of the avatar image.

  • height (Optional[int]) – The height of the avatar image.

  • bytes_ (Optional[bytes])

  • hash_ (Optional[str])

  • type_ (Optional[str])

Return type:

None

matridge.session.PRESENCE_DICT: dict[slidge.util.types.PseudoPresenceShow, str][source]