from django.conf import settings
from django.core.mail import EmailMessage
from django.template import Context
from django.template.loader import render_to_string
from django.contrib.sites.models import Site

import re

from comrade.mail.signals import email_sent

def direct_to_email(template, recipient_list, extra_context=None,
        from_email=None, subject_template=None):
    """
    Sends an email of type email to user.email. Text of subject and message
    rendered from the {template} and {template}_subject using specified context
    """
    extra_context = extra_context or {}
    context = Context(extra_context)
    context['site'] = Site.objects.get_current()
    if not subject_template:
        subject = render_to_string(
                '%s_subject.txt' % template, context).strip('\n')
    message = render_to_string('%s.txt' % template, context)

    email_sent.send(sender=template, recipient_list=recipient_list,
            from_address=(from_email or settings.DEFAULT_FROM_EMAIL))

    return EmailMessage(subject, message, from_email, recipient_list,
            headers={'Reply-To': settings.DEFAULT_FROM_EMAIL}).send()

# Inspired by
# http://stackoverflow.com/questions/1372694/strip-signatures-and-replies-from-emails/2193937#2193937
EMAIL_SIGNATURES = (
        # standard
        re.compile(r'^-- \n$', flags=re.MULTILINE), 
        # standard missing space
        re.compile(r'^--\n$', flags=re.MULTILINE), 
        # outlook
        re.compile("^-----Original Message-----", flags=re.MULTILINE),
        # outlook alternative
        re.compile("^________________________________", flags=re.MULTILINE),
        # mail.app
        re.compile("^On [\w: ]+ wrote:\n$", flags=re.MULTILINE),
        # failsafe
        re.compile("^From: ", flags=re.MULTILINE),
        # ego-booster
        re.compile("^Sent from my", flags=re.MULTILINE),
)

def strip_signatures(body):
    for signature in EMAIL_SIGNATURES:
        split_signature = signature.split(body)
        if len(split_signature) > 1:
            return split_signature[0]
    return body


@@ -1321,17 +1321,18 @@
 mpile(r'
-%5E
+%5Cn
 -- %5Cn$',
@@ -1408,17 +1408,18 @@
 mpile(r'
-%5E
+%5Cn
 --%5Cn$', 
@@ -1432,32 +1432,348 @@
 re.MULTILINE), %0A
+        # ego-booster%0A        re.compile(r'%5CnSent from my', flags=re.MULTILINE),%0A)%0A%0Adef strip_signatures(body):%0A    for signature in EMAIL_SIGNATURES:%0A        split_signature = signature.split(body)%0A        if len(split_signature) %3E 1:%0A            return split_signature%5B0%5D%0A    return body%0A%0AEMAIL_QUOTED_MARKERS = (%0A
         # outloo
@@ -1793,18 +1793,20 @@
 compile(
-%22%5E
+r'%5Cn
 -----Ori
@@ -1823,17 +1823,17 @@
 age-----
-%22
+'
 , flags=
@@ -1896,18 +1896,20 @@
 compile(
-%22%5E
+r'%5Cn
 ________
@@ -1932,17 +1932,17 @@
 ________
-%22
+'
 , flags=
@@ -1994,18 +1994,20 @@
 compile(
-%22%5E
+r'%5Cn
 On %5B%5Cw: 
@@ -2018,17 +2018,17 @@
 rote:%5Cn$
-%22
+'
 , flags=
@@ -2084,17 +2084,19 @@
 ile(
-%22%5E
+r'%5Cn
 From: 
-%22
+'
 , fl
@@ -2120,35 +2120,32 @@
 ,%0A        # 
-ego-booster
+indented
 %0A        re.
@@ -2156,23 +2156,17 @@
 ile(
-%22%5ESent from my%22
+r'%5Cn%3E.*$'
 , fl
@@ -2193,34 +2193,30 @@
 %0A%0Adef strip_
-signatures
+quoted
 (body):%0A    
@@ -2215,33 +2215,30 @@
 y):%0A    for 
-signature
+quoted
  in EMAIL_SI
@@ -2231,33 +2231,37 @@
 ed in EMAIL_
-SIGNATURE
+QUOTED_MARKER
 S:%0A        s
@@ -2265,37 +2265,32 @@
   split_
-signature = signature
+message = quoted
 .split(b
@@ -2311,32 +2311,30 @@
 f len(split_
-signatur
+messag
 e) %3E 1:%0A    
@@ -2350,32 +2350,30 @@
 eturn split_
-signatur
+messag
 e%5B0%5D%0A    ret
@@ -2361,29 +2361,28 @@
 _message%5B0%5D%0A    return body%0A
-%0A

Split up signature and inline quoting e-mail regexes.