From b29374939ce56e9ded77a450ba0de9630aa7d4ee Mon Sep 17 00:00:00 2001
From: Michael Hierweck <michael@hierweck.de>
Date: Tue, 12 Sep 2017 20:29:15 +0200
Subject: [PATCH] Cleanup.

---
 hsarback/src/de/hsadmin/mods/email/EMailAddressProcessorFactory.java |   24 +++++++++++++-----------
 hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java         |   35 ++++++++++++++++++-----------------
 2 files changed, 31 insertions(+), 28 deletions(-)

diff --git a/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java b/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
index 1b82d6f..b6d3505 100644
--- a/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
+++ b/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
@@ -4,6 +4,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javax.persistence.EntityManager;
 import javax.persistence.Query;
@@ -161,19 +162,19 @@
 							"." + dom.getName() + " smtp:" + inetAddr + ":225\n")
 					);
 			cp.appendProcessor(
-					new ShellProcessor("postmap -r -i /etc/postfix-mailin/relayrecipients", 
-							"@" + dom.getName() + " anything\n")
+					new ShellProcessor("postmap -d - /etc/postfix-mailin/relayrecipients", 
+							"@" + dom.getName() + "\n")
 					);			
 		} else {
 			cp.appendProcessor(
-					new ShellProcessor(
-						    "postmap -d '@" + dom.getName() + "' /etc/postfix-mailin/relayrecipients")							
-					);			
-			cp.appendProcessor(
-					new ShellProcessor(
-							"postmap -d '" + dom.getName() + "' /etc/postfix-mailin/transport && " +
-							"postmap -d '." + dom.getName() + "' /etc/postfix-mailin/transport")							
+					new ShellProcessor("postmap -d - /etc/postfix-mailin/transport", 
+							dom.getName() + "\n" +
+							"." + dom.getName() + "\n")							
 					);
+			cp.appendProcessor(
+					new ShellProcessor("postmap -r -i /etc/postfix-mailin/relayrecipients",
+						    "@" + dom.getName() + " anything\n")							
+					);						
 		}
 		return cp;
 	}
@@ -202,14 +203,14 @@
 	}
 
 	private Processor createMailinUnsetupProcessor(EntityManager em, Domain dom) throws ProcessorException {
-		Processor mailQueueProcessor = new CompoundProcessor(
-				createPostgreyConfiguration(em),				
-				new ShellProcessor(
-						"postmap -d '@" + dom.getName() + "' /etc/postfix-mailin/relayrecpients && " +						
-						"postmap -d '" + dom.getName() + "' /etc/postfix-mailin/relaydomains && " +
-						"postmap -d '" + dom.getName() + "' /etc/postfix-mailin/transport && " +
-						"postmap -d '." + dom.getName() + "' /etc/postfix-mailin/relaydomains && " +
-						"postmap -d '." + dom.getName() + "' /etc/postfix-mailin/transport"));
+		final CompoundProcessor mailQueueProcessor = new CompoundProcessor(createPostgreyConfiguration(em));
+		mailQueueProcessor.appendProcessor(new ShellProcessor("postmap -d - /etc/postfix-mailin/relayrecipients",						
+													dom.getName() + "\n" +
+													"." + dom.getName() + "\n" +
+													"@" + dom.getName() + "\n"));
+		mailQueueProcessor.appendProcessor(new ShellProcessor("postmap -d - /etc/postfix-mailin/transports",
+													dom.getName() + "\n" +
+													"." + dom.getName() + "\n"));
 		return mailQueueProcessor;
 	}
 
diff --git a/hsarback/src/de/hsadmin/mods/email/EMailAddressProcessorFactory.java b/hsarback/src/de/hsadmin/mods/email/EMailAddressProcessorFactory.java
index 9b5d642..19efe5c 100644
--- a/hsarback/src/de/hsadmin/mods/email/EMailAddressProcessorFactory.java
+++ b/hsarback/src/de/hsadmin/mods/email/EMailAddressProcessorFactory.java
@@ -12,16 +12,18 @@
 import de.hsadmin.core.qserv.ShellProcessor;
 import de.hsadmin.core.qserv.WaitingTasksProcessor;
 import de.hsadmin.core.util.Config;
-import de.hsadmin.mods.pac.Pac;
 
 public class EMailAddressProcessorFactory implements EntityProcessorFactory {
 
 	public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) {
-		// TODO: combine both keys in a single call (optimization)
-		EMailAddress email = (EMailAddress) entity;
+		// TODO: combine both keys in a single call (optimization)		
 		CompoundProcessor cp = new CompoundProcessor();
-		cp.appendProcessor(
-				new ShellProcessor("postmap -r -i /etc/postfix-mailin/virtual", email.getFullDomain() + " -"));
+		EMailAddress email = (EMailAddress) entity;
+		if (emailAddressCount(em, email) == 1) {
+			// first email address added
+			cp.appendProcessor(new ShellProcessor("postmap -r -i /etc/postfix-mailin/virtual",
+					email.getFullDomain() + " -"));
+		}
 		cp.appendProcessor(new ShellProcessor("postmap -r -i /etc/postfix-mailin/virtual",
 				email.getEMailAddress() + " " + email.getTarget()));
 		WaitingTasksProcessor waitingTasksProcessor = new WaitingTasksProcessor(cp);
@@ -46,17 +48,17 @@
 		EMailAddress email = (EMailAddress) entity;
 		if (emailAddressCount(em, email) == 0) {
 			// last email address removed
-			String fullDomain = email.getFullDomain();
-			cp.appendProcessor(new ShellProcessor("postmap -d '" + fullDomain + "' /etc/postfix-mailin/virtual"));
+			cp.appendProcessor(new ShellProcessor("postmap -d - /etc/postfix-mailin/virtual",
+					email.getFullDomain() + "\n"));
 		}
-		cp.appendProcessor(
-				new ShellProcessor("postmap -d '" + email.getEMailAddress() + "' /etc/postfix-mailin/virtual"));
+		cp.appendProcessor(new ShellProcessor("postmap -d - /etc/postfix-mailin/virtual", 
+				email.getEMailAddress() + "\n"));
 		Config config = Config.getInstance();
 		WaitingTasksProcessor waitingTasksProcessor = new WaitingTasksProcessor(cp);
 		for (String queueName : config.getProperty("queues.mail").split(",")) {
 			waitingTasksProcessor.appendProcessor(queueName,
-					new ShellProcessor(
-							"postmap -d '" + email.getEMailAddress() + "' /etc/postfix-mailin/relayrecipients"),
+					new ShellProcessor("postmap -d - /etc/postfix-mailin/relayrecipients", 
+							email.getEMailAddress() + "\n"),
 					"Remove from relayrecipients");
 		}
 		return waitingTasksProcessor;

--
Gitblit v1.9.0-SNAPSHOT