|
From: <jgr...@us...> - 2003-11-10 20:16:25
|
Update of /cvsroot/popfile/engine/POPFile
In directory sc8-pr-cvs1:/tmp/cvs-serv4335/POPFile
Modified Files:
Configuration.pm Loader.pm Module.pm
Log Message:
"Unixification of POPFile"
--------------------------
1. Change command line parsing to use Getopt so that in future
we can have real command line options. In the past an option
was equivalent to a configuration file item. So you could set
module_param value
inside popfile.cfg, but you could also to
popfile.pl -module_param value
on the command line. The new form of the latter is
popfile.pl --set module_param=value
For the ultimate in laziness you can also use the old style
if you precede the first old style parameter with --, e.g.
the old style command line above would work in the new scheme
if specified as follows
popfile.pl -- -module_param value
It is still even possible to use very old style parameters
from pre-OO days of POPFile with the addition of the --
popfile.pl -- -ui_port 8080
2. Make it possible for a module to disable itself and hence be
unloaded. Unloadable modules have an 'enabled' paramter (currently
supported by all proxies (POP3, NNTP and SMTP) and XML-RPC). If
this parameter is 0 then use if to return the value '2' from start()
which indicates to Loader that the module wishes to be removed.
POPFile/Configuration.pm:
Use Getopt to handle new style command line options. Make
parse_command_line return 0 if there's an error.
POPFile/Module.pm:
Update documentation on start().
POPFile/Loader.pm:
Check the return code from start() and if it is 2 unload the module.
Check the return code from parse_command_line so we halt if there's an
error in command-line parsing.
Proxy/Proxy.pm:
Initialize the 'enabled' parameter to 1.
Proxy/POP3.pm
Proxy/SMTP.pm
Proxy/NNTP.pm:
Use the 'enabled' parameter to return 2 from start() so that if we
are not enabled we get unloaded. Also if not enabled don't register
any UI components.
UI/XMLRPC.pm:
Use the 'enabled' parameter to return 2 from start() so that if we
are not enabled we get unloaded. Also if not enabled don't register
any UI components.
Classifier/Bayes.pm:
When classifying a message without saving to disk, still add the
XPL link.
tests.pl:
Code layout clean up.
tests/TestConfiguration.pm:
Update and improve tests for command-line parsing.
tests/TestProxy.pm:
Check for the initial setting of the 'enabled' parameter.
tests/TestPOP3.pm:
Check that the enabled parameter is interpreted by start() to mean
no start and return 2. Make the XTP tests less time sensitive.
Index: Configuration.pm
===================================================================
RCS file: /cvsroot/popfile/engine/POPFile/Configuration.pm,v
retrieving revision 1.29
retrieving revision 1.30
diff -C2 -d -r1.29 -r1.30
*** Configuration.pm 10 Nov 2003 19:55:35 -0000 1.29
--- Configuration.pm 10 Nov 2003 20:15:15 -0000 1.30
***************
*** 36,39 ****
--- 36,41 ----
use locale;
+ use Getopt::Long;
+
#----------------------------------------------------------------------------
# new
***************
*** 69,80 ****
$self->{save_needed__} = 0;
- # The location where POPFile is installed
-
- $self->{popfile_root__} = './';
-
- # Where the current user's configuration is
-
- $self->{popfile_user__} = './';
-
bless $self, $type;
--- 71,74 ----
***************
*** 135,146 ****
$self->global_config_( 'msgdir', 'messages/' );
- # Read the POPFILE_ROOT and POPFILE_USER variables into
- # the local store, set up defaults if they are not defined
-
- my ( $root, $user ) = ( $ENV{POPFILE_ROOT}, $ENV{POPFILE_USER} );
-
- $self->{popfile_root__} = $root if defined( $root );
- $self->{popfile_user__} = $user if defined( $user );
-
return 1;
}
--- 129,132 ----
***************
*** 160,164 ****
# may be running, warn the user and terminate
! $self->{pid_file__} = $self->get_user_path( $self->config_( 'piddir' ) . 'popfile.pid' );
if (defined($self->live_check_())) {
--- 146,150 ----
# may be running, warn the user and terminate
! $self->{pid_file__} = $self->config_( 'piddir' ) . 'popfile.pid';
if (defined($self->live_check_())) {
***************
*** 251,255 ****
}
-
# ---------------------------------------------------------------------------------------------
#
--- 237,240 ----
***************
*** 266,270 ****
}
-
# ---------------------------------------------------------------------------------------------
#
--- 251,254 ----
***************
*** 319,323 ****
}
-
# ---------------------------------------------------------------------------------------------
#
--- 303,306 ----
***************
*** 333,367 ****
my ( $self ) = @_;
! # It's ok for the command line to be blank, the values of configuration will be drawn from
! # the default values defined at the start of the code and those read from the configuration
! # file
! if ( $#ARGV >= 0 ) {
my $i = 0;
! while ( $i <= $#ARGV ) {
# A command line argument must start with a -
! if ( $ARGV[$i] =~ /^-(.+)$/ ) {
my $parameter = $self->upgrade_parameter__($1);
if ( defined($self->{configuration_parameters__}{$parameter}) ) {
! if ( $i < $#ARGV ) {
! $self->{configuration_parameters__}{$parameter} = $ARGV[$i+1];
$i += 2;
} else {
! print STDERR "Missing argument for $ARGV[$i]\n";
! last;
}
} else {
! print STDERR "Unknown command line option $ARGV[$i]\n";
! last;
}
} else {
! print STDERR "Expected a command line option and got $ARGV[$i]\n";
! last;
}
}
}
}
--- 316,387 ----
my ( $self ) = @_;
! # Options from the command line specified with the --set parameter
! my @set_options;
!
! # The following command line options are supported:
! #
! # --set Permanently sets a configuration item for the current user
! # -- Everything after this point is an old style POPFile option
! #
! # So its possible to do
! #
! # --set bayes_param=value --set=-bayes_parem=value --set -bayes_parem=value -- -bayes_parem value
!
! if ( !GetOptions( "set=s" => \@set_options ) ) {
! return 0;
! }
!
! # Join together the options specified with --set and those after the --, the
! # options in @set_options are going to be of the form foo=bar and hence need to
! # be split into foo bar
!
! my @options;
!
! for my $i (0..$#set_options) {
! $set_options[$i] =~ /-?(.+)=(.+)/;
!
! if ( !defined( $1 ) ) {
! print STDERR "\nBad option: $set_options[$i]\n";
! return 0;
! }
!
! push @options, ("-$1");
! if ( defined( $2 ) ) {
! push @options, ($2);
! }
! }
!
! push @options, @ARGV;
!
! if ( $#options >= 0 ) {
my $i = 0;
! while ( $i <= $#options ) {
# A command line argument must start with a -
! if ( $options[$i] =~ /^-(.+)$/ ) {
my $parameter = $self->upgrade_parameter__($1);
if ( defined($self->{configuration_parameters__}{$parameter}) ) {
! if ( $i < $#options ) {
! $self->{configuration_parameters__}{$parameter} = $options[$i+1];
$i += 2;
} else {
! print STDERR "\nMissing argument for $options[$i]\n";
! return 0;
}
} else {
! print STDERR "\nUnknown option $options[$i]\n";
! return 0;
}
} else {
! print STDERR "\nExpected a command line option and got $options[$i]\n";
! return 0;
}
}
}
+
+ return 1;
}
***************
*** 457,461 ****
my ( $self ) = @_;
! if ( open CONFIG, '<' . $self->get_user_path( 'popfile.cfg' ) ) {
while ( <CONFIG> ) {
s/(\015|\012)//g;
--- 477,481 ----
my ( $self ) = @_;
! if ( open CONFIG, "<popfile.cfg" ) {
while ( <CONFIG> ) {
s/(\015|\012)//g;
***************
*** 491,495 ****
}
! if ( open CONFIG, '>' . $self->get_user_path( 'popfile.cfg' ) ) {
$self->{save_needed__} = 0;
--- 511,515 ----
}
! if ( open CONFIG, ">popfile.cfg" ) {
$self->{save_needed__} = 0;
***************
*** 526,630 ****
}
! # ---------------------------------------------------------------------------------------------
! #
! # get_root_path
! #
! # The POPFILE_ROOT environment variable is converted by the configuration
! # module into an internal variable. This method take a relative or absolute
! # path and returns the same path relative to the POPFILE_ROOT. Hence if the
! # passed in path is absolute it simply returns it, if relative then it returns
! # the full path consisting of the concatenation of the POPFILE_ROOT and the
! # passed in path
! #
! # $path The path to convert
! #
! # ---------------------------------------------------------------------------------------------
! sub get_root_path
! {
! my ( $self, $path ) = @_;
!
! if ( $self->is_absolute_path__( $path ) ) {
! return $path;
! } else {
! return $self->path_join__( $self->{popfile_root__}, $path );
! }
! }
!
! # ---------------------------------------------------------------------------------------------
! #
! # get_user_path
! #
! # The POPFILE_USER environment variable is converted by the configuration
! # module into an internal variable. This method take a relative or absolute
! # path and returns the same path relative to the POPFILE_USER. Hence if the
! # passed in path is absolute it simply returns it, if relative then it returns
! # the full path consisting of the concatenation of the POPFILE_USER and the
! # passed in path
! #
! # $path The path to convert
! #
! # ---------------------------------------------------------------------------------------------
! sub get_user_path
! {
! my ( $self, $path ) = @_;
!
! if ( $self->is_absolute_path__( $path ) ) {
! return $path;
! } else {
! return $self->path_join__( $self->{popfile_user__}, $path );
! }
! }
!
! # ---------------------------------------------------------------------------------------------
! #
! # is_absolute_path__
! #
! # Returns 1 is the path is absolute (i.e. start with / or a drive letter followed by /)
! #
! # $path Path to check
! #
! # ---------------------------------------------------------------------------------------------
! sub is_absolute_path__
! {
! my ( $self, $path ) = @_;
!
! if ( $path =~ /^\// ) {
! return 1;
! }
!
! if ( $path =~ /^[A-Z]:[\/\\]/i ) {
! return 1;
! }
!
! return 0;
! }
!
! # ---------------------------------------------------------------------------------------------
! #
! # path_join__
! #
! # Joins two paths together making sure that the appropriate path separator is inserted
! #
! # $left First part of path
! # $right Second part of path
! #
! # ---------------------------------------------------------------------------------------------
! sub path_join__
! {
! my ( $self, $left, $right ) = @_;
!
! $left =~ s/[\/\\]$//;
! $right =~ s/^[\/\\]//;
!
! my $path = "$left/$right";
!
! # Strip any amount of leading ./
!
! $path =~ s/^(\.\/)+//;
!
! return $path;
! }
!
! # GETTER
sub configuration_parameters
--- 546,550 ----
}
! # GETTERS
sub configuration_parameters
***************
*** 636,637 ****
--- 556,558 ----
1;
+
Index: Loader.pm
===================================================================
RCS file: /cvsroot/popfile/engine/POPFile/Loader.pm,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** Loader.pm 10 Nov 2003 10:37:27 -0000 1.13
--- Loader.pm 10 Nov 2003 20:15:15 -0000 1.14
***************
*** 531,542 ****
print " $name" if $self->{debug__};
flush STDOUT;
! if ( $self->{components__}{$type}{$name}->initialize() == 0 ) {
die "Failed to start while initializing the $name module";
}
! $self->{components__}{$type}{$name}->alive( 1 );
! $self->{components__}{$type}{$name}->forker( $self->{forker__} );
! $self->{components__}{$type}{$name}->pipeready( $self->{pipeready__} );
}
print '} ' if $self->{debug__};
--- 531,547 ----
print " $name" if $self->{debug__};
flush STDOUT;
!
! my $code = $self->{components__}{$type}{$name}->initialize();
!
! if ( $code == 0 ) {
die "Failed to start while initializing the $name module";
}
! if ( $code == 1 ) {
! $self->{components__}{$type}{$name}->alive( 1 );
! $self->{components__}{$type}{$name}->forker( $self->{forker__} );
! $self->{components__}{$type}{$name}->pipeready( $self->{pipeready__} );
! }
}
print '} ' if $self->{debug__};
***************
*** 560,564 ****
$self->{components__}{core}{config}->load_configuration();
! $self->{components__}{core}{config}->parse_command_line();
}
--- 565,569 ----
$self->{components__}{core}{config}->load_configuration();
! return $self->{components__}{core}{config}->parse_command_line();
}
***************
*** 581,588 ****
print "\n {$type:" if $self->{debug__};
foreach my $name (keys %{$self->{components__}{$type}}) {
! print " $name" if $self->{debug__};
! flush STDOUT;
! if ( $self->{components__}{$type}{$name}->start() == 0 ) {
die "Failed to start while starting the $name module";
}
}
--- 586,603 ----
print "\n {$type:" if $self->{debug__};
foreach my $name (keys %{$self->{components__}{$type}}) {
! my $code = $self->{components__}{$type}{$name}->start();
!
! if ( $code == 0 ) {
die "Failed to start while starting the $name module";
+ }
+
+ # If the module said that it didn't want to be loaded then
+ # unload it.
+
+ if ( $code == 2 ) {
+ delete $self->{components__}{$type}{$name};
+ } else {
+ print " $name" if $self->{debug__};
+ flush STDOUT;
}
}
Index: Module.pm
===================================================================
RCS file: /cvsroot/popfile/engine/POPFile/Module.pm,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** Module.pm 10 Nov 2003 19:55:35 -0000 1.13
--- Module.pm 10 Nov 2003 20:15:16 -0000 1.14
***************
*** 74,83 ****
# register_configuration_item_() register a UI configuration item
#
- # get_root_path_() Converts a relative path to an absolute based on the POPFile
- # root path (set through the environment variable POPFILE_ROOT)
- #
- # get_user_path_() Converts a relative path to an absolute based on the POPFile
- # user path (set through the environment variable POPFILE_USER)
- #
# A note on the naming
#
--- 74,77 ----
***************
*** 183,187 ****
#
# The method should return 1 to indicate that it started correctly, if it returns
! # 0 then POPFile will abort loading immediately
#
# ---------------------------------------------------------------------------------------------
--- 177,182 ----
#
# The method should return 1 to indicate that it started correctly, if it returns
! # 0 then POPFile will abort loading immediately, returns 2 if everything OK but this
! # module does not want to continue to be used.
#
# ---------------------------------------------------------------------------------------------
***************
*** 429,474 ****
}
- # ---------------------------------------------------------------------------------------------
- #
- # get_root_path_
- #
- # The POPFILE_ROOT environment variable is converted by the configuration
- # module into an internal variable. This method take a relative or absolute
- # path and returns the same path relative to the POPFILE_ROOT. Hence if the
- # passed in path is absolute it simply returns it, if relative then it returns
- # the full path consisting of the concatenation of the POPFILE_ROOT and the
- # passed in path
- #
- # $path The path to convert
- #
- # ---------------------------------------------------------------------------------------------
- sub get_root_path_
- {
- my ( $self, $path ) = @_;
-
- $self->{configuration__}->get_root_path( $path );
- }
-
- # ---------------------------------------------------------------------------------------------
- #
- # get_user_path_
- #
- # The POPFILE_USER environment variable is converted by the configuration
- # module into an internal variable. This method take a relative or absolute
- # path and returns the same path relative to the POPFILE_USER. Hence if the
- # passed in path is absolute it simply returns it, if relative then it returns
- # the full path consisting of the concatenation of the POPFILE_USER and the
- # passed in path
- #
- # $path The path to convert
- #
- # ---------------------------------------------------------------------------------------------
- sub get_user_path_
- {
- my ( $self, $path ) = @_;
-
- $self->{configuration__}->get_user_path( $path );
- }
-
# GETTER/SETTER methods. Note that I do not expect documentation of these unless they
# are non-trivial since the documentation would be a waste of space
--- 424,427 ----
***************
*** 587,588 ****
--- 540,542 ----
1;
+
|