Alternc  3.2
Alternc logiel libre pour l'hébergement
 All Data Structures Namespaces Files Functions Variables Pages
procmail_to_sieve.php
Go to the documentation of this file.
1 #!/usr/bin/php
2 <?php
3 /*
4  AlternC - Web Hosting System
5  Copyright (C) 2002 by the AlternC Development Team.
6  http://alternc.org/
7  ----------------------------------------------------------------------
8  LICENSE
9 
10  This program is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License (GPL)
12  as published by the Free Software Foundation; either version 2
13  of the License, or (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  To read the license please visit http://www.gnu.org/copyleft/gpl.html
21  ----------------------------------------------------------------------
22  Purpose of file: Based on procmail-builder module, converts
23  the procmail generated by AlternC<=1.0.3 to AlternC>=3.0
24  ----------------------------------------------------------------------
25 */
26 
27 /* ----------------------------------------------------------------- */
28 /**
29  MAIN FUNCTION
30  Read all the mail folders and search for .procmailrc's
31 */
32 function procmail2sieve() {
33  global $ROOT;
34  $d=@opendir($ROOT);
35  if ($d) {
36  while ($c=readdir($d)) {
37  if (substr($c,0,1)==".") continue; // skip hidden files
38  if (is_dir($ROOT."/".$c)) {
39  // Go to level 2.
40  $e=@opendir($ROOT."/".$c);
41  if ($e) {
42  while ($f=readdir($e)) {
43  if (substr($f,0,1)==".") continue; // skip hidden files
44  if (is_file($ROOT."/".$c."/".$f."/.procmailrc")) {
45  // We found one .procmailrc, let's parse it on behalf of his user...
46  parseOneProcmail($f); /* ################## SUB FUNCTION ###################### */
47  }
48  }
49  closedir($e);
50  } else {
51  echo "ERROR: Cannot open ".$ROOT."/".$c."\n";
52  }
53 
54  }
55  }
56  closedir($d);
57  } else {
58  echo "FATAL: cannot open ".$ROOT."\n";
59  exit();
60  }
61 } /* procmail2sieve */
62 
63 
64 
65 /* ----------------------------------------------------------------- */
66 /** Parse ONE procmailrc, and write its sieve rules
67  */
69  global $SIEVEROOT;
70  $email=preg_replace("#_([^_]*)$#","@$1",$user);
71  if ($rules=readrules($user)) { /* ################## SUB FUNCTION ###################### */
72  for($i=0; $i<count($rules); $i++) {
73  list($rules[$i]["conds"],$rules[$i]["actionparam"])=describe($rules[$i]); /* ################## SUB FUNCTION ###################### */
74  }
75 
76  // Now we have $rule["type"] = the ACTION to accomplish + (if 1 or 4) $actionparam
77  // and a list of $rule["conds"][0]=condition type & $rule["conds"][1]=condition parameter (if not 5)
78  // Let's write a sieve script :)
79  $u=substr($u,0,1);
80  @mkdir($SIEVEROOT."/".$u."/".$user."/sieve");
81  @mkdir($SIEVEROOT."/".$u."/".$user."/sieve/tmp");
82  $uid=fileowner($SIEVEROOT."/".$u."/".$user);
83  chown($SIEVEROOT."/".$u."/".$user."/sieve",$uid);
84  chgrp($SIEVEROOT."/".$u."/".$user."/sieve","vmail");
85  chown($SIEVEROOT."/".$u."/".$user."/sieve/tmp",$uid);
86  chgrp($SIEVEROOT."/".$u."/".$user."/sieve/tmp","vmail");
87 
88  $f=fopen($SIEVEROOT."/".$u."/".$user."/sieve/tmp/phpscript.sieve","wb");
89  if (!$f) {
90  echo "ERROR: Can't open '$user' in '$SIEVEROOT' for writing\n";
91  } else {
92  echo "OK: writing sieve script for $user (".count($rules)." rules)\n";
93 
94  $avelsieveversion=array("major" => 1, "minor"=>9, "release" => 9, "string" => "1.9.9");
95  fputs($f,'# This script has been automatically generated by avelsieve
96 # (Sieve Mail Filters Plugin for Squirrelmail)
97 # Warning: If you edit this manually, then the changes will not
98 # be reflected in the users\' front-end!
99 #AVELSIEVE_VERSION'.urlencode(base64_encode(serialize($avelsieveversion))).'
100 #AVELSIEVE_CREATED'.time().'
101 #AVELSIEVE_MODIFIED'.time().'
102 require ["fileinto","envelope","reject","vacation","imap4flags","relational","comparator-i;ascii-numeric","regex","body","date"];
103 ');
104  foreach($rules as $rule) {
105  if ($rule["type"]==2) continue; // IGNORE "Filter the message through SpamAssassin"
106 
107  // Create the avelsieve array:
108  $avelrule=array();
109  // And sieve script:
110  $script="if ";
111  $avelrule["condition"]="and";
112  $avelrule["type"]=1;
113  if (!count($rule["conds"])) {
114  // no conditions
115  $script.="true\n";
116  $avelrule["cond"][]=array("kind" => "message", "type" => "all");
117  } else { // have conditions
118  $script.="allof (";
119  $first=true;
120  foreach($conds as $cond) {
121  if (!$first) $script.=",\n";
122  $first=false;
123  // What kind of condition?
124  switch($cond[0]) {
125  case 0: // subject
126  $script.='header :contains "Subject" "'.str_replace('"','\\"',$cond[1]).'"';
127  $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "Subject", "matchtype" => "contains", "headermatch" => $cond[1] );
128  break;
129  case 1: // sender
130  $script.='header :contains "From" "'.str_replace('"','\\"',$cond[1]).'"';
131  $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "From", "matchtype" => "contains", "headermatch" => $cond[1] );
132  break;
133  case 2: // recipient
134  $script.='address :contains ["to", "cc"] "'.str_replace('"','\\"',$cond[1]).'"';
135  $avelrule["cond"][]=array("kind" => "message", "type" => "address", "address" => "toorcc", "matchtype" => "contains", "addressmatch" => $cond[1] );
136  break;
137  case 3: // List-Post
138  $script.='header :contains "List-Post" "'.str_replace('"','\\"',$cond[1]).'"';
139  $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "List-Post", "matchtype" => "contains", "headermatch" => $cond[1] );
140  break;
141  case 4: // List-Id
142  $script.='header :contains "List-Id" "'.str_replace('"','\\"',$cond[1]).'"';
143  $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "List-Id", "matchtype" => "contains", "headermatch" => $cond[1] );
144  break;
145  case 5: // Spamassassin
146  $script.='header :contains "X-Spam-Status" "Yes"';
147  $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "X-Spam-Status", "matchtype" => "contains", "headermatch" => "Yes" );
148  break;
149  case 6: // Delivered-To
150  $script.='envelope :contains "to" "'.str_replace('"','\\"',$cond[1]).'"';
151  $avelrule["cond"][]=array("kind" => "message", "type" => "envelope", "envelope" => "to", "matchtype" => "contains", "envelopematch" => $cond[1] );
152  break;
153 
154  }
155  }
156  $script.=")\n{\n";
157  }
158  // Now the ACTION:
159  switch($rule["type"]) {
160  case 1: // move to
161  $script.='fileinto "'.str_replace('"','\\"',$rule["actionparam"]).'";
162 stop;
163 ';
164  $avelrule["action"] = 5;
165  $avelrule["folder"] = $rule["actionparam"];
166  $avelrule["stop"] = "on";
167  break;
168  case 3: // Discard (for good)
169  $script.='discard;
170 stop;
171 ';
172  $avelrule["action"] = 2;
173  $avelrule["stop"] = "on";
174  break;
175  case 4: // Forward To (copy)
176  $script.='redirect "'.str_replace('"','\\"',$rule["actionparam"]).'";
177 ';
178  $avelrule["action"] = 4;
179  $avelrule["redirectemail"] = $rule["actionparam"];
180  break;
181  case 5: // Auto-Reply
182  $script.='vacation :days 7 :addresses ["'.$email.'"@ :subject "Auto Reply" text:
183 '.str_replace("\\'","'",@file_get_contents($ROOT."/".$u."/".$user."/".$user.".txt")).'
184 .
185 ;
186 ';
187  $avelrule["action"] = 6;
188  $avelrule["vac_addresses"] = $email;
189  $avelrule["vac_subject"] = "Auto Reply";
190  $avelrule["vac_days"] = 7;
191  $avelrule["vac_message"] = @file_get_contents($ROOT."/".$u."/".$user."/".$user.".txt");
192  break;
193  }
194  $script.="}\n";
195  // Now put it into the script file :
196  fputs($f,"#START_SIEVE_RULE".urlencode(base64_encode(serialize($avelrule)))."END_SIEVE_RULE\n");
197  fputs($f,$script);
198  /*
199 if allof (header :contains "From" "expediteur@coin.pan",
200 header :contains "To" "destinataire@coin.pan")
201 {
202 fileinto "INBOX.test";
203 stop;
204 }
205  */
206  } /* for each rule */
207 
208  fclose($f);
209 
210  // Then Move it to the right place
211  @unlink($SIEVEROOT."/".$u."/".$user."/sieve/phpscript.sieve");
212  rename(
213  $SIEVEROOT."/".$u."/".$user."/sieve/tmp/phpscript.sieve",
214  $SIEVEROOT."/".$u."/".$user."/sieve/phpscript.sieve"
215  );
216  chown($SIEVEROOT."/".$u."/".$user."/sieve/phpscript.sieve",$uid);
217  chgrp($SIEVEROOT."/".$u."/".$user."/sieve/phpscript.sieve","vmail");
218  }
219  } else {
220  echo "ERROR: can't read rules for $user\n";
221  }
222 
223 } /* parseOneProcmauil */
224 
225 
226 
227 /* ----------------------------------------------------------------- */
228 /** Read rules, fill an array
229  from m_procmail.php original file
230  yeah I know, ereg() is deprecated ;)
231 */
232 function readrules($user="") {
233  if (!$user) $user=$this->user;
234  $u=substr($user,0,1);
235  if (!file_exists(ALTERNC_MAIL."/$u/$user/.procmailrc")) {
236  return false;
237  }
238  $f=fopen(ALTERNC_MAIL."/$u/$user/.procmailrc","rb");
239  $state=0; $rulenum=0; $ligne=0;
240  $res=array();
241  while (!feof($f)) {
242  $found=false; // found allow us to know if we found something for each loop
243  $s=fgets($f,1024);
244  $s=trim($s);
245  if ($state==1 && !ereg("^# RuleEnd$",$s)) {
246  $res[$rulenum]["rule"][$res[$rulenum]["count"]++]=$s;
247  $found=true;
248  }
249  if ($state==1 && ereg("^# RuleEnd$",$s)) {
250  $state=0;
251  $rulenum++;
252  $found=true;
253  }
254  if ($state==0 && ereg("^# RuleType ([0-9][0-9]) -- (.*)?$",$s,$r)) {
255  $state=1;
256  $res[$rulenum]["type"]=$r[1];
257  $res[$rulenum]["name"]=$r[2];
258  $res[$rulenum]["count"]=0;
259  $found=true;
260  }
261  if (!$found && $state!=0) {
262  return false;
263  }
264  $ligne++;
265  }
266  fclose($f);
267  return $res;
268 }
269 
270 
271 /* ----------------------------------------------------------------- */
272 /** Take ONE rule array, extract properly
273  returns one array with the conditions (which are arrays with Condition Type and Value)
274  and the action parameter
275 */
276 function describe($rule) {
277 
278  // Lecture des conditions :
279  $cond=array();
280  switch ($rule["type"]) {
281  case 5:
282  $i=1;
283  while ($rule["rule"][$i]!="* !^FROM_DAEMON" && $rule["rule"][$i]!="") {
284  $cond[]=$rule["rule"][$i];
285  $i++;
286  }
287  break;
288  default:
289  $i=1;
290  while (substr($rule["rule"][$i],0,1)=="*") {
291  $cond[]=$rule["rule"][$i];
292  $i++;
293  }
294  break;
295  }
296 
297  // $cond is an array of conditions
298  // let's parse the condition : (see arrays at the top of this file)
299  $conds=array();
300 
301  for($i=0;$i<count($cond);$i++) {
302  if (ereg("^\\* \\^Subject\\.\\*(.*)$",$cond[$i],$t)) {
303  $conds[]=array( 0, $t[1] );
304  }
305  if (ereg("^\\* \\^From\\.\\*(.*)$",$cond[$i],$t)) {
306  $conds[]=array( 1, $t[1] );
307  }
308  if (ereg("^\\* \\^TO_(.*)$",$cond[$i],$t)) {
309  $conds[]=array( 2, $t[1] );
310  }
311  if (ereg("^\\* \\^List-Post: (.*)$",$cond[$i],$t)) {
312  $conds[]=array( 3, $t[1] );
313  }
314  if (ereg("^\\* \\^List-Id: (.*)$",$cond[$i],$t)) {
315  $conds[]=array( 4, $t[1] );
316  }
317  if (ereg("^\\* \\^X-Spam-Status: Yes$",$cond[$i])) {
318  $conds[]=array( 5 );
319  }
320  if (ereg("^\\* \\^Delivered-To:\\.\\*(.*)$",$cond[$i],$t)) {
321  $conds[]=array( 6, $t[1] );
322  }
323  }
324 
325  // Action :
326  $actionparam=false;
327  switch ($rule["type"]) {
328  case 1:
329  $actionparam=$rule["rule"][count($rule["rule"])-2]; /* Folder */
330  break;
331  case 4:
332  $actionparam = substr($rule["rule"][count($rule["rule"])-2],0,15); /* Recipient */
333  break;
334  }
335  return array($conds,$actionparam);
336 }
337 
338 
339 
340 
341 
342 
343 
344 
345 /* Help for humans ... */
346 
348  0 => "Le sujet du message contient ...",
349  1 => "L'expediteur du message est contient ...",
350  2 => "L'un des destinataires du message contient ...",
351  3 => "L'en-tete 'List-Post' du message est ...",
352  4 => "L'en-tete 'List-Id' du message est ...",
353  5 => "SpamAssassin considere qu'il s'agit d'un Spam",
354  6 => "L'en-tete 'Delivered-To' du message contient ...",
355  );
356 $aactions=array(
357  1 => "Move the message to this folder",
358  2 => "Filter the message through SpamAssassin",
359  3 => "Discard the message (for good !)",
360  4 => "Forward the mail to",
361  5 => "Auto-reply",
362  );
363 
364 
365 /* ----------------------------------------------------------------- */
366 // CONFIGURATION :
367 include_once('/usr/share/alternc/panel/class/local.php');
368 $ROOT=$L_ALTERNC_MAIL;
369 
370 $SIEVEROOT="/var/lib/dovecot/sieve";
371 // GO !
373