diff --git a/orchestra/contrib/mailboxes/admin.py b/orchestra/contrib/mailboxes/admin.py index 5315a86c..fe434483 100644 --- a/orchestra/contrib/mailboxes/admin.py +++ b/orchestra/contrib/mailboxes/admin.py @@ -47,7 +47,7 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo ) add_fieldsets = ( (None, { - 'fields': ('account_link', 'name', 'password1', 'password2', 'filtering'), + 'fields': ('account_link', 'name', 'password1', 'password2', 'filtering', 'ratelimit'), }), (_("Custom filtering"), { 'classes': ('collapse',), @@ -61,7 +61,7 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo ) fieldsets = ( (None, { - 'fields': ('name', 'password', 'is_active', 'account_link', 'filtering'), + 'fields': ('name', 'password', 'is_active', 'account_link', 'filtering', 'ratelimit'), }), (_("Custom filtering"), { 'classes': ('collapse',), diff --git a/orchestra/contrib/mailboxes/backends.py b/orchestra/contrib/mailboxes/backends.py index d6c4770f..0b6c2749 100644 --- a/orchestra/contrib/mailboxes/backends.py +++ b/orchestra/contrib/mailboxes/backends.py @@ -679,6 +679,8 @@ class RSpamdRatelimitController(ServiceController): def save(self, mailbox): context = self.get_context(mailbox) self.append(textwrap.dedent(""" + # sed -i '/^%(user)s$/d' %(maps)s + # echo '%(user)s' >> %(path_maps)s%(ratelimit)s.map if ! grep -qx '%(user)s' /etc/rspamd/local.d/maps/usuariosbase.map; then echo '%(user)s' >> /etc/rspamd/local.d/maps/usuariosbase.map RELOAD_RSPAMD=1 @@ -689,6 +691,7 @@ class RSpamdRatelimitController(ServiceController): def delete(self, mailbox): context = self.get_context(mailbox) self.append(textwrap.dedent(""" + # sed -i '/^%(user)s$/d' %(maps)s if grep -qx '%(user)s' /etc/rspamd/local.d/maps/usuariosbase.map; then sed -i '/^%(user)s$/d' /etc/rspamd/local.d/maps/usuariosbase.map RELOAD_RSPAMD=1 @@ -697,11 +700,29 @@ class RSpamdRatelimitController(ServiceController): ) def commit(self): - self.append('[[ $RELOAD_RSPAMD -eq 1 ]] && systemctl reload rspamd.service') + self.append('# [[ $RELOAD_RSPAMD -eq 1 ]] && systemctl reload rspamd.service') super().commit() - + def get_context(self, mailbox): + maps = self.extract_group_maps() context = { 'user': mailbox.name, + 'ratelimit': mailbox.ratelimit, + 'maps': maps, + 'path_maps': settings.MAILBOXES_RATELIMIT_PATH_MAPS, } - return context \ No newline at end of file + return context + + def extract_group_maps(self): + """ + debulve string de todos los ficheros de maps assignados en settings para ratelimit + return string + """ + choice_groups = settings.MAILBOXES_RATELIMIT_GROUP + path = settings.MAILBOXES_RATELIMIT_PATH_MAPS + group_maps = '' + if len(choice_groups) > 0: + for choice in choice_groups: + group_maps += f"{path}{choice[0]}.map " + + return group_maps \ No newline at end of file diff --git a/orchestra/contrib/mailboxes/forms.py b/orchestra/contrib/mailboxes/forms.py index 522b608e..607c246f 100644 --- a/orchestra/contrib/mailboxes/forms.py +++ b/orchestra/contrib/mailboxes/forms.py @@ -54,6 +54,11 @@ class MailboxForm(forms.ModelForm): raise ValidationError("Name length should be less than %i." % max_length) return name + def clean_ratelimit(self): + ratelimit = self.cleaned_data['ratelimit'] + if ratelimit is None: + ratelimit = settings.MAILBOXES_RATELIMIT_GROUP_DEFAULT + return ratelimit class MailboxChangeForm(UserChangeForm, MailboxForm): pass diff --git a/orchestra/contrib/mailboxes/models.py b/orchestra/contrib/mailboxes/models.py index 7122b5e1..7ae16003 100644 --- a/orchestra/contrib/mailboxes/models.py +++ b/orchestra/contrib/mailboxes/models.py @@ -33,6 +33,11 @@ class Mailbox(models.Model): "sieve language. " "This overrides any automatic junk email filtering")) is_active = models.BooleanField(_("active"), default=True) + ratelimit = models.CharField(_("ratelimit"), + max_length=100, null=True, blank=True, + choices=settings.MAILBOXES_RATELIMIT_GROUP, + default=settings.MAILBOXES_RATELIMIT_GROUP_DEFAULT,) + class Meta: verbose_name_plural = _("mailboxes") diff --git a/orchestra/contrib/mailboxes/settings.py b/orchestra/contrib/mailboxes/settings.py index c9412754..e2a61642 100644 --- a/orchestra/contrib/mailboxes/settings.py +++ b/orchestra/contrib/mailboxes/settings.py @@ -203,3 +203,21 @@ MAILBOXES_MOVE_ON_DELETE_PATH = Setting('MAILBOXES_MOVE_ON_DELETE_PATH', help_text="Available fromat names: %s" % ', '.join(_backend_names), validators=[Setting.string_format_validator(_backend_names)], ) + +MAILBOXES_RATELIMIT_GROUP = Setting('MAILBOXES_RATELIMIT_GROUP', ( + ('userbase', 'base'), + ('user100', '100'), + ('user200', '200'), + ('user500', '500'), + ), + help_text="Available messages per second" +) + +MAILBOXES_RATELIMIT_GROUP_DEFAULT = Setting('MAILBOXES_RATELIMIT_GROUP_DEFAULT', + 'userbase', + choices=MAILBOXES_RATELIMIT_GROUP +) + +MAILBOXES_RATELIMIT_PATH_MAPS = Setting('MAILBOXES_RATELIMIT_PATH_MAPS', + '/etc/rspamd/local.d/maps/', +)