Debian Codegraph @

bin/test-filter-module

raw
1 #!/usr/bin/perl
2
3 #
4 # A tester for Courier::Filter modules.
5 #
6 # (C) 2003-2006 Julian Mehnle <[email protected]>
7 # $Id: test-filter-module 210 2008-03-21 19:30:31Z julian $
8 #
9 ##############################################################################
10
11 =head1 NAME
12
13 test-filter-module - A tester for Courier::Filter modules
14
15 =head1 VERSION
16
17 0.18
18
19 =head1 SYNOPSIS
20
21 B<test-filter-module> I<module> [I<options>] I<message> [+I<control> ...] [...]
22
23 =head1 DESCRIPTION
24
25 This is a tester for B<Courier::Filter> modules. It sets up the specified
26 Courier::Filter::Module::I<module> filter module, using any supplied
27 I<options>, and then queries the filter module for consideration of the given
28 I<message> file and zero or more I<control> files. More than one
29 message/control files tuple may also be specified, in which case the filter
30 module is applied to each message and its respective control file(s). Filter
31 module options must be specified in the format -I<option>=I<expr>, where
32 I<expr> may be any valid Perl expression.
33
34 =head1 EXAMPLES
35
36 =over
37
38 =item B<test-filter-module> DNSBL -zones='["bl.foo.org", "bl.bar.org"]'
39 test.rfc2822 +test.control
40
41 Applies the Courier::Filter::Module::DNSBL filter module to the
42 F<test.rfc2822>/F<test.control> message/control file tuple (taking the sending
43 machine's IP address from the control file), using the blacklist DNS zones
44 C<bl.foo.org> and C<bl.bar.org>.
45
46 =back
47
48 =head1 SEE ALSO
49
50 L<Courier::Filter::Overview>, L<Courier::Filter>
51
52 For AVAILABILITY, SUPPORT, and LICENSE information, see
53 L<Courier::Filter::Overview>.
54
55 =head1 AUTHOR
56
57 Julian Mehnle <[email protected]>
58
59 =cut
60
61 use warnings;
62 use strict;
63
64 use lib '/usr/share/courier-filter-perl/perl5';
65
66 use IO::Handle;
67
68 use Courier::Message;
69
70 # Declarations:
71 ##############################################################################
72
73 use constant TRUE => (0 == 0);
74 use constant FALSE => not TRUE;
75
76 use constant MODULE_NAME_PREFIX => 'Courier::Filter::Module::';
77
78 # Implementation:
79 ##############################################################################
80
81 if (@ARGV < 2 or $ARGV[0] =~ /^(?:-h|--help)$/) {
82 my $script_name;
83 ($script_name = $0) =~ s/^.*\///;
84 STDERR->print(
85 "Usage:\n" .
86 " $script_name MODULE [OPTIONS] MESSAGE [+CONTROL ...] [...]\n" .
87 "\n" .
88 "Applies the " . MODULE_NAME_PREFIX . "MODULE filter module to the specified\n" .
89 "MESSAGE file and any CONTROL files (if specified). Multiple MESSAGE files can\n" .
90 "be specified, and each one may be followed by any number of CONTROL files, each\n" .
91 "prefixed by a '+' character.\n" .
92 "Optionally, OPTIONS in the format -<option>=<expr> can be given to the MODULE\n" .
93 "constructor, where <expr> may be any valid Perl expression.\n" .
94 "\n" .
95 "Example:\n" .
96 " $script_name \\\n" .
97 " DNSBL -zones='[qw(bl.foo.org bl.bar.org)]' \\\n" .
98 " test.rfc2822 +test.control\n" .
99 "\n"
100 );
101 exit(64); # EX_USAGE
102 }
103
104 my $module_name = shift();
105
106 my %options;
107 $options{$1} = eval($2), shift()
108 while $ARGV[0] =~ /^-(.*?)=(.*)/;
109
110 my @messages;
111
112 while (@ARGV) {
113 my $file_name = shift();
114 if ($file_name !~ /^\+(.*)/) {
115 # Message file:
116 my $message = Courier::Message->new(
117 file_name => $file_name
118 );
119 push(@messages, $message);
120 }
121 else {
122 # Control file, belonging to the previous message file:
123 STDERR->print("Orphan control file specified. List a message file first.\n"), exit(64) # EX_USAGE
124 if not @messages;
125 push(
126 @{ $messages[$#messages]->{control_file_names} },
127 $1
128 );
129 }
130 }
131
132 my $class = MODULE_NAME_PREFIX . $module_name;
133
134 eval("use $class;");
135 if ($@) {
136 STDERR->print("Unable to load filter module $module_name ($class).\n");
137 exit(1);
138 }
139
140 my $module = $class->new(
141 %options,
142 testing => TRUE # Enable testing mode, so no real-life/persistent things are done.
143 );
144
145 foreach my $message (@messages) {
146 my ($result, $code) = $module->consider($message);
147
148 if ($result) {
149 $code ||= 550;
150 }
151 else {
152 $result = 'Ok';
153 $code ||= 200;
154 }
155
156 my @lines = split(/\n/, $result);
157 my $last_line = pop(@lines);
158 print($message->file_name . ": $code-$_\n") foreach @lines;
159 print($message->file_name . ": $code $last_line\n");
160 }