浏览代码

[Web, Quarantine] Allow to set the max score of a message up to which a quarantine notification will be sent

andryyy 4 年之前
父节点
当前提交
6c697f3f3f

+ 10 - 6
data/Dockerfiles/dovecot/quarantine_notify.py

@@ -27,6 +27,10 @@ while True:
 
 
 time_now = int(time.time())
 time_now = int(time.time())
 
 
+max_score = float(r.get('Q_MAX_SCORE') or "9999.0")
+if max_score == "":
+  max_score = 9999.0
+
 def query_mysql(query, headers = True, update = False):
 def query_mysql(query, headers = True, update = False):
   while True:
   while True:
     try:
     try:
@@ -55,7 +59,7 @@ def query_mysql(query, headers = True, update = False):
     cnx.close()
     cnx.close()
 
 
 def notify_rcpt(rcpt, msg_count, quarantine_acl):
 def notify_rcpt(rcpt, msg_count, quarantine_acl):
-  meta_query = query_mysql('SELECT SHA2(CONCAT(id, qid), 256) AS qhash, id, subject, score, sender, created FROM quarantine WHERE notified = 0 AND rcpt = "%s"' % (rcpt))
+  meta_query = query_mysql('SELECT SHA2(CONCAT(id, qid), 256) AS qhash, id, subject, score, sender, created FROM quarantine WHERE notified = 0 AND rcpt = "%s" AND score < %f' % (rcpt, max_score))
   if r.get('Q_HTML'):
   if r.get('Q_HTML'):
     try:
     try:
       template = Template(r.get('Q_HTML'))
       template = Template(r.get('Q_HTML'))
@@ -100,7 +104,7 @@ def notify_rcpt(rcpt, msg_count, quarantine_acl):
           server.sendmail(msg['From'], [str(redirect)] + [str(bcc)], text)
           server.sendmail(msg['From'], [str(redirect)] + [str(bcc)], text)
       server.quit()
       server.quit()
       for res in meta_query:
       for res in meta_query:
-        query_mysql('UPDATE quarantine SET notified = 1 WHERE id = "%d"' % (res['id']), update = True)
+       query_mysql('UPDATE quarantine SET notified = 1 WHERE id = "%d"' % (res['id']), update = True)
       r.hset('Q_LAST_NOTIFIED', record['rcpt'], time_now)
       r.hset('Q_LAST_NOTIFIED', record['rcpt'], time_now)
       break
       break
     except Exception as ex:
     except Exception as ex:
@@ -108,7 +112,7 @@ def notify_rcpt(rcpt, msg_count, quarantine_acl):
       print('%s'  % (ex))
       print('%s'  % (ex))
       time.sleep(3)
       time.sleep(3)
 
 
-records = query_mysql('SELECT IFNULL(user_acl.quarantine, 0) AS quarantine_acl, count(id) AS counter, rcpt FROM quarantine LEFT OUTER JOIN user_acl ON user_acl.username = rcpt WHERE notified = 0 AND rcpt in (SELECT username FROM mailbox) GROUP BY rcpt')
+records = query_mysql('SELECT IFNULL(user_acl.quarantine, 0) AS quarantine_acl, count(id) AS counter, rcpt FROM quarantine LEFT OUTER JOIN user_acl ON user_acl.username = rcpt WHERE notified = 0 AND score < %f AND rcpt in (SELECT username FROM mailbox) GROUP BY rcpt' % (max_score))
 
 
 for record in records:
 for record in records:
   attrs = ''
   attrs = ''
@@ -134,13 +138,13 @@ for record in records:
     continue
     continue
   if attrs['quarantine_notification'] == 'hourly':
   if attrs['quarantine_notification'] == 'hourly':
     if last_notification == 0 or (last_notification + 3600) < time_now:
     if last_notification == 0 or (last_notification + 3600) < time_now:
-      print("Notifying %s about %d new items in quarantine" % (record['rcpt'], record['counter']))
+      print("Notifying %s: Considering %d new items in quarantine" % (record['rcpt'], record['counter']))
       notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl'])
       notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl'])
   elif attrs['quarantine_notification'] == 'daily':
   elif attrs['quarantine_notification'] == 'daily':
     if last_notification == 0 or (last_notification + 86400) < time_now:
     if last_notification == 0 or (last_notification + 86400) < time_now:
-      print("Notifying %s about %d new items in quarantine" % (record['rcpt'], record['counter']))
+      print("Notifying %s: Considering %d new items in quarantine" % (record['rcpt'], record['counter']))
       notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl'])
       notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl'])
   elif attrs['quarantine_notification'] == 'weekly':
   elif attrs['quarantine_notification'] == 'weekly':
     if last_notification == 0 or (last_notification + 604800) < time_now:
     if last_notification == 0 or (last_notification + 604800) < time_now:
-      print("Notifying %s about %d new items in quarantine" % (record['rcpt'], record['counter']))
+      print("Notifying %s: Considering %d new items in quarantine" % (record['rcpt'], record['counter']))
       notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl'])
       notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl'])

+ 6 - 0
data/web/admin.php

@@ -821,6 +821,12 @@ if (!isset($_SESSION['gal']) && $license_cache = $redis->Get('LICENSE_STATUS_CAC
               <input type="number" class="form-control" name="max_size" value="<?=$q_data['max_size'];?>" placeholder="0" required>
               <input type="number" class="form-control" name="max_size" value="<?=$q_data['max_size'];?>" placeholder="0" required>
             </div>
             </div>
           </div>
           </div>
+          <div class="form-group">
+            <label class="col-sm-4 control-label" for="max_score"><?=$lang['admin']['quarantine_max_score'];?></label>
+            <div class="col-sm-8">
+              <input type="number" class="form-control" name="max_score" value="<?=$q_data['max_score'];?>" placeholder="9999.0">
+            </div>
+          </div>
           <div class="form-group">
           <div class="form-group">
             <label class="col-sm-4 control-label" for="max_age"><?=$lang['admin']['quarantine_max_age'];?></label>
             <label class="col-sm-4 control-label" for="max_age"><?=$lang['admin']['quarantine_max_age'];?></label>
             <div class="col-sm-8">
             <div class="col-sm-8">

+ 8 - 0
data/web/inc/functions.quarantine.inc.php

@@ -299,6 +299,12 @@ function quarantine($_action, $_data = null) {
           $release_format = 'raw';
           $release_format = 'raw';
         }
         }
         $max_size = $_data['max_size'];
         $max_size = $_data['max_size'];
+        if ($_data['max_score'] == "") {
+          $max_score = '';
+        }
+        else {
+          $max_score = floatval($_data['max_score']);
+        }
         $max_age = intval($_data['max_age']);
         $max_age = intval($_data['max_age']);
         $subject = $_data['subject'];
         $subject = $_data['subject'];
         if (!filter_var($_data['bcc'], FILTER_VALIDATE_EMAIL)) {
         if (!filter_var($_data['bcc'], FILTER_VALIDATE_EMAIL)) {
@@ -327,6 +333,7 @@ function quarantine($_action, $_data = null) {
         try {
         try {
           $redis->Set('Q_RETENTION_SIZE', intval($retention_size));
           $redis->Set('Q_RETENTION_SIZE', intval($retention_size));
           $redis->Set('Q_MAX_SIZE', intval($max_size));
           $redis->Set('Q_MAX_SIZE', intval($max_size));
+          $redis->Set('Q_MAX_SCORE', $max_score);
           $redis->Set('Q_MAX_AGE', $max_age);
           $redis->Set('Q_MAX_AGE', $max_age);
           $redis->Set('Q_EXCLUDE_DOMAINS', json_encode($exclude_domains));
           $redis->Set('Q_EXCLUDE_DOMAINS', json_encode($exclude_domains));
           $redis->Set('Q_RELEASE_FORMAT', $release_format);
           $redis->Set('Q_RELEASE_FORMAT', $release_format);
@@ -794,6 +801,7 @@ function quarantine($_action, $_data = null) {
           $settings['exclude_domains'] = json_decode($redis->Get('Q_EXCLUDE_DOMAINS'), true);
           $settings['exclude_domains'] = json_decode($redis->Get('Q_EXCLUDE_DOMAINS'), true);
         }
         }
         $settings['max_size'] = $redis->Get('Q_MAX_SIZE');
         $settings['max_size'] = $redis->Get('Q_MAX_SIZE');
+        $settings['max_score'] = $redis->Get('Q_MAX_SCORE');
         $settings['max_age'] = $redis->Get('Q_MAX_AGE');
         $settings['max_age'] = $redis->Get('Q_MAX_AGE');
         $settings['retention_size'] = $redis->Get('Q_RETENTION_SIZE');
         $settings['retention_size'] = $redis->Get('Q_RETENTION_SIZE');
         $settings['release_format'] = $redis->Get('Q_RELEASE_FORMAT');
         $settings['release_format'] = $redis->Get('Q_RELEASE_FORMAT');

+ 1 - 0
data/web/lang/lang.de.json

@@ -226,6 +226,7 @@
         "quarantine_exclude_domains": "Domains und Alias-Domains ausschließen",
         "quarantine_exclude_domains": "Domains und Alias-Domains ausschließen",
         "quarantine_max_age": "Maximales Alter in Tagen<br><small>Wert muss größer oder gleich 1 Tag sein.</small>",
         "quarantine_max_age": "Maximales Alter in Tagen<br><small>Wert muss größer oder gleich 1 Tag sein.</small>",
         "quarantine_max_size": "Maximale Größe in MiB (größere Elemente werden verworfen):<br><small>0 bedeutet <b>nicht</b> unlimitiert.</small>",
         "quarantine_max_size": "Maximale Größe in MiB (größere Elemente werden verworfen):<br><small>0 bedeutet <b>nicht</b> unlimitiert.</small>",
+        "quarantine_max_score": "Nicht benachrichtigen, wenn der Spam-Score höher ist als der folgende Wert:<br><small>Standardwert 9999.0</small>",
         "quarantine_notification_html": "Benachrichtigungs-E-Mail Inhalt:<br><small>Leer lassen, um Standard-Template wiederherzustellen.</small>",
         "quarantine_notification_html": "Benachrichtigungs-E-Mail Inhalt:<br><small>Leer lassen, um Standard-Template wiederherzustellen.</small>",
         "quarantine_notification_sender": "Benachrichtigungs-E-Mail Absender",
         "quarantine_notification_sender": "Benachrichtigungs-E-Mail Absender",
         "quarantine_notification_subject": "Benachrichtigungs-E-Mail Betreff",
         "quarantine_notification_subject": "Benachrichtigungs-E-Mail Betreff",

+ 1 - 0
data/web/lang/lang.en.json

@@ -226,6 +226,7 @@
         "quarantine_exclude_domains": "Exclude domains and alias-domains",
         "quarantine_exclude_domains": "Exclude domains and alias-domains",
         "quarantine_max_age": "Maximum age in days<br><small>Value must be equal to or greater than 1 day.</small>",
         "quarantine_max_age": "Maximum age in days<br><small>Value must be equal to or greater than 1 day.</small>",
         "quarantine_max_size": "Maximum size in MiB (larger elements are discarded):<br><small>0 does <b>not</b> indicate unlimited.</small>",
         "quarantine_max_size": "Maximum size in MiB (larger elements are discarded):<br><small>0 does <b>not</b> indicate unlimited.</small>",
+        "quarantine_max_score": "Discard notification if spam score of a mail is higher than this value:<br><small>Defaults to 9999.0</small>",
         "quarantine_notification_html": "Notification email template:<br><small>Leave empty to restore default template.</small>",
         "quarantine_notification_html": "Notification email template:<br><small>Leave empty to restore default template.</small>",
         "quarantine_notification_sender": "Notification email sender",
         "quarantine_notification_sender": "Notification email sender",
         "quarantine_notification_subject": "Notification email subject",
         "quarantine_notification_subject": "Notification email subject",