import datetime
import copy
import json
+import socket
+import pwd
+
+from email import encoders
+from email.mime.text import MIMEText
+from email import charset
+
+import smtplib
# Third party modules
import six
from .app import PpApplication
-__version__ = '0.5.2'
+__version__ = '0.5.3'
LOG = logging.getLogger(__name__)
VALID_MAIL_METHODS = ('smtp', 'sendmail')
default_mail_server = 'mx.pixelpark.net'
- whitespace_re = re.compile(r'(?:[,;]+|\s*[,;]*\s+)')
+ current_user_name = pwd.getpwuid(os.getuid()).pw_name
+ current_user_gecos = pwd.getpwuid(os.getuid()).pw_gecos
+ default_mail_from = MailAddress(current_user_name, socket.getfqdn())
+
+ whitespace_re = re.compile(r'(?:[,;]+|\s*[,;]*\s+)+')
+
+ charset.add_charset('utf-8', charset.SHORTEST, charset.QP)
# -------------------------------------------------------------------------
def __init__(
self.log_cfg_files = []
self.mail_recipients = copy.copy(self.default_mail_recipients)
+ self.mail_from = '{n} <{m}>'.format(
+ n=self.current_user_gecos, m=self.default_mail_from)
self.mail_cc = copy.copy(self.default_mail_cc)
self.reply_to = self.default_reply_to
- self.mail_server = self.default_mail_server
self.mail_method = 'smtp'
+ self.mail_server = self.default_mail_server
+ self.smtp_port = 25
super(PpConfigApplication, self).__init__(
appname=appname, verbose=verbose, version=version, base_dir=base_dir,
self.mail_cc = []
if v:
tokens = self.whitespace_re.split(v)
+ if self.verbose > 1:
+ LOG.debug("CC addresses:\n{}".format(pp(tokens)))
for token in tokens:
if MailAddress.valid_address(token):
if token not in self.mail_cc:
tokens[0])
LOG.error(msg)
- if 'mail_server' in section:
- v = section['reply_to'].strip()
- if v:
- self.mail_server = v
-
if 'mail_method' in section:
v = section['mail_method'].strip().lower()
if v:
section['mail_method'])
LOG.error(msg)
+ if 'mail_server' in section:
+ v = section['reply_to'].strip()
+ if v:
+ self.mail_server = v
+
+ if 'smtp_port' in section:
+ v = section['smtp_port']
+ port = self.smtp_port
+ try:
+ port = int(v)
+ except (ValueError, TypeError) as e:
+ msg = "Found invalid SMTP port number {!r} in configuration.".format(v)
+ LOG.error(msg)
+ if port <= 0:
+ msg = "Found invalid SMTP port number {!r} in configuration.".format(port)
+ LOG.error(msg)
+ self.smtp_port = port
+
self.perform_config()
# -------------------------------------------------------------------------
pass
+ # -------------------------------------------------------------------------
+ def send_mail(self, subject, body):
+
+ xmailer = "{a} (Admin Tools version {v})".format(
+ a=self.appname, v=__global_version__)
+
+ mail = MIMEText(body, 'plain', 'utf-8')
+ mail['Subject'] = subject
+ mail['From'] = self.mail_from
+ mail['To'] = ', '.join(self.mail_recipients)
+ mail['Reply-To'] = self.reply_to
+ mail['X-Mailer'] = xmailer
+ if self.mail_cc:
+ mail['Cc'] = ', '.join(self.mail_cc)
+
+ if self.verbose > 1:
+ LOG.debug("Mail to send:\n{}".format(mail.as_string(unixfrom=True)))
+
# -------------------------------------------------------------------------
def post_init(self):
"""
from .cfg_app import PpCfgAppError, PpConfigApplication
-__version__ = '0.4.2'
+__version__ = '0.4.3'
LOG = logging.getLogger(__name__)
self.read_exclude_dirs()
self.read_passwd_homes()
self.check_homes()
+ self.send_results()
# -------------------------------------------------------------------------
def read_exclude_dirs(self):
self.unnecessary_dirs.sort(key=str.lower)
+ # -------------------------------------------------------------------------
+ def send_results(self):
+
+ if not self.unnecessary_dirs:
+ LOG.debug("No unnecessary home directories, nothing to inform.")
+ return
+
+ subject = 'Nicht benötigte Home-Verzeichnisse'
+ body = textwrap.dedent('''\
+ Die folgenden Home-Verzeichnisse befinden sich weder
+ in der lokalen passwd-Datei, im LDAP oder in der exclude-Liste.
+ Sie können damit archiviert und gelöscht werden.''')
+ body += '\n\n'
+ for home in self.unnecessary_dirs:
+ body += ' - ' + home + '\n'
+
+ self.send_mail(subject, body)
+
+
# =============================================================================
if __name__ == "__main__":