slidge.command
==============

.. py:module:: slidge.command

.. autoapi-nested-parse::

   This module implements an unified API to define :term:`adhoc <Ad-hoc Command>`
   or :term:`chatbot <Chatbot Command>` commands. Just subclass a :class:`Command`,
   and make sures it is imported in your legacy module's ``__init__.py``.



Submodules
----------

.. toctree::
   :maxdepth: 1

   /api/auto/slidge/command/register/index


Classes
-------

.. autoapisummary::

   slidge.command.Command
   slidge.command.CommandAccess
   slidge.command.Confirmation
   slidge.command.Form
   slidge.command.FormField
   slidge.command.SearchResult
   slidge.command.TableResult


Package Contents
----------------

.. py:class:: Command(xmpp)



   Abstract base class to implement gateway commands (chatbot and ad-hoc)


   .. py:attribute:: NAME
      :type:  str
      :value: NotImplemented


      Friendly name of the command, eg: "do something with stuff"



   .. py:attribute:: HELP
      :type:  str
      :value: NotImplemented


      Long description of what the command does



   .. py:attribute:: NODE
      :type:  str
      :value: NotImplemented


      Name of the node used for ad-hoc commands



   .. py:attribute:: CHAT_COMMAND
      :type:  str
      :value: NotImplemented


      Text to send to the gateway to trigger the command via a message



   .. py:attribute:: ACCESS
      :type:  CommandAccess
      :value: NotImplemented


      Who can use this command



   .. py:attribute:: CATEGORY
      :type:  Optional[Union[str, slidge.command.categories.CommandCategory]]
      :value: None


      If used, the command will be under this top-level category.
      Use the same string for several commands to group them.
      This hierarchy only used for the adhoc interface, not the chat command
      interface.



   .. py:method:: run(session, ifrom, *args)
      :async:


      Entry point of the command

      :param session: If triggered by a registered user, its slidge Session
      :param ifrom: JID of the command-triggering entity
      :param args: When triggered via chatbot type message, additional words
          after the CHAT_COMMAND string was passed

      :return: Either a TableResult, a Form, a Confirmation, a text, or None



   .. py:method:: raise_if_not_authorized(jid, fetch_session = True, session = None)

      Raise an appropriate error is jid is not authorized to use the command

      :param jid: jid of the entity trying to access the command
      :param fetch_session:
      :param session:

      :return:session of JID if it exists



.. py:class:: CommandAccess



   Defines who can access a given Command


.. py:class:: Confirmation

   A confirmation 'dialog'


   .. py:attribute:: prompt
      :type:  str

      The text presented to the command triggering user



   .. py:attribute:: handler
      :type:  ConfirmationHandlerType

      An async function that should return a ResponseType



   .. py:attribute:: success
      :type:  Optional[str]
      :value: None


      Text in case of success, used if handler does not return anything



   .. py:attribute:: handler_args
      :type:  Iterable[Any]
      :value: []


      arguments passed to the handler



   .. py:attribute:: handler_kwargs
      :type:  dict[str, Any]

      keyword arguments passed to the handler



   .. py:method:: get_form()

      Get the slixmpp form

      :return: some xml



.. py:class:: Form

   A form, to request user input


   .. py:method:: get_values(slix_form)

      Parse form submission

      :param slix_form: the xml received as the submission of a form
      :return: A dict where keys=field.var and values are either strings
          or JIDs (if field.type=jid-single)



   .. py:method:: get_xml()

      Get the slixmpp "form"

      :return: some XML



.. py:class:: FormField

   Represents a field of the form that a user will see when registering to the gateway
   via their XMPP client.


   .. py:attribute:: var
      :type:  str
      :value: ''


      Internal name of the field, will be used to retrieve via :py:attr:`slidge.GatewayUser.registration_form`



   .. py:attribute:: label
      :type:  Optional[str]
      :value: None


      Description of the field that the user will see



   .. py:attribute:: required
      :type:  bool
      :value: False


      Whether this field is mandatory or not



   .. py:attribute:: private
      :type:  bool
      :value: False


      For sensitive info that should not be displayed on screen while the user types.
      Forces field_type to "text-private"



   .. py:attribute:: type
      :type:  slidge.util.types.FieldType
      :value: 'text-single'


      Type of the field, see `XEP-0004 <https://xmpp.org/extensions/xep-0004.html#protocol-fieldtypes>`_



   .. py:attribute:: value
      :type:  str
      :value: ''


      Pre-filled value. Will be automatically pre-filled if a registered user modifies their subscription



   .. py:attribute:: image_url
      :type:  Optional[str]
      :value: None


      An image associated to this field, eg, a QR code



   .. py:method:: validate(value)

      Raise appropriate XMPPError if a given value is valid for this field

      :param value: The value to test
      :return: The same value OR a JID if ``self.type=jid-single``



   .. py:method:: get_xml()

      Get the field in slixmpp format

      :return: some XML



.. py:class:: SearchResult



   Results of the search command (search for contacts via Jabber Search)

   Return type of :meth:`BaseSession.search`.


   .. py:attribute:: description
      :type:  str
      :value: 'Contact search results'


      A description of the content of the table.



   .. py:attribute:: fields
      :type:  Sequence[FormField]

      The 'columns names' of the table.



   .. py:attribute:: items
      :type:  Sequence[dict[str, Union[str, slixmpp.JID]]]

      The rows of the table. Each row is a dict where keys are the fields ``var``
      attribute.



   .. py:method:: get_xml()

      Get a slixmpp "form" (with <reported> header)to represent the data

      :return: some XML



.. py:class:: TableResult

   Structured data as the result of a command


   .. py:attribute:: fields
      :type:  Sequence[FormField]

      The 'columns names' of the table.



   .. py:attribute:: items
      :type:  Sequence[dict[str, Union[str, slixmpp.JID]]]

      The rows of the table. Each row is a dict where keys are the fields ``var``
      attribute.



   .. py:attribute:: description
      :type:  str

      A description of the content of the table.



   .. py:method:: get_xml()

      Get a slixmpp "form" (with <reported> header)to represent the data

      :return: some XML



