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