Skip to content
Back to top
View this page
Edit this page

# superduper.session

User actions

## Classes

`Session` | The session of a registered User.

## Module Contents

class superduper.session.Session(user)

  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.

  Parameters:

    user (slidge.GatewayUser)

  async login()

    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 on_text(chat, text, *, reply_to_msg_id=None, reply_to_fallback_text=None, reply_to=None, thread=None, link_previews=(), mentions=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 (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[str]

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

  SPECIAL_MSG_ID_PREFIX: str | None = None

    If you set this, XMPP message IDs starting with this won’t be converted to legacy ID, but passed as is to `on_react()`, and usual checks for emoji restriction won’t be applied. This can be used to implement voting in polls in a hacky way.

  abstractmethod logout()
    Async:

    Return type:

      None

    Logs out the gateway user from the legacy network.

    Called on gateway shutdown.

  abstractmethod on_file(chat, url, *, http_response, reply_to_msg_id=None, reply_to_fallback_text=None, reply_to=None, thread=None)
    Async:

    Parameters:

      - chat (slidge.util.types.RecipientType)

      - url (str)

      - http_response (aiohttp.ClientResponse)

      - reply_to_msg_id (Optional[slidge.util.types.LegacyMessageType])

      - reply_to_fallback_text (Optional[str])

      - reply_to (Optional[Union[slidge.contact.LegacyContact, slidge.group.participant.LegacyParticipant]])

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

    Return type:

      Optional[slidge.util.types.LegacyMessageType]

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

    Parameters:

      - url (str) – URL of the file

      - chat (slidge.util.types.RecipientType) – See `BaseSession.on_text()`

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

      - reply_to_msg_id (Optional[slidge.util.types.LegacyMessageType]) – See `BaseSession.on_text()`

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

      - reply_to (Optional[Union[slidge.contact.LegacyContact, slidge.group.participant.LegacyParticipant]]) – See `BaseSession.on_text()`

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

  abstractmethod on_sticker(chat, sticker, *, reply_to_msg_id=None, reply_to_fallback_text=None, reply_to=None, thread=None)
    Async:

    Parameters:

      - chat (slidge.util.types.RecipientType)

      - sticker (slidge.util.types.Sticker)

      - reply_to_msg_id (Optional[slidge.util.types.LegacyMessageType])

      - reply_to_fallback_text (Optional[str])

      - reply_to (Optional[Union[slidge.contact.LegacyContact, slidge.group.participant.LegacyParticipant]])

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

    Return type:

      Optional[slidge.util.types.LegacyMessageType]

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

    Parameters:

      - chat (slidge.util.types.RecipientType) – See `BaseSession.on_text()`

      - sticker (slidge.util.types.Sticker) – The sticker sent by the user.

      - reply_to_msg_id (Optional[slidge.util.types.LegacyMessageType]) – See `BaseSession.on_text()`

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

      - reply_to (Optional[Union[slidge.contact.LegacyContact, slidge.group.participant.LegacyParticipant]]) – See `BaseSession.on_text()`

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

  abstractmethod on_active(chat, thread=None)
    Async:

    Parameters:

      - chat (slidge.util.types.RecipientType)

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

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

    Parameters:

      - chat (slidge.util.types.RecipientType) – See `BaseSession.on_text()`

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

  abstractmethod on_inactive(chat, thread=None)
    Async:

    Parameters:

      - chat (slidge.util.types.RecipientType)

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

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

    Parameters:

      - chat (slidge.util.types.RecipientType) – See `BaseSession.on_text()`

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

  abstractmethod on_composing(chat, thread=None)
    Async:

    Parameters:

      - chat (slidge.util.types.RecipientType)

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

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

    Parameters:

      - chat (slidge.util.types.RecipientType) – See `BaseSession.on_text()`

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

  abstractmethod on_paused(chat, thread=None)
    Async:

    Parameters:

      - chat (slidge.util.types.RecipientType)

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

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

    Parameters:

      - chat (slidge.util.types.RecipientType) – See `BaseSession.on_text()`

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

  abstractmethod on_gone(chat, thread=None)
    Async:

    Parameters:

      - chat (slidge.util.types.RecipientType)

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

    Triggered when the user is “gone” in a legacy chat (XEP-0085)

    Parameters:

      - chat (slidge.util.types.RecipientType) – See `BaseSession.on_text()`

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

  abstractmethod on_displayed(chat, legacy_msg_id, thread=None)
    Async:

    Parameters:

      - chat (slidge.util.types.RecipientType)

      - legacy_msg_id (slidge.util.types.LegacyMessageType)

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

    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 (slidge.util.types.RecipientType) – See `BaseSession.on_text()`

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

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

  abstractmethod on_correct(chat, text, legacy_msg_id, *, thread=None, link_previews=(), mentions=None)
    Async:

    Parameters:

      - chat (slidge.util.types.RecipientType)

      - text (str)

      - legacy_msg_id (slidge.util.types.LegacyMessageType)

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

      - link_previews (Iterable[slidge.util.types.LinkPreview])

      - mentions (Optional[list[slidge.util.types.Mention]])

    Return type:

      Optional[slidge.util.types.LegacyMessageType]

    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 (slidge.util.types.RecipientType) – See `BaseSession.on_text()`

      - text (str) – The new text

      - legacy_msg_id (slidge.util.types.LegacyMessageType) – Identifier of the edited message

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

      - 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[slidge.util.types.LegacyMessageType]

  abstractmethod on_react(chat, legacy_msg_id, emojis, thread=None)
    Async:

    Parameters:

      - chat (slidge.util.types.RecipientType)

      - legacy_msg_id (slidge.util.types.LegacyMessageType)

      - emojis (list[str])

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

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

    Parameters:

      - chat (slidge.util.types.RecipientType) – See `BaseSession.on_text()`

      - 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

  abstractmethod on_retract(chat, legacy_msg_id, thread=None)
    Async:

    Parameters:

      - chat (slidge.util.types.RecipientType)

      - legacy_msg_id (slidge.util.types.LegacyMessageType)

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

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

    Parameters:

      - chat (slidge.util.types.RecipientType) – See `BaseSession.on_text()`

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

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

  abstractmethod on_presence(resource, show, status, resources, merged_resource)
    Async:

    Parameters:

      - resource (str)

      - show (slidge.util.types.PseudoPresenceShow)

      - status (str)

      - resources (dict[str, slixmpp.types.ResourceDict])

      - merged_resource (Optional[slixmpp.types.ResourceDict])

    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, slixmpp.types.ResourceDict]) – A summary of all the resources for this user.

      - merged_resource (Optional[slixmpp.types.ResourceDict]) – A global presence for the user account, following rules described in `merge_resources()`

  abstractmethod on_search(form_values)
    Async:

    Parameters:

      form_values (dict[str, str])

    Return type:

      Optional[slidge.command.SearchResult]

    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]

  abstractmethod on_avatar(bytes_, hash_, type_, width, height)
    Async:

    Parameters:

      - bytes_ (Optional[bytes])

      - hash_ (Optional[str])

      - type_ (Optional[str])

      - width (Optional[int])

      - height (Optional[int])

    Return type:

      None

    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

  abstractmethod on_moderate(muc, legacy_msg_id, reason)
    Async:

    Parameters:

      - muc (slidge.group.room.LegacyMUC)

      - legacy_msg_id (slidge.util.types.LegacyMessageType)

      - reason (Optional[str])

    Triggered when the user attempts to retract a message that was sent in a MUC using XEP-0425.

    If retraction is not possible, this should raise the appropriate XMPPError with a human-readable message.

    NB: the legacy module is responsible for calling :method:`LegacyParticipant.moderate` when this is successful, because slidge will acknowledge the moderation IQ, but will not send the moderation message from the MUC automatically.

    Parameters:

      - muc (slidge.group.room.LegacyMUC) – The MUC in which the message was sent

      - legacy_msg_id (slidge.util.types.LegacyMessageType) – The legacy ID of the message to be retracted

      - reason (Optional[str]) – Optionally, a reason for the moderation, given by the user-moderator.

  abstractmethod on_create_group(name, contacts)
    Async:

    Parameters:

      - name (str)

      - contacts (list[slidge.contact.LegacyContact])

    Return type:

      slidge.util.types.LegacyGroupIdType

    Triggered when the user request the creation of a group via the dedicated Command.

    Parameters:

      - name (str) – Name of the group

      - contacts (list[slidge.contact.LegacyContact]) – list of contacts that should be members of the group

    Return type:

      slidge.util.types.LegacyGroupIdType

  async on_invitation(contact, muc, reason)

    Triggered when the user invites a Contact to a legacy MUC via XEP-0249.

    The default implementation calls `LegacyMUC.on_set_affiliation()` with the ‘member’ affiliation. Override if you want to customize this behaviour.

    Parameters:

      - contact (slidge.contact.LegacyContact) – The invitee

      - muc (slidge.group.room.LegacyMUC) – The group

      - reason (Optional[str]) – Optionally, a reason

    Return type:

      None

  abstractmethod on_leave_group(muc_legacy_id)
    Async:

    Parameters:

      muc_legacy_id (slidge.util.types.LegacyGroupIdType)

    Triggered when the user leaves a group via the dedicated slidge command or the XEP-0077 `<remove />` mechanism.

    This should be interpreted as definitely leaving the group.

    Parameters:

      muc_legacy_id (slidge.util.types.LegacyGroupIdType) – The legacy ID of the group to leave

  abstractmethod on_preferences(previous, new)
    Async:

    Parameters:

      - previous (dict[str, Any])

      - new (dict[str, Any])

    Return type:

      None

    This is called when the user updates their preferences.

    Override this if you need set custom preferences field and need to trigger something when a preference has changed.

  static legacy_to_xmpp_msg_id(legacy_msg_id)

    Convert a legacy msg ID to a valid XMPP msg ID. Needed for read marks, retractions 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 <Legacy Network>-specific logic.

    Parameters:

      legacy_msg_id (slidge.util.types.LegacyMessageType)

    Returns:

      A string that is usable as an XMPP stanza ID

    Return type:

      str

  static xmpp_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 <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

    Return type:

      None

  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

    Return type:

      None

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

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

    Parameters:

      - muc (slidge.group.room.LegacyMUC)

      - reason (Optional[str])

      - password (Optional[str])

    Return type:

      None

  async input(text, **msg_kwargs)

    Request user input via direct messages from the gateway component.

    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

    Return type:

      None