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