From acf5b4e0dabda770c92ca4d033a84d73b511a22a Mon Sep 17 00:00:00 2001
From: Nicolas KAROLAK <nicolas@karolak.fr>
Date: Fri, 6 Apr 2018 10:53:54 +0200
Subject: [PATCH] spf check

Try import spf otherwise install it, and use it to validate against spf records.
---
 tests/test_email.py | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/tests/test_email.py b/tests/test_email.py
index 79490d6f..5c2b111a 100755
--- a/tests/test_email.py
+++ b/tests/test_email.py
@@ -11,6 +11,12 @@ import random
 import time
 import imp
 
+try:
+    import spf
+except ImportError:
+    status, output = subprocess.getstatusoutput('apt-get -qq -y install python3-spf')
+    import spf
+
 YELLOW = '\033[93m'
 GREEN = '\033[92m'
 RED = '\033[91m'
@@ -74,10 +80,10 @@ def check_relay():
             print_yellow('/etc/mailname does not contain ubicast.eu or ubicast.tv, mails will probably not be received on ubicast mailing lists')
             return 3
         # check spf
-        status, output = subprocess.getstatusoutput('host -t TXT ubicast.eu')
-        if ip not in output:
-            print_red('ip %s is not in ubicast.eu SPF and emails sent from support@ubicast.eu will be treated as spam' % ip)
-            return 1
+        result, explain = spf.check2(i=ip, s='support@ubicast.eu', h='')
+        if result != 'pass':
+            print_red('ip %s is not in ubicast.eu SPF and emails sent from support@ubicast.eu may be treated as spam' % ip)
+            return 3
     conf_relay = conf.get('EMAIL_SMTP_SERVER', '').strip('[]')
     if conf_relay != configured_relay:
         print_red('Configured STMP relay (%s) does not match the expected value (%s).' % (configured_relay, conf_relay))
-- 
GitLab