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