Created
January 28, 2014 07:34
-
-
Save neilk/8663594 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl -w | |
# | |
# This program is meant to help me focus, by blocking timewasting sites in /etc/hosts. | |
# | |
# If the program is invoked with 'focus on', a section will be added to the /etc/hosts file | |
# routing requests for the domains in __DATA__ to localhost. | |
# | |
# If the program is invoked with 'focus off', that section is removed. | |
# | |
# The section in /etc/hosts should be apparently updated in place. It should begin with the line "# FOCUS" | |
# and, optionally, end with the line "# ENDFOCUS" | |
# | |
use strict; | |
use File::Temp qw/tempfile/; | |
my $hostFileName = "/etc/hosts"; | |
my $isFocusOn = 1; | |
sub usage { | |
die "Usage: $0 [on|off]\n"; | |
} | |
sub printFocusSection { | |
my $outFh = shift; | |
print $outFh "# FOCUS\n"; | |
if ($isFocusOn) { | |
while (my $domainSpec = <DATA>) { | |
chomp($domainSpec); | |
for my $subDomain ('', 'www.') { | |
my $domain = $subDomain . $domainSpec; | |
# need to specify it twice; on MacOS X, will wait for the IPv6 lookup to time out | |
for my $ip ('127.0.0.1', 'fe80::1%lo0') { | |
print $outFh "$ip $domain\n"; | |
} | |
} | |
} | |
} | |
print $outFh "# ENDFOCUS\n"; | |
} | |
if (@ARGV < 1) { | |
usage(); | |
} | |
if ($ARGV[0] eq "on") { | |
$isFocusOn = 1; | |
} elsif ($ARGV[0] eq "off") { | |
$isFocusOn = 0; | |
} else { | |
usage(); | |
} | |
my ($outFh, $outFileName) = tempfile(); | |
open my $inFh, "<", $hostFileName or die $!; | |
my $isReadingFocusSection = 0; | |
my $isFocusSectionPrinted = 0; | |
while (my $line = <$inFh>) { | |
if ($line =~ /^# FOCUS/) { | |
printFocusSection($outFh); | |
$isFocusSectionPrinted = 1; | |
$isReadingFocusSection = 1; | |
next; | |
} | |
if ($line =~ /^# ENDFOCUS/) { | |
$isReadingFocusSection = 0; | |
next; | |
} | |
if (!$isReadingFocusSection) { | |
print $outFh $line; | |
} | |
} | |
if (!$isFocusSectionPrinted) { | |
printFocusSection($outFh); | |
} | |
chmod 0644, $outFileName or die $!; | |
rename $outFileName, $hostFileName or die $!; | |
__DATA__ | |
9gag.com | |
cracked.com | |
digg.com | |
facebook.com | |
hulu.com | |
imgur.com | |
instagram.com | |
lifehacker.com | |
loseit.com | |
netflix.com | |
news.ycombinator.com | |
nytimes.com | |
okcupid.com | |
overcomingbias.com | |
quora.com | |
reddit.com | |
theonion.com | |
tvtropes.com | |
twitter.com | |
wired.com | |
xkcd.com | |
ycombinator.com | |
youtube.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment