3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-02 00:49:26 +01:00
This commit is contained in:
Pragmatic Software 2010-04-05 11:29:49 +00:00
parent 73a12dc981
commit b11722492a

View File

@ -9,6 +9,12 @@ use HTML::Entities;
use HTML::Parse; use HTML::Parse;
use HTML::FormatText; use HTML::FormatText;
my @languages = qw/C C++ D Haskell Lua OCaml PHP Perl Python Ruby Scheme Tcl/;
my %preludes = ( 'C' => "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n",
'C++' => "#include <iostream>\n",
);
if($#ARGV <= 0) { if($#ARGV <= 0) {
print "Usage: $0 <nick> <code>\n"; print "Usage: $0 <nick> <code>\n";
exit 0; exit 0;
@ -17,11 +23,12 @@ if($#ARGV <= 0) {
my $nick = shift @ARGV; my $nick = shift @ARGV;
my $code = join ' ', @ARGV; my $code = join ' ', @ARGV;
open FILE, ">> codepad_log.txt";
print FILE "$nick: $code\n";
my $lang = "C"; my $lang = "C";
$lang = $1 if $code =~ s/-lang=([^\b\s]+)//i; $lang = $1 if $code =~ s/-lang=([^\b\s]+)//i;
my @languages = qw/C C++ D Haskell Lua OCaml PHP Perl Python Ruby Scheme Tcl/;
my $found = 0; my $found = 0;
foreach my $l (@languages) { foreach my $l (@languages) {
if(uc $lang eq uc $l) { if(uc $lang eq uc $l) {
@ -41,18 +48,18 @@ my $ua = LWP::UserAgent->new();
$ua->agent("Mozilla/5.0"); $ua->agent("Mozilla/5.0");
push @{ $ua->requests_redirectable }, 'POST'; push @{ $ua->requests_redirectable }, 'POST';
$code =~ s/#include <([^>]+)>/#include <$1>\n/g; $code =~ s/#include <([^>]+)>/\n#include <$1>\n/g;
$code =~ s/#([^ ]+) (.*?)\\n/\n#$1 $2\n/g;
if(($lang eq "C" or $lang eq "C++") and not $code =~ m/\w+ main\s?\([^)]*\)\s?{/) { $code = $preludes{$lang} . $code;
my $includes = '';
$includes = $1 if $code =~ s/^(#include.*>)//; if(($lang eq "C" or $lang eq "C++") and not $code =~ m/(int|void) main\s*\([^)]*\)\s*{/) {
$code = "$includes\n int main(int argc, char **argv) { $code ; return 0; }"; my $prelude = '';
$prelude = "$1$2" if $code =~ s/^\s*(#.*)(#.*?[>\n])//s;
$code = "$prelude\n int main(int argc, char **argv) { $code ; return 0; }";
} }
# my $escaped_code = uri_escape($code, "\0-\377");
my %post = ( 'lang' => $lang, 'code' => $code, 'private' => 'True', 'run' => 'True', 'submit' => 'Submit' ); my %post = ( 'lang' => $lang, 'code' => $code, 'private' => 'True', 'run' => 'True', 'submit' => 'Submit' );
my $response = $ua->post("http://codepad.org", \%post); my $response = $ua->post("http://codepad.org", \%post);
if(not $response->is_success) { if(not $response->is_success) {
@ -61,10 +68,10 @@ if(not $response->is_success) {
} }
my $text = $response->decoded_content; my $text = $response->decoded_content;
my $redirect = $response->request->uri; my $url = $response->request->uri;
my $output; my $output;
# remove line numbers
$text =~ s/<a style="" name="output-line-\d+">\d+<\/a>//g; $text =~ s/<a style="" name="output-line-\d+">\d+<\/a>//g;
if($text =~ /<span class="heading">Output:<\/span>.+?<div class="code">(.*)<\/div>.+?<\/table>/si) { if($text =~ /<span class="heading">Output:<\/span>.+?<div class="code">(.*)<\/div>.+?<\/table>/si) {
@ -86,4 +93,7 @@ $output =~ s/ \(first use in this function\)//g;
$output =~ s/error: \(Each undeclared identifier is reported only once.*?\)//g; $output =~ s/error: \(Each undeclared identifier is reported only once.*?\)//g;
$output =~ s/error: (.*?) error/error: $1; error/g; $output =~ s/error: (.*?) error/error: $1; error/g;
print FILE "$nick: [$url] $output\n\n";
close FILE;
print "$nick: $output\n"; print "$nick: $output\n";