Add Rust, Julia, Brainfuck, PHP, OCaml and D
[plack-app-gruntmaster.git] / lib / Plack / App / Gruntmaster.pm
CommitLineData
7dc32473
MG
1package Plack::App::Gruntmaster;
2
3use 5.014000;
4use strict;
7dc32473
MG
5our $VERSION = '5999.000_001';
6
3b69df7a 7use Encode qw/encode decode/;
f34254b8 8use File::Slurp qw/read_file/;
3b69df7a 9use JSON::MaybeXS qw/encode_json/;
6c6fe1b1 10use List::Util qw/min/;
594d53ba
MG
11use PerlX::Maybe;
12use Scope::Upper qw/unwind SUB UP/;
3b69df7a 13use Web::Simple;
594d53ba
MG
14
15use Gruntmaster::Data;
3b69df7a
MG
16use Plack::App::Gruntmaster::HTML;
17
18use warnings NONFATAL => 'all';
19no warnings 'illegalproto';
f34254b8 20
594d53ba
MG
21##################################################
22
594d53ba
MG
23use constant USER_REGEX => qr/^\w{2,20}$/a;
24
594d53ba 25use constant FORMAT_EXTENSION => {
7baee29b 26 BRAINFUCK => 'bf',
594d53ba
MG
27 C => 'c',
28 CPP => 'cpp',
7baee29b 29 D => 'd',
a5e355d6
MG
30 GCCGO => 'go',
31 GOLANG => 'go',
d39978bf 32 GOLFSCRIPT => 'gs',
a5e355d6 33 HASKELL => 'hs',
594d53ba 34 JAVA => 'java',
7baee29b
S
35 JULIA => 'jl',
36 MONO => 'cs',
37 OCAML => 'ml',
594d53ba
MG
38 PASCAL => 'pas',
39 PERL => 'pl',
7baee29b 40 PHP => 'php',
594d53ba 41 PYTHON => 'py',
c83dca87 42 RUBY => 'rb',
7baee29b 43 RUST => 'rs',
e6d1bcd4 44 SBCL => 'l',
594d53ba
MG
45};
46
3ef32174 47use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
594d53ba 48
c039d63e 49my ($env, $privacy);
594d53ba 50
594d53ba 51sub remote_user {
2a06b02c
MG
52 unless ($env->{'gruntmaster.user'}) {
53 my $user = $env->{REMOTE_USER};
1f64ef28 54 $user &&= user_entry $user;
2a06b02c
MG
55 $env->{'gruntmaster.user'} = $user;
56 }
57 $env->{'gruntmaster.user'}
594d53ba
MG
58}
59
c5343878 60sub admin { remote_user && remote_user->{admin} }
594d53ba 61
54bc2c5c 62sub redirect { [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] }
69c01de9 63sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] }
594d53ba 64sub response {
6c6fe1b1 65 my ($template, $title, $params, $maxage, $smaxage) = @_;
594d53ba
MG
66 unless ($params) {
67 $params = $title;
68 $title = 'No title';
69 }
9a4806b3 70 $params->{time} = time;
39e2d01a 71 $params->{args} = {%_};
6c6fe1b1 72 bless {template => $template, title => $title, params => $params, maxage => ($maxage // 3600), maybe smaxage => $smaxage}, __PACKAGE__.'::Response'
594d53ba 73}
3b69df7a 74
c039d63e
MG
75sub forbid {
76 my ($condition) = @_;
77 $privacy = 'private' if $condition;
78 return if !$condition || admin;
8d725691 79 unwind $env->{authcomplex}->unauthorized, SUB UP
594d53ba 80}
31d70015 81
594d53ba
MG
82sub dispatch_request{
83 $env = $_[PSGI_ENV];
c039d63e
MG
84 $privacy = 'public';
85
594d53ba 86 sub (GET) {
3ef32174 87 sub (/robots.txt) { NOT_FOUND },
3ef32174 88
594d53ba 89 sub (/src/:job) {
25ed3a34 90 my $job = db->select(jobs => '*', {id => $_{job}})->hash;
c5343878
MG
91 return NOT_FOUND if !$job;
92 my $isowner = remote_user && remote_user->{id} eq $job->{owner};
1f64ef28 93 my $contest = $job->{contest} && contest_entry $job->{contest};
02d17b02 94 my $private = $job->{private} || $contest && ($contest->{started} && !$contest->{finished});
e547b147
MG
95 forbid !$isowner && $private;
96 my $privacy = $private ? 'private' : 'public';
218d6f7e 97 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => 'text/plain');
e547b147 98 push @headers, (Vary => 'Authorization') if $private;
02d17b02 99 [200, \@headers, [$job->{source}]]
594d53ba
MG
100 },
101
d3892d73 102 sub (?:format~) {
feaa8f5a 103 my $format = lc ($_{format} // '');
594d53ba
MG
104 response_filter {
105 my ($r) = @_;
106 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
6c6fe1b1
MG
107 my $smaxage = $r->{smaxage} ? ", s-maxage=$r->{smaxage}" : '';
108 my @hdrs = ('Cache-Control' => "$privacy$smaxage, max-age=$r->{maxage}");
109 push @hdrs, 'X-Forever' => 1 unless $smaxage;
928a611f 110 push @hdrs, Vary => 'Authorization' if $privacy eq 'private';
69c01de9 111 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
928a611f 112 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
69c01de9 113 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]]
594d53ba
MG
114 },
115 },
116
25ed3a34 117 sub (/st/:contest) {
cf22c656 118 my @pb = map { [$_->{id}, $_->{name}] } sort { $a->{value} <=> $b->{value} } @{problem_list contest => $_{contest}};
25ed3a34
MG
119 response st => 'Standings', {problems => \@pb, st => standings $_{contest}}, 10
120 },
594d53ba 121
645cfb7d 122 sub (/ed/:contest) {
25ed3a34
MG
123 my $contest = db->select(contests => '*', {id => $_{contest}})->hash;
124 forbid time < $contest->{stop};
1f64ef28 125 my $pblist = problem_list contest => $_{contest}, solution => 1;
25ed3a34 126 response ed => 'Editorial of ' . $contest->{name}, {pb => $pblist, editorial => $contest->{editorial}}
645cfb7d
MG
127 },
128
462db4aa
MG
129 sub (/login) {
130 forbid !remote_user;
2beb67b4 131 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary => 'Authorization'], [$env->{REMOTE_USER}]]
462db4aa
MG
132 },
133
594d53ba
MG
134 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
135
1f64ef28 136 sub (/us/) { response us => 'Users', {us => user_list} },
6c6fe1b1
MG
137 sub (/ct/ + ?:owner~) {
138 my $cts = contest_list(%_);
139 my $first_event = min
140 map ({ $_->{start} } grep { !$_->{started} } @$cts),
141 map ({ $_->{stop} } grep { $_->{started} && !$_->{finished}} @$cts);
142 response ct => 'Contests', {ct => $cts}, 300, $first_event ? ($first_event - time) : ();
143 },
23e32638 144 sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) {
b8a0fa71 145 forbid $_{private};
25ed3a34 146 my ($jobs, $pageinfo) = job_list(%_);
ea6cce4c 147 response log => 'Job log', {log => $jobs, %$pageinfo}, 5
b8a0fa71 148 },
87ffd88b 149 sub (/pb/ + ?:owner~&:contest~&:private~) {
b8a0fa71 150 forbid $_{private};
1f64ef28 151 my $pending = $_{contest} && !contest_entry($_{contest})->{started};
a18eef28 152 forbid $pending;
25ed3a34 153 response pb => 'Problems', {pb => problem_list %_}
b8a0fa71 154 },
594d53ba 155
c5343878 156 sub (/us/:user) {
1f64ef28 157 my $user = user_entry $_{user};
c5343878
MG
158 response us_entry => $user->{name}, $user
159 },
160 sub (/ct/:contest) {
1f64ef28 161 my $contest = contest_entry $_{contest};
6c6fe1b1
MG
162 my $smaxage;
163 $smaxage = $contest->{start} - time if !$contest->{started};
164 $smaxage = $contest->{stop} - time if $contest->{started} && !$contest->{finished};
165 response ct_entry => $contest->{name}, $contest, 60, $smaxage ? ($smaxage) : ()
c5343878 166 },
28e89d6c 167 sub (/log/:job) {
1f64ef28 168 my $job = job_entry $_{job};
c5343878
MG
169 forbid $job->{private};
170 response log_entry => "Job $_{job}", $job, 10
28e89d6c 171 },
84ca7535
MG
172 sub (/pb/:problem + ?contest~) {
173 my (undef, undef, $contest) = @_;
174 $_{contest} = $contest;
1f64ef28
MG
175 $contest = $contest && contest_entry $_{contest};
176 return NOT_FOUND if $contest && !contest_has_problem $_{contest}, $_{problem};
177 my $problem = problem_entry $_{problem}, $_{contest};
a18eef28
MG
178 forbid $problem->{private} && !$contest;
179 if ($contest) {
180 return redirect "/pb/$_{problem}" if !$contest->{started} || $contest->{finished};
5b76a57d
MG
181 forbid !remote_user;
182 $privacy = 'private';
183 }
a18eef28 184 response pb_entry => $problem->{name}, $problem, $_{contest} ? 10 : ();
594d53ba 185 },
e4d5bdf5 186 sub (/sol/:problem) {
1f64ef28 187 my $problem = problem_entry $_{problem};
a18eef28
MG
188 forbid $problem->{private};
189 response sol => 'Solution of ' . $problem->{name}, {solution => $problem->{solution}};
e4d5bdf5 190 },
594d53ba
MG
191
192 sub (/) { redispatch_to '/index' },
cb0122d7 193 sub (/favicon.ico) { redirect '/static/favicon.ico' },
928a611f 194 sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=3600', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
594d53ba
MG
195 },
196
197 sub (POST) {
08794667
MG
198 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
199 my (undef, undef, $prog) = @_;
594d53ba 200 forbid !remote_user;
1f64ef28 201 my $problem = problem_entry $_{problem};
a18eef28
MG
202 my $private = $problem->{private} ? 1 : 0;
203 if ($_{contest}) {
204 $private = 0;
1f64ef28 205 my $contest = contest_entry $_{contest};
a18eef28
MG
206 return reply 'This contest has not yet started' if !$contest->{started};
207 return reply 'This contest has finished' if $contest->{finished};
208 return reply 'This problem is private' if !admin && $private;
1f64ef28 209 return reply 'This problem does not belong to this contest' unless contest_has_problem $_{contest}, $_{problem};
a18eef28 210 }
08794667 211 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
c5343878 212 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->{lastjob} + 30;
594d53ba 213
08794667
MG
214 my $source = $prog ? read_file $prog->path : $_{source_code};
215 unlink $prog->path if $prog;
1f64ef28 216 my $id = create_job(
594d53ba 217 maybe contest => $_{contest},
cb6adaff 218 private => $private,
594d53ba
MG
219 date => time,
220 extension => FORMAT_EXTENSION->{$_{prog_format}},
221 format => $_{prog_format},
222 problem => $_{problem},
08794667 223 source => $source,
c5343878
MG
224 owner => remote_user->{id},
225 );
594d53ba 226
c5343878 227 [303, [Location => '/log/' . $id], []]
3c434a02 228 },
594d53ba 229 }
7dc32473
MG
230}
231
594d53ba 232
7dc32473
MG
2331;
234__END__
This page took 0.053275 seconds and 4 git commands to generate.