Мост Matrix-IRC

Обновлено 20 мая 2020

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

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

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

Установите утилиту для управления версиями NodeJS:

emerge -a dev-python/nodeenv

Создайте пользователя irc в системе:

useradd -m -d /var/calculate/matrix-irc-bridge -s /bin/bash -p irc irc

Установите Node.js в директорию пользователя:

su - irc

nodeenv --node=10.20.1 .node-10

source .node-10/bin/activate

echo 'source ~/.node-10/bin/activate' >> ~/.bash_profile

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

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

Перейдите в каталог приложения и выполните сборку и установку приложения:

cd live

npm install

npm run-script build

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

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

~/live/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"
# Configuration for logging. Optional. Default: console debug level logging
# only.
logging:
  # Level to log on console/logfile. One of error|warn|info|debug
  level: "debug"
  # The file location to log to. This is relative to the project directory.
  logfile: "debug.log"
  # The file location to log errors to. This is relative to the project
  # directory.
  errfile: "errors.log"
  # Whether to log to the console or not.
  toConsole: false
  # The max number of files to keep. Files will be overwritten eventually due
  # to rotations.
  maxFiles: 5
# Use an external database to store bridge state.
database:
  # database engine (must be 'postgres' or 'nedb'). Default: nedb
  engine: "nedb"
  # Either a PostgreSQL connection string, or a path to the NeDB storage directory.
  # For postgres, it must start with postgres://
  # For NeDB, it must start with nedb://. The path is relative to the project directory.
  connectionString: "nedb://freenode"

Сгенерируйте ключ для шифрования паролей пользователей 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:

~/live/config.yaml
...
ircService:
  ident:
    enabled: true
    port: 1113
...

Запуск моста

Создайте сценарий 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: $

description="Matrix-IRC bridge"
pidfile=/run/matrix-irc-bridge.pid
command_user=irc
output_log=/var/log/matrix-irc-bridge.log
error_log=/var/log/matrix-irc-bridge.log
directory=/var/calculate/matrix-irc-bridge/live
command="/var/calculate/matrix-irc-bridge/.node-10/bin/node"
command_args="app.js -c config.yaml -f registration_freenode.yaml -p 9999"
stopsig="2"
command_background=true

depend() {
        need net
}

start_pre() {
    checkpath -f -o irc -m 0600 /var/log/matrix-irc-bridge.log
}

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

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

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

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

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

rc-update add matrix-irc-bridge