]> iEval git - gruntmaster-page.git/blame_incremental - lib/Plack/App/Gruntmaster.pm
Iterate on tr not tbody in process_pb
[gruntmaster-page.git] / lib / Plack / App / Gruntmaster.pm
... / ...
CommitLineData
1package Plack::App::Gruntmaster;
2
3use 5.014000;
4use strict;
5our $VERSION = '5999.000_001';
6
7use CSS::Minifier::XS;
8use Encode qw/encode decode/;
9use File::Slurp qw/read_file/;
10use JavaScript::Minifier::XS;
11use JSON::MaybeXS qw/encode_json/;
12use PerlX::Maybe;
13use Scope::Upper qw/unwind SUB UP/;
14use Web::Simple;
15
16use Gruntmaster::Data;
17use Plack::App::Gruntmaster::HTML;
18
19use warnings NONFATAL => 'all';
20no warnings 'illegalproto';
21no if $] >= 5.017011, warnings => 'experimental::smartmatch';
22
23##################################################
24
25use constant USER_REGEX => qr/^\w{2,20}$/a;
26
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.
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};
38
39use constant FORMAT_EXTENSION => {
40 C => 'c',
41 CPP => 'cpp',
42 GCCGO => 'go',
43 GOLANG => 'go',
44 HASKELL => 'hs',
45 MONO => 'cs',
46 JAVA => 'java',
47 PASCAL => 'pas',
48 PERL => 'pl',
49 PYTHON => 'py',
50};
51
52use constant NOT_FOUND => [404, ['Content-Type' => 'text/plain'], ['Not found']];
53use constant FORBIDDEN => [401, ['Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="Gruntmaster 6000"'], ['Forbidden']];
54
55sub development() { ($ENV{PLACK_ENV} // 'development') eq 'development' }
56
57my ($env, $privacy);
58
59sub db { $env->{'gruntmaster.dbic'} }
60
61sub remote_user {
62 my $user = $env->{'gruntmaster.user'};
63 $user &&= db->user($user);
64 $user
65}
66
67sub admin { remote_user && remote_user->admin }
68sub contest { db->contest ($_{contest}) }
69sub problem { db->problem ($_{problem}) }
70sub job { db->job ($_{job}) }
71sub user { db->user ($_{user}) }
72
73sub redirect { [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
74sub reply { [200, ['Content-Type' => 'text/plain'], \@_] }
75sub response {
76 my ($template, $title, $params, $maxage) = @_;
77 unless ($params) {
78 $params = $title;
79 $title = 'No title';
80 }
81 $params->{time} = time;
82 $params->{args} = {%_};
83 bless {template => $template, title => $title, params => $params, maxage => ($maxage // 1)}, __PACKAGE__.'::Response'
84}
85
86sub forbid {
87 my ($condition) = @_;
88 $privacy = 'private' if $condition;
89 return if !$condition || admin;
90 unwind FORBIDDEN, SUB UP
91}
92
93sub dispatch_request{
94 $env = $_[PSGI_ENV];
95 $privacy = 'public';
96
97 sub (GET) {
98 sub (/css/:theme) {
99 my $theme = $_{theme};
100 return NOT_FOUND unless -e "css/themes/$theme.css";
101 my $css = read_file "css/themes/$theme.css";
102 $css .= read_file $_ for <css/*.css>;
103 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'text/css');
104 [200, \@headers, [development ? $css : CSS::Minifier::XS::minify $css]]
105 },
106
107 sub (/js.js) {
108 my $js;
109 $js .= read_file $_ for <js/*.js>;
110 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript');
111 [200, \@headers, [development ? $js : JavaScript::Minifier::XS::minify $js]]
112 },
113
114 sub (/src/:job) {
115 return NOT_FOUND if !job;
116 my $isowner = remote_user && remote_user->id eq job->rawowner;
117 forbid !$isowner && (job->private || job->problem->private || job->contest && job->contest->is_running);
118 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => CONTENT_TYPES->{job->format});
119 [200, \@headers, [job->source]]
120 },
121
122 sub (?:contest=) {
123 return NOT_FOUND if !contest;
124 forbid contest->is_pending;
125 response_filter { return shift }
126 },
127
128 sub (?:problem=) {
129 return NOT_FOUND if !problem;
130 forbid problem->is_private;
131 response_filter { return shift }
132 },
133
134 sub (?:format~) {
135 my $format = lc ($_{format} // '');
136 response_filter {
137 my ($r) = @_;
138 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
139 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
140 push @hdrs, Vary => 'Authorization' if $privacy eq 'private';
141 return [200, ['Content-Type' => 'application/json', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
142 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
143 [200, ['Content-Type' => 'text/html', @hdrs], [encode 'UTF-8', $ret]]
144 },
145 },
146
147 sub (/st/:contest) {
148 response st => 'Standings', {
149 st => [ contest->standings ],
150 problems => [
151 map { [$_->id, $_->name] }
152 sort { $a->value <=> $b->value }
153 map { $_->problem } contest->contest_problems],
154 }, 10
155 },
156
157 sub (/ed/:contest) {
158 forbid contest->is_running;
159 response ed => 'Editorial of ' . contest->name, db->problem_list(contest => $_{contest}, solution => 1);
160 },
161
162 sub (/login) {
163 forbid !remote_user;
164
165 my $return = $env->{HTTP_REFERER} // '/';
166 [303, ['Set-Cookie' => "username=".remote_user->id, Location => $return], []]
167 },
168
169 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
170
171 sub (/us/) { response us => 'Users', {us => db->user_list} },
172 sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_) },
173 sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~) {
174 forbid $_{private};
175 response log => 'Job list', {%{db->job_list(%_)}, maybe contest => $_{contest},}
176 },
177 sub (/pb/ + ?:owner~&:contest~&:private~) {
178 forbid $_{private};
179 response pb => 'Problems', {%{db->problem_list(%_)}, maybe contest => $_{contest}}
180 },
181
182 sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) },
183 sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}) },
184 sub (/log/:job) {
185 forbid job->private;
186 response log_entry => "Job $_{job}", db->job_entry($_{job})
187 },
188 sub (/pb/:problem + ?contest~) {
189 my (undef, undef, $contest) = @_;
190 $_{contest} = $contest;
191 return NOT_FOUND if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
192 forbid problem->is_private;
193 if (contest && contest->is_running) {
194 forbid !remote_user;
195 $privacy = 'private';
196 }
197 response pb_entry => problem->name, {%{db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id)}, maybe contest => $_{contest}};
198 },
199 sub (/sol/:problem) {
200 forbid !problem->is_in_archive;
201 response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
202 },
203
204 sub (/) { redispatch_to '/index' },
205 sub (/favicon.ico) { redirect '/static/favicon.ico' },
206 sub (/:article) { [200, ['Content-Type' => 'text/html', 'Cache-Control' => 'public, max-age=60', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
207 },
208
209 sub (POST) {
210 sub (/action/register + %:username=&:password=&:confirm_password=&:name=&:email=&:phone=&:town=&:university=&:country=&:level=) {
211 return reply 'Parameter too long' if grep { length > 200 } values %_;
212 return reply 'Bad username. Allowed characters are letters, digits and underscores, and the username must be between 2 and 20 characters long.' unless $_{username} =~ USER_REGEX;
213 return reply 'Username already in use' if db->user($_{username});
214 return reply 'The two passwords do not match' unless $_{password} eq $_{confirm_password};
215
216 db->users->create({id => $_{username}, name => $_{name}, email => $_{email}, phone => $_{phone}, town => $_{town}, university => $_{university}, country => $_{country}, level => $_{level}});
217 db->user($_{username})->set_passphrase($_{password});
218
219 purge '/us/';
220 reply 'Registered successfully';
221 },
222
223 sub (/action/passwd + %:password=&:new_password=&:confirm_new_password=) {
224 forbid !remote_user;
225 return reply 'Incorrect password' unless remote_user->check_passphrase($_{password});
226 return reply 'The two passwords do not match' unless $_{new_password} eq $_{confirm_new_password};
227 remote_user->set_passphrase($_{new_password});
228 reply 'Password changed successfully';
229 },
230
231 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
232 my (undef, undef, $prog) = @_;
233 forbid !remote_user;
234 return reply 'This contest has finished' if contest && contest->is_finished;
235 return reply 'This contest has not yet started' if !admin && contest && contest->is_pending;
236 return reply 'This problem does not belong to this contest' if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
237 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
238 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30;
239 remote_user->update({lastjob => time});
240
241 my $source = $prog ? read_file $prog->path : $_{source_code};
242 unlink $prog->path if $prog;
243 my $newjob = db->jobs->create({
244 maybe contest => $_{contest},
245 maybe private => problem->private && !$_{contest},
246 date => time,
247 extension => FORMAT_EXTENSION->{$_{prog_format}},
248 format => $_{prog_format},
249 problem => $_{problem},
250 source => $source,
251 owner => remote_user->id,
252 });
253
254 purge '/log/';
255 [303, [Location => '/log/' . $newjob->id], []]
256 }
257 }
258}
259
260
2611;
262__END__
This page took 0.02283 seconds and 4 git commands to generate.