Parse ONE procmailrc, and write its sieve rules.
{
$email=preg_replace(
"#_([^_]*)$#",
"@$1",
$user);
for(
$i=0;
$i<count($rules);
$i++) {
list($rules[
$i][
"conds"],$rules[$i][
"actionparam"])=
describe($rules[
$i]);
}
$u=substr($u,0,1);
@mkdir($SIEVEROOT.
"/".$u.
"/".
$user.
"/sieve");
@mkdir($SIEVEROOT.
"/".$u.
"/".
$user.
"/sieve/tmp");
chown($SIEVEROOT.
"/".$u.
"/".
$user.
"/sieve",
$uid);
chgrp($SIEVEROOT.
"/".$u.
"/".
$user.
"/sieve",
"vmail");
chown($SIEVEROOT.
"/".$u.
"/".
$user.
"/sieve/tmp",
$uid);
chgrp($SIEVEROOT.
"/".$u.
"/".
$user.
"/sieve/tmp",
"vmail");
$f=fopen($SIEVEROOT.
"/".$u.
"/".
$user.
"/sieve/tmp/phpscript.sieve",
"wb");
if (!$f) {
echo "ERROR: Can't open '$user' in '$SIEVEROOT' for writing\n";
} else {
echo "OK: writing sieve script for $user (".count($rules)." rules)\n";
$avelsieveversion=array("major" => 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;
$avelrule=array();
$script="if ";
$avelrule["condition"]="and";
$avelrule["type"]=1;
if (!count($rule["conds"])) {
$script.="true\n";
$avelrule["cond"][]=array("kind" => "message", "type" => "all");
} else {
$script.="allof (";
$first=true;
foreach($conds as $cond) {
if (!$first) $script.=",\n";
$first=false;
switch($cond[0]) {
case 0:
$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:
$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:
$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:
$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:
$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:
$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:
$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";
}
switch($rule["type"]) {
case 1:
$script.='fileinto "'.str_replace('"','\\"',$rule["actionparam"]).'";
stop;
';
$avelrule["action"] = 5;
$avelrule["folder"] = $rule["actionparam"];
$avelrule["stop"] = "on";
break;
case 3:
$script.='discard;
stop;
';
$avelrule["action"] = 2;
$avelrule["stop"] = "on";
break;
case 4:
$script.='redirect "'.str_replace('"','\\"',$rule["actionparam"]).'";
';
$avelrule["action"] = 4;
$avelrule["redirectemail"] = $rule["actionparam"];
break;
case 5:
$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";
fputs($f,"#START_SIEVE_RULE".urlencode(base64_encode(serialize($avelrule)))."END_SIEVE_RULE\n");
fputs($f,$script);
}
fclose($f);
@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";
}
}