Created
March 11, 2009 02:12
-
-
Save tokuhirom/77254 to your computer and use it in GitHub Desktop.
This file contains 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
use strict; | |
use warnings; | |
use URI::Escape 'uri_escape'; | |
use LWP::UserAgent; | |
use JSON 'decode_json'; | |
use Term::ReadLine; | |
my $ua = LWP::UserAgent->new(agent => "Dan the shell"); | |
&main;exit; | |
sub main { | |
if (@ARGV) { | |
my $src = do { local $/; <> }; | |
die "invalid dan" unless $src; | |
do_dan($src); | |
exit; | |
} | |
my $term = Term::ReadLine->new("Dan the shell"); | |
while (defined($_ = $term->readline("dan> "))) { | |
do_dan($_); | |
$term->addhistory($_) if /\S/; | |
} | |
} | |
sub do_dan { | |
my $src = shift; | |
$_ =~ s/\n//; | |
$_ =~ s/^\s+//; | |
$_ =~ s/\s+$//; | |
if ($src =~ /^ls\s+(.+)$/) { | |
$src = sprintf q{use File::Spec; opendir my $d, '%s' or die $!;print join "\n", map { File::Spec->catfile('%s', $_) } readdir($d);}, $1, $1; | |
} elsif ($src =~ /^cat\s+(.+)$/) { | |
$src = sprintf q{open my $f, '%s' or die "$! '%s'";print do { local $/; <$f>;}}, $1, $1; | |
} elsif ($src eq 'ENV') { | |
$src = 'while (my($k, $v) = each %ENV) { print "$k => $v\n" }'; | |
} elsif ($src eq 'INC') { | |
$src = 'print join "\n", @INC;'; | |
} elsif ($src eq 'SEGV') { | |
$src = 'unpack "p", 0xdeadbeef'; | |
} elsif ($src eq 'RUN') { | |
use Path::Class; | |
my $src = file('run.pl')->slurp; | |
$src = $src; | |
} | |
print "Your code is ", length($src), " bytes\n"; | |
my $url = "http://api.dan.co.jp/lleval.cgi?l=pl&c=c&s=" . uri_escape($src); | |
print "access to $url\n"; | |
my $res = $ua->get($url); | |
if ($res->is_success) { | |
my $json = $res->content; | |
$json =~ s/c\((.+)\);\n/$1/; | |
my $dat = eval{ decode_json $json }; | |
if ($dat->{error}) { | |
print $dat->{error}, "\n"; | |
} elsif (defined $dat->{result}) { | |
print $dat->{result}, "\n"; | |
} else { | |
print "invalid json: $json\n"; | |
} | |
} else { | |
print $res->status_line, "\n"; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment