slidge.plugins.dummy#

A pseudo legacy network, to easily test things

Module Contents#

Classes#

Bookmarks

Abstract base class for generic types.

MUC

Abstract base class for generic types.

Participant

Contact

This class centralizes actions in relation to a specific legacy contact.

Gateway

Must be subclassed by a plugin to set up various aspects of the XMPP

Roster

Virtual roster of a gateway user, that allows to represent all

Session

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

Attributes#

slidge.plugins.dummy.ASSETS_DIR[source]#
class slidge.plugins.dummy.Bookmarks(session)[source]#

Bases: slidge.LegacyBookmarks

Abstract base class for generic types.

A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as:

class Mapping(Generic[KT, VT]):
    def __getitem__(self, key: KT) -> VT:
        ...
    # Etc.

This class can then be used as follows:

def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
    try:
        return mapping[key]
    except KeyError:
        return default
Parameters:

session (slidge.core.session.BaseSession) –

async static jid_local_part_to_legacy_id(local_part)[source]#
async fill()[source]#

Establish a user’s known groups.

This has to be overridden in plugins with group support and at the minimum, this should await self.by_legacy_id(group_id) for all the groups a user is part of.

Ideally, set the group subject, friendly, number, etc. in this method

Slidge internals will call this on successful BaseSession.login()

class slidge.plugins.dummy.MUC(*a, **k)[source]#

Bases: slidge.LegacyMUC

Abstract base class for generic types.

A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as:

class Mapping(Generic[KT, VT]):
    def __getitem__(self, key: KT) -> VT:
        ...
    # Etc.

This class can then be used as follows:

def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
    try:
        return mapping[key]
    except KeyError:
        return default
REACTIONS_SINGLE_EMOJI = True[source]#
session: Session[source]#
msg_ids[source]#
async join(p)[source]#
async backfill(oldest_message_id=None, oldest_date=None)[source]#

Override this if the legacy network provide server-side archive. In it, send history messages using self.get_participant().send*, with the archive_only=True kwarg.

You only need to fetch messages older than oldest_message_id.

Parameters:
  • oldest_message_id – The oldest message ID already present in the archive

  • oldest_message_date – The oldest message date already present in the archive

async later_error()[source]#
async update_info()[source]#

Fetch information about this group from the legacy network

This is awaited on MUC instantiation, and should be overridden to update the attributes of the group chat, like title, subject, number of participants etc.

async fill_participants()[source]#

In here, call self.get_participant(), self.get_participant_by_contact(), of self.get_user_participant() to make an initial list of participants.

async send_text(text)[source]#
Parameters:

text (str) –

Return type:

str

class slidge.plugins.dummy.Participant(muc, nickname=None, is_user=False, is_system=False)[source]#

Bases: slidge.LegacyParticipant

Parameters:
class slidge.plugins.dummy.Contact(session, legacy_id, jid_username)[source]#

Bases: slidge.LegacyContact

This class centralizes actions in relation to a specific legacy contact.

You shouldn’t create instances of contacts manually, but rather rely on LegacyRoster.by_legacy_id() to ensure that contact instances are singletons. The LegacyRoster instance of a session is accessible through the BaseSession.contacts attribute.

Typically, your plugin should have methods hook to the legacy events and call appropriate methods here to transmit the “legacy action” to the xmpp user. This should look like this:

Use carbon=True as a keyword arg for methods to represent an action FROM the user TO the contact, typically when the user uses an official client to do an action such as sending a message or marking as message as read. This will use XEP-0363 to impersonate the XMPP user in order.

Parameters:
  • session (slidge.core.session.BaseSession) – The session this contact is part of

  • legacy_id (slidge.util.types.LegacyUserIdType) – The contact’s legacy ID

  • jid_username (str) – User part of this contact’s ‘puppet’ JID. NB: case-insensitive, and some special characters are not allowed

REACTIONS_SINGLE_EMOJI = False[source]#
RETRACTION = True[source]#
CORRECTION = False[source]#
async available_emojis(legacy_msg_id=None)[source]#

Override this to restrict the subset of reactions this recipient can handle.

Returns:

A set of emojis or None if any emoji is allowed

async update_info()[source]#

Fetch information about this contact from the legacy network

This is awaited on Contact instantiation, and should be overridden to update the nickname, avatar, vcard [..] of this contact, by making “legacy API calls”.

class slidge.plugins.dummy.Gateway[source]#

Bases: slidge.BaseGateway

Must be subclassed by a plugin to set up various aspects of the XMPP component behaviour, such as its display name or its registration process.

On slidge launch, a singleton is instantiated, and it will be made available to public classes such LegacyContact or BaseSession as the .xmpp attribute. Since it inherits from slixmpp.componentxmpp.ComponentXMPP, this gives you a hand on low-level XMPP interactions via slixmpp plugins, e.g.:

self.send_presence(
    pfrom="somebody@component.example.com",
    pto="someonwelse@anotherexample.com",
)

However, you should not need to do so often since the classes of the plugin API provides higher level abstractions around most commonly needed use-cases, such as sending messages, or displaying a custom status.

COMPONENT_NAME = 'The great legacy network (slidge)'[source]#
COMPONENT_AVATAR[source]#
COMPONENT_TYPE = 'aim'[source]#
GROUPS = True[source]#
REGISTRATION_INSTRUCTIONS = Multiline-String[source]#
Show Value
"""Only username 'n' is accepted and only 'baba' and 'bibi' contacts exist.
You can use any password you want."""
REGISTRATION_TYPE[source]#
MARK_ALL_MESSAGES = True[source]#
async validate(user_jid, registration_form)[source]#

Validate a registration form from a user.

Since XEP-0077 is pretty limited in terms of validation, it is OK to validate anything that looks good here and continue the legacy auth process via direct messages to the user (using BaseGateway.input() for instance).

Parameters:
async validate_two_factor_code(user, code)[source]#

Called when the user enters their 2FA code.

Should raise the appropriate XMPPError if the login fails

Parameters:
  • user – The gateway user whose registration is pending Use their .bare_jid and/or``.registration_form`` attributes to get what you need

  • code – The code they entered, either via “chatbot” message or adhoc command

async get_qr_text(user)[source]#

Plugins should call this to complete registration with QR codes

Parameters:

user (slidge.GatewayUser) – The not-yet-fully-registered GatewayUser. Use its .bare_jid and/or``.registration_form`` attributes to get what you need

Return type:

str

async later_confirm_qr(user)[source]#
Parameters:

user (slidge.GatewayUser) –

class slidge.plugins.dummy.Roster(session)[source]#

Bases: slidge.LegacyRoster

Virtual roster of a gateway user, that allows to represent all of their contacts as singleton instances (if used properly and not too bugged).

Every BaseSession instance will have its own LegacyRoster instance accessible via the BaseSession.contacts attribute.

Typically, you will mostly use the LegacyRoster.by_legacy_id() function to retrieve a contact instance.

You might need to override LegacyRoster.legacy_id_to_jid_username() and/or LegacyRoster.jid_username_to_legacy_id() to incorporate some custom logic if you need some characters when translation JID user parts and legacy IDs.

Parameters:

session (slidge.core.session.BaseSession) –

async static jid_username_to_legacy_id(jid_username)[source]#

Convert a JID user part to a legacy ID.

Should be overridden in case legacy IDs are not strings, or more generally for any case where the username part of a JID (unescaped with to the mapping defined by XEP-0106) is not enough to identify a contact on the legacy network.

Default implementation is an identity operation

Parameters:

jid_username (str) – User part of a JID, ie “user” in “user@example.com

Returns:

An identifier for the user on the legacy network.

async fill()[source]#

Populate slidge’s “virtual roster”.

Override this and in it, await self.by_legacy_id(contact_id) for the every legacy contacts of the user for which you’d like to set an avatar, nickname, vcard…

Await Contact.add_to_roster() in here to add the contact to the user’s XMPP roster.

class slidge.plugins.dummy.Session(user)[source]#

Bases: slidge.BaseSession

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.

async add_groups()[source]#
async muc_later(muc, text, trigger_msg_id)[source]#
Parameters:
  • muc (MUC) –

  • text (str) –

  • trigger_msg_id (int) –

async backfill()[source]#
async paused(c, thread=None)[source]#

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

Parameters:

c (slidge.LegacyContact) –

async correct(c, text, legacy_msg_id, thread=None)[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:
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’”

async logout()[source]#

Logout the gateway user from the legacy network.

Called on user unregistration and gateway shutdown.

async send_text(chat, text, *, reply_to_msg_id=None, reply_to_fallback_text=None, reply_to=None, thread=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 (Union[slidge.LegacyContact, MUC]) – RecipientType of the message. LegacyContact instance for 1:1 chat, MUC instance for groups.

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

  • reply_to_fallback_text – Content of the quoted text. Not necessarily set by XMPP clients

  • reply_to – 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

Returns:

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

async crash()[source]#
async send_file(chat, url, *_, **__)[source]#

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:

int

async later(c, trigger_msg_id, body)[source]#
Parameters:
async later_carbon_delete(c, trigger_msg_id)[source]#
Parameters:
async active(c, thread=None)[source]#

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

Parameters:

c (slidge.LegacyContact) – RecipientType of the active chat state

async inactive(c, thread=None)[source]#

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

Parameters:

c (slidge.LegacyContact) –

async composing(c, thread=None)[source]#

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

Parameters:

c (slidge.LegacyContact) –

async displayed(c, legacy_msg_id, thread=None)[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:
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:

async react(c, legacy_msg_id, emojis, thread=None)[source]#

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

Parameters:
  • legacy_msg_id – ID of the message the user reacts to

  • emojis – 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 – Contact or MUC the reaction refers to

async retract(c, legacy_msg_id, thread=None)[source]#

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

Parameters:
  • legacy_msg_id – Legacy ID of the retracted message

  • c – The contact this retraction refers to

slidge.plugins.dummy.BUDDIES = ['baba', 'bibi'][source]#
slidge.plugins.dummy.AVATARS = ['https://wallpapercave.com/wp/PSksftM.jpg'][source]#
slidge.plugins.dummy.log[source]#