File: privileged.py

package info (click to toggle)
freedombox 26.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,092 kB
  • sloc: python: 48,542; javascript: 1,730; xml: 481; makefile: 290; sh: 137; php: 32
file content (79 lines) | stat: -rw-r--r-- 2,288 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Configure roundcube."""

import pathlib
import re

from plinth import action_utils
from plinth.actions import privileged

_config_file = pathlib.Path('/etc/roundcube/freedombox-config.php')
_rc_db_file = pathlib.Path('/var/lib/dbconfig-common/sqlite3/'
                           'roundcube/roundcube')


@privileged
def pre_install():
    """Preseed debconf values before packages are installed."""
    action_utils.debconf_set_selections([
        'roundcube-core roundcube/dbconfig-install boolean true',
        'roundcube-core roundcube/database-type string sqlite3'
    ])


@privileged
def setup():
    """Add FreedomBox configuration and include from main configuration."""
    if not _config_file.exists():
        _config_file.write_text('<?php\n', encoding='utf-8')

    base_config = pathlib.Path('/etc/roundcube/config.inc.php')
    lines = base_config.read_text(encoding='utf-8').splitlines()
    exists = any((str(_config_file) in line for line in lines))
    if not exists:
        lines.append(f'include_once("{_config_file}");\n')
        base_config.write_text('\n'.join(lines), encoding='utf-8')


@privileged
def get_config() -> dict[str, bool]:
    """Return the current configuration as a dictionary."""
    pattern = r'\s*\$config\[\s*\'([^\']*)\'\s*\]\s*=\s*\'([^\']*)\'\s*;'
    _config = {}
    try:
        for line in _config_file.read_text(encoding='utf-8').splitlines():
            match = re.match(pattern, line)
            if match:
                _config[match.group(1)] = match.group(2)
    except FileNotFoundError:
        pass

    local_only = _config.get('default_host') == 'localhost'
    return {'local_only': local_only}


@privileged
def set_config(local_only: bool):
    """Set the configuration."""
    config = '''<?php
$config['log_driver'] = 'syslog';
'''

    if local_only:
        config += '''
$config['default_host'] = 'localhost';
$config['mail_domain'] = '%n';

$config['smtp_server'] = 'localhost';
$config['smtp_port'] = 25;
$config['smtp_helo_host'] = 'localhost';
'''

    _config_file.write_text(config, encoding='utf-8')


@privileged
def uninstall():
    """Remove config file and database."""
    for item in _config_file, _rc_db_file:
        item.unlink(missing_ok=True)