From aabb6f6617bcb11c898da17c4539ae1c481ee857 Mon Sep 17 00:00:00 2001 From: Benjamin Sonntag Date: Thu, 7 Feb 2013 16:52:14 +0000 Subject: [PATCH] adding PROCMAILBUILDER to AVELSIEVE migration script \o/ Refers #1262 --- .gitattributes | 1 + squirrelmail/procmail_to_sieve.php | 146 ++++++++++++++++++++++++++++- squirrelmail/unavelsieve | 15 +++ 3 files changed, 157 insertions(+), 5 deletions(-) create mode 100755 squirrelmail/unavelsieve diff --git a/.gitattributes b/.gitattributes index 62e9d0e1..cc93da7a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -592,6 +592,7 @@ squirrelmail/templates/apache2/squirrelmail.conf -text squirrelmail/templates/squirrelmail/alternc-changepass.conf -text squirrelmail/templates/squirrelmail/apache.conf -text squirrelmail/templates/squirrelmail/avelsieve-config.php -text +squirrelmail/unavelsieve -text src/Makefile -text src/alternc-check -text src/alternc-dboptimize -text diff --git a/squirrelmail/procmail_to_sieve.php b/squirrelmail/procmail_to_sieve.php index 3fb1d913..8054e2b0 100755 --- a/squirrelmail/procmail_to_sieve.php +++ b/squirrelmail/procmail_to_sieve.php @@ -30,13 +30,13 @@ */ function procmail2sieve() { global $ROOT; - $d=opendir($ROOT); + $d=@opendir($ROOT); if ($d) { while ($c=readdir($d)) { if (substr($c,0,1)==".") continue; // skip hidden files if (is_dir($ROOT."/".$c)) { // Go to level 2. - $e=opendir($ROOT."/".$c); + $e=@opendir($ROOT."/".$c); if ($e) { while ($f=readdir($e)) { if (substr($f,0,1)==".") continue; // skip hidden files @@ -46,11 +46,16 @@ function procmail2sieve() { } } closedir($e); + } else { + echo "ERROR: Cannot open ".$ROOT."/".$c."\n"; } } } closedir($d); + } else { + echo "FATAL: cannot open ".$ROOT."\n"; + exit(); } } /* procmail2sieve */ @@ -61,6 +66,7 @@ function procmail2sieve() { */ function parseOneProcmail($user) { global $SIEVEROOT; + $email=preg_replace("#_([^_]*)$#","@$1",$user); if ($rules=readrules($user)) { /* ################## SUB FUNCTION ###################### */ for($i=0; $i 1, "minor"=>9, "release" => 9, "string" => "1.9.9"); + fputs($f,'# This script has been automatically generated by avelsieve +# (Sieve Mail Filters Plugin for Squirrelmail) +# Warning: If you edit this manually, then the changes will not +# be reflected in the users\' front-end! +#AVELSIEVE_VERSION'.urlencode(base64_encode(serialize($avelsieveversion))).' +#AVELSIEVE_CREATED'.time().' +#AVELSIEVE_MODIFIED'.time().' +require ["fileinto","envelope","reject","vacation","imap4flags","relational","comparator-i;ascii-numeric","regex","body","date"]; +'); + foreach($rules as $rule) { + if ($rule["type"]==2) continue; // IGNORE "Filter the message through SpamAssassin" + + // Create the avelsieve array: + $avelrule=array(); + // And sieve script: + $script="if "; + $avelrule["condition"]="and"; + $avelrule["type"]=1; + if (!count($rule["conds"])) { + // no conditions + $script.="true\n"; + $avelrule["cond"][]=array("kind" => "message", "type" => "all"); + } else { // have conditions + $script.="allof ("; + $first=true; + foreach($conds as $cond) { + if (!$first) $script.=",\n"; + $first=false; + // What kind of condition? + switch($cond[0]) { + case 0: // subject + $script.='header :contains "Subject" "'.str_replace('"','\\"',$cond[1]).'"'; + $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "Subject", "matchtype" => "contains", "headermatch" => $cond[1] ); + break; + case 1: // sender + $script.='header :contains "From" "'.str_replace('"','\\"',$cond[1]).'"'; + $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "From", "matchtype" => "contains", "headermatch" => $cond[1] ); + break; + case 2: // recipient + $script.='address :contains ["to", "cc"] "'.str_replace('"','\\"',$cond[1]).'"'; + $avelrule["cond"][]=array("kind" => "message", "type" => "address", "address" => "toorcc", "matchtype" => "contains", "addressmatch" => $cond[1] ); + break; + case 3: // List-Post + $script.='header :contains "List-Post" "'.str_replace('"','\\"',$cond[1]).'"'; + $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "List-Post", "matchtype" => "contains", "headermatch" => $cond[1] ); + break; + case 4: // List-Id + $script.='header :contains "List-Id" "'.str_replace('"','\\"',$cond[1]).'"'; + $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "List-Id", "matchtype" => "contains", "headermatch" => $cond[1] ); + break; + case 5: // Spamassassin + $script.='header :contains "X-Spam-Status" "Yes"'; + $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "X-Spam-Status", "matchtype" => "contains", "headermatch" => "Yes" ); + break; + case 6: // Delivered-To + $script.='envelope :contains "to" "'.str_replace('"','\\"',$cond[1]).'"'; + $avelrule["cond"][]=array("kind" => "message", "type" => "envelope", "envelope" => "to", "matchtype" => "contains", "envelopematch" => $cond[1] ); + break; + + } + } + $script.=")\n{\n"; + } + // Now the ACTION: + switch($rule["type"]) { + case 1: // move to + $script.='fileinto "'.str_replace('"','\\"',$rule["actionparam"]).'"; +stop; +'; + $avelrule["action"] = 5; + $avelrule["folder"] = $rule["actionparam"]; + $avelrule["stop"] = "on"; + break; + case 3: // Discard (for good) + $script.='discard; +stop; +'; + $avelrule["action"] = 2; + $avelrule["stop"] = "on"; + break; + case 4: // Forward To (copy) + $script.='redirect "'.str_replace('"','\\"',$rule["actionparam"]).'"; +'; + $avelrule["action"] = 4; + $avelrule["redirectemail"] = $rule["actionparam"]; + break; + case 5: // Auto-Reply + $script.='vacation :days 7 :addresses ["'.$email.'"@ :subject "Auto Reply" text: +'.str_replace("\\'","'",@file_get_contents($ROOT."/".$u."/".$user."/".$user.".txt")).' +. +; +'; + $avelrule["action"] = 6; + $avelrule["vac_addresses"] = $email; + $avelrule["vac_subject"] = "Auto Reply"; + $avelrule["vac_days"] = 7; + $avelrule["vac_message"] = @file_get_contents($ROOT."/".$u."/".$user."/".$user.".txt"); + break; + } + $script.="}\n"; + // Now put it into the script file : + fputs($f,"#START_SIEVE_RULE".urlencode(base64_encode(serialize($avelrule)))."END_SIEVE_RULE\n"); + fputs($f,$script); + /* +if allof (header :contains "From" "expediteur@coin.pan", +header :contains "To" "destinataire@coin.pan") +{ +fileinto "INBOX.test"; +stop; +} + */ + } /* for each rule */ fclose($f); + + // Then Move it to the right place + @unlink($SIEVEROOT."/".$u."/".$user."/sieve/phpscript.sieve"); + rename( + $SIEVEROOT."/".$u."/".$user."/sieve/tmp/phpscript.sieve", + $SIEVEROOT."/".$u."/".$user."/sieve/phpscript.sieve" + ); + chown($SIEVEROOT."/".$u."/".$user."/sieve/phpscript.sieve",$uid); + chgrp($SIEVEROOT."/".$u."/".$user."/sieve/phpscript.sieve","vmail"); } } else { echo "ERROR: can't read rules for $user\n"; diff --git a/squirrelmail/unavelsieve b/squirrelmail/unavelsieve new file mode 100755 index 00000000..4bc71f44 --- /dev/null +++ b/squirrelmail/unavelsieve @@ -0,0 +1,15 @@ +#!/usr/bin/php +\nPrint the content of an avelsieve-encoded string\n"; + exit(); +} +if (is_file($argv[1])) { + $content=file_get_contents($argv[1]); +} else { + $content=$argv[1]; +} + +print_r(unserialize(base64_decode(urldecode($content)))); +