From 9c1acfaf8a27ca3ee61f3a133c0519e1be10fd6a Mon Sep 17 00:00:00 2001 From: jorgepastorr Date: Thu, 27 Jun 2024 18:08:46 +0200 Subject: [PATCH] add backend rspamd ratelimit basic --- orchestra/contrib/mailboxes/backends.py | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/orchestra/contrib/mailboxes/backends.py b/orchestra/contrib/mailboxes/backends.py index 97be7c3e..d6c4770f 100644 --- a/orchestra/contrib/mailboxes/backends.py +++ b/orchestra/contrib/mailboxes/backends.py @@ -666,3 +666,42 @@ class RoundcubeIdentityController(ServiceController): verbose_name = _("Roundcube Identity Controller") model = 'mailboxes.Mailbox' + + +class RSpamdRatelimitController(ServiceController): + """ + rspamd ratelimit to user + """ + + verbose_name = _("rspamd ratelimit user") + model = 'mailboxes.Mailbox' + + 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 + """) % 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 + """) % context + ) + + def commit(self): + self.append('[[ $RELOAD_RSPAMD -eq 1 ]] && systemctl reload rspamd.service') + super().commit() + + def get_context(self, mailbox): + context = { + 'user': mailbox.name, + } + return context \ No newline at end of file