]> iEval git - plack-app-gruntmaster.git/blame - lib/Plack/App/Gruntmaster.pm
Serve lisp as text/plain
[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
e03e380b 7use CSS::Minifier::XS;
3b69df7a 8use Encode qw/encode decode/;
f34254b8 9use File::Slurp qw/read_file/;
e03e380b 10use JavaScript::Minifier::XS;
3b69df7a 11use JSON::MaybeXS qw/encode_json/;
594d53ba
MG
12use PerlX::Maybe;
13use Scope::Upper qw/unwind SUB UP/;
3b69df7a 14use Web::Simple;
594d53ba
MG
15
16use Gruntmaster::Data;
3b69df7a
MG
17use Plack::App::Gruntmaster::HTML;
18
19use warnings NONFATAL => 'all';
20no warnings 'illegalproto';
594d53ba 21no if $] >= 5.017011, warnings => 'experimental::smartmatch';
f34254b8 22
594d53ba
MG
23##################################################
24
594d53ba
MG
25use constant USER_REGEX => qr/^\w{2,20}$/a;
26
e03e380b
MG
27use constant CONTENT_TYPES => +{
28 c => 'text/x-csrc',
29 cpp => 'text/x-c++src',
30 cs => 'text/x-csharp', # Used by GNOME. Not in mime.types.
a5e355d6
MG
31 go => 'text/plain', # ?
32 hs => 'text/x-haskell',
e03e380b
MG
33 java => 'text/x-java',
34 pas => 'text/x-pascal',
35 pl => 'text/x-perl',
36 py => 'text/x-python',
76454bde 37 l => 'text/plain',
e03e380b 38};
594d53ba
MG
39
40use constant FORMAT_EXTENSION => {
41 C => 'c',
42 CPP => 'cpp',
a5e355d6
MG
43 GCCGO => 'go',
44 GOLANG => 'go',
45 HASKELL => 'hs',
594d53ba
MG
46 MONO => 'cs',
47 JAVA => 'java',
48 PASCAL => 'pas',
49 PERL => 'pl',
50 PYTHON => 'py',
e6d1bcd4 51 SBCL => 'l',
594d53ba
MG
52};
53
e03e380b 54use constant NOT_FOUND => [404, ['Content-Type' => 'text/plain'], ['Not found']];
3b69df7a 55use constant FORBIDDEN => [401, ['Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="Gruntmaster 6000"'], ['Forbidden']];
594d53ba 56
1bb102ef
MG
57sub development() { ($ENV{PLACK_ENV} // 'development') eq 'development' }
58
c039d63e 59my ($env, $privacy);
594d53ba
MG
60
61sub db { $env->{'gruntmaster.dbic'} }
62
63sub remote_user {
64 my $user = $env->{'gruntmaster.user'};
65 $user &&= db->user($user);
66 $user
67}
68
3b69df7a 69sub admin { remote_user && remote_user->admin }
594d53ba
MG
70sub contest { db->contest ($_{contest}) }
71sub problem { db->problem ($_{problem}) }
72sub job { db->job ($_{job}) }
73sub user { db->user ($_{user}) }
74
75sub redirect { [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
76sub reply { [200, ['Content-Type' => 'text/plain'], \@_] }
77sub response {
c039d63e 78 my ($template, $title, $params, $maxage) = @_;
594d53ba
MG
79 unless ($params) {
80 $params = $title;
81 $title = 'No title';
82 }
9a4806b3 83 $params->{time} = time;
39e2d01a 84 $params->{args} = {%_};
c039d63e 85 bless {template => $template, title => $title, params => $params, maxage => ($maxage // 1)}, __PACKAGE__.'::Response'
594d53ba 86}
3b69df7a 87
c039d63e
MG
88sub forbid {
89 my ($condition) = @_;
90 $privacy = 'private' if $condition;
91 return if !$condition || admin;
594d53ba
MG
92 unwind FORBIDDEN, SUB UP
93}
31d70015 94
594d53ba
MG
95sub dispatch_request{
96 $env = $_[PSGI_ENV];
c039d63e
MG
97 $privacy = 'public';
98
594d53ba
MG
99 sub (GET) {
100 sub (/css/:theme) {
101 my $theme = $_{theme};
102 return NOT_FOUND unless -e "css/themes/$theme.css";
103 my $css = read_file "css/themes/$theme.css";
104 $css .= read_file $_ for <css/*.css>;
105 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'text/css');
1bb102ef 106 [200, \@headers, [development ? $css : CSS::Minifier::XS::minify $css]]
594d53ba
MG
107 },
108
109 sub (/js.js) {
110 my $js;
111 $js .= read_file $_ for <js/*.js>;
112 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript');
1bb102ef 113 [200, \@headers, [development ? $js : JavaScript::Minifier::XS::minify $js]]
594d53ba
MG
114 },
115
116 sub (/src/:job) {
117 return NOT_FOUND if !job;
c22928ed 118 my $isowner = remote_user && remote_user->id eq job->rawowner;
e547b147
MG
119 my $private = job->private || job->problem->private || job->contest && job->contest->is_running;
120 forbid !$isowner && $private;
121 my $privacy = $private ? 'private' : 'public';
122 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => CONTENT_TYPES->{job->format});
123 push @headers, (Vary => 'Authorization') if $private;
594d53ba
MG
124 [200, \@headers, [job->source]]
125 },
126
127 sub (?:contest=) {
128 return NOT_FOUND if !contest;
129 forbid contest->is_pending;
130 response_filter { return shift }
131 },
132
133 sub (?:problem=) {
134 return NOT_FOUND if !problem;
135 forbid problem->is_private;
136 response_filter { return shift }
137 },
138
d3892d73 139 sub (?:format~) {
feaa8f5a 140 my $format = lc ($_{format} // '');
594d53ba
MG
141 response_filter {
142 my ($r) = @_;
143 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
c039d63e 144 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
be6551aa 145 push @hdrs, Vary => 'Authorization' if $privacy eq 'private';
c039d63e 146 return [200, ['Content-Type' => 'application/json', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
3b69df7a 147 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
c039d63e 148 [200, ['Content-Type' => 'text/html', @hdrs], [encode 'UTF-8', $ret]]
594d53ba
MG
149 },
150 },
151
594d53ba
MG
152 sub (/st/:contest) {
153 response st => 'Standings', {
7468a6a7 154 st => [ contest->standings ],
ebca729d
MG
155 problems => [
156 map { [$_->id, $_->name] }
157 sort { $a->value <=> $b->value }
158 map { $_->problem } contest->contest_problems],
c039d63e 159 }, 10
594d53ba
MG
160 },
161
645cfb7d
MG
162 sub (/ed/:contest) {
163 forbid contest->is_running;
164 response ed => 'Editorial of ' . contest->name, db->problem_list(contest => $_{contest}, solution => 1);
165 },
166
462db4aa
MG
167 sub (/login) {
168 forbid !remote_user;
169
4f3b70b7
MG
170 my $return = $env->{HTTP_REFERER} // '/';
171 [303, ['Set-Cookie' => "username=".remote_user->id, Location => $return], []]
462db4aa
MG
172 },
173
594d53ba
MG
174 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
175
176 sub (/us/) { response us => 'Users', {us => db->user_list} },
177 sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_) },
b8a0fa71
MG
178 sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~) {
179 forbid $_{private};
39e2d01a 180 response log => 'Job list', {%{db->job_list(%_)}, maybe contest => $_{contest},}
b8a0fa71 181 },
87ffd88b 182 sub (/pb/ + ?:owner~&:contest~&:private~) {
b8a0fa71
MG
183 forbid $_{private};
184 response pb => 'Problems', {%{db->problem_list(%_)}, maybe contest => $_{contest}}
185 },
594d53ba
MG
186
187 sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) },
188 sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}) },
28e89d6c
MG
189 sub (/log/:job) {
190 forbid job->private;
191 response log_entry => "Job $_{job}", db->job_entry($_{job})
192 },
84ca7535
MG
193 sub (/pb/:problem + ?contest~) {
194 my (undef, undef, $contest) = @_;
195 $_{contest} = $contest;
594d53ba
MG
196 return NOT_FOUND if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
197 forbid problem->is_private;
5b76a57d
MG
198 if (contest && contest->is_running) {
199 forbid !remote_user;
200 $privacy = 'private';
201 }
33ea2780 202 response pb_entry => problem->name, {%{db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id)}, maybe contest => $_{contest}};
594d53ba 203 },
e4d5bdf5
MG
204 sub (/sol/:problem) {
205 forbid !problem->is_in_archive;
206 response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
207 },
594d53ba
MG
208
209 sub (/) { redispatch_to '/index' },
cb0122d7 210 sub (/favicon.ico) { redirect '/static/favicon.ico' },
c039d63e 211 sub (/:article) { [200, ['Content-Type' => 'text/html', 'Cache-Control' => 'public, max-age=60', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
594d53ba
MG
212 },
213
214 sub (POST) {
ca0c7ea2 215 sub (/action/register + %:username=&:password=&:confirm_password=&:name=&:email=&:phone=&:town=&:university=&:country=&:level=) {
594d53ba
MG
216 return reply 'Parameter too long' if grep { length > 200 } values %_;
217 return reply 'Bad username. Allowed characters are letters, digits and underscores, and the username must be between 2 and 20 characters long.' unless $_{username} =~ USER_REGEX;
218 return reply 'Username already in use' if db->user($_{username});
219 return reply 'The two passwords do not match' unless $_{password} eq $_{confirm_password};
220
ca0c7ea2 221 db->users->create({id => $_{username}, name => $_{name}, email => $_{email}, phone => $_{phone}, town => $_{town}, university => $_{university}, country => $_{country}, level => $_{level}});
594d53ba
MG
222 db->user($_{username})->set_passphrase($_{password});
223
c039d63e 224 purge '/us/';
594d53ba
MG
225 reply 'Registered successfully';
226 },
227
228 sub (/action/passwd + %:password=&:new_password=&:confirm_new_password=) {
229 forbid !remote_user;
230 return reply 'Incorrect password' unless remote_user->check_passphrase($_{password});
231 return reply 'The two passwords do not match' unless $_{new_password} eq $_{confirm_new_password};
232 remote_user->set_passphrase($_{new_password});
233 reply 'Password changed successfully';
234 },
235
08794667
MG
236 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
237 my (undef, undef, $prog) = @_;
594d53ba 238 forbid !remote_user;
3b69df7a
MG
239 return reply 'This contest has finished' if contest && contest->is_finished;
240 return reply 'This contest has not yet started' if !admin && contest && contest->is_pending;
31f5eb01 241 return reply 'This problem does not belong to this contest' if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
08794667 242 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
594d53ba
MG
243 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30;
244 remote_user->update({lastjob => time});
245
08794667
MG
246 my $source = $prog ? read_file $prog->path : $_{source_code};
247 unlink $prog->path if $prog;
cb6adaff 248 my $private = (problem->private && !$_{contest}) ? 1 : 0;
b2597e87 249 $private = 1 if contest && contest->is_pending;
31dc8096 250 my $newjob = db->jobs->create({
594d53ba 251 maybe contest => $_{contest},
cb6adaff 252 private => $private,
594d53ba
MG
253 date => time,
254 extension => FORMAT_EXTENSION->{$_{prog_format}},
255 format => $_{prog_format},
256 problem => $_{problem},
08794667 257 source => $source,
594d53ba
MG
258 owner => remote_user->id,
259 });
260
c039d63e 261 purge '/log/';
31dc8096 262 [303, [Location => '/log/' . $newjob->id], []]
594d53ba
MG
263 }
264 }
7dc32473
MG
265}
266
594d53ba 267
7dc32473
MG
2681;
269__END__
This page took 0.079332 seconds and 4 git commands to generate.