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