mirror of
https://github.com/fscotto/infra.git
synced 2026-11-26 13:49:52 +00:00
Preserve Thunderbird profiles and add OpenCode Mobile support
This commit is contained in:
144
ansible/roles/profile_desktop_gnome/files/thunderbird_profile.py
Normal file
144
ansible/roles/profile_desktop_gnome/files/thunderbird_profile.py
Normal file
@@ -0,0 +1,144 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Read-only discovery and registry plan; never read stored login credentials."""
|
||||
|
||||
import configparser
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
|
||||
def read_ini(path):
|
||||
result = configparser.RawConfigParser()
|
||||
result.optionxform = str
|
||||
if path.exists():
|
||||
with path.open() as source:
|
||||
result.read_file(source)
|
||||
return result
|
||||
|
||||
|
||||
def preferences(path):
|
||||
if not path.exists():
|
||||
return {}
|
||||
result = {}
|
||||
for key, value in re.findall(
|
||||
r'^\s*user_pref\("([^"\n]+)",\s*(.*?)\);',
|
||||
path.read_text(), re.MULTILINE,
|
||||
):
|
||||
try:
|
||||
result[key] = json.loads(value)
|
||||
except ValueError:
|
||||
continue
|
||||
return result
|
||||
|
||||
|
||||
def has_account(path):
|
||||
# user.js alone is only a bootstrap, not an account used by Thunderbird.
|
||||
prefs = preferences(path / 'prefs.js')
|
||||
for account in str(prefs.get('mail.accountmanager.accounts', '')).split(','):
|
||||
server = prefs.get('mail.account.' + account.strip() + '.server')
|
||||
prefix = 'mail.server.' + str(server) + '.'
|
||||
if (prefs.get(prefix + 'type') in ('imap', 'pop3', 'nntp')
|
||||
and prefs.get(prefix + 'hostname')):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def profile_path(root, registry, section):
|
||||
value = registry.get(section, 'Path')
|
||||
return (root / value if registry.get(section, 'IsRelative', fallback='1') == '1'
|
||||
else Path(value)).resolve()
|
||||
|
||||
|
||||
def render(registry):
|
||||
output = io.StringIO()
|
||||
registry.write(output, space_around_delimiters=False)
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
def plan(home, profile_dir, profile_name):
|
||||
home = Path(home)
|
||||
# A legacy directory takes precedence over the XDG registry on this build.
|
||||
roots = [home / '.thunderbird', home / '.config/thunderbird']
|
||||
root = next((p for p in roots if p.exists()), roots[1])
|
||||
registries = {p: read_ini(p / 'profiles.ini') for p in roots}
|
||||
candidates = []
|
||||
install_ids = set()
|
||||
for base, registry in registries.items():
|
||||
installs = read_ini(base / 'installs.ini')
|
||||
defaults = {registry.get(s, 'Default', fallback='')
|
||||
for s in registry.sections() if s.startswith('Install')}
|
||||
defaults.update(installs.get(s, 'Default', fallback='')
|
||||
for s in installs.sections())
|
||||
for section in registry.sections():
|
||||
if not re.fullmatch(r'Profile\d+', section):
|
||||
continue
|
||||
path = profile_path(base, registry, section)
|
||||
if has_account(path):
|
||||
candidates.append((registry.get(section, 'Path') not in defaults,
|
||||
base != root, str(path), path))
|
||||
# Reuse installation associations, including those in the other root.
|
||||
install_ids.update(s[7:] for s in registry.sections() if s.startswith('Install'))
|
||||
install_ids.update(installs.sections())
|
||||
# Also find unregistered profiles, without following arbitrary subtrees.
|
||||
if base.exists():
|
||||
for child in sorted(base.iterdir()):
|
||||
if child.is_dir() and has_account(child):
|
||||
candidates.append((True, base != root, str(child.resolve()), child.resolve()))
|
||||
existing = bool(candidates)
|
||||
selected = min(candidates)[3] if existing else root / profile_dir
|
||||
if not existing:
|
||||
number = 0
|
||||
while selected.exists():
|
||||
bootstrap = selected / 'user.js'
|
||||
if (not (selected / 'prefs.js').exists() and bootstrap.exists()
|
||||
and bootstrap.read_text().startswith('// Managed by Ansible from profile_desktop_gnome')):
|
||||
break
|
||||
number += 1
|
||||
selected = root / (profile_dir + '-' + str(number))
|
||||
registry = registries[root]
|
||||
section = next((s for s in registry.sections()
|
||||
if re.fullmatch(r'Profile\d+', s)
|
||||
and profile_path(root, registry, s) == selected), None)
|
||||
if section is None:
|
||||
number = 0
|
||||
while registry.has_section('Profile' + str(number)):
|
||||
number += 1
|
||||
section = 'Profile' + str(number)
|
||||
registry.add_section(section)
|
||||
registry.set(section, 'Name', profile_name if not existing else selected.name)
|
||||
registry.set(section, 'IsRelative', '0')
|
||||
registry.set(section, 'Path', str(selected))
|
||||
for other in registry.sections():
|
||||
if re.fullmatch(r'Profile\d+', other):
|
||||
registry.remove_option(other, 'Default')
|
||||
registry.set(section, 'Default', '1')
|
||||
if not registry.has_section('General'):
|
||||
registry.add_section('General')
|
||||
registry.set('General', 'StartWithLastProfile', '1')
|
||||
registry.set('General', 'Version', '2')
|
||||
value = registry.get(section, 'Path')
|
||||
installs = read_ini(root / 'installs.ini')
|
||||
for install in sorted(install_ids):
|
||||
# Do not redirect a separate installation with another configured account.
|
||||
defaults = []
|
||||
for base, source in registries.items():
|
||||
default = source.get('Install' + install, 'Default', fallback='')
|
||||
if not default:
|
||||
default = read_ini(base / 'installs.ini').get(install, 'Default', fallback='')
|
||||
if default:
|
||||
defaults.append((base / default).resolve())
|
||||
if any(p != selected and has_account(p) for p in defaults):
|
||||
continue
|
||||
for target, name in ((registry, 'Install' + install), (installs, install)):
|
||||
if not target.has_section(name):
|
||||
target.add_section(name)
|
||||
target.set(name, 'Default', value)
|
||||
target.set(name, 'Locked', '1')
|
||||
return dict(root=str(root), profile=str(selected), existing_account=existing,
|
||||
profiles_ini=render(registry), installs_ini=render(installs))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(json.dumps(plan(*sys.argv[1:])))
|
||||
@@ -1,52 +1,126 @@
|
||||
---
|
||||
- name: Require the iCloud address for the managed Thunderbird profile
|
||||
- name: Inspect existing Thunderbird profiles without changing them
|
||||
tags: [thunderbird]
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- "{{ ansible_facts.python.executable }}"
|
||||
- -c
|
||||
- "{{ lookup('ansible.builtin.file', role_path + '/files/thunderbird_profile.py') }}"
|
||||
- "{{ user_home }}"
|
||||
- "{{ thunderbird_profile_dir }}"
|
||||
- "{{ thunderbird_profile_name }}"
|
||||
become: true
|
||||
become_user: "{{ username }}"
|
||||
register: thunderbird_profile_probe
|
||||
changed_when: false
|
||||
check_mode: false
|
||||
|
||||
- name: Select an existing account or plan an iCloud bootstrap
|
||||
tags: [thunderbird]
|
||||
ansible.builtin.set_fact:
|
||||
thunderbird_selected: "{{ thunderbird_profile_probe.stdout | from_json }}"
|
||||
|
||||
- name: Require the iCloud address only when no configured account exists
|
||||
tags: [thunderbird]
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- vault_icloud_email is defined
|
||||
- vault_icloud_email | length > 0
|
||||
- vault_icloud_email != 'REPLACE_ME'
|
||||
- vault_icloud_email is match('^[^@\s]+@[^@\s]+$')
|
||||
- vault_icloud_email is match('^[^@\\s]+@[^@\\s]+$')
|
||||
fail_msg: >-
|
||||
vault_icloud_email must hold the iCloud mail address before the
|
||||
vault_icloud_email must hold the iCloud mail address before a new
|
||||
Thunderbird profile can be provisioned.
|
||||
quiet: true
|
||||
when: not thunderbird_selected.existing_account
|
||||
|
||||
- name: Create the Thunderbird configuration directory
|
||||
- name: Check whether Thunderbird is running
|
||||
tags: [thunderbird]
|
||||
ansible.builtin.command:
|
||||
argv: [pgrep, -u, "{{ username }}", -x, thunderbird]
|
||||
register: thunderbird_running
|
||||
changed_when: false
|
||||
failed_when: thunderbird_running.rc not in [0, 1]
|
||||
check_mode: false
|
||||
|
||||
- name: Require Thunderbird to be closed before selecting its profile
|
||||
tags: [thunderbird]
|
||||
ansible.builtin.assert:
|
||||
that: thunderbird_running.rc == 1
|
||||
fail_msg: Close Thunderbird before running the thunderbird tag; it is never stopped automatically.
|
||||
|
||||
- name: Create the selected Thunderbird registry directory
|
||||
tags: [thunderbird]
|
||||
ansible.builtin.file:
|
||||
path: "{{ user_home }}/.thunderbird"
|
||||
path: "{{ thunderbird_selected.root }}"
|
||||
state: directory
|
||||
owner: "{{ username }}"
|
||||
group: "{{ user_group }}"
|
||||
mode: "0700"
|
||||
|
||||
- name: Create the managed Thunderbird profile directory
|
||||
- name: Create a new profile only when no configured account exists
|
||||
tags: [thunderbird]
|
||||
ansible.builtin.file:
|
||||
path: "{{ user_home }}/.thunderbird/{{ thunderbird_profile_dir }}"
|
||||
path: "{{ thunderbird_selected.profile }}"
|
||||
state: directory
|
||||
owner: "{{ username }}"
|
||||
group: "{{ user_group }}"
|
||||
mode: "0700"
|
||||
when: not thunderbird_selected.existing_account
|
||||
|
||||
- name: Seed the Thunderbird profile registry when absent
|
||||
tags: [thunderbird]
|
||||
ansible.builtin.template:
|
||||
src: thunderbird/profiles.ini.j2
|
||||
dest: "{{ user_home }}/.thunderbird/profiles.ini"
|
||||
owner: "{{ username }}"
|
||||
group: "{{ user_group }}"
|
||||
mode: "0600"
|
||||
# Never rewrite a profile list Thunderbird has already taken over.
|
||||
force: false
|
||||
|
||||
- name: Render the managed Thunderbird account defaults
|
||||
- name: Seed iCloud defaults only in a profile without a configured account
|
||||
tags: [thunderbird]
|
||||
ansible.builtin.template:
|
||||
src: thunderbird/user.js.j2
|
||||
dest: "{{ user_home }}/.thunderbird/{{ thunderbird_profile_dir }}/user.js"
|
||||
dest: "{{ thunderbird_selected.profile }}/user.js"
|
||||
owner: "{{ username }}"
|
||||
group: "{{ user_group }}"
|
||||
mode: "0600"
|
||||
force: false
|
||||
no_log: true
|
||||
when: not thunderbird_selected.existing_account
|
||||
|
||||
- name: Discover the Thunderbird installation directory for first-start profile adoption
|
||||
tags: [thunderbird]
|
||||
ansible.builtin.stat:
|
||||
path: "{{ item }}/application.ini"
|
||||
loop: [/usr/lib64/thunderbird, /usr/lib/thunderbird]
|
||||
register: thunderbird_installation
|
||||
when: not thunderbird_selected.existing_account
|
||||
|
||||
- name: Seed compatibility information so Thunderbird adopts the new default profile
|
||||
tags: [thunderbird]
|
||||
ansible.builtin.copy:
|
||||
content: |
|
||||
[Compatibility]
|
||||
LastPlatformDir={{ item.item }}
|
||||
LastAppDir={{ item.item }}
|
||||
dest: "{{ thunderbird_selected.profile }}/compatibility.ini"
|
||||
owner: "{{ username }}"
|
||||
group: "{{ user_group }}"
|
||||
mode: "0600"
|
||||
force: false
|
||||
loop: "{{ thunderbird_installation.results }}"
|
||||
loop_control:
|
||||
label: "{{ item.item }}"
|
||||
when:
|
||||
- not thunderbird_selected.existing_account
|
||||
- item.stat.exists | default(false)
|
||||
|
||||
- name: Select the profile while preserving other registered profiles
|
||||
tags: [thunderbird]
|
||||
ansible.builtin.copy:
|
||||
content: "{{ item.content }}"
|
||||
dest: "{{ thunderbird_selected.root }}/{{ item.name }}"
|
||||
owner: "{{ username }}"
|
||||
group: "{{ user_group }}"
|
||||
mode: "0600"
|
||||
backup: true
|
||||
loop:
|
||||
- name: profiles.ini
|
||||
content: "{{ thunderbird_selected.profiles_ini }}"
|
||||
- name: installs.ini
|
||||
content: "{{ thunderbird_selected.installs_ini }}"
|
||||
loop_control:
|
||||
label: "{{ item.name }}"
|
||||
when: item.content | length > 0
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
[Profile0]
|
||||
Name={{ thunderbird_profile_name }}
|
||||
IsRelative=1
|
||||
Path={{ thunderbird_profile_dir }}
|
||||
Default=1
|
||||
|
||||
[General]
|
||||
StartWithLastProfile=1
|
||||
Version=2
|
||||
@@ -1,6 +1,7 @@
|
||||
// Managed by Ansible from profile_desktop_gnome (tag: thunderbird).
|
||||
// Thunderbird rewrites prefs.js on exit, so the declared defaults live here
|
||||
// and are re-applied at every start. Do not edit this file by hand.
|
||||
// and are re-applied at every start. Seeded only when no account exists;
|
||||
// Ansible does not overwrite this file or configure an existing account.
|
||||
//
|
||||
// The account password is intentionally absent: iCloud mail requires an
|
||||
// app-specific password, which Thunderbird prompts for and stores in its own
|
||||
@@ -20,7 +21,7 @@ user_pref("mail.server.server1.authMethod", 3);
|
||||
user_pref("mail.server.server1.userName", "{{ thunderbird_icloud_email }}");
|
||||
user_pref("mail.server.server1.name", "{{ thunderbird_icloud_account_name }}");
|
||||
user_pref("mail.server.server1.directory-rel", "[ProfD]ImapMail/{{ thunderbird_icloud_imap_host }}");
|
||||
user_pref("mail.server.server1.directory", "{{ user_home }}/.thunderbird/{{ thunderbird_profile_dir }}/ImapMail/{{ thunderbird_icloud_imap_host }}");
|
||||
user_pref("mail.server.server1.directory", "{{ thunderbird_selected.profile }}/ImapMail/{{ thunderbird_icloud_imap_host }}");
|
||||
user_pref("mail.server.server1.max_cached_connections", 5);
|
||||
user_pref("mail.server.server1.check_new_mail", true);
|
||||
user_pref("mail.server.server1.check_time", 10);
|
||||
|
||||
Reference in New Issue
Block a user