From 14f62f3294ad1871eda62f0aef435e913aba4744 Mon Sep 17 00:00:00 2001
From: Bastien Le Querrec <blq@laquadrature.net>
Date: Mon, 18 Mar 2024 15:26:50 +0100
Subject: [PATCH] =?UTF-8?q?RAAspotter:=20g=C3=A8re=20l'envoi=20de=20mail?=
 =?UTF-8?q?=20dans=20la=20classe=20principale?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 RAAspotter.py         | 97 +++++++++++++++++++++++++++----------------
 RAAspotter_ppparis.py |  1 +
 RAAspotter_pref13.py  |  2 +
 RAAspotter_pref35.py  |  2 +
 ppparis.py            | 10 ++---
 pref13.py             | 10 ++---
 pref35.py             | 10 ++---
 7 files changed, 75 insertions(+), 57 deletions(-)

diff --git a/RAAspotter.py b/RAAspotter.py
index a6597e0..2c45158 100644
--- a/RAAspotter.py
+++ b/RAAspotter.py
@@ -58,6 +58,7 @@ class RAAspotter:
     self.tor_max_requests = 0
     self.tor_requests     = 0
     self.not_before       = datetime.datetime(1970, 1, 1)
+    self.smtp_configured  = False
 
     self.update_user_agent(user_agent)
 
@@ -243,39 +244,63 @@ class RAAspotter:
   def get_raa(self, page_content):
     logger.error('Cette fonction doit être surchargée')
 
-  def mailer(smtp_host, smtp_username, smtp_password, smtp_port,
-             smtp_starttls, smtp_ssl, email_from, email_to, email_object, 
-             email_content):
-    try:
-      message = EmailMessage()
-      message.set_content(email_content)
-
-      message['Subject'] = email_object
-      message['From'] = email_from
-
-      context = ssl.create_default_context()
-
-      if smtp_ssl == True:
-        for address in email_to.split(','):
-          message['To'] = address
-          smtp = smtplib.SMTP_SSL(smtp_host, port, context=context)
-          smtp.login(smtp_username, smtp_password)
-          smtp.send_message(message)
-          smtp.quit()
-      elif smtp_starttls == True:
-        for address in email_to.split(','):
-          message['To'] = address
-          smtp = smtplib.SMTP(smtp_host)
-          smtp.starttls(context=context)
-          smtp.login(smtp_username, smtp_password)
-          smtp.send_message(message)
-          smtp.quit()
-      else:
-        for address in email_to.split(','):
-          message['To'] = address
-          smtp = smtplib.SMTP(smtp_host)
-          smtp.login(smtp_username, smtp_password)
-          smtp.send_message(message)
-          smtp.quit()
-    except Exception as exc:
-      logger.warning(f'Impossible d\'envoyer le courrier électronique : {exc}')
+  def configure_mailer(self, smtp_host, smtp_username, smtp_password,
+                       smtp_port, smtp_starttls, smtp_ssl, email_from,
+                       email_to, email_object):
+    self.smtp_host     = smtp_host
+    self.smtp_username = smtp_username
+    self.smtp_password = smtp_password
+    if smtp_port <= 0:
+      self.smtp_port   = 587
+    else:
+      self.smtp_port   = int(smtp_port)
+    self.smtp_starttls = smtp_starttls
+    self.smtp_ssl      = smtp_ssl
+    self.email_from    = email_from
+    self.email_to      = email_to
+    self.email_object  = email_object
+
+    if smtp_host and smtp_username and smtp_password and email_from and email_to and email_object:
+      self.smtp_configured = True
+
+  def mailer(self):
+    if self.smtp_configured and self.found:
+      try:
+        message = EmailMessage()
+        message.set_content(open(self.output_file_path).read())
+
+        message['Subject'] = self.email_object
+        message['From'] = self.email_from
+
+        context = ssl.create_default_context()
+
+        if self.smtp_ssl == True:
+          for address in self.email_to.split(','):
+            del message['To']
+            message['To'] = address
+            smtp = smtplib.SMTP_SSL(self.smtp_host, port, context=context)
+            if self.smtp_username:
+              smtp.login(self.smtp_username, self.smtp_password)
+            smtp.send_message(message)
+            smtp.quit()
+        elif self.smtp_starttls == True:
+          for address in self.email_to.split(','):
+            del message['To']
+            message['To'] = address
+            smtp = smtplib.SMTP(self.smtp_host)
+            smtp.starttls(context=context)
+            if self.smtp_username:
+              smtp.login(self.smtp_username, self.smtp_password)
+            smtp.send_message(message)
+            smtp.quit()
+        else:
+          for address in self.email_to.split(','):
+            del message['To']
+            message['To'] = address
+            smtp = smtplib.SMTP(self.smtp_host)
+            if self.smtp_username:
+              smtp.login(self.smtp_username, self.smtp_password)
+            smtp.send_message(message)
+            smtp.quit()
+      except Exception as exc:
+        logger.warning(f'Impossible d\'envoyer le courrier électronique : {exc}')
diff --git a/RAAspotter_ppparis.py b/RAAspotter_ppparis.py
index 412dabc..7c09dbd 100644
--- a/RAAspotter_ppparis.py
+++ b/RAAspotter_ppparis.py
@@ -23,6 +23,7 @@ class RAAspotter_ppparis(RAAspotter):
     page_content = self.get_session()
     raa_elements = self.get_raa_elements(page_content)
     self.parse_raa(raa_elements, keywords.split(','))
+    self.mailer()
 
   def get_raa_elements(self, page_content):
     elements = []
diff --git a/RAAspotter_pref13.py b/RAAspotter_pref13.py
index 7d56006..a7cf2c2 100644
--- a/RAAspotter_pref13.py
+++ b/RAAspotter_pref13.py
@@ -32,6 +32,8 @@ class RAAspotter_pref13(RAAspotter):
       raa_elements = self.get_raa_elements(page_content)
       self.parse_raa(raa_elements, keywords.split(','))
 
+    self.mailer()
+
   def get_raa_elements(self, page_content):
     elements = []
     # On charge le parser
diff --git a/RAAspotter_pref35.py b/RAAspotter_pref35.py
index e7765b4..67dd21d 100644
--- a/RAAspotter_pref35.py
+++ b/RAAspotter_pref35.py
@@ -32,6 +32,8 @@ class RAAspotter_pref35(RAAspotter):
       raa_elements = self.get_raa_elements(page_content)
       self.parse_raa(raa_elements, keywords.split(','))
 
+    self.mailer()
+
   def get_raa_elements(self, page_content):
     elements = []
     # On charge le parser
diff --git a/ppparis.py b/ppparis.py
index e108a16..7b4678e 100755
--- a/ppparis.py
+++ b/ppparis.py
@@ -88,11 +88,7 @@ os.makedirs(__DATA_DIR, exist_ok=True)
 
 raa_spotter = RAAspotter_ppparis(__DATA_DIR)
 raa_spotter.not_before = __NOT_BEFORE
+raa_spotter.configure_mailer(__SMTP_HOSTNAME, __SMTP_USERNAME, __SMTP_PASSWORD, __SMTP_PORT,
+                             __SMTP_STARTTLS, __SMTP_SSL, __EMAIL_FROM, __EMAIL_TO,
+                             '[RAAspotter] [ppparis] Nouveaux éléments trouvés')
 raa_spotter.get_raa(__KEYWORDS)
-
-if raa_spotter.found == True and __SMTP_HOSTNAME and __SMTP_USERNAME and __SMTP_PASSWORD and __SMTP_PORT and __EMAIL_TO and __EMAIL_FROM:
-  output = open(raa_spotter.output_file_path)
-  RAAspotter.mailer(__SMTP_HOSTNAME, __SMTP_USERNAME, __SMTP_PASSWORD, __SMTP_PORT,
-                    __SMTP_STARTTLS, __SMTP_SSL, __EMAIL_FROM, __EMAIL_TO,
-                    '[RAAspotter] [ppparis] Nouveaux éléments trouvés', 
-                    output.read())
diff --git a/pref13.py b/pref13.py
index 87b64f4..75eadcb 100755
--- a/pref13.py
+++ b/pref13.py
@@ -88,11 +88,7 @@ os.makedirs(__DATA_DIR, exist_ok=True)
 
 raa_spotter = RAAspotter_pref13(__DATA_DIR)
 raa_spotter.not_before = __NOT_BEFORE
+raa_spotter.configure_mailer(__SMTP_HOSTNAME, __SMTP_USERNAME, __SMTP_PASSWORD, __SMTP_PORT,
+                             __SMTP_STARTTLS, __SMTP_SSL, __EMAIL_FROM, __EMAIL_TO,
+                             '[RAAspotter] [pref13] Nouveaux éléments trouvés')
 raa_spotter.get_raa(__KEYWORDS)
-
-if raa_spotter.found == True and __SMTP_HOSTNAME and __SMTP_USERNAME and __SMTP_PASSWORD and __SMTP_PORT and __EMAIL_TO and __EMAIL_FROM:
-  output = open(raa_spotter.output_file_path)
-  RAAspotter.mailer(__SMTP_HOSTNAME, __SMTP_USERNAME, __SMTP_PASSWORD, __SMTP_PORT,
-                    __SMTP_STARTTLS, __SMTP_SSL, __EMAIL_FROM, __EMAIL_TO,
-                    '[RAAspotter] [pref13] Nouveaux éléments trouvés', 
-                    output.read())
diff --git a/pref35.py b/pref35.py
index 70c544d..5de33c6 100755
--- a/pref35.py
+++ b/pref35.py
@@ -88,11 +88,7 @@ os.makedirs(__DATA_DIR, exist_ok=True)
 
 raa_spotter = RAAspotter_pref35(__DATA_DIR)
 raa_spotter.not_before = __NOT_BEFORE
+raa_spotter.configure_mailer(__SMTP_HOSTNAME, __SMTP_USERNAME, __SMTP_PASSWORD, __SMTP_PORT,
+                             __SMTP_STARTTLS, __SMTP_SSL, __EMAIL_FROM, __EMAIL_TO,
+                             '[RAAspotter] [ppparis] Nouveaux éléments trouvés')
 raa_spotter.get_raa(__KEYWORDS)
-
-if raa_spotter.found == True and __SMTP_HOSTNAME and __SMTP_USERNAME and __SMTP_PASSWORD and __SMTP_PORT and __EMAIL_TO and __EMAIL_FROM:
-  output = open(raa_spotter.output_file_path)
-  RAAspotter.mailer(__SMTP_HOSTNAME, __SMTP_USERNAME, __SMTP_PASSWORD, __SMTP_PORT,
-                    __SMTP_STARTTLS, __SMTP_SSL, __EMAIL_FROM, __EMAIL_TO,
-                    '[RAAspotter] [pref35] Nouveaux éléments trouvés', 
-                    output.read())
-- 
GitLab