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