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