Search code examples
erlangelixirejabberd

How to register custom implementation of Ejabberd pubsub node?


I'd like to use custom implementation of pubsub node (gen_pubsub_node). I created dummy implementation in elixir (delegates everything to node_flat_sql):

defmodule CustomNodePlugin do
  @behaviour :gen_pubsub_node

  defdelegate init(host, server_host, opts), to: :node_flat_sql
  defdelegate terminate(host, server_host), to: :node_flat_sql
  defdelegate options(), to: :node_flat_sql
  defdelegate features(), to: :node_flat_sql
  defdelegate create_node_permission(host, server_host, node, parent_node, owner), to: :node_flat_sql
  defdelegate create_node(node_idx, owner), to: :node_flat_sql
  defdelegate delete_node(nodes), to: :node_flat_sql
  defdelegate purge_node(node_idx), to: :node_flat_sql
  defdelegate subscribe_node(
    node_idx,
    sender,
    subscriber,
    access_model,
    send_last,
    pesesnce_subscription,
    roster_group, options
  ), to: :node_flat_sql
  defdelegate unsubscribe_node(
    node_idx,
    sender,
    subscriber,
    sub_id
  ), to: :node_flat_sql
  defdelegate publish_item(
    node_idx,
    publisher,
    publish_model,
    max_items,
    item_id,
    payload,
    options
  ), to: :node_flat_sql
  defdelegate delete_item(
    node_idx,
    publisher,
    publish_model,
    item_id
  ), to: :node_flat_sql
  defdelegate remove_extra_items(
    node_idx,
    max_items,
    item_ids
  ), to: :node_flat_sql
  defdelegate get_node_affiliations(node_idx), to: :node_flat_sql
  defdelegate get_entity_affiliations(
    host,
    owner
  ), to: :node_flat_sql
  defdelegate get_affiliation(node_idx, owner), to: :node_flat_sql
  defdelegate set_affiliation(node_idx, owner, affiliation), to: :node_flat_sql

  # needs to be implemented
  defdelegate get_node_subscriptions(node_idx), to: :node_flat_sql
  defdelegate get_entity_subscriptions(host, key), to: :node_flat_sql
  defdelegate get_subscriptions(node_idx, owner), to: :node_flat_sql
  defdelegate get_pending_nodes(host, owner), to: :node_flat_sql
  defdelegate get_states(node_idx), to: :node_flat_sql
  defdelegate get_state(node_idx, key), to: :node_flat_sql
  defdelegate set_state(state), to: :node_flat_sql
  defdelegate get_items(
    node_idx,
    jid,
    access_model,
    param1,
    param2,
    param3,
    param4
  ), to: :node_flat_sql
  defdelegate get_items(
    node_idx,
    jid,
    access_model
  ), to: :node_flat_sql
  defdelegate get_last_items(node_idx, jid, rsm_set), to: :node_flat_sql
  defdelegate get_only_item(node_idx, jid), to: :node_flat_sql
  defdelegate get_item(
    node_idx,
    jid,
    access_model,
    presence_subscription,
    roster_group,
    sub_id
  ), to: :node_flat_sql
  defdelegate get_item(
    node_idx,
    item_id
  ), to: :node_flat_sql
  defdelegate set_item(), to: :node_flat_sql
  defdelegate get_item_name(host, server_host, node_idx), to: :node_flat_sql
  defdelegate node_to_path(node_idx), to: :node_flat_sql
  defdelegate path_to_node(node_idx), to: :node_flat_sql
end

I cannot figure out how to integrate it into ejabberd, I tried simply registrating it inside ejabberd.yml:

...
  mod_pubsub:
    access_createnode: pubsub_createnode
    db_type: sql
    plugins:
      - "flat"
      - "pep"
      - "CustomNodePlugin"
    force_node_config:
      ## Change from "whitelist" to "open" to enable OMEMO support
      ## See https://github.com/processone/ejabberd/issues/2425
      "eu.siacs.conversations.axolotl.*":
        access_model: open
      ## Avoid buggy clients to make their bookmarks public
      "storage:bookmarks":
        access_model: open
...

but I get following error: Failed to start ejabberd application: Invalid value of option modules->mod_pubsub->plugins->3: unexpected value: CustomNodePlugin. Did you mean flat? Possible values are: flat, pep

Tried looking into documentation (https://docs.ejabberd.im/developer/extending-ejabberd/pubsub/#pubsub-overview) and https://github.com/processone/ejabberd/blob/master/src/mod_pubsub.erl#L257, but cannot find where I can hook new implementation in. Is there a way to register new node through config file? How?


Solution

  • I managet to figure it out, bun only for pubsub. Configuration documentation states that PEP nodes can use cutom implementation through pep_mapping (https://docs.ejabberd.im/admin/configuration/#mod-pubsub search for pep_mapping), here you can force node to use your custom implementation by adding pep_mapping item, e.g.

    mod_pubsub:
      pep_mapping:
        "custom:node": custom
    

    In runtime (with default configuration) Ejabberd will look for module named node_custom. If SQL is configured to be backend, Ejabberd will look for module named node_custom_sql. Elixir modules can have atoms as names so you should define module as:

    defmodule :node_custom_sql do
    ...
    

    To sumarize:

    1. Register PEP mapping in config
    2. Define module with name "node_" + + "_sql" (for SQL backend, if configured). Module name is atom
    3. Implement gen_pubsub_node behaviour