]> iEval git - plack-app-gruntmaster.git/blob - lib/Plack/App/Gruntmaster.pm
814c70ff273ac8108cf31fad3eb736323cd6d008
[plack-app-gruntmaster.git] / lib / Plack / App / Gruntmaster.pm
1 package Plack::App::Gruntmaster;
2
3 use 5.014000;
4 use strict;
5 our $VERSION = '5999.000_001';
6
7 use Encode qw/encode decode/;
8 use File::Slurp qw/read_file/;
9 use JSON::MaybeXS qw/encode_json/;
10 use PerlX::Maybe;
11 use Scope::Upper qw/unwind SUB UP/;
12 use Web::Simple;
13
14 use Gruntmaster::Data;
15 use Plack::App::Gruntmaster::HTML;
16
17 use Email::Sender::Simple qw/sendmail/;
18 use Email::Simple;
19
20 use warnings NONFATAL => 'all';
21 no warnings 'illegalproto';
22
23 ##################################################
24
25 use constant USER_REGEX => qr/^\w{2,20}$/a;
26
27 use 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.
31 go => 'text/plain', # ?
32 hs => 'text/x-haskell',
33 java => 'text/x-java',
34 pas => 'text/x-pascal',
35 pl => 'text/x-perl',
36 py => 'text/x-python',
37 l => 'text/plain',
38 };
39
40 use constant FORMAT_EXTENSION => {
41 C => 'c',
42 CPP => 'cpp',
43 GCCGO => 'go',
44 GOLANG => 'go',
45 HASKELL => 'hs',
46 MONO => 'cs',
47 JAVA => 'java',
48 PASCAL => 'pas',
49 PERL => 'pl',
50 PYTHON => 'py',
51 SBCL => 'l',
52 };
53
54 use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
55
56 my ($env, $privacy);
57
58 sub db { $env->{'gruntmaster.dbic'} }
59
60 sub remote_user {
61 my $user = $env->{REMOTE_USER};
62 $user &&= db->user($user);
63 $user
64 }
65
66 sub admin { remote_user && remote_user->admin }
67 sub contest { db->contest ($_{contest}) }
68 sub problem { db->problem ($_{problem}) }
69 sub job { db->job ($_{job}) }
70 sub user { db->user ($_{user}) }
71
72 sub redirect { [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
73 sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] }
74 sub response {
75 my ($template, $title, $params, $maxage) = @_;
76 unless ($params) {
77 $params = $title;
78 $title = 'No title';
79 }
80 $params->{time} = time;
81 $params->{args} = {%_};
82 bless {template => $template, title => $title, params => $params, maxage => ($maxage // 1)}, __PACKAGE__.'::Response'
83 }
84
85 sub forbid {
86 my ($condition) = @_;
87 $privacy = 'private' if $condition;
88 return if !$condition || admin;
89 unwind $env->{authcomplex}->unauthorized, SUB UP
90 }
91
92 sub dispatch_request{
93 $env = $_[PSGI_ENV];
94 $privacy = 'public';
95
96 sub (GET) {
97 sub (/robots.txt) { NOT_FOUND },
98
99 sub (/src/:job) {
100 return NOT_FOUND if !job;
101 my $isowner = remote_user && remote_user->id eq job->rawowner;
102 my $private = job->private || job->problem->private || job->contest && job->contest->is_running;
103 forbid !$isowner && $private;
104 my $privacy = $private ? 'private' : 'public';
105 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => CONTENT_TYPES->{job->extension});
106 push @headers, (Vary => 'Authorization') if $private;
107 [200, \@headers, [job->source]]
108 },
109
110 sub (?:contest=) {
111 return NOT_FOUND if !contest;
112 forbid contest->is_pending;
113 response_filter { return shift }
114 },
115
116 sub (?:problem=) {
117 return NOT_FOUND if !problem;
118 forbid problem->private;
119 response_filter { return shift }
120 },
121
122 sub (?:format~) {
123 my $format = lc ($_{format} // '');
124 response_filter {
125 my ($r) = @_;
126 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
127 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
128 push @hdrs, Vary => 'Authorization' if $privacy eq 'private';
129 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
130 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
131 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]]
132 },
133 },
134
135 sub (/st/:contest) {
136 response st => 'Standings', {
137 st => [ contest->standings ],
138 problems => [
139 map { [$_->id, $_->name] }
140 sort { $a->value <=> $b->value }
141 map { $_->problem } contest->contest_problems],
142 }, 10
143 },
144
145 sub (/ed/:contest) {
146 forbid !contest->is_finished;
147 my $pblist = db->problem_list(contest => $_{contest}, solution => 1);
148 response ed => 'Editorial of ' . contest->name, {%$pblist, editorial => contest->editorial};
149 },
150
151 sub (/login) {
152 forbid !remote_user;
153
154 my $return = $env->{HTTP_REFERER} // '/';
155 [303, ['Set-Cookie' => "username=".remote_user->id, Location => $return], []]
156 },
157
158 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
159
160 sub (/us/) { response us => 'Users', {us => db->user_list} },
161 sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_) },
162 sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~) {
163 forbid $_{private};
164 response log => 'Job list', {%{db->job_list(%_)}, maybe contest => $_{contest},}
165 },
166 sub (/pb/ + ?:owner~&:contest~&:private~) {
167 forbid $_{private};
168 response pb => 'Problems', db->problem_list(%_)
169 },
170
171 sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) },
172 sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}) },
173 sub (/log/:job) {
174 forbid job->private;
175 response log_entry => "Job $_{job}", db->job_entry($_{job})
176 },
177 sub (/pb/:problem + ?contest~) {
178 my (undef, undef, $contest) = @_;
179 $_{contest} = $contest;
180 return NOT_FOUND if contest && !db->contest_problems->find($_{contest}, $_{problem});
181 forbid problem->private && !contest;
182 if (contest) {
183 return redirect "/pb/$_{problem}" unless contest->is_running;
184 forbid !remote_user;
185 $privacy = 'private';
186 }
187 response pb_entry => problem->name, {%{db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id)}, maybe contest => $_{contest}};
188 },
189 sub (/sol/:problem) {
190 forbid problem->private;
191 response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
192 },
193
194 sub (/) { redispatch_to '/index' },
195 sub (/favicon.ico) { redirect '/static/favicon.ico' },
196 sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=60', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
197 },
198
199 sub (POST) {
200 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
201 my (undef, undef, $prog) = @_;
202 forbid !remote_user;
203 my $private = (problem->private && !contest) ? 1 : 0;
204 return reply 'This contest has finished' if contest && contest->is_finished;
205 return reply 'This contest has not yet started' if contest && contest->is_pending;
206 return reply 'This problem is private' if !admin && $private;
207 return reply 'This problem does not belong to this contest' if contest && !db->contest_problems->find($_{contest}, $_{problem});
208 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
209 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30;
210 remote_user->update({lastjob => time});
211
212 my $source = $prog ? read_file $prog->path : $_{source_code};
213 unlink $prog->path if $prog;
214 my $newjob = db->jobs->create({
215 maybe contest => $_{contest},
216 private => $private,
217 date => time,
218 extension => FORMAT_EXTENSION->{$_{prog_format}},
219 format => $_{prog_format},
220 problem => $_{problem},
221 source => $source,
222 owner => remote_user->id,
223 });
224
225 [303, [Location => '/log/' . $newjob->id], []]
226 },
227 }
228 }
229
230
231 1;
232 __END__
This page took 0.069873 seconds and 3 git commands to generate.