From c2bcda59b6401dae5263240007bfd39f3da57fce Mon Sep 17 00:00:00 2001
From: admin <admin@whale.am28.uni-tuebingen.de>
Date: Fri, 11 Oct 2019 13:33:34 +0200
Subject: [PATCH] extract credentials to config file

---
 README.md               | 15 ++++++++++++++-
 credentials_template.py | 13 +++++++++++++
 email_client.py         | 32 +++++++++++++++++---------------
 3 files changed, 44 insertions(+), 16 deletions(-)
 create mode 100644 credentials_template.py

diff --git a/README.md b/README.md
index b3021fa..3604f27 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,15 @@
-# etho2020_registration
+# Etho2020 email client listening for registration emails
+
+* checks every 120s for new messages on a given email address, parses the information and sends and confirmation email to the participant and a copy to the conference office.
+
+## Configuration
+
+The root folder contains a ```credentials_template.py``` file
+containing dummy credentials. Fill in the valid information and rename
+file to ```credentials.py```.  The email client expects to find this
+file, otherwise it will not work.
+
+**Beware** the valid ```credentials.py``` file must not be committed to
+  the version controll system and be read-only for all exept the user
+  running the client.
 
diff --git a/credentials_template.py b/credentials_template.py
new file mode 100644
index 0000000..7a7a772
--- /dev/null
+++ b/credentials_template.py
@@ -0,0 +1,13 @@
+EMAIL_ACCOUNT = 'yourlogin'
+EMAIL_FOLDER = 'INBOX'
+EMAIL_ADDRESS = 'the email address that needs to be checked'
+EMAIL_PSWD = 'your_password for email account'
+EMAIL_REPLY =  'email address of the conference office'
+SMTP_SERVER = 'outgoing smtp server address'
+IMAP_SERVER = 'server address for incoming mails'
+
+SUBJECT_PATTERN = "etho2020"
+
+CHECK_INTERVAL = 120  # check every 120s for new mails
+LOGFILE_NAME = 'etho_registration.log'
+LOG_STATUS_INTERVAL = 60  # write status message every 60 checks (2h)
diff --git a/email_client.py b/email_client.py
index 25e9240..a76e744 100644
--- a/email_client.py
+++ b/email_client.py
@@ -13,13 +13,15 @@ import datetime as dt
 import time
 import glob
 
+from credentials import *
 EMAIL_ACCOUNT = "bzigr02"
 EMAIL_FOLDER = "INBOX"
 EMAIL_ADDRESS = 'etho2020registration@biologie.uni-tuebingen.de'
 EMAIL_REPLY = 'etho2020@biologie.uni-tuebingen.de'
 SMTP_SERVER = 'smtpserv.uni-tuebingen.de'
 IMAP_SERVER = 'mailserv.uni-tuebingen.de'
-EMAIL_PSWD = "r.8-*0Fc"
+EMAIL_PSWD = "4npBaMv9"
+
 
 PARTICIPANTS_FOLDER = 'participants'
 
@@ -194,7 +196,7 @@ class Participation(object):
         values = [self._first_name, self._last_name, self._email, self._phone, self._institution,
                   self._address_street, "%s %s" % (self._address_zip, self._address_city),
                   self.registration_date.isoformat(), self._role, str(self.amount_due), str(self.farewell_event),
-                  str(self.gwinner_award), str(self.labtour), str(self._icebreaker), str(self._food_vegi), str(self._food_vegan),
+                  str(self.gwinner_award), str(self.labtour), str(self.icebreaker), str(self._food_vegi), str(self._food_vegan),
                   str(self._food_gluten), str(self._food_normal)]
 
         with io.open(filename, mode="w", encoding="UTF8") as fd:
@@ -235,16 +237,15 @@ def process_mailbox(M):
     Do something with emails messages in the folder.  
     For the sake of this example, print some headers.
     """
-
     rv, data = M.search(None, "UNSEEN")
     if rv != 'OK':
-        print("No messages found!")
+        print("No messages found!", file=log_file)
         return
 
     for num in data[0].split():
         rv, data = M.fetch(num, '(RFC822)')
         if rv != 'OK':
-            print("ERROR getting message", num)
+            print("ERROR getting message", num, file=log_file)
             return
 
         msg = email.message_from_bytes(data[0][1])
@@ -264,17 +265,17 @@ def process_mailbox(M):
 def process_message(M, msg_index):
     rv, data = M.fetch(msg_index.encode('ascii'), '(RFC822)')
     if rv != 'OK':
-        print("ERROR getting message", rv)
+        print("ERROR getting message", rv, file=log_file)
         return
     msg = email.message_from_bytes(data[0][1])
     return Participation(msg)
 
 
 def process_registrations(M):
-    rv, msgs = M.search(None, "SUBJECT", "etho2020", "UNSEEN")
+    rv, msgs = M.search(None, "SUBJECT", SUBJECT_PATTERN, "UNSEEN")
     participations = []
     if rv != "OK":
-        print("ERROR searching messages", rv)
+        print("ERROR searching messages", rv, file=log_file)
         return participations
 
     msgs = msgs[0].decode('ascii').split()
@@ -334,16 +335,17 @@ def check_for_mails():
     try:
         rv, data = M.login(EMAIL_ACCOUNT, EMAIL_PSWD)
     except imaplib.IMAP4.error:
-        print("LOGIN FAILED!!! ")
-        sys.exit(1)
+        print("LOGIN FAILED!!! ", file=log_file)
+        return
+    #        sys.exit(1)
     # rv, mailboxes = M.list()
     # if rv == 'OK':
     #    print("Mailboxes:")
     #    print(mailboxes)
     rv, data = M.select(EMAIL_FOLDER)
     if rv != 'OK':
-        print("ERROR: Unable to open mailbox folder ", rv)
-
+        print("ERROR: Unable to open mailbox folder ", rv, file=log_file)
+        return
     new_participations = process_registrations(M)
     for p in new_participations:
         print("%s\tNew registration by %s" % (dt.datetime.now().isoformat(), p.name), file=log_file)
@@ -354,12 +356,12 @@ def check_for_mails():
 
 
 if __name__ == "__main__":
-    with open('etho_registration.log', 'a', buffering=1) as log_file:
+    with open(LOGFILE_NAME, 'a', buffering=1) as log_file:
         count = 0
         while True:
             check_for_mails()
-            time.sleep(120)
+            time.sleep(CHECK_INTERVAL)
             count +=1
-            if count % 60 == 0:
+            if count % LOG_STATUS_INTERVAL == 0:
                 print("%s\t Still awake and waiting for registrations!" % (dt.datetime.now().isoformat()), file=log_file)