Compare commits

..

2 Commits

Author SHA1 Message Date
Jorge Pastor e15d2acd13 mailboxes ratelimit and groups 2024-07-09 17:36:32 +02:00
Jorge Pastor 67c291cb99 mailboxes ratelimit prepare 2024-07-08 18:30:00 +02:00
5 changed files with 58 additions and 15 deletions

View File

@ -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',),

View File

@ -679,29 +679,44 @@ class RSpamdRatelimitController(ServiceController):
def save(self, mailbox):
context = self.get_context(mailbox)
self.append(textwrap.dedent("""
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
fi
sed -i '/^%(user)s$/d' %(maps)s
echo '%(user)s' >> %(path_maps)s%(ratelimit)s.map
systemctl reload rspamd.service
""") % context
)
def delete(self, mailbox):
context = self.get_context(mailbox)
self.append(textwrap.dedent("""
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
fi
sed -i '/^%(user)s$/d' %(maps)s
systemctl reload rspamd.service
""") % context
)
def commit(self):
self.append('[[ $RELOAD_RSPAMD -eq 1 ]] && systemctl reload rspamd.service')
super().commit()
# def commit(self):
# 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
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

View File

@ -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

View File

@ -33,6 +33,11 @@ class Mailbox(models.Model):
"<a href='https://tty1.net/blog/2011/sieve-tutorial_en.html'>sieve language</a>. "
"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")

View File

@ -203,3 +203,21 @@ MAILBOXES_MOVE_ON_DELETE_PATH = Setting('MAILBOXES_MOVE_ON_DELETE_PATH',
help_text="Available fromat names: <tt>%s</tt>" % ', '.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/',
)