Мост Matrix-IRC

Обновлено 13 мая 2019

Подготовка LXC контейнера

Мы рекомендуем выполнить настройку моста в отдельном контейнере, настроив его согласно инструкции.

Установка программного обеспечения

Установите NodeJS:

emerge -a net-libs/nodejs

Скачайте последнюю версию matrix-appservice-irc из github в каталог /var/calculate:

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

Выполните установку приложения из каталога /var/calculate/matrix-appservice-irc:

npm install

Настройка моста

Скопируйте файл настроек config.sample.yaml в config.yaml и выполните настройку:

config.yaml
homeserver:
  # The URL of the homeserver hosting media files. This is only used to transform
  # mxc URIs to http URIs when bridging m.room.[file|image] events. Optional. By
  # default, this is the homeserver URL, specified above.
  #
  media_url: "https://matrix.example.org"
  # The 'domain' part for user IDs on this home server. Usually (but not always)
  # is the "domain name" part of the HS URL.
  domain: "matrix.example.org"
# Configuration specific to the IRC service
ircService:
  # The address of the server to connect to.
  irc.freenode.org:
    name: "FreeNode"
    # additionalAddresses: [ "irc2.example.com" ]
    botConfig:
      # Enable the presence of the bot in IRC channels.
      enabled: false
    # Configuration for mappings not explicitly listed in the 'mappings'
    # section.
    dynamicChannels:
      groupId: +ircfreenode:matrix.example.org
    # The room alias template to apply when creating new aliases. This only
    # applies if createAlias is 'true'. The following variables are exposed:
    # $SERVER => The IRC server address (e.g. "irc.example.com")
    # $CHANNEL => The IRC channel (e.g. "#python")
    # This MUST have $CHANNEL somewhere in it.
    # Default: '#irc_$SERVER_$CHANNEL'
    aliasTemplate: "#freenode_$CHANNEL"
    # Configuration for controlling how Matrix and IRC membership lists are
    # synced.
    membershipLists:
      # Enable the syncing of membership lists between IRC and Matrix. This
      # can have a significant effect on performance on startup as the lists are
      # synced. This must be enabled for anything else in this section to take
      # effect. Default: false.
      enabled: true

      global:
        ircToMatrix:
          # Get a snapshot of all real IRC users on a channel (via NAMES) and
          # join their virtual matrix clients to the room.
          initial: true
          # Make virtual matrix clients join and leave rooms as their real IRC
          # counterparts join/part channels. Default: false.
          incremental: true
        matrixToIrc:
          # Get a snapshot of all real Matrix users in the room and join all of
          # them to the mapped IRC channel on startup. Default: false.
          initial: true
          # Make virtual IRC clients join and leave channels as their real Matrix
          # counterparts join/leave rooms. Make sure your 'maxClients' value is
          # high enough! Default: false.
          incremental: true
    mapping:
      # 1:many mappings from IRC channels to room IDs on this IRC server.
      # The matrix room must already exist. Your matrix client should expose
      # the room ID in a "settings" page for the room.
      "#chatexample": ["!kieouiJuedJoxtVdaG:matrix.example.org"]
    # Configuration for virtual matrix users. The following variables are
    # exposed:
    # $NICK => The IRC nick
    # $SERVER => The IRC server address (e.g. "irc.example.com")
    matrixClient:
      # The user ID template to use when creating virtual matrix users. This
      # MUST have $NICK somewhere in it.
      # Optional. Default: "@$SERVER_$NICK".
      # Example: "@irc.example.com_Alice:example.com"
      userTemplate: "@freenode_$NICK"
      # The display name to use for created matrix clients. This should have
      # $NICK somewhere in it if it is specified. Can also use $SERVER to
      # insert the IRC domain.
      # Optional. Default: "$NICK (IRC)". Example: "Alice (IRC)"
      displayName: "$NICK"
    # Configuration for virtual IRC users. The following variables are exposed:
    # $LOCALPART => The user ID localpart ("alice" in @alice:localhost)
    # $USERID => The user ID
    # $DISPLAY => The display name of this user, with excluded characters
    #             (e.g. space) removed. If the user has no display name, this
    #             falls back to $LOCALPART.
    ircClient:
      # The template to apply to every IRC client nick. This MUST have either
      # $DISPLAY or $USERID or $LOCALPART somewhere in it.
      # Optional. Default: "M-$DISPLAY". Example: "M-Alice".
      nickTemplate: "$DISPLAY[m]"
      # The max number of IRC clients that will connect. If the limit is
      # reached, the client that spoke the longest time ago will be
      # disconnected and replaced.
      # Optional. Default: 30.
      maxClients: 150
      # A list of user modes to set on every IRC client. For example, "RiG" would set
      # +R, +i and +G on every IRC connection when they have successfully connected.
      # User modes vary wildly depending on the IRC network you're connecting to,
      # so check before setting this value. Some modes may not work as intended
      # through the bridge e.g. caller ID as there is no way to /ACCEPT.
      # Default: "" (no user modes)
      userModes: "R"

Сгенерируйте ключ для шифрования паролей пользователей IRC:

openssl genpkey -out passkey.pem -outform PEM -algorithm RSA -pkeyopt rsa_keygen_bits:2048

Сгенерируйте регистрационный файл, который позволит приложению взаимодействовать с Matrix сервером, указав matrix-irc.example.org в качестве имени хоста IRC моста:

node app.js -r -f registration_freenode.yaml -u "http://matrix-irc.example.org:9999" -c config.yaml -l freenodebot

Скопируйте регистрационный файл на сервер Synapse:

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

Добавьте файл регистраций в настройку Synapse:

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

Перезапустите Synapse:

/etc/init.d/synapse restart

Использование Ident сервера

Для использвания моста с протоколом идентификации, включите в конфигурационном файле IRC сервис Ident:

config.yaml
...
ircService:
  ident:
    enabled: true
    port: 1113
...

При использовании большого количества пользователей необходимо применить патч:

curl -o token.patch https://github.com/matrix-org/matrix-appservice-irc/commit/5cd033a2f5cf57b89e19475c1a441a24ec56b63e.patch

patch -p1 <token.patch

Запуск моста

Создайте сценарий OpenRC для управления демоном Matrix IRC bridge:

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

name="Matrix-IRC daemon"
description="Matrix IRC bridge daemon"
command=/usr/bin/node
command_args="app.js -c config.yaml -f registration_freenode.yaml -p 9999"
directory=/var/calculate/matrix-appservice-irc
pidfile="/run/matrixirc.pid"
command_background=true

depend() {
        need net
}

Установите права на запуск:

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

Запустите демон:

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

Добавьте демон в автозагрузку:

rc-update add matrix-irc-bridge