summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2021-02-27 13:16:18 +0100
committermathieui <mathieui@mathieui.net>2021-02-27 13:16:18 +0100
commit059cb290d8ae567ef189d83c45a1e38b1f3ab9dc (patch)
treeb8a85ab9a91bf7663701a077607a67a07fcc485a /docs
parent5f9ab45a5e161c3035a844184736b3180dae6047 (diff)
parent3cdec464a550b775d8c251f37b863a6e2212c5d5 (diff)
downloadslixmpp-059cb290d8ae567ef189d83c45a1e38b1f3ab9dc.tar.gz
slixmpp-059cb290d8ae567ef189d83c45a1e38b1f3ab9dc.tar.bz2
slixmpp-059cb290d8ae567ef189d83c45a1e38b1f3ab9dc.tar.xz
slixmpp-059cb290d8ae567ef189d83c45a1e38b1f3ab9dc.zip
Merge branch 'async-interal-api-break-everything' into 'master'
Make the internal "api" async See merge request poezio/slixmpp!128
Diffstat (limited to 'docs')
-rw-r--r--docs/api/api.rst88
-rw-r--r--docs/api/index.rst1
-rw-r--r--docs/api/plugins/xep_0012.rst38
-rw-r--r--docs/api/plugins/xep_0027.rst44
-rw-r--r--docs/api/plugins/xep_0047.rst71
-rw-r--r--docs/api/plugins/xep_0054.rst34
-rw-r--r--docs/api/plugins/xep_0065.rst42
-rw-r--r--docs/api/plugins/xep_0077.rst47
-rw-r--r--docs/api/plugins/xep_0115.rst48
-rw-r--r--docs/api/plugins/xep_0128.rst35
-rw-r--r--docs/api/plugins/xep_0153.rst37
-rw-r--r--docs/api/plugins/xep_0231.rst35
-rw-r--r--docs/api/plugins/xep_0319.rst27
-rw-r--r--docs/howto/index.rst1
-rw-r--r--docs/howto/internal_api.rst94
15 files changed, 640 insertions, 2 deletions
diff --git a/docs/api/api.rst b/docs/api/api.rst
new file mode 100644
index 00000000..55642ef6
--- /dev/null
+++ b/docs/api/api.rst
@@ -0,0 +1,88 @@
+.. _internal-api:
+
+Internal "API"
+==============
+
+Slixmpp has a generic API registry that can be used by its plugins to allow
+access control, redefinition of behaviour, without having to inherit from the
+plugin or do more dark magic.
+
+The idea is that each api call can be replaced, most of them use a form
+of in-memory storage that can be, for example, replaced with database
+or file-based storaged.
+
+
+Each plugin is assigned an API proxy bound to itself, but only a few make use
+of it.
+
+See also :ref:`api-simple-tuto`.
+
+Description of a generic API call
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. code-block:: python
+
+ def get_toto(jid, node, ifrom, args):
+ return 'toto'
+
+ self.xmpp.plugin['xep_XXXX'].api.register(handler, 'get_toto')
+
+Each API call will receive 4 parameters (which can be ``None`` if data
+is not relevant to the operation), which are ``jid`` (``Optional[JID]``),
+``node`` (``Optional[str]``), ``ifrom`` (``Optional[JID]``), and ``args``
+(``Any``).
+
+- ``jid``, if relevant, represents the JID targeted by that operation
+- ``node``, if relevant is an arbitrary string, but was thought for, e.g.,
+ a pubsub or disco node.
+- ``ifrom``, if relevant, is the JID the event is coming from.
+- ``args`` is the event-specific data passed on by the plugin, often a dict
+ of arguments (can be None as well).
+
+.. note::
+ Since 1.8.0, API calls can be coroutines.
+
+
+Handler hierarchy
+~~~~~~~~~~~~~~~~~
+
+The ``self.api.register()`` signature is as follows:
+
+.. code-block:: python
+
+ def register(handler, op, jid=None, node=None, dedfault=False):
+ pass
+
+As you can see, :meth:`~.APIRegistry.register` takes an additional ctype
+parameter, but the :class:`~.APIWrapper` takes care of that for us (in most
+cases, it is the name of the XEP plugin, such as ``'xep_0XXX'``).
+
+When you register a handler, you register it for an ``op``, for **operation**.
+For example, ``get_vcard``.
+
+``handler`` and ``op`` are the only two required parameters (and in many cases,
+all you will ever need). You can, however, go further and register handlers
+for specific values of the ``jid`` and ``node`` parameters of the calls.
+
+The priority of the execution of handlers is as follows:
+
+- Check if a handler for both values of ``node`` and ``jid`` has been defined
+- If not found, check if a handler for this value of ``jid`` has been defined
+- If not found, check if a handler for this value of ``node`` has been defined
+- If still not found, get the global handler (no parameter registered)
+
+
+Raw documentation
+~~~~~~~~~~~~~~~~~
+
+This documentation is provided for reference, but :meth:`~.APIRegistry.register`
+should be all you need.
+
+
+.. module:: slixmpp.api
+
+.. autoclass:: APIRegistry
+ :members:
+
+.. autoclass:: APIWrapper
+
diff --git a/docs/api/index.rst b/docs/api/index.rst
index aefa0f56..e9aa6264 100644
--- a/docs/api/index.rst
+++ b/docs/api/index.rst
@@ -14,3 +14,4 @@ API Reference
xmlstream/matcher
xmlstream/xmlstream
xmlstream/tostring
+ api
diff --git a/docs/api/plugins/xep_0012.rst b/docs/api/plugins/xep_0012.rst
index 9a12eac3..527e28d2 100644
--- a/docs/api/plugins/xep_0012.rst
+++ b/docs/api/plugins/xep_0012.rst
@@ -9,6 +9,44 @@ XEP-0012: Last Activity
:exclude-members: session_bind, plugin_init, plugin_end
+.. _api-0012:
+
+Internal API methods
+--------------------
+
+This plugin uses an in-memory storage by default to keep track of the
+received and sent last activities.
+
+.. glossary::
+
+ get_last_activity
+ - **jid**: :class:`~.JID` of whom to retrieve the last activity
+ - **node**: unused
+ - **ifrom**: who the request is from (None = local)
+ - **args**: ``None`` or an :class:`~.Iq` that is requesting the
+ - **returns**
+ information.
+
+ Get the last activity of a JID from the storage.
+
+ set_last_activity
+ - **jid**: :class:`~.JID` of whom to set the last activity
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: A dict containing ``'seconds'`` and ``'status'``
+ ``{'seconds': Optional[int], 'status': Optional[str]}``
+
+ Set the last activity of a JID in the storage.
+
+ del_last_activity
+ - **jid**: :class:`~.JID` to delete from the storage
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: unused
+
+ Remove the last activity of a JID from the storage.
+
+
Stanza elements
---------------
diff --git a/docs/api/plugins/xep_0027.rst b/docs/api/plugins/xep_0027.rst
index 418baada..3a7fb561 100644
--- a/docs/api/plugins/xep_0027.rst
+++ b/docs/api/plugins/xep_0027.rst
@@ -9,6 +9,50 @@ XEP-0027: Current Jabber OpenPGP Usage
:exclude-members: session_bind, plugin_init, plugin_end
+.. _api-0027:
+
+Internal API methods
+--------------------
+
+The default API here is managing a JID→Keyid dict in-memory.
+
+.. glossary::
+
+ get_keyid
+ - **jid**: :class:`~.JID` to get.
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: unused
+ - **returns**: ``Optional[str]``, the keyid or None
+
+ Get the KeyiD for a JID, None if it is not found.
+
+ set_keyid
+ - **jid**: :class:`~.JID` to set the id for.
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: ``str``, keyid to set
+
+ Set the KeyiD for a JID.
+
+ del_keyid
+ - **jid**: :class:`~.JID` to delete from the mapping.
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: unused
+
+ Delete the KeyiD for a JID.
+
+ get_keyids
+ - **jid**: unused
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: unused
+ - **returns**: ``Dict[JID, str]`` the full internal mapping
+
+ Get all currently stored KeyIDs.
+
+
Stanza elements
---------------
diff --git a/docs/api/plugins/xep_0047.rst b/docs/api/plugins/xep_0047.rst
index c8aea741..0e81ae10 100644
--- a/docs/api/plugins/xep_0047.rst
+++ b/docs/api/plugins/xep_0047.rst
@@ -8,11 +8,78 @@ XEP-0047: In-band Bytestreams
:members:
:exclude-members: session_bind, plugin_init, plugin_end
-.. module:: slixmpp.plugins.xep_0047
-
.. autoclass:: IBBytestream
:members:
+.. _api-0047:
+
+Internal API methods
+--------------------
+
+The API here is used to manage streams and authorize. The default handlers
+work with the config parameters.
+
+.. glossary::
+
+ authorized_sid (0047 version)
+ - **jid**: :class:`~.JID` receiving the stream initiation.
+ - **node**: stream id
+ - **ifrom**: who the stream is from.
+ - **args**: :class:`~.Iq` of the stream request.
+ - **returns**: ``True`` if the stream should be accepted,
+ ``False`` otherwise.
+
+ Check if the stream should be accepted. Uses
+ the information setup by :term:`preauthorize_sid (0047 version)`
+ by default.
+
+ authorized (0047 version)
+ - **jid**: :class:`~.JID` receiving the stream initiation.
+ - **node**: stream id
+ - **ifrom**: who the stream is from.
+ - **args**: :class:`~.Iq` of the stream request.
+ - **returns**: ``True`` if the stream should be accepted,
+ ``False`` otherwise.
+
+ A fallback handler (run after :term:`authorized_sid (0047 version)`)
+ to check if a stream should be accepted. Uses the ``auto_accept``
+ parameter by default.
+
+ preauthorize_sid (0047 version)
+ - **jid**: :class:`~.JID` receiving the stream initiation.
+ - **node**: stream id
+ - **ifrom**: who the stream will be from.
+ - **args**: Unused.
+
+ Register a stream id to be accepted automatically (called from
+ other plugins such as XEP-0095).
+
+ get_stream
+ - **jid**: :class:`~.JID` of local receiver.
+ - **node**: stream id
+ - **ifrom**: who the stream is from.
+ - **args**: unused
+ - **returns**: :class:`~.IBBytestream`
+
+ Return a currently opened stream between two JIDs.
+
+ set_stream
+ - **jid**: :class:`~.JID` of local receiver.
+ - **node**: stream id
+ - **ifrom**: who the stream is from.
+ - **args**: unused
+
+ Register an opened stream between two JIDs.
+
+ del_stream
+ - **jid**: :class:`~.JID` of local receiver.
+ - **node**: stream id
+ - **ifrom**: who the stream is from.
+ - **args**: unused
+
+ Delete a stream between two JIDs.
+
+
Stanza elements
---------------
diff --git a/docs/api/plugins/xep_0054.rst b/docs/api/plugins/xep_0054.rst
index db8e29ef..ace5f10a 100644
--- a/docs/api/plugins/xep_0054.rst
+++ b/docs/api/plugins/xep_0054.rst
@@ -8,6 +8,40 @@ XEP-0054: vcard-temp
:members:
:exclude-members: session_bind, plugin_init, plugin_end
+.. _api-0054:
+
+Internal API methods
+--------------------
+
+This plugin maintains by default an in-memory cache of the received
+VCards.
+
+.. glossary::
+
+ set_vcard
+ - **jid**: :class:`~.JID` of whom to set the vcard
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: :class:`~.VCardTemp` object to store for this JID.
+
+ Set a VCard for a JID.
+
+ get_vcard
+ - **jid**: :class:`~.JID` of whom to set the vcard
+ - **node**: unused
+ - **ifrom**: :class:`~.JID` the request is coming from
+ - **args**: unused
+ - **returns**: :class:`~.VCardTemp` object for this JID or None.
+
+ Get a stored VCard for a JID.
+
+ del_vcard
+ - **jid**: :class:`~.JID` of whom to set the vcard
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: unused
+
+ Delete a stored VCard for a JID.
Stanza elements
---------------
diff --git a/docs/api/plugins/xep_0065.rst b/docs/api/plugins/xep_0065.rst
index d6aec058..954af2b6 100644
--- a/docs/api/plugins/xep_0065.rst
+++ b/docs/api/plugins/xep_0065.rst
@@ -8,6 +8,48 @@ XEP-0065: SOCKS5 Bytestreams
:members:
:exclude-members: session_bind, plugin_init, plugin_end
+.. _api-0065:
+
+Internal API methods
+--------------------
+
+The internal API is used here to authorize or pre-authorize streams.
+
+.. glossary::
+
+ authorized_sid (0065 version)
+ - **jid**: :class:`~.JID` receiving the stream initiation.
+ - **node**: stream id
+ - **ifrom**: who the stream is from.
+ - **args**: :class:`~.Iq` of the stream request.
+ - **returns**: ``True`` if the stream should be accepted,
+ ``False`` otherwise.
+
+ Check if the stream should be accepted. Uses
+ the information setup by :term:`preauthorize_sid (0065 version)`
+ by default.
+
+ authorized (0065 version)
+ - **jid**: :class:`~.JID` receiving the stream initiation.
+ - **node**: stream id
+ - **ifrom**: who the stream is from.
+ - **args**: :class:`~.Iq` of the stream request.
+ - **returns**: ``True`` if the stream should be accepted,
+ ``False`` otherwise.
+
+ A fallback handler (run after :term:`authorized_sid (0065 version)`)
+ to check if a stream should be accepted. Uses the ``auto_accept``
+ parameter by default.
+
+ preauthorize_sid (0065 version)
+ - **jid**: :class:`~.JID` receiving the stream initiation.
+ - **node**: stream id
+ - **ifrom**: who the stream will be from.
+ - **args**: Unused.
+
+ Register a stream id to be accepted automatically (called from
+ other plugins such as XEP-0095).
+
Stanza elements
---------------
diff --git a/docs/api/plugins/xep_0077.rst b/docs/api/plugins/xep_0077.rst
index 725c16b0..33c433e0 100644
--- a/docs/api/plugins/xep_0077.rst
+++ b/docs/api/plugins/xep_0077.rst
@@ -8,6 +8,53 @@ XEP-0077: In-Band Registration
:members:
:exclude-members: session_bind, plugin_init, plugin_end
+Internal APi methods
+--------------------
+
+The API here is made to allow components to manage registered users.
+The default handlers make use of the plugin options and store users
+in memory.
+
+.. glossary::
+
+ user_get
+ - **jid**: unused
+ - **node**: unused
+ - **ifrom**: who the request is coming from
+ - **args**: :class:`~.Iq` registration request.
+ - **returns**: ``dict`` containing user data or None.
+
+ Get user data for a user.
+
+ user_validate
+ - **jid**: unused
+ - **node**: unused
+ - **ifrom**: who the request is coming from
+ - **args**: :class:`~.Iq` registration request, 'register' payload.
+ - **raises**: ValueError if some fields are invalid
+
+ Validate form fields and save user data.
+
+ user_remove
+ - **jid**: unused
+ - **node**: unused
+ - **ifrom**: who the request is coming from
+ - **args**: :class:`~.Iq` registration removal request.
+ - **raises**: KeyError if the user is not found.
+
+ Remove a user from the store.
+
+ make_registration_form
+ - **jid**: unused
+ - **node**: unused
+ - **ifrom**: who the request is coming from
+ - **args**: :class:`~.Iq` registration request.
+ - **raises**: KeyError if the user is not found.
+
+ Return an :class:`~.Iq` reply for the request, with a form and
+ options set. By default, use ``form_fields`` and ``form_instructions``
+ plugin config options.
+
Stanza elements
---------------
diff --git a/docs/api/plugins/xep_0115.rst b/docs/api/plugins/xep_0115.rst
index 73d0e77b..e3507fb8 100644
--- a/docs/api/plugins/xep_0115.rst
+++ b/docs/api/plugins/xep_0115.rst
@@ -8,6 +8,54 @@ XEP-0115: Entity Capabilities
:members:
:exclude-members: session_bind, plugin_init, plugin_end
+.. _api-0115:
+
+Internal API methods
+--------------------
+
+This internal API extends the Disco internal API, and also manages an
+in-memory cache of verstring→disco info, and fulljid→verstring.
+
+.. glossary::
+
+ cache_caps
+ - **jid**: unused
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: a ``dict`` containing the verstring and
+ :class:`~.DiscoInfo` payload (
+ ``{'verstring': Optional[str], 'info': Optional[DiscoInfo]}``)
+
+ Cache a verification string with its payload.
+
+ get_caps
+ - **jid**: JID to retrieve the verstring for (unused with the default
+ handler)
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: a ``dict`` containing the verstring
+ ``{'verstring': str}``
+ - **returns**: The :class:`~.DiscoInfo` payload for that verstring.
+
+ Get a disco payload from a verstring.
+
+ assign_verstring
+ - **jid**: :class:`~.JID` (full) to assign the verstring to
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: a ``dict`` containing the verstring
+ ``{'verstring': str}``
+
+ Cache JID→verstring information.
+
+ get_verstring
+ - **jid**: :class:`~.JID` to use for fetching the verstring
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: unused
+ - **returns**: ``str``, the verstring
+
+ Retrieve a verstring for a JID.
Stanza elements
---------------
diff --git a/docs/api/plugins/xep_0128.rst b/docs/api/plugins/xep_0128.rst
index 1e080928..7ecb436c 100644
--- a/docs/api/plugins/xep_0128.rst
+++ b/docs/api/plugins/xep_0128.rst
@@ -7,3 +7,38 @@ XEP-0128: Service Discovery Extensions
.. autoclass:: XEP_0128
:members:
:exclude-members: session_bind, plugin_init, plugin_end
+
+.. _api-0128:
+
+Internal API methods
+--------------------
+
+
+
+.. glossary::
+
+ add_extended_info
+ - **jid**: JID to set the extended info for
+ - **node**: note to set the info at
+ - **ifrom**: unused
+ - **args**: A :class:`~.Form` or list of forms to add to the disco
+ extended info for this JID/node.
+
+ Add extended info for a JID/node.
+
+ set_extended_info
+ - **jid**: JID to set the extended info for
+ - **node**: note to set the info at
+ - **ifrom**: unused
+ - **args**: A :class:`~.Form` or list of forms to set as the disco
+ extended info for this JID/node.
+
+ Set extended info for a JID/node.
+
+ del_extended_info
+ - **jid**: JID to delete the extended info from
+ - **node**: note to delete the info from
+ - **ifrom**: unused
+ - **args**: unused
+
+ Delete extended info for a JID/node.
diff --git a/docs/api/plugins/xep_0153.rst b/docs/api/plugins/xep_0153.rst
index 00e22098..d4ce342f 100644
--- a/docs/api/plugins/xep_0153.rst
+++ b/docs/api/plugins/xep_0153.rst
@@ -8,6 +8,43 @@ XEP-0153: vCard-Based Avatars
:members:
:exclude-members: session_bind, plugin_init, plugin_end
+.. _api-0153:
+
+Internal API methods
+--------------------
+
+The internal API is used here to maintain an in-memory JID→avatar hash
+cache.
+
+.. glossary::
+
+ set_hash
+ - **jid**: :class:`~.JID` of whom to retrieve the last activity
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: ``str``, avatar hash
+
+ Set the avatar hash for a JID.
+
+ reset_hash
+ - **jid**: :class:`~.JID` of whom to retrieve the last activity
+ - **node**: unused
+ - **ifrom**: :class:`~.JID` of the entity requesting the reset.
+ - **args**: unused
+ - **returns**
+ information.
+
+ Reset the avatar hash for a JID. This downloads the vcard and computes
+ the hash.
+
+ get_hash
+ - **jid**: :class:`~.JID` of whom to retrieve the last activity
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: unused
+ - **returns**: ``Optional[str]``, the avatar hash
+
+ Get the avatar hash for a JID.
Stanza elements
---------------
diff --git a/docs/api/plugins/xep_0231.rst b/docs/api/plugins/xep_0231.rst
index 29f403ca..c8a863cb 100644
--- a/docs/api/plugins/xep_0231.rst
+++ b/docs/api/plugins/xep_0231.rst
@@ -8,6 +8,41 @@ XEP-0231: Bits of Binary
:members:
:exclude-members: session_bind, plugin_init, plugin_end
+.. _api-0231:
+
+Internal API methods
+--------------------
+
+The default API handlers for this plugin manage an in-memory cache of
+bits of binary by content-id.
+
+.. glossary::
+
+ set_bob
+ - **jid**: :class:`~.JID` sending the bob
+ - **node**: unused
+ - **ifrom**: :class:`~JID` receiving the bob
+ - **args**: :class:`~.BitsOfBinary` element.
+
+ Set a BoB in the cache.
+
+ get_bob
+ - **jid**: :class:`~.JID` receiving the bob
+ - **node**: unused
+ - **ifrom**: :class:`~JID` sending the bob
+ - **args**: ``str`` content-id of the bob
+ - **returns**: :class:`~.BitsOfBinary` element.
+
+ Get a BoB from the cache.
+
+ del_bob
+ - **jid**: unused
+ - **node**: unused
+ - **ifrom**: :class:`~JID` sending the bob
+ - **args**: ``str`` content-id of the bob
+
+ Delete a BoB from the cache.
+
Stanza elements
---------------
diff --git a/docs/api/plugins/xep_0319.rst b/docs/api/plugins/xep_0319.rst
index a3ab9d28..7be01cb2 100644
--- a/docs/api/plugins/xep_0319.rst
+++ b/docs/api/plugins/xep_0319.rst
@@ -9,6 +9,33 @@ XEP-0319: Last User Interaction in Presence
:exclude-members: session_bind, plugin_init, plugin_end
+.. _api-0319:
+
+Internal API methods
+--------------------
+
+The default API manages an in-memory cache of idle periods.
+
+.. glossary::
+
+ set_idle
+ - **jid**: :class:`~.JID` who has been idling
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: :class:`datetime`, timestamp of the idle start
+
+ Set the idle start for a JID.
+
+ get_idle
+ - **jid**: :class:`~.JID` to get the idle time of
+ - **node**: unused
+ - **ifrom**: unused
+ - **args**: : unused
+ - **returns**: :class:`datetime`
+
+ Get the idle start timestamp for a JID.
+
+
Stanza elements
---------------
diff --git a/docs/howto/index.rst b/docs/howto/index.rst
index b05dc499..e4dee4d7 100644
--- a/docs/howto/index.rst
+++ b/docs/howto/index.rst
@@ -6,6 +6,7 @@ Tutorials, FAQs, and How To Guides
stanzas
create_plugin
+ internal_api
features
sasl
handlersmatchers
diff --git a/docs/howto/internal_api.rst b/docs/howto/internal_api.rst
new file mode 100644
index 00000000..003225c4
--- /dev/null
+++ b/docs/howto/internal_api.rst
@@ -0,0 +1,94 @@
+.. _api-simple-tuto:
+
+Flexible internal API usage
+===========================
+
+The :ref:`internal-api` in slixmpp is used to override behavior or simply
+to override the default, in-memory storage backend with something persistent.
+
+
+We will use the XEP-0231 (Bits of Binary) plugin as an example here to show
+very basic functionality. Its API reference is in the plugin documentation:
+:ref:`api-0231`.
+
+Let us assume we want to keep each bit of binary in a file named with its
+content-id, with all metadata.
+
+First, we have to load the plugin:
+
+.. code-block:: python
+
+ from slixmpp import ClientXMPP
+ xmpp = ClientXMPP(...)
+ xmpp.register_plugin('xep_0231')
+
+This enables the default, in-memory storage.
+
+We have 3 methods to override to provide similar functionality and keep things
+coherent.
+
+Here is a class implementing very basic file storage for BoB:
+
+.. code-block:: python
+
+ from slixmpp.plugins.xep_0231 import BitsOfBinary
+ from os import makedirs, remove
+ from os.path import join, exists
+ import base64
+ import json
+
+ class BobLoader:
+ def __init__(self, directory):
+ makedirs(directory, exist_ok=True)
+ self.dir = directory
+
+ def set_bob(self, jid=None, node=None, ifrom=None, args=None):
+ payload = {
+ 'data': base64.b64encode(args['data']).decode(),
+ 'type': args['type'],
+ 'cid': args['cid'],
+ 'max_age': args['max_age']
+ }
+ with open(join(self.dir, args['cid']), 'w') as fd:
+ fd.write(json.dumps(payload))
+
+ def get_bob(self, jid=None, node=None, ifrom=None, args=None):
+ with open(join(self.dir, args), 'r') as fd:
+ payload = json.loads(fd.read())
+ bob = BitsOfBinary()
+ bob['data'] = base64.b64decode(payload['data'])
+ bob['type'] = payload['type']
+ bob['max_age'] = payload['max_age']
+ bob['cid'] = payload['cid']
+ return bob
+
+ def del_bob(self, jid=None, node=None, ifrom=None, args=None):
+ path = join(self.dir, args)
+ if exists(path):
+ remove(path)
+
+Now we need to replace the default handler with ours:
+
+.. code-block:: python
+
+
+ bobhandler = BobLoader('/tmp/bobcache')
+ xmpp.plugin['xep_0231'].api.register(bobhandler.set_bob, 'set_bob')
+ xmpp.plugin['xep_0231'].api.register(bobhandler.get_bob, 'get_bob')
+ xmpp.plugin['xep_0231'].api.register(bobhandler.del_bob, 'del_bob')
+
+
+And that’s it, the BoB storage is now made of JSON files living in a
+directory (``/tmp/bobcache`` here).
+
+
+To check that everything works, you can do the following:
+
+.. code-block:: python
+
+ cid = await xmpp.plugin['xep_0231'].set_bob(b'coucou', 'text/plain')
+ # A new bob file should appear
+ content = await xmpp.plugin['xep_0231'].get_bob(cid=cid)
+ assert content['bob']['data'] == b'coucou'
+
+A file should have been created in that directory.