color: add ColoredFormatter for more transparent coloring.
[pygrader.git] / pygrader / email.py
index 3839e51fc828524c70713e244e4cfb54c211f2c4..4fbf70a33f955315e150266df398b0a51a291069 100644 (file)
@@ -19,6 +19,8 @@ from __future__ import absolute_import
 
 from email.header import Header as _Header
 from email.header import decode_header as _decode_header
+from email.mime.message import MIMEMessage as _MIMEMessage
+from email.mime.multipart import MIMEMultipart as _MIMEMultipart
 import email.utils as _email_utils
 import logging as _logging
 import smtplib as _smtplib
@@ -27,9 +29,6 @@ import pgp_mime as _pgp_mime
 
 from . import ENCODING as _ENCODING
 from . import LOG as _LOG
-from .color import standard_colors as _standard_colors
-from .color import color_string as _color_string
-from .color import write_color as _write_color
 from .model.person import Person as _Person
 
 
@@ -47,8 +46,7 @@ def test_smtp(smtp, author, targets, msg=None):
     smtp.send_message(msg=msg)
 test_smtp.__test__ = False  # not a test for nose
 
-def send_emails(emails, smtp=None, use_color=None, debug_target=None,
-                dry_run=False):
+def send_emails(emails, smtp=None, debug_target=None, dry_run=False):
     """Iterate through `emails` and mail them off one-by-one
 
     >>> from email.mime.text import MIMEText
@@ -62,15 +60,12 @@ def send_emails(emails, smtp=None, use_color=None, debug_target=None,
     ...     emails.append(
     ...         (msg,
     ...          lambda status: stdout.write('SUCCESS: {}\\n'.format(status))))
-    >>> send_emails(emails, use_color=False, dry_run=True)
+    >>> send_emails(emails, dry_run=True)
     ... # doctest: +REPORT_UDIFF, +NORMALIZE_WHITESPACE
-    sending message to ['Moneypenny <mp@sis.gov.uk>', 'James Bond <007@sis.gov.uk>']...\tDRY-RUN
     SUCCESS: None
-    sending message to ['M <m@sis.gov.uk>', 'James Bond <007@sis.gov.uk>']...\tDRY-RUN
     SUCCESS: None
     """
     local_smtp = smtp is None
-    highlight,lowlight,good,bad = _standard_colors(use_color=use_color)
     for msg,callback in emails:
         sources = [
             _email_utils.formataddr(a) for a in _pgp_mime.email_sources(msg)]
@@ -82,10 +77,8 @@ def send_emails(emails, smtp=None, use_color=None, debug_target=None,
             # TODO: remove convert_content_transfer_encoding?
             #if msg.get('content-transfer-encoding', None) == 'base64':
             #    convert_content_transfer_encoding(msg, '8bit')
-            _LOG.debug(_color_string(
-                    '\n{}\n'.format(msg.as_string()), color=lowlight))
-        _write_color('sending message to {}...'.format(targets),
-                     color=highlight)
+            _LOG.debug('\n{}\n'.format(msg.as_string()))
+        _LOG.info('sending message to {}...'.format(targets))
         if not dry_run:
             try:
                 if local_smtp:
@@ -96,19 +89,31 @@ def send_emails(emails, smtp=None, use_color=None, debug_target=None,
                 if local_smtp:
                     smtp.quit()
             except:
-                _write_color('\tFAILED\n', bad)
+                _LOG.warning('failed to send message to {}'.format(targets))
                 if callback:
                     callback(False)
                 raise
             else:
-                _write_color('\tOK\n', good)
+                _LOG.info('sent message to {}'.format(targets))
                 if callback:
                     callback(True)
         else:
-            _write_color('\tDRY-RUN\n', good)
+            _LOG.info('dry run, so no message sent to {}'.format(targets))
             if callback:
                 callback(None)
 
+
+class Responder (object):
+    def __init__(self, *args, **kwargs):
+        self.args = args
+        if kwargs is None:
+            kwargs = {}
+        self.kwargs = kwargs
+
+    def __call__(self, message):
+        send_emails([(message, None)], *self.args, **self.kwargs)
+
+
 def get_address(person, header=False):
     r"""
     >>> from pygrader.model.person import Person as Person
@@ -150,14 +155,60 @@ def get_address(person, header=False):
         return _email_utils.formataddr((name, person.emails[0]))
     return _email_utils.formataddr((person.name, person.emails[0]))
 
-def construct_email(author, targets, subject, text, cc=None):
+def construct_email(author, targets, subject, message, cc=None):
+    if author.pgp_key:
+        signers = [author.pgp_key]
+    else:
+        signers = []
+    recipients = [p.pgp_key for p in targets if p.pgp_key]
+    encrypt = True
+    for person in targets:
+        if not person.pgp_key:
+            encrypt = False  # cannot encrypt to every recipient
+            break
+    if cc:
+        recipients.extend([p.pgp_key for p in cc if p.pgp_key])
+        for person in cc:
+            if not person.pgp_key:
+                encrypt = False
+                break
+    if not recipients:
+        encrypt = False  # noone to encrypt to
+    if signers and encrypt:
+        if author.pgp_key not in recipients:
+            recipients.append(author.pgp_key)
+        message = _pgp_mime.sign_and_encrypt(
+            message=message, signers=signers, recipients=recipients,
+            always_trust=True)
+    elif signers:
+        message = _pgp_mime.sign(message=message, signers=signers)
+    elif encrypt:
+        message = _pgp_mime.encrypt(message=message, recipients=recipients)
+
+    message['Date'] = _email_utils.formatdate()
+    message['From'] = get_address(author, header=True)
+    message['Reply-to'] = message['From']
+    message['To'] = ', '.join(
+        get_address(target, header=True) for target in targets)
+    if cc:
+        message['Cc'] = ', '.join(
+            get_address(target, header=True) for target in cc)
+    subject_encoding = _pgp_mime.guess_encoding(subject)
+    if subject_encoding == 'us-ascii':
+        message['Subject'] = subject
+    else:
+        message['Subject'] = _Header(subject, subject_encoding)
+
+    return message
+
+def construct_text_email(author, targets, subject, text, cc=None):
     r"""Build a text/plain email using `Person` instances
 
     >>> from pygrader.model.person import Person as Person
     >>> author = Person(name='Джон Доу', emails=['jdoe@a.gov.ru'])
     >>> targets = [Person(name='Jill', emails=['c@d.net'])]
     >>> cc = [Person(name='H.D.', emails=['hd@wall.net'])]
-    >>> msg = construct_email(author, targets, cc=cc,
+    >>> msg = construct_text_email(author, targets, cc=cc,
     ...     subject='Once upon a time', text='Bla bla bla...')
     >>> print(msg.as_string())  # doctest: +REPORT_UDIFF, +ELLIPSIS
     Content-Type: text/plain; charset="us-ascii"
@@ -175,7 +226,7 @@ def construct_email(author, targets, subject, text, cc=None):
 
     With unicode text:
 
-    >>> msg = construct_email(author, targets, cc=cc,
+    >>> msg = construct_text_email(author, targets, cc=cc,
     ...     subject='Once upon a time', text='Funky ✉.')
     >>> print(msg.as_string())  # doctest: +REPORT_UDIFF, +ELLIPSIS
     Content-Type: text/plain; charset="utf-8"
@@ -192,35 +243,59 @@ def construct_email(author, targets, subject, text, cc=None):
     RnVua3kg4pyJLg==
     <BLANKLINE>
     """
-    msg = _pgp_mime.encodedMIMEText(text)
-    if author.pgp_key:
-        signers = [author.pgp_key]
-    else:
-        signers = []
-    recipients = [p.pgp_key for p in targets if p.pgp_key]
-    if signers and recipients:
-        if author.pgp_key not in recipients:
-            recipients.append(author.pgp_key)
-        msg = _pgp_mime.sign_and_encrypt(
-            message=msg, signers=signers, recipients=recipients,
-            always_trust=True)
-    elif signers:
-        msg = _pgp_mime.sign(message=msg, signers=signers)
-    elif recipients:
-        msg = _pgp_mime.encrypt(message=msg, recipients=recipients)
-
-    msg['Date'] = _email_utils.formatdate()
-    msg['From'] = get_address(author, header=True)
-    msg['Reply-to'] = msg['From']
-    msg['To'] = ', '.join(
-        get_address(target, header=True) for target in targets)
-    if cc:
-        msg['Cc'] = ', '.join(
-            get_address(target, header=True) for target in cc)
-    subject_encoding = _pgp_mime.guess_encoding(subject)
-    if subject_encoding == 'us-ascii':
-        msg['Subject'] = subject
-    else:
-        msg['Subject'] = _Header(subject, subject_encoding)
+    message = _pgp_mime.encodedMIMEText(text)
+    return construct_email(
+        author=author, targets=targets, subject=subject, message=message,
+        cc=cc)
+
+def construct_response(author, targets, subject, text, original, cc=None):
+    r"""Build a multipart/mixed response email using `Person` instances
 
-    return msg
+    >>> from pygrader.model.person import Person as Person
+    >>> student = Person(name='Джон Доу', emails=['jdoe@a.gov.ru'])
+    >>> assistant = Person(name='Jill', emails=['c@d.net'])
+    >>> cc = [assistant]
+    >>> msg = construct_text_email(author=student, targets=[assistant],
+    ...     subject='Assignment 1 submission', text='Bla bla bla...')
+    >>> rsp = construct_response(author=assistant, targets=[student],
+    ...     subject='Received assignment 1 submission', text='3 hours late',
+    ...     original=msg)
+    >>> print(rsp.as_string())  # doctest: +REPORT_UDIFF, +ELLIPSIS
+    Content-Type: multipart/mixed; boundary="===============...=="
+    MIME-Version: 1.0
+    Date: ...
+    From: Jill <c@d.net>
+    Reply-to: Jill <c@d.net>
+    To: =?utf-8?b?0JTQttC+0L0g0JTQvtGD?= <jdoe@a.gov.ru>
+    Subject: Received assignment 1 submission
+    <BLANKLINE>
+    --===============...==
+    Content-Type: text/plain; charset="us-ascii"
+    MIME-Version: 1.0
+    Content-Transfer-Encoding: 7bit
+    Content-Disposition: inline
+    <BLANKLINE>
+    3 hours late
+    --===============...==
+    Content-Type: message/rfc822
+    MIME-Version: 1.0
+    <BLANKLINE>
+    Content-Type: text/plain; charset="us-ascii"
+    MIME-Version: 1.0
+    Content-Transfer-Encoding: 7bit
+    Content-Disposition: inline
+    Date: ...
+    From: =?utf-8?b?0JTQttC+0L0g0JTQvtGD?= <jdoe@a.gov.ru>
+    Reply-to: =?utf-8?b?0JTQttC+0L0g0JTQvtGD?= <jdoe@a.gov.ru>
+    To: Jill <c@d.net>
+    Subject: Assignment 1 submission
+    <BLANKLINE>
+    Bla bla bla...
+    --===============...==--
+    """
+    message = _MIMEMultipart('mixed')
+    message.attach(_pgp_mime.encodedMIMEText(text))
+    message.attach(_MIMEMessage(original))
+    return construct_email(
+        author=author, targets=targets, subject=subject, message=message,
+        cc=cc)