<commit_before>#!/usr/bin/env python
# coding: utf-8
import sys
import datetime
from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone
from modoboa.lib import parameters
from modoboa.lib.emailutils import split_mailbox, sendmail_simple
from modoboa.admin.models import Mailbox
from modoboa.extensions.postfix_autoreply import PostfixAutoreply
from modoboa.extensions.postfix_autoreply.models import ARmessage, ARhistoric

def send_autoreply(sender, mailbox, armessage):
    today = timezone.make_aware(datetime.datetime.today(), 
                                timezone.get_default_timezone())
    if armessage.untildate is not None \
        and armessage.untildate < today:
        armessage.enabled = False
        armessage.save()
        return

    try:
        lastar = ARhistoric.objects.get(armessage=armessage.id, sender=sender)
        PostfixAutoreply().load()
        timeout = parameters.get_admin("AUTOREPLIES_TIMEOUT", 
                                       app="postfix_autoreply")
        delta = datetime.timedelta(seconds=int(timeout))
        now = timezone.make_aware(datetime.datetime.now(), 
                                  timezone.get_default_timezone())
        if lastar.last_sent + delta > now:
            sys.exit(0)
    except ARhistoric.DoesNotExist:
        lastar = ARhistoric()
        lastar.armessage = armessage
        lastar.sender = sender

    sendmail_simple(mailbox.name_and_address, sender, armessage.subject,
                    armessage.content.encode('utf-8'))
    
    lastar.last_sent = datetime.datetime.now()
    lastar.save()


class Command(BaseCommand):
    args = '<sender> <recipient ...>'
    help = 'Send autoreply emails'

    def handle(self, *args, **options):
        if len(args) < 2:
            raise CommandError("usage: ./manage.py autoreply <sender> <recipient ...>")

        sender = args[0]
        for fulladdress in args[1:]:
            address, domain = split_mailbox(fulladdress)
            try:
                mbox = Mailbox.objects.get(address=address, domain__name=domain)
            except Mailbox.DoesNotExist:
                print "Unknown recipient %s" % (mbox)
                continue
            try:
                armessage = ARmessage.objects.get(mbox=mbox.id, enabled=True)
            except ARmessage.DoesNotExist:
                continue

            send_autoreply(sender, mbox, armessage)
<commit_msg>Previous fix was too heavy :p<commit_after>#!/usr/bin/env python
# coding: utf-8
import sys
import datetime
from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone
from modoboa.lib import parameters
from modoboa.lib.emailutils import split_mailbox, sendmail_simple
from modoboa.admin.models import Mailbox
from modoboa.extensions.postfix_autoreply import PostfixAutoreply
from modoboa.extensions.postfix_autoreply.models import ARmessage, ARhistoric

def send_autoreply(sender, mailbox, armessage):
    if armessage.untildate is not None \
        and armessage.untildate < datetime.date.today():
        armessage.enabled = False
        armessage.save()
        return

    try:
        lastar = ARhistoric.objects.get(armessage=armessage.id, sender=sender)
        PostfixAutoreply().load()
        timeout = parameters.get_admin("AUTOREPLIES_TIMEOUT", 
                                       app="postfix_autoreply")
        delta = datetime.timedelta(seconds=int(timeout))
        now = timezone.make_aware(datetime.datetime.now(), 
                                  timezone.get_default_timezone())
        if lastar.last_sent + delta > now:
            sys.exit(0)
    except ARhistoric.DoesNotExist:
        lastar = ARhistoric()
        lastar.armessage = armessage
        lastar.sender = sender

    sendmail_simple(mailbox.name_and_address, sender, armessage.subject,
                    armessage.content.encode('utf-8'))
    
    lastar.last_sent = datetime.datetime.now()
    lastar.save()


class Command(BaseCommand):
    args = '<sender> <recipient ...>'
    help = 'Send autoreply emails'

    def handle(self, *args, **options):
        if len(args) < 2:
            raise CommandError("usage: ./manage.py autoreply <sender> <recipient ...>")

        sender = args[0]
        for fulladdress in args[1:]:
            address, domain = split_mailbox(fulladdress)
            try:
                mbox = Mailbox.objects.get(address=address, domain__name=domain)
            except Mailbox.DoesNotExist:
                print "Unknown recipient %s" % (mbox)
                continue
            try:
                armessage = ARmessage.objects.get(mbox=mbox.id, enabled=True)
            except ARmessage.DoesNotExist:
                continue

            send_autoreply(sender, mbox, armessage)

{'fraction_non_alphanumeric': 0.09200415368639668, 'fraction_numerical': 0.0024922118380062306, 'mean_word_length': 2.4277580071174376, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5013149', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1332, 'n_words': 290}