App-GSD

 view release on metacpan or  search on metacpan

lib/App/GSD.pm  view on Meta::CPAN

        $netcmd ||= $self->_platform_network_command;
        system(@$netcmd);

        if ($^O eq 'linux') {
            $self->_flush_nscd;
        }
    }
}

# Return best method of flushing DNS for the target platform
sub _platform_network_command {
    my $self = shift;
    my $platform = $^O;
    my $cmd;
    if ($platform eq 'linux') {
        $cmd = $self->_flush_dns_linux;
    }
    elsif ($platform eq 'darwin') {
        $cmd = ['dscacheutil', '-flushcache'];
    }
    else {
        croak "don't know how to flush DNS for platform '$platform'";
    }
    return $cmd;
}

# Return DNS flush command for Linux. It needs to support a few scenarios:
#   Ubuntu e.g. '/etc/init.d/networking restart' (or via upstart: 'restart networking')
#   Arch using network module: '/etc/rc.d/network restart'
#   Arch using NetworkManager or wicd: '/etc/rc.d/$foo restart'
sub _flush_dns_linux {
    my $self = shift;
    my $cmd;

    if (-x '/usr/sbin/rc.d') {
        # Try to guess the user's preferred network module by looking for AUTO
        my $services = `/usr/sbin/rc.d list | fgrep AUTO`;
        for my $service (qw(networkmanager wicd network)) {
            if ($services =~ /^\[STARTED\]\[AUTO\] $service$/m) {
                $cmd = ['/usr/sbin/rc.d', 'restart', $service];
                last;
            }
        }
        if (!defined $cmd) {
            croak "You appear to be using rc.d but I can't figure out which network module you are using.";
        }
    }
    elsif (-x '/etc/init.d/networking') {
        $cmd = ['/etc/init.d/networking', 'restart'];
    }
    elsif (-x '/etc/init.d/network') {
        $cmd = ['/etc/init.d/network', 'restart'];
    }
    else {
        croak "I can't figure out how to restart your network.";
    }

    return $cmd;
}

# Try to invalidate nscd/unscd cache if present
sub _flush_nscd {
    my $self = shift;
    return if $^O ne 'linux';
    for my $nscd (qw(nscd unscd)) {
        # Ignore errors if the daemon is installed, but not running
        CORE::system("/usr/sbin/$nscd", '-i', 'hosts');
    }
    return;
}

1;



=pod

=head1 NAME

App::GSD - boost productivity by blocking distracting websites

=head1 VERSION

version 0.4

=head1 SYNOPSIS

 use App::GSD;
 my $app = App:GSD->new({ block => [qw(foo.com bar.com baz.com)] });
 $app->work; # sites are now blocked
 $app->play; # unblocked

=head1 METHODS

=head2 new ( \%args )

The following arguments are accepted:

=over

=item block

An arrayref of hostnames to block, without a 'www.' prefix (if
present) as these will be blocked automatically.

=item hosts_file

Path to the hosts file (e.g. '/etc/hosts'), overriding the
module's guess based on current operating system.

=item network_command

A reference to an array passable to C<system()> that will restart
the network, e.g.

 ['/etc/init.d/network', 'restart']

=back

=head2 work



( run in 0.992 second using v1.01-cache-2.11-cpan-39bf76dae61 )