# Type: Input # Category: Admin # Description: Allows to evaluate a code inside Orangutan # Author: Andriy Lesyuk new Orangutan::Context( response => sub { my ($context, $user, $message) = @_; my @result = ( ); my $regexp = '^(?:Please )?(?:eval(?:uate)?|run|execute)(?:(?: (?:this|my|the following))? code)?[^A-Z0-9]*$'; if ($context->GetField('eval')) { $regexp = '.*'; } if ($message =~ /$regexp/i) { if ($context->GetField('eval')) { if ($message =~ /^End!*\.*$/i) { push(@result, 3); } else { push(@result, 2); push(@result, $message); } } else { push(@result, 1); } } return @result; }, handler => sub { my ($context, $user, $item, $code) = @_; if ($context->GetField('eval')) { if ($item == 2) { my $buffer = ''; local $SIG{'__DIE__'} = 'DEFAULT'; open(OLDOUT, '>&:utf8', \*STDOUT); open(OLDERR, '>&:utf8', \*STDERR); close(STDOUT); close(STDERR); open(STDOUT, '>>:utf8', \$buffer); open(STDERR, '>>:utf8', \$buffer); eval $code; open(STDOUT, '>&:utf8', \*OLDOUT); open(STDERR, '>&:utf8', \*OLDERR); if (defined($buffer) && ($buffer ne '')) { $user->SendMessage($buffer); } if ($@) { $user->SendMessage("Error:\n".$@); } } else { $context->UnsetField('eval'); $user->SendMessage([ 'Thanks for being so kind to let me live.', "Wow! I'm still alive! :) Thanks." ]); } } else { $context->SetField('eval', 1); $user->SendMessage([ "Please don't hurt me. Give your code! Type \"End\" to stop...", 'Go ahead! Put your code here. Send "End" to finish.' ]); } }, weight => sub { my ($context, $user) = @_; if ($user->IsCreator) { return ( undef, -100, undef ); } else { return ( undef, undef, undef ); } }, copy => 1 ); # kate: syntax perl