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