diff --git a/matrix.yml b/matrix.yml index 1975e50..4767ca5 100644 --- a/matrix.yml +++ b/matrix.yml @@ -13,3 +13,9 @@ - hosts: riot.adm.auro.re roles: - matrix-riot + +# Install Matrix services +- hosts: matrix-services.adm.auro.re + roles: + - debian-backports + - matrix-appservice-discord diff --git a/roles/matrix-appservice-discord/handlers/main.yml b/roles/matrix-appservice-discord/handlers/main.yml new file mode 100644 index 0000000..ac3842d --- /dev/null +++ b/roles/matrix-appservice-discord/handlers/main.yml @@ -0,0 +1,4 @@ +--- +# Reload systemd daemons when a service file changes +- name: Reload systemd daemons + command: systemctl daemon-reload diff --git a/roles/matrix-appservice-discord/tasks/0_apt_dependencies.yml b/roles/matrix-appservice-discord/tasks/0_apt_dependencies.yml new file mode 100644 index 0000000..40fbbce --- /dev/null +++ b/roles/matrix-appservice-discord/tasks/0_apt_dependencies.yml @@ -0,0 +1,20 @@ +--- +- name: Configure NodeJS pin + when: + - ansible_distribution == 'Debian' + - ansible_distribution_release == 'stretch' + template: + src: apt/nodejs.j2 + dest: /etc/apt/preferences.d/nodejs + mode: 0644 + +- name: Install required packages + apt: + name: "{{ item }}" + state: present + update_cache: true + with_items: + - git + - nodejs + - npm + - build-essential diff --git a/roles/matrix-appservice-discord/tasks/1_user_group.yml b/roles/matrix-appservice-discord/tasks/1_user_group.yml new file mode 100644 index 0000000..2fe23be --- /dev/null +++ b/roles/matrix-appservice-discord/tasks/1_user_group.yml @@ -0,0 +1,26 @@ +--- +# Security #1 +- name: Create matrix-appservice-discord system group + group: + name: matrix-appservice-discord + system: yes + state: present + +# Security #2 +- name: Create matrix-appservice-discord user + user: + name: matrix-appservice-discord + group: matrix-appservice-discord + home: /var/local/matrix-appservice-discord + comment: Matrix Appservice Discord + system: yes + state: present + +# Security #3 +- name: Secure matrix-appservice-discord home directory + file: + path: /var/local/matrix-appservice-discord + state: directory + owner: matrix-appservice-discord + group: matrix-appservice-discord + mode: 0750 diff --git a/roles/matrix-appservice-discord/tasks/main.yml b/roles/matrix-appservice-discord/tasks/main.yml new file mode 100644 index 0000000..a625a00 --- /dev/null +++ b/roles/matrix-appservice-discord/tasks/main.yml @@ -0,0 +1,57 @@ +--- +# Install APT dependencies +- include_tasks: 0_apt_dependencies.yml + +# Create CodiMD user and group +- include_tasks: 1_user_group.yml + +# Download CodiMD +- name: Clone matrix-appservice-discord project + git: + repo: https://github.com/Half-Shot/matrix-appservice-discord.git + dest: /var/local/matrix-appservice-discord/matrix-appservice-discord + version: v0.4.0 + become: true + become_user: matrix-appservice-discord + +# Setup dependencies +- name: Install matrix-appservice-discord depedencies + command: npm ci + args: + chdir: /var/local/matrix-appservice-discord/matrix-appservice-discord + become: true + become_user: matrix-appservice-discord + +# Typescript into javascript +- name: Compile matrix-appservice-discord + command: npm run build + args: + chdir: /var/local/matrix-appservice-discord/matrix-appservice-discord + become: true + become_user: matrix-appservice-discord + +# Configure +- name: Configure matrix-appservice-discord + template: + src: config.yaml.j2 + dest: /var/local/matrix-appservice-discord/matrix-appservice-discord/config.yaml + owner: matrix-appservice-discord + group: matrix-appservice-discord + mode: 0600 + +# Service file +- name: Install matrix-appservice-discord systemd unit + template: + src: 'systemd/matrix-appservice-discord.service.j2' + dest: '/etc/systemd/system/matrix-appservice-discord.service' + owner: root + group: root + mode: 0644 + notify: Reload systemd daemons + +# Run +#- name: Ensure that matrix-appservice-discord is started +# service: +# name: matrix-appservice-discord +# state: started +# enabled: true diff --git a/roles/matrix-appservice-discord/templates/apt/nodejs.j2 b/roles/matrix-appservice-discord/templates/apt/nodejs.j2 new file mode 100644 index 0000000..65e5110 --- /dev/null +++ b/roles/matrix-appservice-discord/templates/apt/nodejs.j2 @@ -0,0 +1,5 @@ +# {{ ansible_managed }} + +Package: node* libuv1* +Pin: release a=stretch-backports +Pin-Priority: 600 diff --git a/roles/matrix-appservice-discord/templates/config.yaml.j2 b/roles/matrix-appservice-discord/templates/config.yaml.j2 new file mode 100644 index 0000000..64e17dd --- /dev/null +++ b/roles/matrix-appservice-discord/templates/config.yaml.j2 @@ -0,0 +1,85 @@ +# {{ ansible_managed }} + +bridge: + # Domain part of the bridge, e.g. matrix.org + domain: "auro.re" + # This should be your publically facing URL because Discord may use it to + # fetch media from the media store. + homeserverUrl: "https://auro.re" + # Interval at which to process users in the 'presence queue'. If you have + # 5 users, one user will be processed every 500 milliseconds according to the + # value below. This has a minimum value of 250. + # WARNING: This has a high chance of spamming the homeserver with presence + # updates since it will send one each time somebody changes state or is online. + presenceInterval: 500 + # Disable setting presence for 'ghost users' which means Discord users on Matrix + # will not be shown as away or online. + disablePresence: false + # Disable sending typing notifications when somebody on Discord types. + disableTypingNotifications: false + # Disable deleting messages on Discord if a message is redacted on Matrix. + disableDeletionForwarding: false + # Enable users to bridge rooms using !discord commands. See + # https://t2bot.io/discord for instructions. + enableSelfServiceBridging: false + # Disable sending of read receipts for Matrix events which have been + # successfully bridged to Discord. + disableReadReceipts: false +# Authentication configuration for the discord bot. +auth: + clientID: "12345" + botToken: "foobar" +logging: + # What level should the logger output to the console at. + console: "warn" #silly, verbose, info, http, warn, error, silent + lineDateFormat: "MMM-D HH:mm:ss.SSS" # This is in moment.js format + files: + - file: "warn.log" # Will capture warnings + level: "warn" + - file: "botlogs.log" # Will capture logs from DiscordBot + level: "info" + enable: + - "DiscordBot" +database: + userStorePath: "user-store.db" + roomStorePath: "room-store.db" + # You may either use SQLite or Postgresql for the bridge database, which contains + # important mappings for events and user puppeting configurations. + # Use the filename option for SQLite, or connString for Postgresql. + # If you are migrating, see https://github.com/Half-Shot/matrix-appservice-discord/blob/master/docs/howto.md#migrate-to-postgres-from-sqlite + # WARNING: You will almost certainly be fine with sqlite unless your bridge + # is in heavy demand and you suffer from IO slowness. + filename: "discord.db" + # connString: "postgresql://user:password@localhost/database_name" +room: + # Set the default visibility of alias rooms, defaults to "public". + # One of: "public", "private" + defaultVisibility: "public" +channel: + # Pattern of the name given to bridged rooms. + # Can use :guild for the guild name and :name for the channel name. + namePattern: "[Discord] :guild :name" + # Changes made to rooms when a channel is deleted. + deleteOptions: + # Prefix the room name with a string. + #namePrefix: "[Deleted]" + # Prefix the room topic with a string. + #topicPrefix: "This room has been deleted" + # Disable people from talking in the room by raising the event PL to 50 + disableMessaging: false + # Remove the discord alias from the room. + unsetRoomAlias: true + # Remove the room from the directory. + unlistFromDirectory: true + # Set the room to be unavaliable for joining without an invite. + setInviteOnly: true + # Make all the discord users leave the room. + ghostsLeave: true +limits: + # Delay in milliseconds between discord users joining a room. + roomGhostJoinDelay: 6000 + # Delay in milliseconds before sending messages to discord to avoid echos. + # (Copies of a sent message may arrive from discord before we've + # fininished handling it, causing us to echo it back to the room) + discordSendDelay: 750 + diff --git a/roles/matrix-appservice-discord/templates/systemd/matrix-appservice-discord.service.j2 b/roles/matrix-appservice-discord/templates/systemd/matrix-appservice-discord.service.j2 new file mode 100644 index 0000000..afc1b4b --- /dev/null +++ b/roles/matrix-appservice-discord/templates/systemd/matrix-appservice-discord.service.j2 @@ -0,0 +1,18 @@ +# {{ ansible_managed }} + +[Unit] +Description=A bridge between Matrix and Discord +After=syslog.target network-online.target mysql.service postgresql.service +Conflicts=shutdown.target + +[Service] +Type=simple +User=matrix-appservice-discord +Group=matrix-appservice-discord +WorkingDirectory=/var/local/matrix-appservice-discord/matrix-appservice-discord +Environment="NODE_ENV=production" +ExecStart=/usr/bin/nodejs /var/local/matrix-appservice-discord/matrix-appservice-discord/app.js +Restart=always + +[Install] +WantedBy=multi-user.target