]> iEval git - plack-app-gruntmaster.git/blame_incremental - lib/Plack/App/Gruntmaster.pm
Merge branch 'master' into newmc
[plack-app-gruntmaster.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 Email::Sender::Simple qw/sendmail/;
20use Email::Simple;
21
22use warnings NONFATAL => 'all';
23no warnings 'illegalproto';
24
25##################################################
26
27use constant USER_REGEX => qr/^\w{2,20}$/a;
28
29use constant CONTENT_TYPES => +{
30 c => 'text/x-csrc',
31 cpp => 'text/x-c++src',
32 cs => 'text/x-csharp', # Used by GNOME. Not in mime.types.
33 go => 'text/plain', # ?
34 hs => 'text/x-haskell',
35 java => 'text/x-java',
36 pas => 'text/x-pascal',
37 pl => 'text/x-perl',
38 py => 'text/x-python',
39 l => 'text/plain',
40};
41
42use constant FORMAT_EXTENSION => {
43 C => 'c',
44 CPP => 'cpp',
45 GCCGO => 'go',
46 GOLANG => 'go',
47 HASKELL => 'hs',
48 MONO => 'cs',
49 JAVA => 'java',
50 PASCAL => 'pas',
51 PERL => 'pl',
52 PYTHON => 'py',
53 SBCL => 'l',
54};
55
56use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
57use constant FORBIDDEN => [401, ['Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="Gruntmaster 6000"'], ['Forbidden']];
58
59sub development() { ($ENV{PLACK_ENV} // 'development') eq 'development' }
60
61my ($env, $privacy);
62
63sub db { $env->{'gruntmaster.dbic'} }
64
65sub remote_user {
66 my $user = $env->{'gruntmaster.user'};
67 $user &&= db->user($user);
68 $user
69}
70
71sub admin { remote_user && remote_user->admin }
72sub contest { db->contest ($_{contest}) }
73sub problem { db->problem ($_{problem}) }
74sub job { db->job ($_{job}) }
75sub user { db->user ($_{user}) }
76
77sub redirect { [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
78sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] }
79sub response {
80 my ($template, $title, $params, $maxage) = @_;
81 unless ($params) {
82 $params = $title;
83 $title = 'No title';
84 }
85 $params->{time} = time;
86 $params->{args} = {%_};
87 bless {template => $template, title => $title, params => $params, maxage => ($maxage // 1)}, __PACKAGE__.'::Response'
88}
89
90sub forbid {
91 my ($condition) = @_;
92 $privacy = 'private' if $condition;
93 return if !$condition || admin;
94 unwind FORBIDDEN, SUB UP
95}
96
97sub dispatch_request{
98 $env = $_[PSGI_ENV];
99 $privacy = 'public';
100
101 sub (GET) {
102 sub (/css/:theme) {
103 my $theme = $_{theme};
104 return NOT_FOUND unless -e "css/themes/$theme.css";
105 my $css = read_file "css/themes/$theme.css";
106 $css .= read_file $_ for <css/*.css>;
107 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'text/css; charset=utf-8');
108 [200, \@headers, [development ? $css : CSS::Minifier::XS::minify $css]]
109 },
110
111 sub (/js.js) {
112 my $js;
113 $js .= read_file $_ for <js/*.js>;
114 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript; charset=utf-8');
115 [200, \@headers, [development ? $js : JavaScript::Minifier::XS::minify $js]]
116 },
117
118 sub (/robots.txt) { NOT_FOUND },
119 sub (/favicon.ico) { NOT_FOUND },
120
121 sub (/src/:job) {
122 return NOT_FOUND if !job;
123 my $isowner = remote_user && remote_user->id eq job->rawowner;
124 my $private = job->private || job->problem->private || job->contest && job->contest->is_running;
125 forbid !$isowner && $private;
126 my $privacy = $private ? 'private' : 'public';
127 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => CONTENT_TYPES->{job->format});
128 push @headers, (Vary => 'Authorization') if $private;
129 [200, \@headers, [job->source]]
130 },
131
132 sub (?:contest=) {
133 return NOT_FOUND if !contest;
134 forbid contest->is_pending;
135 response_filter { return shift }
136 },
137
138 sub (?:problem=) {
139 return NOT_FOUND if !problem;
140 forbid problem->is_private;
141 response_filter { return shift }
142 },
143
144 sub (?:format~) {
145 my $format = lc ($_{format} // '');
146 response_filter {
147 my ($r) = @_;
148 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
149 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
150 push @hdrs, Vary => 'Authorization' if $privacy eq 'private';
151 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
152 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
153 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]]
154 },
155 },
156
157 sub (/st/:contest) {
158 response st => 'Standings', {
159 st => [ contest->standings ],
160 problems => [
161 map { [$_->id, $_->name] }
162 sort { $a->value <=> $b->value }
163 map { $_->problem } contest->contest_problems],
164 }, 10
165 },
166
167 sub (/ed/:contest) {
168 forbid contest->is_running;
169 my $pblist = db->problem_list(contest => $_{contest}, solution => 1);
170 response ed => 'Editorial of ' . contest->name, {%$pblist, editorial => contest->editorial};
171 },
172
173 sub (/login) {
174 forbid !remote_user;
175
176 my $return = $env->{HTTP_REFERER} // '/';
177 [303, ['Set-Cookie' => "username=".remote_user->id, Location => $return], []]
178 },
179
180 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
181
182 sub (/us/) { response us => 'Users', {us => db->user_list} },
183 sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_) },
184 sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~) {
185 forbid $_{private};
186 response log => 'Job list', {%{db->job_list(%_)}, maybe contest => $_{contest},}
187 },
188 sub (/pb/ + ?:owner~&:contest~&:private~) {
189 forbid $_{private};
190 response pb => 'Problems', {%{db->problem_list(%_)}, maybe contest => $_{contest}}
191 },
192
193 sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) },
194 sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}) },
195 sub (/log/:job) {
196 forbid job->private;
197 response log_entry => "Job $_{job}", db->job_entry($_{job})
198 },
199 sub (/pb/:problem + ?contest~) {
200 my (undef, undef, $contest) = @_;
201 $_{contest} = $contest;
202 return NOT_FOUND if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
203 forbid problem->is_private;
204 if (contest && contest->is_running) {
205 forbid !remote_user;
206 $privacy = 'private';
207 }
208 response pb_entry => problem->name, {%{db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id)}, maybe contest => $_{contest}};
209 },
210 sub (/sol/:problem) {
211 forbid !problem->is_in_archive;
212 response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
213 },
214
215 sub (/) { redispatch_to '/index' },
216 sub (/favicon.ico) { redirect '/static/favicon.ico' },
217 sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=60', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
218 },
219
220 sub (POST) {
221 sub (/action/register + %:username=&:password=&:confirm_password=&:name=&:email=&:phone=&:town=&:university=&:country=&:level=) {
222 return reply 'Parameter too long' if grep { length > 200 } values %_;
223 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;
224 return reply 'Username already in use' if db->user($_{username});
225 return reply 'The two passwords do not match' unless $_{password} eq $_{confirm_password};
226
227 db->users->create({id => $_{username}, name => $_{name}, email => $_{email}, phone => $_{phone}, town => $_{town}, university => $_{university}, country => $_{country}, level => $_{level}});
228 db->user($_{username})->set_passphrase($_{password});
229
230 reply 'Registered successfully';
231 },
232
233 sub (/action/passwd + %:password=&:new_password=&:confirm_new_password=) {
234 forbid !remote_user;
235 return reply 'Incorrect password' unless remote_user->check_passphrase($_{password});
236 return reply 'The two passwords do not match' unless $_{new_password} eq $_{confirm_new_password};
237 remote_user->set_passphrase($_{new_password});
238 reply 'Password changed successfully';
239 },
240
241 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
242 my (undef, undef, $prog) = @_;
243 forbid !remote_user;
244 return reply 'This contest has finished' if contest && contest->is_finished;
245 return reply 'This contest has not yet started' if !admin && contest && contest->is_pending;
246 return reply 'This problem does not belong to this contest' if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
247 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
248 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30;
249 remote_user->update({lastjob => time});
250
251 my $source = $prog ? read_file $prog->path : $_{source_code};
252 unlink $prog->path if $prog;
253 my $private = (problem->private && !$_{contest}) ? 1 : 0;
254 $private = 1 if contest && contest->is_pending;
255 my $newjob = db->jobs->create({
256 maybe contest => $_{contest},
257 private => $private,
258 date => time,
259 extension => FORMAT_EXTENSION->{$_{prog_format}},
260 format => $_{prog_format},
261 problem => $_{problem},
262 source => $source,
263 owner => remote_user->id,
264 });
265
266 [303, [Location => '/log/' . $newjob->id], []]
267 },
268
269 sub (/action/request-reset + %:username=) {
270 return reply 'Password resets are disabled' unless $ENV{GRUNTMASTER_RESET_FROM};
271 my $user = db->user($_{username});
272 return reply 'No such user' unless $user;
273 my $token = join ':', $user->make_reset_hmac;
274 my $body = <<EOF;
275Someone has requested a password reset for your account.
276
277To reset your password, please submit the reset password form on the
278website using the following information:
279
280Username: $_{username}
281Password: <your new password>
282Reset token: $token
283
284The token is valid for 24 hours.
285EOF
286 my $email = Email::Simple->create(
287 header => [
288 From => $ENV{GRUNTMASTER_RESET_FROM},
289 To => $user->email,
290 Subject => 'Password reset token',
291 ],
292 body => $body,
293 );
294
295 my $ok = 0;
296 eval {
297 sendmail $email;
298 $ok = 1;
299 };
300 return reply 'Email sent' if $ok;
301 reply "Failure sending email: $@";
302 },
303
304 sub (/action/reset + %:username=&:password=&:token=) {
305 my $user = db->user($_{username});
306 return reply 'No such user' unless $user;
307 my ($token, $exp) = split ':', $_{token};
308 return reply 'Reset token is expired' if time >= $exp;
309 return reply 'Bad reset token' unless $user->make_reset_hmac($exp) eq $token;
310 $user->set_passphrase($_{password});
311 reply 'Password reset successfully';
312 },
313 }
314}
315
316
3171;
318__END__
This page took 0.024086 seconds and 4 git commands to generate.