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