From 6f2818b769e87fac41e9cf9088eaa55c27771c17 Mon Sep 17 00:00:00 2001
From: Simeon Keske <git@n0emis.eu>
Date: Wed, 6 May 2020 02:10:47 +0200
Subject: [PATCH] initial commit

---
 defaults/main.yml                       | 15 ++++
 tasks/main.yml                          | 91 +++++++++++++++++++++++++
 templates/config.yaml                   | 84 +++++++++++++++++++++++
 templates/matrix-appservice-irc.service | 18 +++++
 templates/registration.yaml             | 16 +++++
 5 files changed, 224 insertions(+)
 create mode 100644 defaults/main.yml
 create mode 100644 tasks/main.yml
 create mode 100644 templates/config.yaml
 create mode 100644 templates/matrix-appservice-irc.service
 create mode 100644 templates/registration.yaml

diff --git a/defaults/main.yml b/defaults/main.yml
new file mode 100644
index 0000000..8908964
--- /dev/null
+++ b/defaults/main.yml
@@ -0,0 +1,15 @@
+---
+matrix_appservice_irc_user: "matrix-appservice-irc"
+matrix_appservice_irc_group: "matrix-appservice-irc"
+matrix_appservice_irc_install_path: "/opt/matrix-appservice-irc"
+matrix_appservice_irc_config_path: "/etc/matrix-appservice-irc"
+matrix_appservice_irc_database_path: "/var/lib/matrix-appservice-irc"
+matrix_appservice_irc_log_path: "/var/log/matrix-appservice-irc"
+
+matrix_appservice_irc_repository: "https://github.com/matrix-org/matrix-appservice-irc"
+matrix_appservice_irc_version: "0.17.0"
+
+matrix_appservice_irc_port: "9999"
+matrix_appservice_irc_listen: "127.0.0.1"
+
+matrix_appservice_irc_id: "matrix-appservice-irc"
\ No newline at end of file
diff --git a/tasks/main.yml b/tasks/main.yml
new file mode 100644
index 0000000..c06d26d
--- /dev/null
+++ b/tasks/main.yml
@@ -0,0 +1,91 @@
+---
+- name: Create group
+  group:
+    name: "{{ matrix_appservice_irc_group }}"
+    state: present
+
+- name: Create user
+  user:
+    name: "{{ matrix_appservice_irc_user }}"
+    group: "{{ matrix_appservice_irc_group }}"
+    create_home: no
+    system: yes
+
+- name: Create matrix-appservice-irc installation directory
+  file:
+    path: "{{ matrix_appservice_irc_install_path }}"
+    recurse: yes
+    state: directory
+    owner: "{{ matrix_appservice_irc_user }}"
+    group: "{{ matrix_appservice_irc_group }}"
+
+- name: Create matrix-appservice-irc config directory
+  file:
+    path: "{{ matrix_appservice_irc_config_path }}"
+    recurse: yes
+    state: directory
+    owner: "{{ matrix_appservice_irc_user }}"
+    group: "{{ matrix_appservice_irc_group }}"
+
+- name: Create matrix-appservice-irc database directory
+  file:
+    path: "{{ matrix_appservice_irc_database_path }}"
+    recurse: yes
+    state: directory
+    owner: "{{ matrix_appservice_irc_user }}"
+    group: "{{ matrix_appservice_irc_group }}"
+
+- name: Create matrix-appservice-irc log directory
+  file:
+    path: "{{ matrix_appservice_irc_log_path }}"
+    recurse: yes
+    state: directory
+    owner: "{{ matrix_appservice_irc_user }}"
+    group: "{{ matrix_appservice_irc_group }}"
+
+- name: Add matrix-appservice-irc config to host
+  template:
+    src: "config.yaml"
+    dest: "{{ matrix_appservice_irc_config_path }}/config.yaml"
+    owner: "{{ matrix_appservice_irc_user }}"
+    group: "{{ matrix_appservice_irc_group }}"
+
+- name: Add matrix-appservice-irc registration config to host
+  template:
+    src: "registration.yaml"
+    dest: "{{ matrix_appservice_irc_config_path }}/registration.yaml"
+    owner: "{{ matrix_appservice_irc_user }}"
+    group: "{{ matrix_appservice_irc_group }}"
+
+- name: Clone matrix-appservice-irc source
+  git:
+    dest: "{{ matrix_appservice_irc_install_path }}"
+    repo: "{{ matrix_appservice_irc_repository }}"
+    version: "{{ matrix_appservice_irc_version }}"
+    force: yes
+
+- name: Install matrix-appservice-ircs dependencies
+  npm:
+    path: "{{ matrix_appservice_irc_install_path }}"
+    state: present
+  become_user: "{{ matrix_appservice_irc_user }}"
+  become: yes
+
+- name: Build the source with NPM
+  command: "npm run build"
+  args:
+    chdir: "{{ matrix_appservice_irc_install_path }}"
+  become_user: "{{ matrix_appservice_irc_user }}"
+  become: yes
+
+- name: Add systemd service file for matrix-appservice-irc
+  template:
+    src: "matrix-appservice-irc.service"
+    dest: "/etc/systemd/system/matrix-appservice-irc.service"
+
+- name: Ensure matrix-appservice-irc systemd service is enabled and running
+  systemd:
+    name: "matrix-appservice-irc"
+    daemon_reload: yes
+    enabled: yes
+    state: started
\ No newline at end of file
diff --git a/templates/config.yaml b/templates/config.yaml
new file mode 100644
index 0000000..2d9f211
--- /dev/null
+++ b/templates/config.yaml
@@ -0,0 +1,84 @@
+homeserver:
+  url: "https://matrix.noemis.me"
+  domain: "noemis.me"
+ircService:
+  servers:
+    irc.hackint.org:
+      name: "Hackint"
+      port: 6697
+      ssl: true
+      sslselfsign: false
+      botConfig:
+        enabled: true
+        nick: "n0emisBOT"
+        joinChannelsIfNoUsers: false
+
+      privateMessages:
+        enabled: true
+        federate: false
+
+      dynamicChannels:
+        enabled: true
+        createAlias: true
+        published: false
+        joinRule: invite
+        federate: true
+        aliasTemplate: "#irc_hackint_$CHANNEL"
+        whitelist:
+           - "@n0emis:noemis.me"
+      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
+
+      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: "@irc_pm_hackint_$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)"
+        # Number of tries a client can attempt to join a room before the request
+        # is discarded. You can also use -1 to never retry or 0 to never give up.
+        # Optional. Default: -1
+        joinAttempts: -1
+  # Set information about the bridged channel in the room state, so that client's may
+  # present relevant UI to the user. MSC2346
+  bridgeInfoState:
+    enabled: true
+    initial: true
+  # 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: "{{ matrix_appservice_irc_log_path }}/debug.log"
+    # The file location to log errors to. This is relative to the project
+    # directory.
+    errfile: "{{ matrix_appservice_irc_log_path }}/errors.log"
+    # Whether to log to the console or not.
+    toConsole: true
+    # The max number of files to keep. Files will be overwritten eventually due
+    # to rotations.
+    maxFiles: 5
+
+  passwordEncryptionKeyPath: "{{ matrix_appservice_irc_database_path }}/passkey.pem"
+
+# 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://{{ matrix_appservice_irc_database_path }}"
diff --git a/templates/matrix-appservice-irc.service b/templates/matrix-appservice-irc.service
new file mode 100644
index 0000000..123dd1a
--- /dev/null
+++ b/templates/matrix-appservice-irc.service
@@ -0,0 +1,18 @@
+[Unit]
+Description=matrix-appservice-irc service
+After=network.target
+
+[Service]
+Type=simple
+WorkingDirectory={{ matrix_appservice_irc_install_path }}
+ExecStart=/usr/bin/env node {{ matrix_appservice_irc_install_path }}/app.js \
+    -c {{ matrix_appservice_irc_config_path }}/config.yaml \
+    -f {{ matrix_appservice_irc_config_path }}/registration.yaml \
+    -p {{ matrix_appservice_irc_port }}
+
+Restart=always
+User={{ matrix_appservice_irc_user }}
+Group={{ matrix_appservice_irc_group }}
+
+[Install]
+WantedBy=multi-user.target
\ No newline at end of file
diff --git a/templates/registration.yaml b/templates/registration.yaml
new file mode 100644
index 0000000..c179589
--- /dev/null
+++ b/templates/registration.yaml
@@ -0,0 +1,16 @@
+id: {{ matrix_appservice_irc_id }}
+hs_token: {{ matrix_appservice_irc_hs_token }}
+as_token: {{ matrix_appservice_irc_as_token }}
+namespaces:
+  users:
+    - exclusive: true
+      regex: '@irc_pm_hackint_.*:noemis\.me'
+  aliases:
+    - exclusive: true
+      regex: '#irc_hackint_.*:noemis\.me'
+  rooms: []
+url: 'http://{{ matrix_appservice_irc_listen }}:{{ matrix_appservice_irc_port }}'
+sender_localpart: my_b
+rate_limited: false
+protocols:
+  - irc