Browse Source

Merge branch 'email' of lcmstaff/lcmlog-server into master

Matteo Zeccoli Marazzini 2 years ago
parent
commit
5041c043ac
2 changed files with 26 additions and 14 deletions
  1. 1 0
      README.md
  2. 25 14
      lcmlog-server

+ 1 - 0
README.md

@@ -20,6 +20,7 @@ The `POST` method is used to add a log to the database.
 * The second line of the input is the type (se below).
 * The third one is the date.
 * The fourth one is a list of comma separated tags.
+* The fifth one is, optionally, "MAIL" if the server should email the log contents after storing them in the database (the mail program should be preconfigured for this to work)
 * The remainder of the file (until EOF is met) is the content of the log.
 
 ### UPDATE

+ 25 - 14
lcmlog-server

@@ -3,7 +3,7 @@
 DIR         = "/var/local/log/lcmlog-data"
 FROM_DOMAIN = "lcm.mi.infn.it"
 TO_ADDRESS  = "working@lcm.mi.infn.it"
-REPLY_TO    = True              # Does this make sense?
+REPLY_TO    = True
 
 import os
 import os.path
@@ -69,10 +69,20 @@ def main():
 			if kind != "150" and kind != "Admin":	# We only have this two log types
 				raise KindError
 			if method == "POST":
-				send_mail = input()
-				auth(user_id, "POST", kind)     # Check if the user can post for the requested kind
-				log = sys.stdin.read() # Read the log content
-				method_post(kind, user_name, date, tags, log, send_mail == "Yes")
+                                optional_line = input()
+                                auth(user_id, "POST", kind) # Check if the user can post for the requested kind
+                                log = sys.stdin.read()      # Read the log content
+                                if optional_line == "MAIL":
+                                        send_mail = True
+                                        TO_ADDRESS = "working@lcm.mi.infn.it"
+                                        REPLY_TO   = True
+                                else:
+                                        send_mail = False
+                                        # optional_line consumes the input but it is not
+                                        # a recognized header, so it's
+                                        # the first line of the log
+                                        log = optional_line + log
+                                method_post(kind, user_name, date, tags, log, send_mail)
 			elif method == "GET":
 				auth(user_id, "GET", kind)	# Check if the user can get logs for the requested kind
 				user_to_find = input() # Read the user name of the log writer to search
@@ -228,12 +238,11 @@ def log_hash(name):
 # True add the 'Reply-to: ' header to the email
 def log_mail(kind, user_name, date, tags, log):
 	if REPLY_TO:
-                subprocess.run("printf " + shlex.quote(log) + " | mail -s \"Log" + kind + " " + date + " " + tags +
-                               "\" -r " + user + FROM_DOMAIN + " -a \"Reply-to:" TO_ADDRESS + " \" -- "
-		               + TO_ADDRESS, shell=True)
+                subprocess.run('printf "%s\n" ' + f'{shlex.quote(log)} | mail -s "Log{kind} {date} {tags}" ' +
+                               f'-r {user_name}@{FROM_DOMAIN} -S replyto="{TO_ADDRESS}" {TO_ADDRESS}', shell=True)
 	else:
-		subprocess.run("printf \"%s\n\" " + shlex.quote(log) + " | mail -s \"Log" | kind + " " + date + " " + tags +
-		               "\" -r " + user + FROM_DOMAIN + " -- " + TO_ADDRESS, shell=True)
+                subprocess.run('printf "%s\n" ' + f'{shlex.quote(log)} | mail -s "Log{kind} {date} {tags}" ' +
+                               f'-r {user_name}@{FROM_DOMAIN} {TO_ADDRESS}', shell=True)
 	return
 
 
@@ -256,10 +265,12 @@ def method_get(kind, user_to_find, date, tags):
 
 # Write log
 def method_post(kind, user_name, date, tags, log, send_mail):
-	name = log_create(kind, user_name, date, tags, log)
-	if send_mail:
-		log_mail(kind, user_name, date, tags, log)
-	logger.info("POST successful: hash " + name)
+        name = log_create(kind, user_name, date, tags, log)
+        logger.info("POST successful: hash " + name)
+        if send_mail:
+                log_mail(kind, user_name, date, tags, log)
+                logger.info("MAIL sent.")
+
 
 
 # Generate .data file