Мост Matrix-XMPP

Updated 30 Septembre 2019

Preparing a LXC container

We suggest that you set up the bridge in a separate container you shall have prepared according to the manual.

Prosody configuration

Configure the external component, matrix.xmpp.example.org according to the documentation, as well as muc.xmpp.example.org, the conference component.

Installing matrix-bifrost

Install the nodeenv package:

emerge -a dev-python/nodeenv

Fetch the latest version of matrix-bifrost from github and copy it to /var/calculate:

git clone https://github.com/matrix-org/matrix-bifrost /var/calculate/matrix-bifrost

Create and activate virtual environment NodeJS-10.15.3 in the application directory:

nodeenv --node=10.15.3 node-10

source node-10/bin/activate

Install the application while in /var/calculate/matrix-bifrost:

npm install

npm run build

Bridge configuration

Copy config.sample.yaml to config.yaml and configure the bridge with the following parameters:

  • matrix.example.org is the Matrix server
  • xmpp.example.org is the Jabber server
  • matrix.xmpp.example.org is the external Jabber component
  • muc.xmpp.example.org is the conference Jabber component
  • xmpp_ is the prefix used to address Jabber users and conferences
/var/calculate/matrix-bifrost/config.yaml
bridge:
  # Your homeserver server_name.
  domain: "matrix.example.org"
  # A internally reachable endpoint for the CS api of the homeserver.
  homeserverUrl: "https://matrix.example.org"
  # Prefix of all users of the bridge.
  userPrefix: "xmpp_"
purple:
  # For selecting a specific backend. One of "node-purple", "xmpp.js". Defaults to "node-purple"
  backend: "xmpp.js"
  backendOpts:
    # endpoint to reach the component on. The default port is 5347
    service: "xmpp://xmpp.example.org:5347"
    # domin assigned to the component.
    domain: "matrix.xmpp.example.org"
    # password needed by the component.
    password: "secret"
portal:
  # List of regexes to match a alias that can be turned into a bridge.
  aliases:
    # This matches _purple_ followed by anything
    "^xmpp_(.+)$":
      # Use the prpl-jabber protocol.
      protocol: "xmpp.js"
      properties:
        # Set room to the first regex match
        room: "regex:1"
        # Set the server to be conf.localhost
        server: "muc.xmpp.example.org"
# Automatically register users with accounts if they join/get invited
# a room with a protocol they are not bound to.
# This is REQUIRED for xmpp.js to work.
autoRegistration:
  enabled: true
  protocolSteps:
    # For xmpp.js, please use:
    xmpp-js:
      type: "implicit"
      parameters:
        username: "<T_MXID_SANE>@matrix.xmpp.example.org"

Create a registration file for the service located on matrix-xmpp.example.org:

npm run genreg -- -u http://matrix-xmpp.example.org:9555 -f registration_xmpp.yaml

Replace the _purple_ prefix with xmpp_

/var/calculate/matrix-bifrost/registration_xmpp.yaml
id: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
hs_token: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
as_token: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
namespaces:
  users:
    - exclusive: true
      regex: '@xmpp_.*'
  aliases:
    - exclusive: true
    regex: '#xmpp_.*'
  rooms: []
url: 'http://matrix-xmpp.example.org:9555'
sender_localpart: xmpp_bot
rate_limited: true
protocols:
  - xmpp

Copy registration_xmpp.yaml to the /var/calculate/synapse directory on the Matrix server.

scp registration_xmpp.yaml root@synapse:/var/calculate/synapse/

Add the registration file to the configuration of Synapse:

homeserver.yaml
...
# A list of application service config file to use
app_service_config_files:
  - registration_xmpp.yaml
...

Restart Synapse:

/etc/init.d/synapse restart

Starting the bridge

Create an OpenRC script to manage the Matrix XMPP bridge daemon:

/etc/init.d/matrix-xmpp-bridge
#!/sbin/openrc-run
# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

name="Matrix-XMPP daemon"
description="Matrix XMPP bridge daemon"
command=/var/calculate/matrix-bifrost/node-10/bin/node
command_args="build/src/Program.js -c config.yaml -p 9555 -f registration_xmpp.yaml"
directory=/var/calculate/matrix-bifrost
pidfile="/run/matrixxmpp.pid"
command_background=true

depend() {
        need net
}

Set the privileges:

chmod 0755 /etc/init.d/matrix-xmpp-bridge

Launch the daemon:

/etc/init.d/matrix-xmpp-bridge start

Add it to autostart:

rc-update add matrix-xmpp-bridge

Connecting XMPP to Matrix

Connecting XMPP users with Matrix users

A Matrix user's JID in XMPP is [HTML_REMOVED]_[HTML_REMOVED]@matrix.xmpp.example.org.

To open a dialog with user user:matrix.example.org while on XMPP, start it with user_matrix.example.org@matrix.xmpp.example.org.

An XMPP user's MXID is @xmpp_<XMPP_nick>=40<jabber_server>:matrix.example.org.

To open a dialog with XMPP user foo@xmpp.example.org while on Matrix, start it with ~~@xmpp_foo=40xmpp.example.org:matrix.example.org~.

Connecting a Matrix room to XMPP chats

The alias of an XMPP chat in Matrix is #xmpp_(name):matrix.example.org

To create a room connected to XMPP chat ~room@muc.xmpp.example.org while on Matrix, join the room #xmpp_room:matrix.example.org~.

Connecting an XMPP chat room to an existing Matrix room

Ask for the MXID of the command you want to connect mychat@muc.xmpp.example.org (! with:PHAqztyseimYIVPwqu:matrix.example.org)

Create a chat, mychat@muc.xmpp.example.org in XMPP.

Add processing for room ! to the registration file:
PHAqztyseimYIVPwqu:matrix.example.org

/var/calculate/matrix-bifrost/registration_xmpp.yaml
...
namespaces:
...
  rooms:
    - exclusive: false
      regex: '!PHAqztyseimYIVPwqu:matrix.example.org'

Copy registration_xmpp.yaml to the /var/calculate/synapse directory on the Matrix server.

scp registration_xmpp.yaml root@synapse:/var/calculate/synapse/

Restart the Synapse server.

/etc/init.d/synapse restart

Restart the bridge:

/etc/init.d/matrix-xmpp-bridge restart

Connect the current Matrix room to XMPP chat room mychat@muc.xmpp.example.org by sending the following message to it:

!purple bridge xmpp-js muc.xmpp.example.org mychat