From: Frank Brehm Date: Thu, 11 Apr 2024 13:56:13 +0000 (+0200) Subject: Defining commandline parameters for bin/get-intern-used-from-addresses X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=d03aee16e8ff1be7967a24ce2dae6a351c92f23b;p=pixelpark%2Fpp-admin-tools.git Defining commandline parameters for bin/get-intern-used-from-addresses --- diff --git a/lib/pp_admintools/app/get_from_addr.py b/lib/pp_admintools/app/get_from_addr.py index d2bab55..dc294e6 100644 --- a/lib/pp_admintools/app/get_from_addr.py +++ b/lib/pp_admintools/app/get_from_addr.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- """ -@summary: An application module for get all From-mail addresses, which are used in outbound mails +@summary: An application module for get all From-mail addresses, which are used in outbound mails. @author: Frank Brehm @contact: frank.brehm@pixelpark.com @@ -9,14 +9,19 @@ from __future__ import absolute_import # Standard modules +import ipaddress +import logging +from pathlib import Path # Third party modules +from fb_tools.common import pp # Own modules from . import BaseDPXApplication from ..handler.pflogparse import PostfixLogfileParser +from ..xlate import XLATOR -__version__ = '0.2.0' +__version__ = '0.2.1' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -27,9 +32,129 @@ ngettext = XLATOR.ngettext class GetFromAddressesApp(BaseDPXApplication): """Application class for the get-intern-used-from-addresses application.""" + show_assume_options = False show_simulate_option = False show_quiet_option = False + default_logdir = Path('/var/log') + default_logfile_pattern = 'maillog*' + + local_networks = [ + ipaddress.ip_network('10.0.0.0/8'), + ipaddress.ip_network('172.16.0.0/12'), + ipaddress.ip_network('192.168.0.0/16'), + ipaddress.ip_network('77.74.232.0/21'), + ipaddress.ip_network('93.188.104.0/21'), + ipaddress.ip_network('217.66.48.0/20'), + ] + + except_sources = [ + ipaddress.ip_address('77.74.239.66'), + ipaddress.ip_address('77.74.239.67'), + ] + + # ------------------------------------------------------------------------- + def __init__(self, appname=None, base_dir=None): + """Initialize the SetLdapPasswordApplication object.""" + self.pflogparser = PostfixLogfileParser( + appname=appname, base_dir=base_dir, initialized=True) + + self.logdir = self.default_logdir + self.logfile_pattern = self.default_logfile_pattern + self.logfiles = [] + + self.show_address_results = False + + desc1 = _( + 'This script collects all envelop-From-addresses from /var/log/maillog*, ' + 'which are used for outbound mails. ') + desc2 = _( + 'Please note, that these addresses are those from the mail envelope. They ' + 'may not be the same like the From addresses in the mail headers.') + + desc = desc1 + '\n\n' + desc2 + + super(GetFromAddressesApp, self).__init__( + appname=appname, description=desc, base_dir=base_dir, initialized=False) + + self.initialized = True + + # ------------------------------------------------------------------------- + def init_arg_parser(self): + """Initialize specific command line parameters for this application.""" + app_group = self.arg_parser.add_argument_group(_('Options for {}').format( + self.appname)) + + app_group.add_argument( + '-A', '--show-address-results', action='store_true', dest='show_address_results', + help=_('Show also results for particular mail addresses.'), + ) + + logfiles_default = str(self.logdir / self.logfile_pattern) + app_group.add_argument( + 'logfiles', nargs='*', metavar=_('FILE'), + help=_('The logfiles, which should be inspected. Default: {!r}').format( + logfiles_default), + ) + + super(GetFromAddressesApp, self).init_arg_parser() + + # ------------------------------------------------------------------------- + def post_init(self): + """Execute some steps before calling run().""" + super(GetFromAddressesApp, self).post_init() + + if self.verbose > 5: + msg = 'Given args:\n' + pp(self.args.__dict__) + LOG.debug(msg) + + self.pflogparser.verbose = self.verbose + self.pflogparser.terminal_has_colors = self.terminal_has_colors + self.pflogparser.initialized = True + + if getattr(self.args, 'show_address_results', False): + self.show_address_results = True + + if self.args.logfiles: + for lfile in self.args.logfiles: + logfile = Path(lfile) + if logfile.exists(): + if logfile.is_file(): + if logfile not in self.logfiles: + self.logfiles.append(logfile) + else: + LOG.warn(_('File {!r} is not a regular file.').format(lfile)) + else: + log_dir = logfile.parent + if not log_dir.exists(): + LOG.warn(_('Directory {!r} does not exists.').format(str(log_dir))) + elif not log_dir.is_dir(): + LOG.warn(_('Path {!r} is not a directory.').format(str(log_dir))) + else: + found_files = log_dir.glob(logfile.name) + for f in found_files: + if f.is_file(): + if f not in self.logfiles: + self.logfiles.append(f) + else: + LOG.warn(_('File {!r} is not a regular file.').format(str(f))) + + else: + found_files = self.logdir.glob(self.logfile_pattern) + for f in found_files: + if f.is_file(): + if f not in self.logfiles: + self.logfiles.append(f) + else: + LOG.warn(_('File {!r} is not a regular file.').format(str(f))) + + # ------------------------------------------------------------------------- + def _run(self): + + self.empty_line() + print(self.colored('On the run ...', 'CYAN')) + self.empty_line() + # ============================================================================= if __name__ == '__main__':