From a82d5ed28194e008b46c73554c0bf6db34959705 Mon Sep 17 00:00:00 2001 From: Hugues Morisset Date: Wed, 8 May 2019 16:26:15 +0200 Subject: [PATCH] Add tulir mautrix-facebook (https://github.com/tulir/mautrix-facebook) --- roles/matrix-synapse/defaults/main.yml | 12 ++ roles/matrix-synapse/tasks/ext/init.yml | 2 + .../tasks/ext/mautrix-facebook/init.yml | 3 + .../tasks/ext/mautrix-facebook/setup.yml | 101 ++++++++++++++++ roles/matrix-synapse/tasks/ext/setup.yml | 2 + .../ext/mautrix-facebook/config.yaml.j2 | 109 ++++++++++++++++++ .../matrix-mautrix-facebook.service.j2 | 34 ++++++ 7 files changed, 263 insertions(+) create mode 100644 roles/matrix-synapse/tasks/ext/mautrix-facebook/init.yml create mode 100644 roles/matrix-synapse/tasks/ext/mautrix-facebook/setup.yml create mode 100644 roles/matrix-synapse/templates/ext/mautrix-facebook/config.yaml.j2 create mode 100644 roles/matrix-synapse/templates/ext/mautrix-facebook/systemd/matrix-mautrix-facebook.service.j2 diff --git a/roles/matrix-synapse/defaults/main.yml b/roles/matrix-synapse/defaults/main.yml index 177cf7936..19968d5eb 100644 --- a/roles/matrix-synapse/defaults/main.yml +++ b/roles/matrix-synapse/defaults/main.yml @@ -261,6 +261,18 @@ matrix_mautrix_whatsapp_docker_image: "tulir/mautrix-whatsapp:latest" matrix_mautrix_whatsapp_base_path: "{{ matrix_base_data_path }}/mautrix-whatsapp" +# Matrix mautrix is a Matrix <-> Facebook bridge +# Enable facebook bridge +matrix_mautrix_facebook_enabled: false + +matrix_mautrix_facebook_docker_image: "tulir/mautrix-facebook:latest" + +matrix_mautrix_facebook_base_path: "{{ matrix_base_data_path }}/mautrix-facebook" + +# Get your own API keys at https://developers.facebook.com/docs/apis-and-sdks/ +matrix_mautrix_facebook_api_id: YOUR_FACEBOOK_APP_ID +matrix_mautrix_facebook_api_hash: YOUR_FACEBOOK_API_HASH + # Matrix Appservice IRC is a Matrix <-> IRC bridge # Enable IRC bridge matrix_appservice_irc_enabled: false diff --git a/roles/matrix-synapse/tasks/ext/init.yml b/roles/matrix-synapse/tasks/ext/init.yml index aa19ef6e3..9ebaf5e2c 100644 --- a/roles/matrix-synapse/tasks/ext/init.yml +++ b/roles/matrix-synapse/tasks/ext/init.yml @@ -4,6 +4,8 @@ - import_tasks: "{{ role_path }}/tasks/ext/mautrix-whatsapp/init.yml" +- import_tasks: "{{ role_path }}/tasks/ext/mautrix-facebook/init.yml" + - import_tasks: "{{ role_path }}/tasks/ext/appservice-irc/init.yml" - import_tasks: "{{ role_path }}/tasks/ext/appservice-discord/init.yml" diff --git a/roles/matrix-synapse/tasks/ext/mautrix-facebook/init.yml b/roles/matrix-synapse/tasks/ext/mautrix-facebook/init.yml new file mode 100644 index 000000000..3c23eb992 --- /dev/null +++ b/roles/matrix-synapse/tasks/ext/mautrix-facebook/init.yml @@ -0,0 +1,3 @@ +- set_fact: + matrix_systemd_services_list: "{{ matrix_systemd_services_list + ['matrix-mautrix-facebook'] }}" + when: matrix_mautrix_facebook_enabled diff --git a/roles/matrix-synapse/tasks/ext/mautrix-facebook/setup.yml b/roles/matrix-synapse/tasks/ext/mautrix-facebook/setup.yml new file mode 100644 index 000000000..2c13886ae --- /dev/null +++ b/roles/matrix-synapse/tasks/ext/mautrix-facebook/setup.yml @@ -0,0 +1,101 @@ +--- + +- name: Ensure Mautrix Facebook image is pulled + docker_image: + name: "{{ matrix_mautrix_facebook_docker_image }}" + when: "matrix_mautrix_facebook_enabled" + +- name: Ensure Mautrix Facebook configuration path exists + file: + path: "{{ matrix_mautrix_facebook_base_path }}" + state: directory + mode: 0750 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_username }}" + when: "matrix_mautrix_facebook_enabled" + +- name: Check if a mautrix-facebook configuration file exists + stat: + path: "{{ matrix_mautrix_facebook_base_path }}/config.yaml" + register: mautrix_facebook_config_file_stat + when: "matrix_mautrix_facebook_enabled" + +- name: Ensure Matrix Mautrix facebook config installed + template: + src: "{{ role_path }}/templates/ext/mautrix-facebook/config.yaml.j2" + dest: "{{ matrix_mautrix_facebook_base_path }}/config.yaml" + mode: 0644 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_username }}" + when: "matrix_mautrix_facebook_enabled and not mautrix_facebook_config_file_stat.stat.exists" + +- name: (Migration) Fix up old configuration + lineinfile: + path: "{{ matrix_mautrix_facebook_base_path }}/config.yaml" + regexp: "{{ item.regexp }}" + line: "{{ item.line }}" + backrefs: yes + with_items: + - {'regexp': '^(\s+)filename: \./mautrix-facebook.log', 'line': '\1filename: /data/mautrix-facebook.log'} + - {'regexp': '^(\s+)database:', 'line': '\1database: sqlite:////data/mautrix-facebook.db'} + when: "matrix_mautrix_facebook_enabled and mautrix_facebook_config_file_stat.stat.exists" + +- name: Ensure matrix-mautrix-facebook.service installed + template: + src: "{{ role_path }}/templates/ext/mautrix-facebook/systemd/matrix-mautrix-facebook.service.j2" + dest: "/etc/systemd/system/matrix-mautrix-facebook.service" + mode: 0644 + register: matrix_mautrix_facebook_systemd_service_result + when: "matrix_mautrix_facebook_enabled" + +- name: Ensure systemd reloaded after matrix-mautrix-facebook.service installation + service: + daemon_reload: yes + when: "matrix_mautrix_facebook_enabled and matrix_mautrix_facebook_systemd_service_result.changed" + +- name: Check if a mautrix-facebook registration file exists + stat: + path: "{{ matrix_mautrix_facebook_base_path }}/registration.yaml" + register: mautrix_facebook_registration_file_stat + when: "matrix_mautrix_facebook_enabled" + +- name: Generate matrix-mautrix-facebook registration.yaml if it doesn't exist + shell: + cmd: >- + /usr/bin/docker run + --rm + --user={{ matrix_user_uid }}:{{ matrix_user_gid }} + --cap-drop=ALL + --name matrix-mautrix-facebook-gen + -v {{ matrix_mautrix_facebook_base_path }}:/data:z + {{ matrix_mautrix_facebook_docker_image }} + python3 -m mautrix_facebook -g -c /data/config.yaml -r /data/registration.yaml + when: "matrix_mautrix_facebook_enabled and not mautrix_facebook_registration_file_stat.stat.exists" + +- set_fact: + matrix_synapse_app_service_config_file_mautrix_facebook: '/app-registration/mautrix-facebook.yml' + when: "matrix_mautrix_facebook_enabled" + +- set_fact: + matrix_synapse_container_additional_volumes: > + {{ matrix_synapse_container_additional_volumes }} + + + {{ [{'src': '{{ matrix_mautrix_facebook_base_path }}/registration.yaml', 'dst': '{{ matrix_synapse_app_service_config_file_mautrix_facebook }}', 'options': 'ro'}] }} + when: "matrix_mautrix_facebook_enabled" + +- set_fact: + matrix_synapse_app_service_config_files: > + {{ matrix_synapse_app_service_config_files }} + + + {{ ["{{ matrix_synapse_app_service_config_file_mautrix_facebook }}"] | to_nice_json }} + when: "matrix_mautrix_facebook_enabled" + +# +# Tasks related to getting rid of matrix-mautrix-facebook (if it was previously enabled) +# + +- name: Ensure matrix-mautrix-facebook.service doesn't exist + file: + path: "/etc/systemd/system/matrix-mautrix-facebook.service" + state: absent + when: "not matrix_mautrix_facebook_enabled" diff --git a/roles/matrix-synapse/tasks/ext/setup.yml b/roles/matrix-synapse/tasks/ext/setup.yml index fe113281b..3b2a61ce3 100644 --- a/roles/matrix-synapse/tasks/ext/setup.yml +++ b/roles/matrix-synapse/tasks/ext/setup.yml @@ -10,6 +10,8 @@ - import_tasks: "{{ role_path }}/tasks/ext/mautrix-whatsapp/setup.yml" +- import_tasks: "{{ role_path }}/tasks/ext/mautrix-facebook/setup.yml" + - import_tasks: "{{ role_path }}/tasks/ext/appservice-irc/setup.yml" - import_tasks: "{{ role_path }}/tasks/ext/appservice-discord/setup.yml" diff --git a/roles/matrix-synapse/templates/ext/mautrix-facebook/config.yaml.j2 b/roles/matrix-synapse/templates/ext/mautrix-facebook/config.yaml.j2 new file mode 100644 index 000000000..714449f6e --- /dev/null +++ b/roles/matrix-synapse/templates/ext/mautrix-facebook/config.yaml.j2 @@ -0,0 +1,109 @@ +#jinja2: lstrip_blocks: "True" +# Homeserver details +homeserver: + # The address that this appservice can use to connect to the homeserver. + address: https://{{ matrix_server_fqn_matrix }} + # The domain of the homeserver (for MXIDs, etc). + domain: {{ matrix_domain }} + # Whether or not to verify the SSL certificate of the homeserver. + # Only applies if address starts with https:// + verify_ssl: true + +# Application service host/registration related details +# Changing these values requires regeneration of the registration. +appservice: + # The address that the homeserver can use to connect to this appservice. + address: http://matrix-mautrix-facebook:8080 + + # The hostname and port where this appservice should listen. + hostname: 0.0.0.0 + port: 8080 + # The maximum body size of appservice API requests (from the homeserver) in mebibytes + # Usually 1 is enough, but on high-traffic bridges you might need to increase this to avoid 413s + max_body_size: 1 + + # The full URI to the database. SQLite and Postgres are fully supported. + # Other DBMSes supported by SQLAlchemy may or may not work. + # Format examples: + # SQLite: sqlite:///filename.db + # Postgres: postgres://username:password@hostname/dbname + database: sqlite:////data/mautrix-facebook.db + + # The unique ID of this appservice. + id: facebook + # Username of the appservice bot. + bot_username: facebookbot + # Display name and avatar for bot. Set to "remove" to remove display name/avatar, leave empty + # to leave display name/avatar as-is. + bot_displayname: Facebook bridge bot + bot_avatar: mxc://maunium.net/ddtNPZSKMNqaUzqrHuWvUADv + + # Authentication tokens for AS <-> HS communication. Autogenerated; do not modify. + as_token: "This value is generated when generating the registration" + hs_token: "This value is generated when generating the registration" + +# Bridge config +bridge: + # Localpart template of MXIDs for Facebook users. + # {userid} is replaced with the user ID of the Facebook user. + username_template: "facebook_{userid}" + + # The prefix for commands. Only required in non-management rooms. + command_prefix: "!fb" + + # Number of chats to sync (and create portals for) on startup/login. + # Maximum 20, set 0 to disable automatic syncing. + initial_chat_sync: 10 + # Whether or not the Facebook users of logged in Matrix users should be + # invited to private chats when the user sends a message from another client. + invite_own_puppet_to_pm: false + # Whether or not to use /sync to get presence, read receipts and typing notifications when using + # your own Matrix account as the Matrix puppet for your Facebook account. + sync_with_custom_puppets: true + # Whether or not to bridge presence in both directions. Facebook allows users not to broadcast + # presence, but then it won't send other users' presence to the client. + presence: true + + # Permissions for using the bridge. + # Permitted values: + # user - Use the bridge with puppeting. + # admin - Use and administrate the bridge. + # Permitted keys: + # * - All Matrix users + # domain - All users on that homeserver + # mxid - Specific user + permissions: + '{{ matrix_domain }}': user + +# Python logging configuration. +# +# See section 16.7.2 of the Python documentation for more info: +# https://docs.python.org/3.6/library/logging.config.html#configuration-dictionary-schema +logging: + version: 1 + formatters: + colored: + (): mautrix_facebook.util.ColorFormatter + format: "[%(asctime)s] [%(levelname)s@%(name)s] %(message)s" + normal: + format: "[%(asctime)s] [%(levelname)s@%(name)s] %(message)s" + handlers: + file: + class: logging.handlers.RotatingFileHandler + formatter: normal + filename: /data/mautrix-facebook.log + maxBytes: 10485760 + backupCount: 10 + console: + class: logging.StreamHandler + formatter: colored + loggers: + mau: + level: DEBUG + fbchat: + level: DEBUG + aiohttp: + level: INFO + root: + level: DEBUG + handlers: [file, console] diff --git a/roles/matrix-synapse/templates/ext/mautrix-facebook/systemd/matrix-mautrix-facebook.service.j2 b/roles/matrix-synapse/templates/ext/mautrix-facebook/systemd/matrix-mautrix-facebook.service.j2 new file mode 100644 index 000000000..ec5ffadb7 --- /dev/null +++ b/roles/matrix-synapse/templates/ext/mautrix-facebook/systemd/matrix-mautrix-facebook.service.j2 @@ -0,0 +1,34 @@ +#jinja2: lstrip_blocks: "True" +[Unit] +Description=Matrix Mautrix Facebook server +After=docker.service +Requires=docker.service +Requires=matrix-synapse.service +After=matrix-synapse.service + +[Service] +Type=simple +ExecStartPre=-/usr/bin/docker kill matrix-mautrix-facebook +ExecStartPre=-/usr/bin/docker rm matrix-mautrix-facebook +ExecStartPre=/usr/bin/docker run --rm --name matrix-mautrix-facebook-db \ + --log-driver=none \ + --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ + --cap-drop=ALL \ + -v {{ matrix_mautrix_facebook_base_path }}:/data:z \ + {{ matrix_mautrix_facebook_docker_image }} \ + alembic -x config=/data/config.yaml upgrade head +ExecStart=/usr/bin/docker run --rm --name matrix-mautrix-facebook \ + --log-driver=none \ + --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ + --cap-drop=ALL \ + --network={{ matrix_docker_network }} \ + -v {{ matrix_mautrix_facebook_base_path }}:/data:z \ + {{ matrix_mautrix_facebook_docker_image }} \ + python3 -m mautrix_facebook -c /data/config.yaml +ExecStop=-/usr/bin/docker kill matrix-mautrix-facebook +ExecStop=-/usr/bin/docker rm matrix-mautrix-facebook +Restart=always +RestartSec=30 + +[Install] +WantedBy=multi-user.target