Conditions | 20 |
Paths | 27 |
Total Lines | 156 |
Code Lines | 107 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | #!/usr/bin/php |
||
69 | function parseOneProcmail($user) { |
||
70 | global $SIEVEROOT; |
||
71 | $email=preg_replace("#_([^_]*)$#","@$1",$user); |
||
72 | if ($rules=readrules($user)) { /* ################## SUB FUNCTION ###################### */ |
||
73 | for($i=0; $i<count($rules); $i++) { |
||
74 | list($rules[$i]["conds"],$rules[$i]["actionparam"])=describe($rules[$i]); /* ################## SUB FUNCTION ###################### */ |
||
75 | } |
||
76 | |||
77 | // Now we have $rule["type"] = the ACTION to accomplish + (if 1 or 4) $actionparam |
||
78 | // and a list of $rule["conds"][0]=condition type & $rule["conds"][1]=condition parameter (if not 5) |
||
79 | // Let's write a sieve script :) |
||
80 | $u=substr($u,0,1); |
||
81 | @mkdir($SIEVEROOT."/".$u."/".$user."/sieve"); |
||
82 | @mkdir($SIEVEROOT."/".$u."/".$user."/sieve/tmp"); |
||
83 | $uid=fileowner($SIEVEROOT."/".$u."/".$user); |
||
84 | chown($SIEVEROOT."/".$u."/".$user."/sieve",$uid); |
||
85 | chgrp($SIEVEROOT."/".$u."/".$user."/sieve","vmail"); |
||
86 | chown($SIEVEROOT."/".$u."/".$user."/sieve/tmp",$uid); |
||
87 | chgrp($SIEVEROOT."/".$u."/".$user."/sieve/tmp","vmail"); |
||
88 | |||
89 | $f=fopen($SIEVEROOT."/".$u."/".$user."/sieve/tmp/phpscript.sieve","wb"); |
||
90 | if (!$f) { |
||
91 | echo "ERROR: Can't open '$user' in '$SIEVEROOT' for writing\n"; |
||
92 | } else { |
||
93 | echo "OK: writing sieve script for $user (".count($rules)." rules)\n"; |
||
94 | |||
95 | $avelsieveversion=array("major" => 1, "minor"=>9, "release" => 9, "string" => "1.9.9"); |
||
96 | fputs($f,'# This script has been automatically generated by avelsieve |
||
97 | # (Sieve Mail Filters Plugin for Squirrelmail) |
||
98 | # Warning: If you edit this manually, then the changes will not |
||
99 | # be reflected in the users\' front-end! |
||
100 | #AVELSIEVE_VERSION'.urlencode(base64_encode(serialize($avelsieveversion))).' |
||
101 | #AVELSIEVE_CREATED'.time().' |
||
102 | #AVELSIEVE_MODIFIED'.time().' |
||
103 | require ["fileinto","envelope","reject","vacation","imap4flags","relational","comparator-i;ascii-numeric","regex","body","date"]; |
||
104 | '); |
||
105 | foreach($rules as $rule) { |
||
106 | if ($rule["type"]==2) continue; // IGNORE "Filter the message through SpamAssassin" |
||
107 | |||
108 | // Create the avelsieve array: |
||
109 | $avelrule=array(); |
||
110 | // And sieve script: |
||
111 | $script="if "; |
||
112 | $avelrule["condition"]="and"; |
||
113 | $avelrule["type"]=1; |
||
114 | if (!count($rule["conds"])) { |
||
115 | // no conditions |
||
116 | $script.="true\n"; |
||
117 | $avelrule["cond"][]=array("kind" => "message", "type" => "all"); |
||
118 | } else { // have conditions |
||
119 | $script.="allof ("; |
||
120 | $first=true; |
||
121 | foreach($conds as $cond) { |
||
122 | if (!$first) $script.=",\n"; |
||
123 | $first=false; |
||
124 | // What kind of condition? |
||
125 | switch($cond[0]) { |
||
126 | case 0: // subject |
||
127 | $script.='header :contains "Subject" "'.str_replace('"','\\"',$cond[1]).'"'; |
||
128 | $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "Subject", "matchtype" => "contains", "headermatch" => $cond[1] ); |
||
129 | break; |
||
130 | case 1: // sender |
||
131 | $script.='header :contains "From" "'.str_replace('"','\\"',$cond[1]).'"'; |
||
132 | $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "From", "matchtype" => "contains", "headermatch" => $cond[1] ); |
||
133 | break; |
||
134 | case 2: // recipient |
||
135 | $script.='address :contains ["to", "cc"] "'.str_replace('"','\\"',$cond[1]).'"'; |
||
136 | $avelrule["cond"][]=array("kind" => "message", "type" => "address", "address" => "toorcc", "matchtype" => "contains", "addressmatch" => $cond[1] ); |
||
137 | break; |
||
138 | case 3: // List-Post |
||
139 | $script.='header :contains "List-Post" "'.str_replace('"','\\"',$cond[1]).'"'; |
||
140 | $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "List-Post", "matchtype" => "contains", "headermatch" => $cond[1] ); |
||
141 | break; |
||
142 | case 4: // List-Id |
||
143 | $script.='header :contains "List-Id" "'.str_replace('"','\\"',$cond[1]).'"'; |
||
144 | $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "List-Id", "matchtype" => "contains", "headermatch" => $cond[1] ); |
||
145 | break; |
||
146 | case 5: // Spamassassin |
||
147 | $script.='header :contains "X-Spam-Status" "Yes"'; |
||
148 | $avelrule["cond"][]=array("kind" => "message", "type" => "header", "header" => "X-Spam-Status", "matchtype" => "contains", "headermatch" => "Yes" ); |
||
149 | break; |
||
150 | case 6: // Delivered-To |
||
151 | $script.='envelope :contains "to" "'.str_replace('"','\\"',$cond[1]).'"'; |
||
152 | $avelrule["cond"][]=array("kind" => "message", "type" => "envelope", "envelope" => "to", "matchtype" => "contains", "envelopematch" => $cond[1] ); |
||
153 | break; |
||
154 | |||
155 | } |
||
156 | } |
||
157 | $script.=")\n{\n"; |
||
158 | } |
||
159 | // Now the ACTION: |
||
160 | switch($rule["type"]) { |
||
161 | case 1: // move to |
||
162 | $script.='fileinto "'.str_replace('"','\\"',$rule["actionparam"]).'"; |
||
163 | stop; |
||
164 | '; |
||
165 | $avelrule["action"] = 5; |
||
166 | $avelrule["folder"] = $rule["actionparam"]; |
||
167 | $avelrule["stop"] = "on"; |
||
168 | break; |
||
169 | case 3: // Discard (for good) |
||
170 | $script.='discard; |
||
171 | stop; |
||
172 | '; |
||
173 | $avelrule["action"] = 2; |
||
174 | $avelrule["stop"] = "on"; |
||
175 | break; |
||
176 | case 4: // Forward To (copy) |
||
177 | $script.='redirect "'.str_replace('"','\\"',$rule["actionparam"]).'"; |
||
178 | '; |
||
179 | $avelrule["action"] = 4; |
||
180 | $avelrule["redirectemail"] = $rule["actionparam"]; |
||
181 | break; |
||
182 | case 5: // Auto-Reply |
||
183 | $script.='vacation :days 7 :addresses ["'.$email.'"@ :subject "Auto Reply" text: |
||
184 | '.str_replace("\\'","'",@file_get_contents($ROOT."/".$u."/".$user."/".$user.".txt")).' |
||
185 | . |
||
186 | ; |
||
187 | '; |
||
188 | $avelrule["action"] = 6; |
||
189 | $avelrule["vac_addresses"] = $email; |
||
190 | $avelrule["vac_subject"] = "Auto Reply"; |
||
191 | $avelrule["vac_days"] = 7; |
||
192 | $avelrule["vac_message"] = @file_get_contents($ROOT."/".$u."/".$user."/".$user.".txt"); |
||
193 | break; |
||
194 | } |
||
195 | $script.="}\n"; |
||
196 | // Now put it into the script file : |
||
197 | fputs($f,"#START_SIEVE_RULE".urlencode(base64_encode(serialize($avelrule)))."END_SIEVE_RULE\n"); |
||
198 | fputs($f,$script); |
||
199 | /* |
||
200 | if allof (header :contains "From" "[email protected]", |
||
201 | header :contains "To" "[email protected]") |
||
202 | { |
||
203 | fileinto "INBOX.test"; |
||
204 | stop; |
||
205 | } |
||
206 | */ |
||
207 | } /* for each rule */ |
||
208 | |||
209 | fclose($f); |
||
210 | |||
211 | // Then Move it to the right place |
||
212 | @unlink($SIEVEROOT."/".$u."/".$user."/sieve/phpscript.sieve"); |
||
213 | rename( |
||
214 | $SIEVEROOT."/".$u."/".$user."/sieve/tmp/phpscript.sieve", |
||
215 | $SIEVEROOT."/".$u."/".$user."/sieve/phpscript.sieve" |
||
216 | ); |
||
217 | chown($SIEVEROOT."/".$u."/".$user."/sieve/phpscript.sieve",$uid); |
||
218 | chgrp($SIEVEROOT."/".$u."/".$user."/sieve/phpscript.sieve","vmail"); |
||
219 | } |
||
220 | } else { |
||
221 | echo "ERROR: can't read rules for $user\n"; |
||
222 | } |
||
223 | |||
224 | } /* parseOneProcmauil */ |
||
225 | |||
375 |