Verified Commit 6de34c3e authored by Jan Alexander Steffens (heftig)'s avatar Jan Alexander Steffens (heftig)
Browse files

roles/matrix: Refine and add IRC bridge

parent b3a45fb7
This diff is collapsed.
......@@ -17,7 +17,7 @@ handlers:
SYSLOG_IDENTIFIER: synapse
root:
level: INFO
level: WARNING
handlers: [journal]
disable_existing_loggers: False
[Unit]
Description=Matrix IRC Bridge
Requires=synapse.service
After=network.target synapse.service
[Service]
User=synapse
WorkingDirectory=/var/lib/synapse/matrix-appservice-irc
ExecStart=/usr/bin/node app.js \
-c /etc/synapse/irc-bridge.yaml \
-f /etc/synapse/appservice-registration-irc.yaml \
-p 8499
[Install]
WantedBy=multi-user.target
......@@ -5,7 +5,7 @@
when: 'matrix_domain != ""'
- name: install packages
pacman: name=python2-virtualenv,git
pacman: name=python2-virtualenv,git,npm
- name: add synapse group
group: name=synapse system=yes gid=198
......@@ -14,7 +14,12 @@
user: name=synapse system=yes uid=198 group=synapse home=/var/lib/synapse shell=/bin/false createhome=no
- name: create synapse home
file: path=/var/lib/synapse state=directory owner=synapse group=synapse mode=0755
file: path={{ item }} state=directory owner=synapse group=synapse mode=0755
with_items:
- /var/lib/synapse
- /var/lib/synapse/irc-nedb
- /var/lib/synapse/media_store
- /var/lib/synapse/uploads
- name: create venv
command: virtualenv2 /var/lib/synapse/venv
......@@ -32,6 +37,14 @@
become_user: synapse
become_method: sudo
- name: download matrix-appservice-irc
command: git clone https://github.com/heftig/matrix-appservice-irc /var/lib/synapse/matrix-appservice-irc
args:
creates: /var/lib/synapse/matrix-appservice-irc/package.json
become: yes
become_user: synapse
become_method: sudo
- name: install synapse
command: /var/lib/synapse/venv/bin/pip install -e /var/lib/synapse/synapse psycopg2 systemd-python lxml
args:
......@@ -40,6 +53,15 @@
become_user: synapse
become_method: sudo
- name: install matrix-appservice-irc
command: npm install
args:
chdir: /var/lib/synapse/matrix-appservice-irc
creates: /var/lib/synapse/matrix-appservice-irc/node_modules
become: yes
become_user: synapse
become_method: sudo
- name: add synapse postgres db
postgresql_db: db=synapse
become: yes
......@@ -52,14 +74,7 @@
become_user: postgres
become_method: su
- name: install matrix units
copy: src={{ item }} dest=/etc/systemd/system/{{ item }} owner=root group=root mode=0644
with_items:
- synapse.service
notify:
- daemon reload
- name: add synapse config dir
- name: create synapse config dir
file: state=directory path=/etc/synapse owner=root group=synapse mode=0750
- name: install homeserver config
......@@ -68,17 +83,23 @@
- name: install log config
copy: src=log_config.yaml dest=/etc/synapse/log_config.yaml owner=root group=root mode=0644
- name: install irc-bridge config
template: src=irc-bridge.yaml.j2 dest=/etc/synapse/irc-bridge.yaml owner=root group=synapse mode=0640
- name: install irc-bridge registration
template: src=appservice-registration-irc.yaml.j2 dest=/etc/synapse/appservice-registration-irc.yaml owner=root group=synapse mode=0640
- name: install federation certificate
copy:
content: "{{ matrix_secrets[matrix_server_name].federation_crt }}"
content: '{{ matrix_secrets[matrix_server_name].federation_crt }}'
dest: /etc/synapse/{{ matrix_server_name }}.tls.crt
owner: root
group: synapse
mode: 0640
group: root
mode: 0644
- name: install federation key
copy:
content: "{{ matrix_secrets[matrix_server_name].federation_key }}"
content: '{{ matrix_secrets[matrix_server_name].federation_key }}'
dest: /etc/synapse/{{ matrix_server_name }}.tls.key
owner: root
group: synapse
......@@ -86,16 +107,19 @@
- name: install signing key
copy:
content: "{{ matrix_secrets[matrix_server_name].signing_key }}"
content: '{{ matrix_secrets[matrix_server_name].signing_key }}'
dest: /etc/synapse/{{ matrix_server_name }}.signing.key
owner: root
group: synapse
mode: 0640
- name: start and enable synapse
service: name={{ item }} enabled=yes state=started
with_items:
- synapse.service
- name: install ircpass key
copy:
content: '{{ matrix_secrets[matrix_server_name].ircpass_key }}'
dest: /etc/synapse/{{ matrix_server_name }}.ircpass.key
owner: root
group: synapse
mode: 0640
- name: make nginx log dir
file: path=/var/log/nginx/{{ matrix_domain }} state=directory owner=root group=root mode=0755
......@@ -105,3 +129,17 @@
notify:
- reload nginx
when: 'matrix_domain != ""'
- name: install matrix units
copy: src={{ item }} dest=/etc/systemd/system/{{ item }} owner=root group=root mode=0644
with_items:
- synapse.service
- matrix-appservice-irc.service
notify:
- daemon reload
- name: start and enable synapse
service: name={{ item }} enabled=yes state=started
with_items:
- synapse.service
- matrix-appservice-irc.service
id: {{ matrix_secrets[matrix_server_name].irc_appservice_id }}
hs_token: {{ matrix_secrets[matrix_server_name].irc_hs_token }}
as_token: {{ matrix_secrets[matrix_server_name].irc_as_token }}
namespaces:
users:
{% for network in matrix_secrets[matrix_server_name].irc_networks.itervalues() %}
- exclusive: true
regex: '@{{ network.name | regex_escape() }}_.*:{{ matrix_server_name | regex_escape() }}'
{% endfor %}
aliases:
{% for network in matrix_secrets[matrix_server_name].irc_networks.itervalues() %}
- exclusive: true
regex: '#{{ network.name | regex_escape() }}_.*:{{ matrix_server_name | regex_escape() }}'
{% endfor %}
rooms:
{% for room in matrix_secrets[matrix_server_name] | json_query("irc_networks.*[].mappings.*[][]") %}
- exclusive: false
regex: '{{ room | regex_escape() }}'
{% endfor %}
url: 'http://127.0.0.1:8499'
sender_localpart: irc-bridge
rate_limited: false
......@@ -4,10 +4,10 @@
# autogenerates on launch with your own SSL certificate + key pair
# if you like. Any required intermediary certificates can be
# appended after the primary certificate in hierarchical order.
tls_certificate_path: "/etc/synapse/{{ matrix_server_name }}.tls.crt"
tls_certificate_path: '/etc/synapse/{{ matrix_server_name }}.tls.crt'
# PEM encoded private key for TLS
tls_private_key_path: "/etc/synapse/{{ matrix_server_name }}.tls.key"
tls_private_key_path: '/etc/synapse/{{ matrix_server_name }}.tls.key'
# PEM dh parameters for ephemeral keys
tls_dh_params_path: /etc/ssl/dhparams.pem
......@@ -44,13 +44,13 @@ tls_fingerprints: []
# This is used by remote servers to connect to this server,
# e.g. matrix.org, localhost:8080, etc.
# This is also the last part of your UserID.
server_name: "{{ matrix_server_name }}"
server_name: '{{ matrix_server_name }}'
# When running as a daemon, the file to store the pid in
#pid_file: /var/lib/synapse/homeserver.pid
# Whether to serve a web client from the HTTP/HTTPS root resource.
web_client: true
web_client: false
# The public-facing base URL for the client API (not including _matrix/...)
# public_baseurl: https://example.com:8448/
......@@ -96,7 +96,7 @@ listeners:
# List of resources to host on this listener.
names:
- client # The client-server APIs, both v1 and v2
- webclient # The bundled webclient.
#- webclient # The bundled webclient.
# Should synapse compress HTTP responses to clients that support it?
# This should be disabled if running synapse behind a load balancer
......@@ -116,7 +116,7 @@ listeners:
x_forwarded: true
resources:
- names: [client, webclient]
- names: [client]
compress: true
- names: [federation]
compress: false
......@@ -337,7 +337,7 @@ enable_registration: False
# If set, allows registration by anyone who also has the shared
# secret, even if registration is otherwise disabled.
registration_shared_secret: "{{ matrix_secrets[matrix_server_name].registration_shared_secret }}"
registration_shared_secret: '{{ matrix_secrets[matrix_server_name].registration_shared_secret }}'
# Set the number of bcrypt rounds used to generate password hash.
# Larger numbers increase the work factor needed to generate the hash.
......@@ -374,10 +374,10 @@ room_invite_state_types:
# A list of application service config file to use
app_service_config_files: []
app_service_config_files: ['/etc/synapse/appservice-registration-irc.yaml']
macaroon_secret_key: "{{ matrix_secrets[matrix_server_name].macaroon_secret_key }}"
macaroon_secret_key: '{{ matrix_secrets[matrix_server_name].macaroon_secret_key }}'
# Used to enable access token expiration.
expire_access_token: False
......@@ -385,7 +385,7 @@ expire_access_token: False
## Signing Keys ##
# Path to the signing key to sign messages with
signing_key_path: "/etc/synapse/{{ matrix_server_name }}.signing.key"
signing_key_path: '/etc/synapse/{{ matrix_server_name }}.signing.key'
# The keys that the server used to sign messages with but won't use
# to sign new messages. E.g. it has lost its private key
......@@ -447,7 +447,7 @@ password_config:
enabled: true
# Uncomment and change to a secret random string for extra security.
# DO NOT CHANGE THIS AFTER INITIAL SETUP!
pepper: "{{ matrix_secrets[matrix_server_name].pepper }}"
pepper: '{{ matrix_secrets[matrix_server_name].pepper }}'
......
# Configuration specific to AS registration. Unless other marked, all fields
# are *REQUIRED*.
homeserver:
# The URL to the home server for client-server API calls, also used to form the
# media URLs as displayed in bridged IRC channels:
url: 'https://{{ matrix_domain }}'
#
# 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: "http://media.repo:8008"
# Drop Matrix messages which are older than this number of seconds, according to
# the event's origin_server_ts.
# If the bridge is down for a while, the homeserver will attempt to send all missed
# events on reconnection. These events may be hours old, which can be confusing to
# IRC users if they are then bridged. This option allows these old messages to be
# dropped.
# CAUTION: This is a very coarse heuristic. Federated homeservers may have different
# clock times and hence produce different origin_server_ts values, which may be old
# enough to cause *all* events from the homeserver to be dropped.
# Default: 0 (don't ever drop)
# dropMatrixMessagesAfterSecs: 300 # 5 minutes
# 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_server_name }}'
# Configuration specific to the IRC service
ircService:
servers:
{% for address, settings in matrix_secrets[matrix_server_name].irc_networks.iteritems() %}
# The address of the server to connect to.
'{{ address }}':
# A human-readable short name. This is used to label IRC status rooms
# where matrix users control their connections.
# E.g. 'ExampleNet IRC Bridge status'.
# It is also used in the Third Party Lookup API as the instance `desc`
# property, where each server is an instance.
name: '{{ settings.name }}'
#
# [DEPRECATED] Use `name`, above, instead.
# A human-readable description string
# description: "Example.com IRC network"
# An ID for uniquely identifying this server amongst other servers being bridged.
# networkId: "example"
# The port to connect to. Optional.
port: {{ settings.port }}
# Whether to use SSL or not. Default: false.
ssl: true
# Whether or not IRC server is using a self-signed cert or not providing CA Chain
sslselfsign: false
# A specific CA to trust instead of the default CAs. Optional.
#ca: |
# -----BEGIN CERTIFICATE-----
# ...
# -----END CERTIFICATE-----
#
# The connection password to send for all clients as a PASS command. Optional.
# password: 'pa$$w0rd'
#
# Whether or not to send connection/error notices to real Matrix users. Default: true.
sendConnectionMessages: true
quitDebounce:
# Whether parts due to net-splits are debounced for delayMs, to allow
# time for the netsplit to resolve itself. A netsplit is detected as being
# a QUIT rate higher than quitsPerSecond. Default: false.
enabled: false
# The maximum number of quits per second acceptable above which a netsplit is
# considered ongoing. Default: 5.
quitsPerSecond: 5
# The time window in which to wait before bridging a QUIT to Matrix that occurred during
# a netsplit. Debouncing is jittered randomly between delayMinMs and delayMaxMs so that the HS
# is not sent many requests to leave rooms all at once if a netsplit occurs and many
# people to not rejoin.
# If the user with the same IRC nick as the one who sent the quit rejoins a channel
# they are considered back online and the quit is not bridged, so long as the rejoin
# occurs before the randomly-jittered timeout is not reached.
# Default: 3600000, = 1h
delayMinMs: 3600000 # 1h
# Default: 7200000, = 2h
delayMaxMs: 7200000 # 2h
# A map for conversion of IRC user modes to Matrix power levels. This enables bridging
# of IRC ops to Matrix power levels only, it does not enable the reverse. If a user has
# been given multiple modes, the one that maps to the highest power level will be used.
modePowerMap:
o: 50
botConfig:
# Enable the presence of the bot in IRC channels. The bot serves as the entity
# which maps from IRC -> Matrix. You can disable the bot entirely which
# means IRC -> Matrix chat will be shared by active "M-Nick" connections
# in the room. If there are no users in the room (or if there are users
# but their connections are not on IRC) then nothing will be bridged to
# Matrix. If you're concerned about the bot being treated as a "logger"
# entity, then you may want to disable the bot. If you want IRC->Matrix
# but don't want to have TCP connections to IRC unless a Matrix user speaks
# (because your client connection limit is low), then you may want to keep
# the bot enabled. Default: true.
# NB: If the bot is disabled, you SHOULD have matrix-to-IRC syncing turned
# on, else there will be no users and no bot in a channel (meaning no
# messages to Matrix!) until a Matrix user speaks which makes a client
# join the target IRC channel.
# NBB: The bridge bot IRC client will still join the target IRC network so
# it can service bridge-specific queries from the IRC-side e.g. so
# real IRC clients have a way to change their Matrix display name.
# See https://github.com/matrix-org/matrix-appservice-irc/issues/55
enabled: true
# The nickname to give the AS bot.
nick: '{{ settings.nick }}'
# The password to give to NickServ or IRC Server for this nick. Optional.
password: '{{ settings.password }}'
#
# Join channels even if there are no Matrix users on the other side of
# the bridge. Set to false to prevent the bot from joining channels which have no
# real matrix users in them, even if there is a mapping for the channel.
# Default: true
joinChannelsIfNoUsers: true
# Configuration for PMs / private 1:1 communications between users.
privateMessages:
# Enable the ability for PMs to be sent to/from IRC/Matrix.
# Default: true.
enabled: false
# Prevent Matrix users from sending PMs to the following IRC nicks.
# Optional. Default: [].
# exclude: ["Alice", "Bob"] # NOT YET IMPLEMENTED
# Should created Matrix PM rooms be federated? If false, only users on the
# HS attached to this AS will be able to interact with this room.
# Optional. Default: true.
federate: true
# Configuration for mappings not explicitly listed in the 'mappings'
# section.
dynamicChannels:
# Enable the ability for Matrix users to join *any* channel on this IRC
# network.
# Default: false.
enabled: false
# Should the AS create a room alias for the new Matrix room? The form of
# the alias can be modified via 'aliasTemplate'. Default: true.
createAlias: true
# Should the AS publish the new Matrix room to the public room list so
# anyone can see it? Default: true.
published: true
# What should the join_rule be for the new Matrix room? If 'public',
# anyone can join the room. If 'invite', only users with an invite can
# join the room. Note that if an IRC channel has +k or +i set on it,
# join_rules will be set to 'invite' until these modes are removed.
# Default: "public".
joinRule: public
# Should created Matrix rooms be federated? If false, only users on the
# HS attached to this AS will be able to interact with this room.
# Default: true.
federate: true
# 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: '#{{ settings.name }}_$CHANNEL'
# A list of user IDs which the AS bot will send invites to in response
# to a !join. Only applies if joinRule is 'invite'. Default: []
# whitelist:
# - "@foo:example.com"
# - "@bar:example.com"
#
# Prevent the given list of channels from being mapped under any
# circumstances.
# exclude: ["#foo", "#bar"]
# 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
# Syncing membership lists at startup can result in hundreds of members to
# process all at once. This timer drip feeds membership entries at the
# specified rate. Default: 10000. (10s)
floodDelayMs: 10000
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
# Apply specific rules to Matrix rooms. Only matrix-to-IRC takes effect.
rooms: []
#- room: "!fuasirouddJoxtwfge:localhost"
# matrixToIrc:
# initial: false
# incremental: false
# Apply specific rules to IRC channels. Only IRC-to-matrix takes effect.
channels: []
# - channel: "#foo"
# ircToMatrix:
# initial: false
# incremental: false
mappings:
# 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.
{% for channel, rooms in settings.mappings.iteritems() %}
'{{ channel }}':
{% for room in rooms %}
- '{{ room }}'
{% endfor %}
{% endfor %}
mappings_keys:
{% for channel, key in settings.mappings_keys.iteritems() %}
'{{ channel }}': '{{ key }}'
{% endfor %}
# Configuration for virtual matrix users. The following variables are
# exposed:
# $NICK => The IRC nick
# $SERVER => The IRC server address (e.g. "irc.example.com")
matrixClients:
# 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: '@{{ settings.name }}_$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 (IRC)"
# 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.
ircClients:
# 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'
# True to allow virtual IRC clients to change their nick on this server
# by issuing !nick <server> <nick> commands to the IRC AS bot.
# This is completely freeform: it will NOT follow the nickTemplate.
allowNickChanges: true
# 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: 50
# IPv6 configuration.
ipv6:
# Optional. Set to true to force IPv6 for outgoing connections.
only: false
# Optional. The IPv6 prefix to use for generating unique addresses for each
# connected user. If not specified, all users will connect from the same
# (default) address. This may require additional OS-specific work to allow
# for the node process to bind to multiple different source addresses
# e.g IP_FREEBIND on Linux, which requires an LD_PRELOAD with the library
# https://github.com/matrix-org/freebindfree as Node does not expose setsockopt.
# prefix: "2001:0db8:85a3::" # modify appropriately
#
# The maximum amount of time in seconds that the client can exist
# without sending another message before being disconnected. Use 0 to
# not apply an idle timeout. This value is ignored if this IRC server is
# mirroring matrix membership lists to IRC. Default: 172800 (48 hours)
idleTimeout: 10800
# The number of millseconds to wait between consecutive reconnections if a
# client gets disconnected. Setting to 0 will cause the scheduling to be
# disabled, i.e. it will be scheduled immediately (with jitter.
# Otherwise, the scheduling interval will be used such that one client
# reconnect for this server will be handled every reconnectIntervalMs ms using
# a FIFO queue.
# Default: 5000 (5 seconds)
reconnectIntervalMs: 5000
# The number of lines to allow being sent by the IRC client that has received
# a large block of text to send from matrix. If the number of lines that would
# be sent is > lineLimit, the text will instead be uploaded to matrix and the
# resulting URI is treated as a file. As such, a link will be sent to the IRC
# side instead of potentially spamming IRC and getting the IRC client kicked.
# Default: 3.
lineLimit: 3
# 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: '{{ settings.user_modes }}'
{% endfor %}
# Configuration for an ident server. If you are running a public bridge it is
# advised you setup an ident server so IRC mods can ban specific matrix users
# rather than the application service itself.
ident:
# True to listen for Ident requests and respond with the
# matrix user's user_id (converted to ASCII, respecting RFC 1413).
# Default: false.
enabled: false
# The port to listen on for incoming ident requests.
# Ports below 1024 require root to listen on, and you may not want this to
# run as root. Instead, you can get something like an Apache to yank up
# incoming requests to 113 to a high numbered port. Set the port to listen
# on instead of 113 here.
# Default: 113.
port: 1113
# Configuration for logging. Optional. Default: console debug level logging
# only.
logging:
# Level to log on console/logfile. One of error|warn|info|debug
level: warn
# 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: true
# The max size each file can get to in bytes before a new file is created.
maxFileSizeBytes: 134217728 # 128 MB
# The max number of files to keep. Files will be overwritten eventually due
# to rotations.
maxFiles: 5