/usr/share/perl5/Net/Sieve/Script.pm is in libnet-sieve-script-perl 0.08-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | package Net::Sieve::Script;
use strict;
use warnings;
BEGIN {
use Exporter ();
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = '0.08';
@ISA = qw(Exporter);
#Give a hoot don't pollute, do not export more than needed by default
@EXPORT = qw(_strip);
@EXPORT_OK = qw(_strip);
%EXPORT_TAGS = ();
}
use base qw(Class::Accessor::Fast);
use Net::Sieve::Script::Rule;
=head1 NAME
Net::Sieve::Script - Parse and write sieve scripts
=head1 SYNOPSIS
use Net::Sieve::Script;
my $test_script = 'require "fileinto";
# Place all these in the "Test" folder
if header :contains "Subject" "[Test]" {
fileinto "Test";
}';
my $script = Net::Sieve::Script->new ($test_script);
print "OK" if ( $script->parsing_ok ) ;
print $script->write_script;
or
my $script = Net::Sieve::Script->new();
my $cond = Net::Sieve::Script::Condition->new('header');
$cond->match_type(':contains');
$cond->header_list('"Subject"');
$cond->key_list('"Re: Test2"');
my $actions = 'fileinto "INBOX.test"; stop;';
my $rule = Net::Sieve::Script::Rule->new();
$rule->add_condition($cond);
$rule->add_action($actions);
$script->add_rule($rule);
print $script->write_script;
=head1 DESCRIPTION
Manage sieve script
Read and parse file script, make L<Net::Sieve::Script::Rule>, L<Net::Sieve::Script::Action>, L<Net::Sieve::Script::Condition> objects
Write sieve script
Support RFC 5228 - sieve base
RFC 5231 - relationnal
RFC 5230 - vacation
Draft regex
missing
5229 variables
5232 imapflags
5233 subaddress
5235 spamtest
notify draft
=cut
__PACKAGE__->mk_accessors(qw(raw rules require max_priority));
=head1 CONSTRUCTOR
=head2 new
Argument : optional text script
Purpose : if param, put script in raw, parse script
Return : main Script object
Accessors :
->raw() : read or set original text script
->require() : require part of script
->rules() : array of rules
->max_priority() : last rule id
=cut
sub new
{
my ($class, $param) = @_;
my $self = bless ({}, ref ($class) || $class);
my @LISTS = qw((\[.*?\]|".*?"));
if ($param) {
$self->raw($param);
$self->require($1) if ( $param =~ m/require @LISTS;/si );
$self->read_rules();
}
# break if more than 50 rules
die "50 rules does not sound reasonable !"
if ( $self->max_priority() && $self->max_priority() >= 50 );
return $self;
}
=head1 METHODS
=head2 parsing_ok
return 1 on raw parsing success
=cut
sub parsing_ok
{
my $self = shift;
return ( $self->_strip eq _strip($self->write_script) );
}
=head2 write_script
Purpose : write full script, require and rules parts
Return : set current require,
return rules ordered by priority in text format
=cut
sub write_script {
my $self = shift;
my $text;
my %require = ();
foreach my $rule ( sort { $a->priority() <=> $b->priority() } @{$self->rules()} ) {
$text .= $rule->write."\n";
foreach my $req ($rule->require()) {
$require{$req->[0]} = 1 if defined $req->[0];
}
}
#TODO keep original require if current is include, for test parsing
my $require_line;
my $count;
foreach my $req (sort keys %require) {
next if(!$req);
$require_line .= ', "'.$req.'"';
$count++;
};
$require_line =~ s/^, //;
$require_line = '['.$require_line.']' if ($count > 1);
$self->require($require_line);
$require_line = "require $require_line;\n" if $require_line;
return $require_line.$text;
}
=head2 equals
$object->equals($test_object): return 1 if $object and $test_object are equals
=cut
sub equals {
my $self = shift;
my $object = shift;
return 0 unless (defined $object);
return 0 unless ($object->isa('Net::Sieve::Script'));
my @accessors = qw( require );
foreach my $accessor ( @accessors ) {
my $myvalue = $self->$accessor;
my $theirvalue = $object->$accessor;
if (defined $myvalue) {
return 0 unless (defined $theirvalue);
return 0 unless ($myvalue eq $theirvalue);
} else {
return 0 if (defined $theirvalue);
}
}
if (defined $self->rules) {
my @myrules = sort { $a->priority() <=> $b->priority() } @{$self->rules()};
my @theirrules = sort { $a->priority() <=> $b->priority() } @{$object->rules()} ;
return 0 unless ($#myrules == $#theirrules);
unless ($#myrules == -1) {
foreach my $index (0..$#myrules) {
my $myrule = $myrules[$index];
my $theirrule = $theirrules[$index];
if (defined ($myrule)) {
return 0 unless ($myrule->isa(
'Net::Sieve::Script::Rule'));
return 0 unless ($myrule->equals($theirrule));
} else {
return 0 if (defined ($theirrule));
}
}
}
} else {
return 0 if (defined ($object->rules));
}
return 1;
}
=head2 read_rules
$script->read_rules() : read rules from raw
$script->read_rules($some_text) : parse text rules
use of read_rules set $script->rules()
Return 1 on success
=cut
sub read_rules
{
my $self = shift;
my $text_rules = shift || $self->raw();
my @LISTS = qw((\[.*?\]|".*?"));
$self->require($1) if ( $text_rules =~ m/require @LISTS;/si );
#read rules from raw or from $text_rules if set
my $script_raw = $self->_strip($text_rules);
my @Rules;
# for simple vacation RFC 5230
if ($script_raw =~m/^(vacation .*)$/) {
push @Rules, Net::Sieve::Script::Rule->new(ctrl => 'vacation',block => $1,order =>1)
}
my $order;
while ($script_raw =~m/(if|else|elsif) (.*?){(.*?)}([\s;]?)/isg) {
my $ctrl = lc($1);
my $test_list = $2;
my $block = $3;
++$order;
# break if more than 50 rules
die "50 rules does not sound reasonable !"
if ( $order >= 50 );
my $pRule = Net::Sieve::Script::Rule->new (
ctrl => $ctrl,
test_list => $test_list,
block => $block,
order => $order
);
push @Rules, $pRule;
};
$self->rules(\@Rules);
$self->max_priority($order);
return 1;
}
=head2 find_rule
Return L<Net::Sieve::Script::Rule> pointer find by priority
Return 0 on error, 1 on not find
=cut
sub find_rule
{
my $self = shift;
my $priority = shift;
return 0 if $priority > $self->max_priority || $priority <= 0;
return 0 if not defined $self->rules;
foreach my $rule (@{$self->rules}) {
return $rule if ($rule->priority == $priority );
}
return 1;
}
=head2 swap_rules
Swap priorities,
now don't take care of if/else/elsif
Return 1 on success, 0 on error
=cut
sub swap_rules
{
my $self = shift;
my $swap1 = shift;
my $swap2 = shift;
return 0 if $swap1 == $swap2;
my $pr1 = $self->find_rule($swap1);
my $pr2 = $self->find_rule($swap2);
return 0 if ref($pr1) ne 'Net::Sieve::Script::Rule';
return 0 if ref($pr2) ne 'Net::Sieve::Script::Rule';
my $mem_pr2 = $pr2->priority();
$pr2->priority($pr1->priority());
$pr1->priority($mem_pr2);
return 1;
}
=head2 reorder_rules
Reorder rules with a list of number, start with 1, and with blanck separator. Usefull for ajax sort functions.
Thank you jeanne for your help in brain storming.
Return 1 on success, 0 on error
=cut
sub reorder_rules
{
my $self = shift;
my $list = shift;
return 0 if ( ! $list );
my @swap = split ' ',$list;
return 0 if ( ! scalar @swap );
return 0 if ( scalar @swap != $self->max_priority );
my @new_ordered_rules;
foreach my $swap ( @swap ) {
if ($swap =~ m/\d+/) {
my $rule = $self->find_rule($swap);
push @new_ordered_rules, $rule;
}
}
my $i=1;
foreach my $rule (@new_ordered_rules) {
$rule->priority($i);
$i++;
};
return 1;
}
=head2 delete_rule
Delete rule and change priority, delete rule take care for 'if' test
if deleted is 'if'
delete next if next is 'else'
change next in 'if' next is 'elsif'
Return : 1 on success, 0 on error
=cut
sub delete_rule
{
my $self = shift;
my $id = shift;
my $deleted = 0;
my @Rules = defined $self->rules?@{$self->rules}:();
my @NewRules = ();
my $order = 0;
for ( my $i = 0; $i < scalar(@Rules); $i++ ) {
my $rule = $Rules[$i];
my $next=$i+1;
if ($rule->priority == $id) {
$deleted = 1;
if ( defined $Rules[$next] && $rule->alternate eq 'if') {
$Rules[$next]->alternate('if')
if ($Rules[$next]->alternate eq 'elsif' );
if ($Rules[$next]->alternate eq 'else' ) {
$i++;
$rule = $Rules[$i];
}
}
}
else {
++$order;
$rule->priority($order);
push @NewRules, $rule;
}
}
$self->max_priority($order);
$self->rules(\@NewRules);
return $deleted;
}
=head2 add_rule
Purpose : add a rule in end of script
Return : priority on success, 0 on error
Argument : Net::Sieve::Script::Rule object
=cut
sub add_rule
{
my $self = shift;
my $rule = shift;
return 0 if ref($rule) ne 'Net::Sieve::Script::Rule';
my $order = $self->max_priority();
my @Rules = defined $self->rules?@{$self->rules}:();
++$order;
$rule->priority($order);
push @Rules, $rule;
$self->max_priority($order);
$self->rules(\@Rules);
return $order;
}
# private and exported tool _strip
# strip a string or strip raw
# return a string
# usefull for parsing or tests
#
# default remove require line or set $keep_require
sub _strip {
my ( $self, $script_raw, $keep_require ) = @_;
if ( ref($self) eq 'Net::Sieve::Script' ) {
$script_raw = $self->raw() if (! $script_raw );
} else {
$script_raw = $self;
}
$script_raw =~ s/\#.*//g; # hash-comment
$script_raw =~ s!/\*.*.\*/!!g; # bracket-comment
$script_raw =~ s/\t/ /g; # remove tabs
$script_raw =~ s/\(/ \( /g; # add white-space around (
$script_raw =~ s/\)/ \) /g; # add white-space around )
#$script_raw =~ s/\s+\[/ \[ /g; # add white-space around [
#$script_raw =~ s/\]\s+/ \] /g; # add white-space around ]
$script_raw =~ s/\]\s*,/\],/g; # add white-space around ]
$script_raw =~ s/"\s*,/", /g; # add white-space after , in list
$script_raw =~ s/"\s+;/";/g; # remove white-space between " and ;
$script_raw =~ s/\s+/ /g; # remove doubs white-space
$script_raw =~ s/^\s+//; # trim
$script_raw =~ s/\s+$//; #trim
$script_raw =~ s/require.*?["\]];\s+//sgi if (!$keep_require); #remove require
return $script_raw;
}
=head1 BUGS
Rewrite a hand made script will lose comments. Verify parsing success with parsing_ok method before write a new script.
=head1 SUPPORT
Please report any bugs or feature requests to "bug-net-sieve-script at rt.cpan.org", or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Sieve-Script>.
I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
=head1 AUTHOR
Yves Agostini - Univ Metz - <agostini@univ-metz.fr>
L<http://www.crium.univ-metz.fr>
=head1 COPYRIGHT
Copyright 2008 Yves Agostini - <agostini@univ-metz.fr>
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=head1 SEE ALSO
L<Net::Sieve>
=cut
1;
# The preceding line will help the module return a true value
|