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