Dre4m Shell
Server IP : 85.214.239.14  /  Your IP : 3.22.75.92
Web Server : Apache/2.4.62 (Debian)
System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.18
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
MySQL : OFF  |  cURL : OFF  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : OFF
Directory :  /srv/modoboa/env/lib/python3.5/site-packages/modoboa_amavis/management/commands/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME SHELL ]     

Current File : /srv/modoboa/env/lib/python3.5/site-packages/modoboa_amavis/management/commands/amnotify.py
# -*- coding: utf-8 -*-

from __future__ import print_function, unicode_literals

from django.contrib.sites import models as sites_models
from django.core import mail
from django.core.management.base import BaseCommand
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.translation import ugettext as _

from modoboa.admin.models import Domain
from modoboa.core.models import User
from modoboa.parameters import tools as param_tools
from ...models import Msgrcpt
from ...modo_extension import Amavis
from ...sql_connector import SQLconnector


class Command(BaseCommand):
    help = "Amavis notification tool"  # NOQA:A003

    sender = None
    baseurl = None
    listingurl = None

    def add_arguments(self, parser):
        """Add extra arguments to command line."""
        parser.add_argument(
            "--smtp_host", type=str, default="localhost",
            help="The address of the SMTP server used to send notifications")
        parser.add_argument(
            "--smtp_port", type=int, default=25,
            help=("The listening port of the SMTP server used to send "
                  "notifications"))
        parser.add_argument("--verbose", action="store_true",
                            help="Activate verbose mode")

    def handle(self, *args, **options):
        Amavis().load()
        self.options = options
        self.notify_admins_pending_requests()

    def _build_message(self, rcpt, total, reqs):
        """Build new EmailMessage instance."""
        if self.options["verbose"]:
            print("Sending notification to %s" % rcpt)
        context = {
            "total": total,
            "requests": reqs,
            "baseurl": self.baseurl,
            "listingurl": self.listingurl
        }
        content = render_to_string(
            "modoboa_amavis/notifications/pending_requests.html",
            context
        )
        msg = mail.EmailMessage(
            _("[modoboa] Pending release requests"),
            content,
            self.sender,
            [rcpt]
        )
        return msg

    def notify_admins_pending_requests(self):
        self.sender = param_tools.get_global_parameter(
            "notifications_sender", app="modoboa_amavis")
        self.baseurl = "https://{}".format(
            sites_models.Site.objects.get_current().domain)
        self.listingurl = "{}{}?viewrequests=1".format(
            self.baseurl, reverse("modoboa_amavis:_mail_list"))

        messages = []
        # Check domain administators first.
        for da in User.objects.filter(groups__name="DomainAdmins"):
            if not hasattr(da, "mailbox"):
                continue
            rcpt = da.mailbox.full_address
            reqs = SQLconnector().get_domains_pending_requests(
                Domain.objects.get_for_admin(da).values_list("name", flat=True)
            )
            total = reqs.count()
            reqs = reqs.all()[:10]
            if reqs.count():
                messages.append(self._build_message(rcpt, total, reqs))

        # Then super administators.
        reqs = Msgrcpt.objects.filter(rs="p")
        total = reqs.count()
        if total:
            reqs = reqs.all()[:10]
            for su in User.objects.filter(is_superuser=True):
                if not hasattr(su, "mailbox"):
                    continue
                rcpt = su.mailbox.full_address
                messages.append(self._build_message(rcpt, total, reqs))

        # Finally, send emails.
        if not len(messages):
            return
        kwargs = {
            "host": self.options["smtp_host"],
            "port": self.options["smtp_port"]
        }
        with mail.get_connection(**kwargs) as connection:
            connection.send_messages(messages)

Anon7 - 2022
AnonSec Team