slidge.core.session#

Module Contents#

Classes#

BaseSession

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

Functions#

Attributes#

slidge.core.session.GatewayType[source]#
slidge.core.session.ignore_message_to_component_and_sent_carbons(func)[source]#
slidge.core.session.T[source]#
class slidge.core.session.BaseSession(user)[source]#

Bases: Generic[slidge.core.contact.LegacyContactType, slidge.core.contact.LegacyRosterType, slidge.core.gateway.GatewayType]

Represents a gateway user logged in to the 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) –

sent :slidge.util.BiDict[slidge.util.types.LegacyMessageType, str][source]#

Since we cannot set the XMPP ID of messages sent by XMPP clients, we need to keep a mapping between XMPP IDs and legacy message IDs if we want to further refer to a message that was sent by the user. This also applies to ‘carboned’ messages, ie, messages sent by the user from the official client of a legacy network.

xmpp :slidge.core.gateway.GatewayType[source]#

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

static legacy_msg_id_to_xmpp_msg_id(legacy_msg_id)[source]#

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

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)[source]#

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

classmethod _from_user_or_none(user)[source]#
classmethod from_user(user)[source]#
classmethod from_stanza(s)[source]#

Get a user’s LegacySession using the “from” field of a stanza

Meant to be called from BaseGateway only.

Parameters

s

Returns

Return type

T

classmethod from_jid(jid)[source]#

Get a user’s LegacySession using its jid

Meant to be called from BaseGateway only.

Parameters

jid (slixmpp.JID) –

Returns

Return type

T

async classmethod kill_by_jid(jid)[source]#

Terminate a user session.

Meant to be called from BaseGateway only.

Parameters

jid (slixmpp.JID) –

Returns

async send_from_msg(m)[source]#

Meant to be called from BaseGateway only.

Parameters

m (slixmpp.Message) –

Returns

async active_from_msg(m)[source]#

Meant to be called from BaseGateway only.

Parameters

m (slixmpp.Message) –

Returns

async inactive_from_msg(m)[source]#

Meant to be called from BaseGateway only.

Parameters

m (slixmpp.Message) –

Returns

async composing_from_msg(m)[source]#

Meant to be called from BaseGateway only.

Parameters

m (slixmpp.Message) –

Returns

async paused_from_msg(m)[source]#

Meant to be called from BaseGateway only.

Parameters

m (slixmpp.Message) –

Returns

async displayed_from_msg(m)[source]#

Meant to be called from BaseGateway only.

Parameters

m (slixmpp.Message) –

Returns

async correct_from_msg(m)[source]#
Parameters

m (slixmpp.Message) –

async react_from_msg(m)[source]#
Parameters

m (slixmpp.Message) –

async retract_from_msg(m)[source]#
Parameters

m (slixmpp.Message) –

send_gateway_status(status=None, show=Optional[Literal['away', 'chat', 'dnd', 'xa']], **kwargs)[source]#

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)[source]#

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 – A text

async input(text, **msg_kwargs)[source]#

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)[source]#

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()[source]#

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()[source]#

Logout the gateway user from the legacy network.

Called on user unregistration and gateway shutdown.

re_login()[source]#

Logout then re-login

No reason to override this

abstract async send_text(t, c, *, reply_to_msg_id=None)[source]#

Triggered when the user sends a text message from xmpp to a bridged contact, e.g. to translated_user_name@slidge.example.com.

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

Parameters
  • t (str) – Content of the message

  • c (slidge.core.contact.LegacyContactType) – Recipient of the message

  • reply_to_msg_id (Optional[slidge.util.types.LegacyMessageType]) –

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(u, c, *, reply_to_msg_id=None)[source]#

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

Parameters
  • u (str) – URL of the file

  • c (slidge.core.contact.LegacyContactType) – Recipient of the file

  • reply_to_msg_id (Optional[slidge.util.types.LegacyMessageType]) –

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)[source]#

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

Parameters

c (slidge.core.contact.LegacyContactType) – Recipient of the active chat state

abstract async inactive(c)[source]#

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

Parameters

c (slidge.core.contact.LegacyContactType) –

abstract async composing(c)[source]#

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

Parameters

c (slidge.core.contact.LegacyContactType) –

abstract async paused(c)[source]#

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

Parameters

c (slidge.core.contact.LegacyContactType) –

abstract async displayed(legacy_msg_id, c)[source]#

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
abstract async correct(text, legacy_msg_id, c)[source]#

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
  • text (str) –

  • legacy_msg_id (Any) –

  • c (slidge.core.contact.LegacyContactType) –

Return type

Optional[slidge.util.types.LegacyMessageType]

abstract async search(form_values)[source]#

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.SearchResult]

abstract async react(legacy_msg_id, emojis, c)[source]#

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

Parameters
  • legacy_msg_id (Any) – 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.core.contact.LegacyContactType) – Contact the reaction refers to

abstract async retract(legacy_msg_id, c)[source]#

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

Parameters
  • legacy_msg_id (Any) – Legacy ID of the retracted message

  • c (slidge.core.contact.LegacyContactType) – The contact this retraction refers to

slidge.core.session.SessionType[source]#
slidge.core.session._sessions :dict[slidge.util.db.GatewayUser, BaseSession][source]#
slidge.core.session.log[source]#