From aa78fde6a92f66b84d626e114d8b54a5fb6ece0c Mon Sep 17 00:00:00 2001
From: ftimme <ft@falkotimme.com>
Date: Wed, 14 Nov 2012 07:19:43 -0500
Subject: [PATCH] - Added function for IPv6 prefixes in multiserver mirror setups to nginx plugin.

---
 server/plugins-available/maildrop_plugin.inc.php |   85 +++++++++++++++++++++++++++++-------------
 1 files changed, 59 insertions(+), 26 deletions(-)

diff --git a/server/plugins-available/maildrop_plugin.inc.php b/server/plugins-available/maildrop_plugin.inc.php
index ce5e643..085a992 100644
--- a/server/plugins-available/maildrop_plugin.inc.php
+++ b/server/plugins-available/maildrop_plugin.inc.php
@@ -41,7 +41,7 @@
 	function onInstall() {
 		global $conf;
 		
-		if($conf['services']['mail'] == true) {
+		if($conf['services']['mail'] == true && isset($conf['courier']['installed']) && $conf['courier']['installed'] == true) {
 			return true;
 		} else {
 			return false;
@@ -60,6 +60,7 @@
 		Register for the events
 		*/
 		
+		$app->plugins->registerEvent('mail_user_insert','maildrop_plugin','update');
 		$app->plugins->registerEvent('mail_user_update','maildrop_plugin','update');
 		$app->plugins->registerEvent('mail_user_delete','maildrop_plugin','delete');
 		
@@ -82,8 +83,8 @@
 		if(!is_dir($this->mailfilter_config_dir)) {
 			$app->log("Mailfilter config directory '".$this->mailfilter_config_dir."' does not exist. Creating it now.",LOGLEVEL_WARN);
 			mkdir($this->mailfilter_config_dir);
-			exec("chown vmail ".$this->mailfilter_config_dir);
-			exec("chmod 770 ".$this->mailfilter_config_dir);
+			chown($this->mailfilter_config_dir, 'vmail');
+			chmod($this->mailfilter_config_dir, 0770);
 		}
 		
 		if(isset($data["new"]["email"])) {
@@ -95,20 +96,21 @@
 		// make sure that the config directories exist
 		if(!is_dir($this->mailfilter_config_dir.'/'.$email_parts[1])) {
 			mkdir($this->mailfilter_config_dir.'/'.$email_parts[1]);
-			exec("chown vmail ".$this->mailfilter_config_dir.'/'.$email_parts[1]);
-			exec("chmod 770 ".$this->mailfilter_config_dir.'/'.$email_parts[1]);
+			chown($this->mailfilter_config_dir.'/'.$email_parts[1], 'vmail');
+			chmod($this->mailfilter_config_dir.'/'.$email_parts[1], 0770);
 		}
 		if(!is_dir($this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0])) {
 			mkdir($this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0]);
-			exec("chown vmail ".$this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0]);
-			exec("chmod 770 ".$this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0]);
+			chown($this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0], 'vmail');
+			chmod($this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0], 0770);
 		}
-		
 		
 		// Check if something has been changed regarding the autoresponders
 		if($data["old"]["autoresponder_text"] != $data["new"]["autoresponder_text"] 
 			    or $data["old"]["autoresponder"] != $data["new"]["autoresponder"]
-			    or (isset($data["new"]["email"]) and $data["old"]["email"] != $data["new"]["email"])) {
+			    or (isset($data["new"]["email"]) and $data["old"]["email"] != $data["new"]["email"])
+			    or $data["old"]["autoresponder_start_date"] != $data["new"]["autoresponder_start_date"]
+			    or $data["old"]["autoresponder_end_date"] != $data["new"]["autoresponder_end_date"]) {
 			   		
 			// We delete the old autoresponder, if it exists
 			$email_parts = explode("@",$data["old"]["email"]);
@@ -124,7 +126,8 @@
 			if(is_file($file)) unlink($file) or $app->log("Unable to delete file: $file",LOGLEVEL_WARN);
 			$file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.autoresponder';
 			if(is_file($file)) unlink($file) or $app->log("Unable to delete file: $file",LOGLEVEL_WARN);
-			   		
+			
+			
 			//Now we create the new autoresponder, if it is enabled
 			if($data["new"]["autoresponder"] == 'y') {
 				if(isset($data["new"]["email"])) {
@@ -136,29 +139,43 @@
 				// Load the master template
 				$tpl = file_get_contents($conf["rootpath"].'/conf/autoresponder.master');
 				$tpl = str_replace('{vmail_mailbox_base}',$mail_config["homedir_path"],$tpl);
+				
+				if ($data["new"]["autoresponder_start_date"] != '0000-00-00 00:00:00') { // Dates have been set
+					$tpl = str_replace('{start_date}',strtotime($data["new"]["autoresponder_start_date"]),$tpl);
+					$tpl = str_replace('{end_date}',strtotime($data["new"]["autoresponder_end_date"]),$tpl);
+				} else {
+					$tpl = str_replace('{start_date}',-7200,$tpl);
+					$tpl = str_replace('{end_date}',2147464800,$tpl);
+				}
+				
 				// Write the config file.
 				$config_file_path = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.autoresponder';
 				file_put_contents($config_file_path,$tpl);
 				$app->log("Writing Autoresponder mailfilter file: $config_file_path",LOGLEVEL_DEBUG);
-				exec("chmod 770 $config_file_path");
-				exec("chown vmail $config_file_path");
+				chmod($config_file_path, 0770);
+				chown($config_file_path, 'vmail');
 				unset($tpl);
 				unset($config_file_path);
 						
 				// Write the autoresponder message file
 				$config_file_path = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.msg';
 				file_put_contents($config_file_path,$data["new"]["autoresponder_text"]);
-				exec("chmod 770 $config_file_path");
-				exec("chown vmail $config_file_path");
+				chmod($config_file_path, 0770);
+				chown($config_file_path, 'vmail');
 				$app->log("Writing Autoresponder message file: $config_file_path",LOGLEVEL_DEBUG);
 			}
 		}
 			
-			// Write the custom mailfilter script, if mailfilter recipe has changed
-			if($data["old"]["custom_mailfilter"] != $data["new"]["custom_mailfilter"] or
-			   $data["old"]["move_junk"] != $data["new"]["move_junk"]) {
+		// Write the custom mailfilter script, if mailfilter recipe has changed
+		if($data["old"]["custom_mailfilter"] != $data["new"]["custom_mailfilter"]
+			or $data["old"]["move_junk"] != $data["new"]["move_junk"]
+			or $data["old"]["cc"] != $data["new"]["cc"]) {
+			   
 				$app->log("Mailfilter config has been changed",LOGLEVEL_DEBUG);
-				if(trim($data["new"]["custom_mailfilter"]) != '' or $data["new"]["move_junk"] != 'n') {
+				if(trim($data["new"]["custom_mailfilter"]) != '' 
+					or $data["new"]["move_junk"] != 'n' 
+					or $data["new"]["cc"] != '') {
+					
 					// Delete the old filter recipe
 					$email_parts = explode("@",$data["old"]["email"]);
 					$file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.mailfilter';
@@ -173,24 +190,36 @@
 					$config_file_path = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.mailfilter';
 					
 					$mailfilter_content = '';
+					
+					if($data["new"]["cc"] != '') {
+						$mailfilter_content .= "cc \"!".$data["new"]["cc"]."\"\n";
+						$app->log("Added CC address ".$data["new"]["cc"].' to mailfilter file.',LOGLEVEL_DEBUG);
+					}
+					
 					if($data["new"]["move_junk"] == 'y') {
 						$mailfilter_content .= file_get_contents($conf["rootpath"].'/conf/mailfilter_move_junk.master')."\n";
 					}
 					$mailfilter_content .= $data["new"]["custom_mailfilter"];
 					
+					// Replace windows linebreaks in mailfilter file
+					$mailfilter_content = str_replace("\r\n","\n",$mailfilter_content);
+					
 					file_put_contents($config_file_path,$mailfilter_content);
 					$app->log("Writing new custom Mailfiter".$config_file_path,LOGLEVEL_DEBUG);
-					exec("chmod 770 $config_file_path");
-					exec("chown vmail $config_file_path");
+					chmod($config_file_path, 0770);
+					chown($config_file_path, 'vmail');
 					unset($config_file_path);
 				} else {
 					// Delete the mailfilter recipe
-					$email_parts = explode("@",$data["old"]["email"]);
-					$file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.mailfilter';
-					if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file",LOGLEVEL_WARN);
-					$app->log("Deleting custom Mailfiter".$file,LOGLEVEL_DEBUG);
+					if(isset($data["old"]["email"])) {
+						$email_parts = explode("@",$data["old"]["email"]);
+						$file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.mailfilter';
+						if(is_file($file)) {
+							unlink($file)  or $app->log("Unable to delete file: $file",LOGLEVEL_WARN);
+							$app->log("Deleting custom Mailfiter".$file,LOGLEVEL_DEBUG);
+						}
+					}
 				}
-			//}
 		}
 	}
 	
@@ -215,6 +244,10 @@
 			if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file",LOGLEVEL_WARN);
 			$file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.mailfilter';
 			if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file",LOGLEVEL_WARN);
+			$file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lst.gdbm';
+			if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file",LOGLEVEL_WARN);
+			$file = $this->mailfilter_config_dir.'/'.$email_parts[1].'/'.$email_parts[0].'/.vacation.lst.lock';
+			if(is_file($file)) unlink($file)  or $app->log("Unable to delete file: $file",LOGLEVEL_WARN);
 			rmdir($dir) or $app->log("Unable to delete directory: $dir",LOGLEVEL_WARN);
 		}
 	}
@@ -222,4 +255,4 @@
 
 } // end class
 
-?>
\ No newline at end of file
+?>

--
Gitblit v1.9.1