From: Chris Prather Date: Tue, 10 Jul 2007 05:07:08 +0000 (+0000) Subject: fix a critic test X-Git-Tag: 0.01^0 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cecbee2d97a70885e3dc57bba62e0673d894b1e9;p=gitmo%2FMooseX-Daemonize.git fix a critic test r24925@alice-3: perigrin | 2007-07-08 18:36:52 -0500 add basedir attribute r24982@alice-3: perigrin | 2007-07-09 15:18:40 -0500 attempt to hastily patch what could be a security flaw r25011@alice-3: perigrin | 2007-07-10 00:06:38 -0500 Update before release - Add Example (moose_room IRC server) - add .cvsignore --- diff --git a/.cvsignore b/.cvsignore new file mode 100644 index 0000000..e69de29 diff --git a/examples/moose_room.pl b/examples/moose_room.pl new file mode 100644 index 0000000..720013a --- /dev/null +++ b/examples/moose_room.pl @@ -0,0 +1,120 @@ +package MooseRoom; +use strict; +our $VERSION = '0.0.1'; +use Moose; +use POE qw(Component::Server::IRC); + +with qw(MooseX::Getopt); +with qw(MooseX::Daemonize); + +has servername => ( + isa => 'Str', + is => 'ro', + default => sub { 'moose.room' }, +); + +has nicklen => ( + isa => 'Int', + is => 'ro', + default => sub { 15 }, +); + +has network => ( + isa => 'Str', + is => 'ro', + default => sub { 'MooseRoom' }, +); + +has ircd => ( + isa => 'POE::Component::Server::IRC', + is => 'ro', + lazy => 1, + default => sub { + POE::Component::Server::IRC->spawn( + { + servername => $_[0]->servername, + nicklen => $_[0]->nicklen, + network + } + ); + }, +); + +has operators => ( + isa => 'ArrayRef', + is => 'ro', + auto_deref => 1, + default => sub { + [ { username => 'perigrin', password => 'hobbit' }, ]; + }, +); + +sub BUILD { + my ($self) = @_; + POE::Session->create( object_states => [ $self => [qw(_start _default)], ], + ); + +} + +sub _start { + my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; + $self->ircd->yield('register'); + + # Anyone connecting from the loopback gets spoofed hostname + $self->ircd->add_auth( + mask => '*@localhost', + spoof => $self->hostname, + no_tilde => 1 + ); + + # We have to add an auth as we have specified one above. + $self->ircd->add_auth( mask => '*@*' ); + + # Start a listener on the 'standard' IRC port. + $self->ircd->add_listener( port => 6667 ); + + # Add an operator who can connect from localhost + $self->ircd->add_operator($_) for $self->operators; + return; +} + +sub _default { + my ( $event, $args ) = @_[ ARG0 .. $#_ ]; + print STDOUT "$event: "; + foreach (@$args) { + SWITCH: { + if ( ref($_) eq 'ARRAY' ) { + print STDOUT "[", join( ", ", @$_ ), "] "; + last SWITCH; + } + if ( ref($_) eq 'HASH' ) { + print STDOUT "{", join( ", ", %$_ ), "} "; + last SWITCH; + } + print STDOUT "'$_' "; + } + } + print STDOUT "\n"; + return 0; # Don't handle signals. +} + +before new => sub { POE::Kernel->run(); }; +after start => sub { POE::Kernel->run(); }; + +unless ( caller() ) { + require Cwd; + my $cmd = lc $ARGV[-1]; + my $app = MooseRoom->new_with_options( pidbase => Cwd::cwd() ); + print STDERR "trying to $cmd server\n"; + if ( $cmd eq 'start' ) { + print STDERR qq{ + pidfile: @{ [ $app->pidfile ] } + port: @{ [ $app->Port ] } + }; + } + $app->$cmd; +} + +no Moose; +1; # Magic true value required at end of module +__END__ diff --git a/lib/MooseX/Daemonize.pm b/lib/MooseX/Daemonize.pm index c02346f..b3be19d 100644 --- a/lib/MooseX/Daemonize.pm +++ b/lib/MooseX/Daemonize.pm @@ -127,7 +127,7 @@ sub get_pid { sub stop { my ( $self, %args ) = @_; my $pid = $self->get_pid; - $self->kill($pid) unless $self->foreground(); + $self->_kill($pid) unless $self->foreground(); $self->remove_pid; return 1 if $args{no_exit}; exit; @@ -148,7 +148,8 @@ sub setup_signals { sub handle_sigint { $_[0]->stop; } sub handle_sighup { $_[0]->restart; } -sub kill { +sub _kill { + confess "_kill isn't public" unless caller eq __PACKAGE__; my ( $self, $pid ) = @_; return unless $pid; unless ( CORE::kill 0 => $pid ) { @@ -281,21 +282,27 @@ Setup the signal handlers, by default it only sets up handlers for SIGINT and SI =item handle_sigint() -Handle a INT signal, by default calls C<$self->stop()>; +Handle a INT signal, by default calls C<$self->stop()> =item handle_sighup() -Handle a HUP signal. Nothing is done by default. +Handle a HUP signal. By default calls C<$self->restart()> =item get_pid +Lookup the pid from our pidfile. + =item save_pid +Save the current pid in our pidfile + =item remove_pid +Delete our pidfile + =item meta() -the C method from L +The C method from L =back