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