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 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
|
#!/usr/bin/perl -w
#
# build/mkrules -- compile the SpamAssassin rules into installable form
#
# <@LICENSE>
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# </@LICENSE>
# This is an implementation of
# http://wiki.apache.org/spamassassin/RulesProjPromotion
sub usage {
die "build/mkrules [--src srcdir] [--exit_on_no_src] [--out outputdir]\n";
}
my $RULE_DEFINE_KEYWORDS_RE = qr{
header|rawbody|body|full|uri
|meta|mimeheader|urirhssub|uridnsbl
}x;
my $RULE_KEYWORDS_RE = qr{
${RULE_DEFINE_KEYWORDS_RE}|
describe|tflags|reuse|score
}x;
my $fail_message = "";
use strict;
use File::Find;
use File::Copy;
use File::Basename;
use Getopt::Long;
# use SpamAssassin classes directly, so we can lint rules
# as we go
use lib 'lib';
use Mail::SpamAssassin;
our ( @opt_srcs, $opt_out, $opt_sandboxout, $opt_manifest,
$opt_manifestskip, $opt_listpromotable, $opt_active,
$opt_activeout, $default_file_header,
$opt_rulemetadata, $opt_exit_on_no_src);
GetOptions("src=s" => \@opt_srcs,
"out=s",
"sandboxout=s",
"activeout=s",
"active=s",
"manifest=s",
"manifestskip=s",
"rulemetadata=s",
"exit_on_no_src",
);
if (!@opt_srcs) {
foreach ( 'rulescode', 'rulesrc' ) {
if (-d $_) {
# print "using default src $_\n";
push(@opt_srcs, $_);
}
}
}
if (!$opt_manifest && -f "MANIFEST") {
$opt_manifest = "MANIFEST";
}
if (!$opt_manifestskip && -f "MANIFEST.SKIP") {
$opt_manifestskip = "MANIFEST.SKIP";
}
if (!$opt_active && -f "rules/active.list") {
$opt_active = "rules/active.list";
}
if ($opt_exit_on_no_src) {
my $foundone = 0;
foreach my $src (@opt_srcs) {
if (-d $src) { $foundone++; last; }
}
if (!$foundone) {
print "no source directory found: exiting\n";
exit 0;
}
}
# else
die "no src" unless (@opt_srcs >= 1);
my $promolist;
die "no out" unless ($opt_out);
die "unreadable out" unless (-d $opt_out);
die "unreadable active" unless (-f $opt_active);
$opt_sandboxout ||= "70_sandbox.cf";
$opt_activeout ||= "72_active.cf";
# source files that need compilation, and their targets
my $needs_compile = { };
my $found_output = { };
my $current_src;
my $newest_src_mtime = 0;
my $newest_out_mtime = 0;
$default_file_header = join('', <DATA>);
compile_utf8ify_function();
foreach my $src (@opt_srcs) {
if (!-d $src) {
warn "WARNING: unreadable src '$src'\n";
next;
}
$current_src = $src;
File::Find::find ({
wanted => \&src_wanted,
no_chdir => 1
}, $src);
}
# get mtimes of output files; we can be sure that all
# output is under the "opt_out" dir, so recurse there
File::Find::find ({
wanted => \&out_wanted,
no_chdir => 1
}, $opt_out);
# we must rebuild if a compiled .pm is missing, too
my $found_all_pm_files = 1;
foreach my $f (keys %{$needs_compile}) {
next unless ($f =~ /\.pm$/i);
if (!exists $found_output->{basename $f}) {
$found_all_pm_files = 0;
}
}
# check mtime on the active.list file, too
{
my @st = stat $opt_active;
if ($st[9] && $st[9] > $newest_src_mtime) {
$newest_src_mtime = $st[9];
}
}
# check mtimes, and also require that the two required output files
# really do exist
if ($newest_src_mtime && $newest_out_mtime
&& $newest_src_mtime < $newest_out_mtime
&& -f $opt_out.'/'.$opt_sandboxout
&& -f $opt_out.'/'.$opt_activeout
&& $found_all_pm_files)
{
print "mkrules: no rules updated\n";
exit 0;
}
my $rules = { };
my $file_manifest = { };
my $file_manifest_skip = [ ];
if ($opt_manifest) {
read_manifest($opt_manifest);
}
if ($opt_manifestskip) {
read_manifest_skip($opt_manifestskip);
}
my $active_rules = { };
read_active($opt_active);
# context for the rules compiler
my $seen_rules = { };
my $renamed_rules = { };
my $output_files = { };
my $output_file_text = { };
my $files_to_lint = { };
my $entries_for_rule_name = { };
# $COMMENTS is a "catch-all" "name", for lines that appear after the last line
# that refers to a rule by name. Those lines are not published by themselves;
# they'll be published to all pubfiles found in the file.
#
# It's assumed they are comments, because they generally are, but could be all
# sorts of unparseable lines.
my $COMMENTS = '!comments!';
# another "fake name" for lines that should always be published. They'll
# be published to the non-sandbox file.
my $ALWAYS_PUBLISH = '!always_publish!';
read_all_rules($needs_compile);
read_rules_from_output_dir();
compile_output_files();
lint_output_files();
write_output_files();
# mkrules.t relies on the script exiting cleanly Bug #7302 and Bug #7692
exit if ($ENV{'TEST_ACTIVE'}) ;
die "$fail_message" if ( $fail_message =~ m/./) ;
exit;
# ---------------------------------------------------------------------------
sub lint_output_files {
foreach my $file (keys %{$files_to_lint}) {
my $text = join("\n", "file start $file", $output_file_text->{$file}, "file end $file");
if (lint_rule_text($text) != 0) {
warn "\nERROR: LINT FAILED, suppressing output: $file\n\n";
$fail_message = $fail_message . "ERROR: LINT FAILED, suppressing output: $file\n";
# don't suppress entirely, otherwise 'make distcheck'/'disttest'
# will fail since the MANIFEST-listed output files will be
# empty.
# delete $output_file_text->{$file};
$output_file_text->{$file} = '';
}
}
}
sub lint_rule_text {
my ($text) = @_;
# ensure we turn off slow/optional stuff for linting, but keep the essentials
my $pretext = q{
loadplugin Mail::SpamAssassin::Plugin::Check
loadplugin Mail::SpamAssassin::Plugin::URIDNSBL
util_rb_tld com # skip "need to run sa-update" warn
use_bayes 0
};
my $mailsa = Mail::SpamAssassin->new({
rules_filename => "./rules",
# debug => 1,
local_tests_only => 1,
dont_copy_prefs => 1,
config_text => $pretext.$text
});
my $errors = 0;
$mailsa->{lint_callback} = sub {
my %opts = @_;
return if ($opts{msg} =~ /
(?:score\sset\sfor\snon-existent|description\sexists)
/x);
warn "lint: $opts{msg}";
if ($opts{iserror}) {
$errors++;
}
};
$mailsa->lint_rules();
$mailsa->finish();
return $errors; # 0 means good
}
sub src_wanted {
my $path = $File::Find::name;
# record stat times of directories, too, to catch file additions/removals
# in the source tree
my @st = stat $path;
if ($st[9] && $st[9] > $newest_src_mtime) {
$newest_src_mtime = $st[9];
}
# only files from now on, though
return if (!-f $path);
# limit what will be copied from sandboxes
return if ($path =~ /\bsandbox\b/ && !/(?:\d.*\.cf|\.pm)$/i);
# don't use generated scores; they can be out of sync with what is currently
# in the sandboxes or the most current active.list file at any given time
return if ($path =~ /\bscores\b/);
# a bit of sanity please - no svn metadata ;)
return if ($path =~ /\.svn/);
my $dir = $path;
$dir =~ s/^${current_src}[\/\\\:]//s;
$dir =~ s/([^\/\\\:]+)$//;
my $filename = $1;
my $f = "$current_src/$dir$filename";
my $t;
$t = "$opt_out/$filename";
$needs_compile->{$f} = {
f => $f,
t => $t,
dir => $dir,
filename => $filename
};
}
sub out_wanted {
my $path = $File::Find::name;
return unless (-f $path);
return if ($path =~ /\.svn/);
return unless ($path =~ /\.(?:cf|pm)$/i);
my @st = stat $path;
if ($st[9] && $st[9] > $newest_out_mtime) {
$newest_out_mtime = $st[9];
}
my $dir = $path;
$dir =~ s/^${current_src}[\/\\\:]//s;
$dir =~ s/([^\/\\\:]+)$//;
my $filename = $1;
if ($path =~ /\.pm$/i) {
$found_output->{$filename} = 1;
}
}
# compile all the source files found by the src_wanted() sub, in sorted
# order so that the order of precedence makes sense.
sub read_all_rules {
my ($sources) = @_;
# deal with the perl modules first, so that later linting w/ loadplugin will
# work appropriately.
foreach my $f (sort {
my ($ae) = $a =~ /\.(cf|pm)$/;
my ($be) = $b =~ /\.(cf|pm)$/;
return $be cmp $ae || $a cmp $b;
} keys %$sources)
{
my $entry = $needs_compile->{$f};
my $t = $entry->{t};
# TODO: dependency checking optimization?
## my $needs_rebuild = 0;
## if (!-f $t || -M $t > -M $f) {
## # the source file is newer, or dest is not there
## $needs_rebuild = 1;
## }
my $needs_rebuild = 1;
if ($entry->{filename} =~ /\.pm$/) {
plugin_file_compile($entry);
}
elsif ($entry->{dir} =~ /sandbox/) {
rule_file_compile($f, $t, $entry->{filename},
{ issandbox => 1 });
}
elsif ($entry->{dir} =~ /scores/) {
rule_file_compile($f, $t, $entry->{filename},
{ issandbox => 1, isscores => 1 });
}
elsif ($entry->{dir} =~ /extra/) {
# 'extra' rulesets; not built by default (TODO)
next;
}
else {
# rules in "core" and "lang" are always copied
if ($needs_rebuild) {
rule_file_compile($f, $t, $entry->{filename}, { });
}
}
}
}
###########################################################################
# Rules are compiled from source dir to output dir.
#
# Rules in "rules/active.list" are promoted to "72_active.cf"; rules not
# listed there are relegated to "70_sandbox.cf". There is code to allow
# other filenames to be selected from the rulesrc .cf file, but I'm not
# sure if it works anymore ;)
#
# Rules will be autorenamed, if there's a collision between a new rule name and
# one that's already been output by the compiler in another source file. The
# autorenaming is very simple -- portions of the current source path are
# appended to the rule name, sanitised.
sub rule_file_compile {
my ($f, $t, $filename, $flags) = @_;
my $issandbox = $flags->{issandbox};
my $isscores = $flags->{isscores};
open (IN, "<$f") or die "cannot read $f";
# a fast parser for the config file format; don't need the
# full deal here, and it must be fast, since it's run on every
# "make" invocation
my $rule_order = [ ];
my $lastrule = $COMMENTS;
if (!defined $rules->{$ALWAYS_PUBLISH}) {
$rules->{$ALWAYS_PUBLISH} = rule_entry_create();
}
# zero or more "ifplugin" or "if" scopes
my @current_conditionals = ();
my $current_comments = '';
while (<IN>) {
my $orig = $_;
s/#.*$//g; s/^\s+//; s/\s+$//;
# drop comments/blank lines from output
next if (/^$/);
# save "lang" declarations
my $lang = '';
if (s/^lang\s+(\S+)\s+//) {
$lang = $1;
}
if (/^(${RULE_KEYWORDS_RE})\s+(\S+)\s+(.*)$/)
{
# rule definitions
my $type = $1;
my $name = $2;
my $val = $3;
my $origname = $name;
if ($issandbox) {
$name = sandbox_rule_name_avoid_collisions($name, $f);
}
my $origname_w_T_prefix = $name;
# non-sandbox rules always use the same name
if (scalar @current_conditionals) {
# ensure the current conditionals are used in the block name;
# this ensures that we scope alternative (#ifdef-style) dupe
# rule definitions in their own ifplugin scopes
$name .= " ".join("", @current_conditionals);
$name =~ s/\s+/ /gs; $name =~ s/ $//;
}
# track this as a rule-entry block for that rule name
# (and it's T_ prefixed variant, if relevant)
push @{$entries_for_rule_name->{$origname}}, $name;
push @{$entries_for_rule_name->{$origname_w_T_prefix}}, $name;
# comment "score" lines for sandbox rules (bug 5558)
# use generated scores, though, if the rule is active
if ($type eq 'score' && $issandbox &&
!($isscores && $active_rules->{$name}))
{
$orig =~ s/^/#/g;
}
if (!$rules->{$name}) { $rules->{$name} = rule_entry_create(); }
$rules->{$name}->{issandbox} = $issandbox;
$rules->{$name}->{isscores} = $isscores;
$rules->{$name}->{origname} = $origname;
$rules->{$name}->{origname_w_T_prefix} = $origname_w_T_prefix;
$rules->{$name}->{cond} = [@current_conditionals];
$rules->{$name}->{text} .= $current_comments . $orig;
$rules->{$name}->{plugin_dependencies} = {};
# note if the conditional is a plugin reference, as we need to
# ensure that "loadplugin" lines stay in the same place
foreach my $c (@current_conditionals) {
if ($c =~ /^ifplugin\s+(\S+)/) {
$rules->{$name}->{plugin_dependencies}->{$1} = 1;
} elsif ($c =~ /^if.*plugin/) {
while ($c =~ /plugin\s*\(\s*(\S+)\s*\)/g) {
$rules->{$name}->{plugin_dependencies}->{$1} = 1;
}
}
}
# note if we found the rule defn or not. if we did not,
# that means the rule was a code-tied rule, which should always
# have its descriptions/scores/etc. published in "active".
if ($type =~ /^${RULE_DEFINE_KEYWORDS_RE}$/x) {
$rules->{$name}->{found_definition} = 1;
$rules->{$name}->{srcfile} = $f;
$rules->{$name}->{code} = $orig;
}
elsif ($type eq 'tflags') {
# userconf rules are always published in "active"
if ($val =~ /\buserconf\b/) {
$rules->{$name}->{forceactive} = 1;
}
# record for rulemetadata code
$val =~ s/\s+/ /gs;
if ($rules->{$name}->{tflags}) {
$rules->{$name}->{tflags} .= ' '.$val;
} else {
$rules->{$name}->{tflags} = $val;
}
}
$current_comments = '';
$lastrule = $name;
push (@$rule_order, $name);
}
elsif (/^
(pubfile|publish)
\s+(\S+)\s*(.*?)$
/x)
{
# preprocessor directives
my $command = $1;
my $name = $2;
my $val = $3;
my $origname = $name;
# note: if we call sandbox_rule_name_avoid_collisions(), it'll
# rename to 'T_RULENAME' -- which is exactly what we're trying
# to avoid in 'publish RULENAME' lines! so don't call it here.
# if ($issandbox) {
# $name = sandbox_rule_name_avoid_collisions($name, $f);
# }
if (!$rules->{$name}) { $rules->{$name} = rule_entry_create(); }
$rules->{$name}->{origname} = $origname;
$rules->{$name}->{origname_w_T_prefix} = $origname;
if ($command eq 'publish') {
# the 'publish' command defaults to "1", unless it explicitly
# is set to "0". iow: publish RULE_NAME [(0 | 1)] [default: 1]
if (!defined $val || $val eq '') { $val = '1'; }
}
elsif ($command eq 'pubfile') {
if (!filename_in_manifest($opt_out.'/'.$val)) {
warn "$val: WARNING: not listed in manifest file, using default\n";
next; # don't set 'pubfile' below
}
}
$rules->{$name}->{$command} = $val;
# if we see "publish NAMEOFRULE", that means the rule is
# considered active
if ($rules->{$name}->{publish}) {
$rules->{$name}->{forceactive} = 1;
}
}
elsif (/^
(if|ifplugin)
\s+(.*?)$
/x)
{
push @current_conditionals, $orig;
}
elsif (/^else\b/x)
{
if (!scalar @current_conditionals) {
warn "WARNING: 'else' without 'if'/'ifplugin' conditional\n";
} else {
my $cond = invert_conditional(pop @current_conditionals);
push @current_conditionals, $cond;
}
}
elsif (/^endif\b/x)
{
if (!scalar @current_conditionals) {
warn "WARNING: 'endif' without 'if'/'ifplugin' conditional\n";
} else {
pop @current_conditionals;
}
}
elsif (/^require_version\s*(\S+)\b/) {
# silently ignored. TODO? (meh)
}
elsif (/^loadplugin\s*(\S+)\b/) {
my $name = 'loadplugin_'.$1;
unless ($rules->{$name}) {
$rules->{$name} = rule_entry_create();
$rules->{$name}->{origname} = $name;
$rules->{$name}->{origname_w_T_prefix} = $name;
$rules->{$name}->{issandbox} = $issandbox;
$rules->{$name}->{iscommand} = 1;
}
if (/^loadplugin\s*\S+\s+(\S+)/) {
my $fname = $1;
my $fpath = dirname($f)."/".$fname;
if (!-f $fpath) {
warn "$f: WARNING: plugin code file '$fpath' not found, line ignored: $orig";
next;
}
if ($fpath =~ /sandbox/i) {
# Since this is a sandbox plugin, force its output to the sandbox area.
$rules->{$name}->{sandbox_plugin} = 1;
}
# If a 'loadplugin' line is found, and the plugin .pm is not listed in
# the MANIFEST file, this will mean that the .pm will not be copied
# during "make dist". This causes failures during "make disttest",
# since the file does not exist.
#
# However, we do want to preserve these lines in the 'rules' dir, for
# use during development -- without requiring that the .pm's be put
# into MANIFEST -- ie. before the plugin is considered release-ready,
# ie. sandbox plugins.
#
# fix: make it a "tryplugin" line instead; these are ignored if the
# target file is nonexistent.
if (!filename_in_manifest($opt_out.'/'.$fname)) {
warn "$f: WARNING: '$opt_out/$fname' not listed in manifest file, making 'tryplugin': $orig";
$orig =~ s/^\s*loadplugin\b/tryplugin/;
}
}
$rules->{$name}->{text} .= $orig;
unshift (@$rule_order, $name);
}
else {
# an unhandled configuration line; "redirector_pattern",
# "report", something like that. This should be sent to
# the active.cf output (or sandbox if it appeared in a sandbox
# input file).
# use the line itself as a key
my $name = $_;
/^\s*(\S+)/ and $name = $1;
$name =~ s/\s+/ /gs;
my $forceactive = 1;
# always send 'test' lines to the sandbox files
if (/^test\s*/) {
$forceactive = 0;
$name = $_; # ensure we don't drag rules with us though!
$name =~ s/\s+/ /gs;
}
if (scalar @current_conditionals) {
$name = join("", @current_conditionals);
$name =~ s/\s+/ /gs; $name =~ s/ $//;
}
if ($issandbox) {
$name .= "_sandbox";
}
unless ($rules->{$name}) {
$rules->{$name} = rule_entry_create();
$rules->{$name}->{origname} = $name;
$rules->{$name}->{origname_w_T_prefix} = $name;
}
$rules->{$name}->{cond} = [@current_conditionals];
$rules->{$name}->{issandbox} = $issandbox;
$rules->{$name}->{forceactive} = $forceactive;
# $rules->{$name}->{forceactive} = 1;
$rules->{$name}->{iscommand} = 1;
# TODO: bug 6241: 'replace_rules' should be handled ok, but isn't
# warn "unknown line in rules file '$f', saving to default: $orig";
$rules->{$name}->{text} .= $orig;
unshift (@$rule_order, $name);
}
}
close IN;
if ($current_comments) {
$rules->{$COMMENTS}->{text} .= $current_comments;
}
# now append all the found text to the output file buffers
copy_to_output_buffers($rule_order, $issandbox, $f, $filename);
# ok; file complete. now mark all those rules as "seen"; future
# refs to those rule names will trigger an autorename.
foreach my $name (@$rule_order) {
$seen_rules->{$name} = 1;
}
}
# this is only run if we're generating rulemetadata!
sub read_rules_from_output_dir {
return unless ($opt_rulemetadata);
foreach my $file (<$opt_out/*.cf>) {
next unless ($file =~ /\d\d_\S+\.cf$/);
next if (pubfile_is_activeout($file));
next if (pubfile_is_sandboxout($file));
read_output_file($file);
}
}
sub read_output_file {
my ($file) = @_;
open (IN, "<$file") or warn "cannot read $file";
while (<IN>) {
my $orig = $_;
s/#.*$//g; s/^\s+//; s/\s+$//;
# drop comments/blank lines from output
next if (/^$/);
# save "lang" declarations
my $lang = '';
if (s/^lang\s+(\S+)\s+//) {
$lang = $1;
}
if (/^(${RULE_KEYWORDS_RE})\s+(\S+)\s+(.*)$/) {
# rule definitions
my $type = $1;
my $name = $2;
my $val = $3;
# note: we only want to do this if --rulemetadata is in use!
if (!$rules->{$name}) { $rules->{$name} = rule_entry_create(); }
if ($type eq 'tflags') {
$val =~ s/\s+/ /gs;
if ($rules->{$name}->{tflags}) {
$rules->{$name}->{tflags} .= ' '.$val;
} else {
$rules->{$name}->{tflags} = $val;
}
}
if ($type =~ /^${RULE_DEFINE_KEYWORDS_RE}$/x) {
$rules->{$name}->{srcfile} = $file;
$rules->{$name}->{code} = $orig;
}
}
}
close IN;
}
sub copy_to_output_buffers {
my ($rule_order, $issandbox, $f, $filename) = @_;
# always output these two files, even if they're empty!
foreach my $pubfile ($opt_out.'/'.$opt_sandboxout,
$opt_out.'/'.$opt_activeout)
{
$output_files->{$pubfile} = {
header => $default_file_header
};
}
my %already_done = ();
my $copied_active = 0;
my $copied_other = 0;
foreach my $name (@$rule_order)
{
# only do each rule once, please ;)
next if exists $already_done{$name};
$already_done{$name} = undef;
my $text = $rules->{$name}->{text};
if (!$text) {
next; # nothing to write!
}
my $srcfile = $rules->{$name}->{srcfile};
my $pubfile = pubfile_for_rule($rules, $rules->{$name}->{origname_w_T_prefix});
my $is_active = 0;
if (pubfile_is_activeout($pubfile)) {
$is_active++;
}
my $cond = $rules->{$name}->{cond};
if ($cond) {
foreach my $pluginclass (keys %{$rules->{$name}->{plugin_dependencies}}) {
my $ifplugin_text_name = "loadplugin_".($pluginclass || "");
if ($rules->{$ifplugin_text_name}) {
# if the plugin is a sandbox plugin, ensure it's not
# sent to the active file
if ($rules->{$ifplugin_text_name}->{sandbox_plugin}) {
$pubfile = $opt_out.'/'.$opt_sandboxout;
$is_active = 0;
}
# either way, ensure the "loadplugin" line, if there is one,
# goes to the same file
$rules->{$ifplugin_text_name}->{output_file} = $pubfile;
}
}
# ensure we produce enough "endif"s to match however many
# nested conditions there are
my $endifs = "endif\n" x (scalar @{$cond});
$rules->{$name}->{output_text} = "\n"
.join("", @{$cond})
.$text
.$endifs;
} else {
$rules->{$name}->{output_text} = $text;
}
# note the target file
$rules->{$name}->{output_file} = $pubfile;
$output_files->{$pubfile} = {
header => $default_file_header
};
if ($is_active) {
$copied_active++;
} else {
$copied_other++;
}
}
print "$f: $copied_active active rules, ".
"$copied_other other\n";
}
sub pubfile_for_rule {
my ($rules, $name) = @_;
my $pubfile;
if ($rules->{$name}->{publish}) {
# "publish NAMEOFRULE" => send it to active
$pubfile = $opt_out.'/'.$opt_activeout;
}
# default: "70_sandbox.cf" or "72_active.cf"
if (!$pubfile) {
if ($active_rules->{$name} # is active
|| $rules->{$name}->{forceactive} # or is forced to be
|| (!$rules->{$name}->{found_definition} && !$rules->{$name}->{iscommand}
&& !$rules->{$name}->{isscores}))
# or is a rule-related setting in reference to an unknown rule
# but isn't a generated score
{
$pubfile = $opt_out.'/'.$opt_activeout;
}
elsif ($rules->{$name}->{issandbox}) {
$pubfile = $opt_out.'/'.$opt_sandboxout;
}
else {
warn "oops? inactive rule, non-sandbox, shouldn't be possible anymore";
$pubfile = $opt_out.'/'.$opt_sandboxout;
}
}
return $pubfile;
}
sub plugin_file_compile {
my ($entry) = @_;
return if $opt_listpromotable;
# just copy the raw perl module over to the new area
# we can't really rename to avoid conflicts since the loadplugin lines
# are going to be all screwed up in that case.
# jm: we always want to update the output file in case the input
# has been changed!
if (0 && -e $entry->{t}) {
warn "The perl module ".$entry->{t}." already exists, can't copy from ".$entry->{f}."\n";
}
else {
copy($entry->{f}, $entry->{t}) || warn "Couldn't copy ".$entry->{f}.": $!";
}
}
###########################################################################
sub compile_output_files {
my $always = $rules->{$ALWAYS_PUBLISH}->{output_text};
# create all known output files
foreach my $file (keys %$output_files) {
$output_file_text->{$file} = $output_files->{$file}->{header};
if ($always && pubfile_is_activeout($file)) {
$output_file_text->{$file} .= $always;
}
}
# this is a horrible kluge.
# at this point in the game, we've lost the ordered list of rules, so the
# loadplugin lines have no guarantee that they'll be loaded before the rules
# that require them. so we kluge the sort to always have loadplugin lines
# appear at the very top of the array so we know they'll be listed before
# anything else.
my @rulenames = sort {
if ($a =~ /^loadplugin_/) {
return -1;
}
elsif ($b =~ /^loadplugin_/) {
return 1;
}
return $a cmp $b;
} keys %$rules;
my %seen = ();
# go through the rules looking for meta subrules we
# may have forgotten; this happens if a non-subrule is
# listed in active.list, the subrules will not be! fix them
# to appear in the same output file as the master rule.
foreach my $rule (@rulenames) {
fix_up_rule_dependencies($rule);
}
# now repeat, just for rules in the active set; their dependencies should
# always be likewise promoted into the active set, overriding the prev step.
foreach my $rule (@rulenames) {
my $pubfile = $rules->{$rule}->{output_file};
next unless ($pubfile && pubfile_is_activeout($pubfile));
fix_up_rule_dependencies($rule);
}
my $rulemd = '';
# output the known rules that are not meta subrules.
foreach my $rule (@rulenames) {
$rulemd .= get_rulemetadata_string($rule); # all metadata strings
next if ($rule =~ /^__/);
my $pubfile = $rules->{$rule}->{output_file};
my $text = $rules->{$rule}->{output_text};
next unless defined ($text);
# DOS - bug 6297 - HACK HACK HACK HACK
# this will probably screw up meta rules that do something like '&& !$rule'
# avoid publishing 'tflags nopublish' rules
if (pubfile_is_activeout($pubfile) && exists $rules->{$rule}->{tflags} &&
$rules->{$rule}->{tflags} =~ /\bnopublish\b/)
{
print "omitting rule $rule due to tflags nopublish (tflags $rules->{$rule}->{tflags})\n";
next;
}
# DOS - END HACK
$output_file_text->{$pubfile} .= "##{ $rule\n".
$text.
"##} ".$rule."\n\n";
}
# now output all subrules (in a slightly more compact form)
foreach my $rule (@rulenames) {
next unless ($rule =~ /^__/);
my $pubfile = $rules->{$rule}->{output_file};
my $text = $rules->{$rule}->{output_text};
next unless defined ($text);
# DOS - bug 6297 - HACK HACK HACK HACK
# this will probably screw up meta rules that do something like '&& !$rule'
# avoid publishing 'tflags nopublish' rules
if (pubfile_is_activeout($pubfile) && exists $rules->{$rule}->{tflags} &&
$rules->{$rule}->{tflags} =~ /\bnopublish\b/)
{
print "omitting rule $rule due to tflags nopublish (tflags $rules->{$rule}->{tflags})\n";
next;
}
# DOS - END HACK
$output_file_text->{$pubfile} .= $text;
}
# finally, finish off all output files
foreach my $file (keys %$output_files) {
# and get them lint-checked!
$files_to_lint->{$file} = 1;
}
if ($opt_rulemetadata) {
open (RULEMD, ">".$opt_rulemetadata)
or die "cannot write rulemd to $opt_rulemetadata";
print RULEMD "<?xml version='1.0' encoding='UTF-8'?>\n",
"<rulemds>", $rulemd, "</rulemds>\n";
close RULEMD or die "cannot close rulemd to $opt_rulemetadata";
}
}
# conditionally build a method to UTF-8-encode a string. this is only required
# for the rulemetadata XML output, so don't make it mandatory!
sub compile_utf8ify_function {
if (!eval '
sub utf8ify { use Encode; return Encode::encode("UTF-8", $_[0]); } 1;
')
{
eval '
sub utf8ify { die "unimplemented -- Encode module required!" } 1;
'
}
}
sub get_rulemetadata_string {
my ($rule) = @_;
return '' unless ($opt_rulemetadata);
my $mod = 0;
my $srcfile = '';
my $code = '';
my $name = $rule;
# if we found a rule definition with a T_ prefix, use that data
if (!$rules->{$name}->{srcfile} && $rules->{"T_".$name}->{srcfile}) {
$name = "T_".$name;
}
if ($rules->{$name}->{srcfile}) {
$srcfile = $rules->{$name}->{srcfile};
if ($srcfile) {
my @s = stat $srcfile;
if (@s) { $mod = $s[9]; }
}
}
if ($rules->{$name}->{code}) {
$code = $rules->{$name}->{code};
$code =~ s/\]\]>/\](defanged by mkrules)\]>/gs; # ensure it's CDATA-safe
$code = utf8ify($code);
}
my $tf = $rules->{$name}->{tflags} || '';
return "<rulemetadata>".
"<name>$rule</name>".
"<src>$srcfile</src>".
"<srcmtime>$mod</srcmtime>".
# don't include <code> blocks; they bloat up the XML badly (to 800KB)
# and make it very slow to parse later
# "<code><![CDATA[$code]]></code>".
"<tf>$tf</tf>".
"</rulemetadata>\n";
}
sub fix_up_rule_dependencies {
my $rule = shift;
my $pubfile = $rules->{$rule}->{output_file};
my $text = $rules->{$rule}->{output_text};
return unless $text;
while ($text =~ /^\s*meta\s+(.*)$/mg) {
my $line = $1;
while ($line =~ /\b([_A-Za-z0-9]+)\b/g) {
# force that subrule (if it exists) to output in the
# same pubfile
my $rule2 = $1;
# deal with rules that changed name from "FOO" to "T_FOO"
sed_renamed_rule_names(\$rule2);
if (!$entries_for_rule_name->{$rule2}) {
# we may not always have a rule entry, if the rule was from a non-sandbox
# source
# warn "cannot find entries_for_rule_name '$rule2'";
}
foreach my $entryname2 (@{$entries_for_rule_name->{$rule2}}) {
next unless ($rules->{$entryname2} && $rules->{$entryname2}->{output_file});
# don't do this if the subrule would be moved *out* of the
# active file!
my $pubfile2 = $rules->{$entryname2}->{output_file};
next if (pubfile_is_activeout($pubfile2));
$rules->{$entryname2}->{output_file} = $pubfile;
}
}
}
}
sub pubfile_is_activeout {
return 1 if ($_[0] && $_[0] =~ /\b\Q$opt_activeout\E$/);
return 0;
}
sub pubfile_is_sandboxout {
return 1 if ($_[0] && $_[0] =~ /\b\Q$opt_sandboxout\E$/);
return 0;
}
sub write_output_files {
foreach my $pubfile (sort keys %$output_files) {
if (-f $pubfile) {
unlink $pubfile or die "cannot remove output file '$pubfile'";
}
if (!filename_in_manifest($pubfile)) {
warn "$pubfile: WARNING: not listed in manifest file\n";
}
my $text = $output_file_text->{$pubfile};
if ($text) {
open (OUT, ">".$pubfile) or die "cannot write to output file '$pubfile'";
sed_renamed_rule_names(\$text);
print OUT $text;
close OUT or die "cannot close output file '$pubfile'";
# print "$pubfile: written\n"; # too noisy
}
else {
print "$pubfile: no rules promoted\n";
# create an empty file anyway to satisfy MANIFEST
open (OUT, ">".$pubfile) or die "cannot write to output file '$pubfile'";
close OUT or die "cannot close output file '$pubfile'";
}
}
}
###########################################################################
sub rule_entry_create {
return {
text => '',
publish => 0
};
}
###########################################################################
sub sandbox_rule_name_avoid_collisions {
my ($rule, $path) = @_;
my $new;
my $newreason;
my $dowarn = 0;
return $rule if $opt_listpromotable;
return $rule if $active_rules->{$rule};
return $rule if $rules->{$rule}->{forceactive};
if ($rule !~ /^(?:T_|__)/) {
$new = "T_".$rule;
$newreason = "missing T_ prefix";
}
elsif (!exists $seen_rules->{$rule}) {
return $rule;
}
else {
$new = $path;
$new =~ s/[^A-Za-z0-9]+/_/gs;
$new =~ s/_+/_/gs;
$new =~ s/^_//;
$new =~ s/_$//;
$new = $rule.'_'.$new;
$newreason = "collision with existing rule";
$dowarn = 1;
}
if (!$renamed_rules->{$new}) {
$renamed_rules->{$new} = $rule;
if ($dowarn) {
warn "WARNING: $rule: renamed as $new due to $newreason\n";
}
}
return $new;
}
sub sed_renamed_rule_names {
my ($textref) = @_;
foreach my $new (keys %{$renamed_rules}) {
my $rule = $renamed_rules->{$new};
$$textref =~ s/\b${rule}\b/${new}/gs;
}
}
###########################################################################
sub invert_conditional {
my $cond = shift;
if ($cond =~ /^ \s* ifplugin \s+(.*?)$ /x) {
return "if !plugin($1)\n";
} elsif ($cond =~ /^ \s* if \s+(.*?)$ /x) {
return "if !($1)\n";
} else {
warn "WARNING: cannot parse '$cond' for 'else'\n";
return 'if 0';
}
}
###########################################################################
sub read_manifest {
my ($fname) = @_;
parse_line_delimited_config_file($fname, sub {
/^\s*(.*?)\s*$/ and $file_manifest->{$1} = 1;
});
}
sub read_manifest_skip {
my ($fname) = @_;
parse_line_delimited_config_file($fname, sub {
/^\s*(.*?)\s*$/ and push (@{$file_manifest_skip}, qr/$1/);
});
}
sub read_active {
my ($fname) = @_;
parse_line_delimited_config_file($fname, sub {
/^(\S+)/ and $active_rules->{$1} = 1;
});
}
sub filename_in_manifest {
my ($fname) = @_;
return 1 if ($file_manifest->{$fname});
foreach my $skipre (@{$file_manifest_skip}) {
return 1 if ($fname =~ $skipre);
}
return 0;
}
sub parse_line_delimited_config_file {
my ($fname, $callback) = @_;
if (!open (IN, "<$fname")) {
warn "cannot read $fname\n";
} else {
while (<IN>) {
next if /^#/;
$callback->();
}
close IN;
}
}
__DATA__
# SpamAssassin rules file
#
# Please don't modify this file as your changes will be overwritten with
# the next update. Use @@LOCAL_RULES_DIR@@/local.cf instead.
# See 'perldoc Mail::SpamAssassin::Conf' for details.
#
# <@LICENSE>
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# </@LICENSE>
#
###########################################################################
require_version @@VERSION@@
|