FreedomBox/actions/roundcube
Sunil Mohan Adapa 3c7bc4a192
*: pylint: Explicitly specify encoding when open a file
This is recommended by PEP-0597: https://peps.python.org/pep-0597/

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2022-07-04 19:45:57 -04:00

98 lines
3.0 KiB
Python
Executable File

#!/usr/bin/python3
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Configuration helper for Roundcube server.
"""
import argparse
import json
import pathlib
import re
from plinth import action_utils
_config_file = pathlib.Path('/etc/roundcube/freedombox-config.php')
def parse_arguments():
"""Return parsed command line arguments as dictionary."""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
subparsers.add_parser('pre-install',
help='Perform Roundcube pre-install configuration')
subparsers.add_parser('setup', help='Setup basic configuration')
subparsers.add_parser('get-config', help='Print current configuration')
subparser = subparsers.add_parser('set-config', help='Set configuration')
subparser.add_argument('--local-only', choices=['True', 'False'],
help='Set current configuration')
subparsers.required = True
return parser.parse_args()
def subcommand_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'
])
def subcommand_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')
def subcommand_get_config(_):
"""Print the current configuration as JSON."""
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'
print(json.dumps({'local_only': local_only}))
def subcommand_set_config(arguments):
"""Set the configuration."""
config = '<?php\n'
if arguments.local_only == 'True':
config = '''<?php
$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')
def main():
"""Parse arguments and perform all duties."""
arguments = parse_arguments()
subcommand = arguments.subcommand.replace('-', '_')
subcommand_method = globals()['subcommand_' + subcommand]
subcommand_method(arguments)
if __name__ == '__main__':
main()