slidge.core.session#

Module Contents#

Classes#

BaseSession

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

class slidge.core.session.BaseSession(user)#

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

Will be instantiated automatically when a user sends an online presence to the gateway component, as per XEP-0100.

Must be subclassed for a functional slidge plugin.

Parameters:

user (slidge.util.db.GatewayUser) –

xmpp: slidge.core.gateway.BaseGateway#

The gateway instance singleton. Use it for low-level XMPP calls or custom methods that are not session-specific.

MESSAGE_IDS_ARE_THREAD_IDS = False#

Set this to True if the legacy service uses message IDs as thread IDs, eg Mattermost, where you can only ‘create a thread’ by replying to the message, in which case the message ID is also a thread ID (and all messages are potential threads).

static legacy_msg_id_to_xmpp_msg_id(legacy_msg_id)#

Convert a legacy msg ID to a valid XMPP msg ID. Needed for read marks and message corrections.

The default implementation just converts the legacy ID to a str, but this should be overridden in case some characters needs to be escaped, or to add some additional, legacy network-specific logic.

Parameters:

legacy_msg_id (slidge.util.types.LegacyMessageType) –

Returns:

Should return a string that is usable as an XMPP stanza ID

Return type:

str

static xmpp_msg_id_to_legacy_msg_id(i)#

Convert a legacy XMPP ID to a valid XMPP msg ID. Needed for read marks and message corrections.

The default implementation just converts the legacy ID to a str, but this should be overridden in case some characters needs to be escaped, or to add some additional, legacy network-specific logic.

The default implementation is an identity function

Parameters:

i (str) – The XMPP stanza ID

Returns:

An ID that can be used to identify a message on the legacy network

Return type:

slidge.util.types.LegacyMessageType

send_gateway_status(status=None, show=Optional[PresenceShows], **kwargs)#

Send a presence from the gateway to the user.

Can be used to indicate the user session status, ie “SMS code required”, “connected”, …

Parameters:
  • status (Optional[str]) – A status message

  • show – Presence stanza ‘show’ element. I suggest using “dnd” to show that the gateway is not fully functional

send_gateway_message(text, **msg_kwargs)#

Send a message from the gateway component to the user.

Can be used to indicate the user session status, ie “SMS code required”, “connected”, …

Parameters:

text (str) – A text

send_gateway_invite(muc, reason=None, password=None)#

Send an invitation to join a MUC, emanating from the gateway component.

Parameters:
async input(text, **msg_kwargs)#

Request user input via direct messages.

Wraps call to BaseSession.input()

Parameters:
  • text (str) – The prompt to send to the user

  • msg_kwargs – Extra attributes

Returns:

async send_qr(text)#

Sends a QR code generated from ‘text’ via HTTP Upload and send the URL to self.user

Parameters:

text (str) – Text to encode as a QR code

abstract async login()#

Login 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’”

Return type:

Optional[str]

abstract async logout()#

Logout the gateway user from the legacy network.

Called on user unregistration and gateway shutdown.

re_login()#

Logout then re-login

No reason to override this

abstract async send_text(chat, text, *, reply_to_msg_id=None, reply_to_fallback_text=None, reply_to=None, thread=None)#

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 (slidge.util.types.RecipientType) – RecipientType of the message. LegacyContact instance for 1:1 chat, MUC instance for groups.

  • reply_to_msg_id (Optional[slidge.util.types.LegacyMessageType]) – 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[slidge.util.types.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.

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

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]

abstract async send_file(chat, url, *, http_response, reply_to_msg_id=None, reply_to_fallback_text=None, reply_to=None, thread=None)#

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

Parameters:
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]

abstract async active(c, thread=None)#

Triggered when the user sends an ‘active’ chat state to the legacy network (XEP-0085)

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

  • c (slidge.util.types.RecipientType) – RecipientType of the active chat state

abstract async inactive(c, thread=None)#

Triggered when the user sends an ‘inactive’ chat state to the legacy network (XEP-0085)

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

  • c (slidge.util.types.RecipientType) –

abstract async composing(c, thread=None)#

Triggered when the user starts typing in the window of a legacy contact (XEP-0085)

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

  • c (slidge.util.types.RecipientType) –

abstract async paused(c, thread=None)#

Triggered when the user pauses typing in the window of a legacy contact (XEP-0085)

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

  • c (slidge.util.types.RecipientType) –

abstract async displayed(c, legacy_msg_id, thread=None)#

Triggered when the user reads a message sent by a legacy contact. (XEP-0333)

This is only possible if a valid legacy_msg_id was passed when transmitting a message from a contact to the user in LegacyContact.sent_text() or slidge.LegacyContact.send_file().

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

  • legacy_msg_id (slidge.util.types.LegacyMessageType) – Identifier of the message, passed to slidge.LegacyContact.send_text() or slidge.LegacyContact.send_file()

  • c (slidge.util.types.RecipientType) –

abstract async correct(c, text, legacy_msg_id, thread=None)#

Triggered when the user corrected a message using XEP-0308

This is only possible if a valid legacy_msg_id was passed when transmitting a message from a contact to the user in LegacyContact.send_text() or slidge.LegacyContact.send_file().

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

  • text (str) –

  • legacy_msg_id (slidge.util.types.LegacyMessageType) –

  • c (slidge.util.types.RecipientType) –

Return type:

Optional[slidge.util.types.LegacyMessageType]

abstract async search(form_values)#

Triggered when the user uses Jabber Search (XEP-0055) on the component

Form values is a dict in which keys are defined in BaseGateway.SEARCH_FIELDS

Parameters:

form_values (dict[str, str]) – search query, defined for a specific plugin by overriding in BaseGateway.SEARCH_FIELDS

Returns:

Return type:

Optional[slidge.command.SearchResult]

abstract async react(c, legacy_msg_id, emojis, thread=None)#

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

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

  • legacy_msg_id (slidge.util.types.LegacyMessageType) – 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

  • c (slidge.util.types.RecipientType) – Contact or MUC the reaction refers to

abstract async retract(c, legacy_msg_id, thread=None)#

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

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

  • legacy_msg_id (slidge.util.types.LegacyMessageType) – Legacy ID of the retracted message

  • c (slidge.util.types.RecipientType) – The contact this retraction refers to

abstract async presence(resource, show, status, resources, merged_resource)#

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()