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