1 package Plack
::App
::Gruntmaster
;
5 our $VERSION = '5999.000_001';
7 use Encode qw
/encode decode/;
8 use File
::Slurp qw
/read_file/;
9 use JSON
::MaybeXS qw
/encode_json/;
10 use List
::Util qw
/min/;
12 use Scope
::Upper qw
/unwind SUB UP/;
15 use Gruntmaster
::Data
;
16 use Plack
::App
::Gruntmaster
::HTML
;
18 use warnings NONFATAL
=> 'all';
19 no warnings
'illegalproto';
21 ##################################################
23 use constant USER_REGEX
=> qr/^\w{2,20}$/a;
25 use constant FORMAT_EXTENSION
=> {
47 use constant NOT_FOUND
=> [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
52 unless ($env->{'gruntmaster.user'}) {
53 my $user = $env->{REMOTE_USER
};
54 $user &&= user_entry
$user;
55 $env->{'gruntmaster.user'} = $user;
57 $env->{'gruntmaster.user'}
60 sub admin
{ remote_user
&& remote_user
->{admin
} }
62 sub redirect
{ [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] }
63 sub reply
{ [200, ['Content-Type' => 'text/plain; charset=utf-8'], \
@_] }
65 my ($template, $title, $params, $maxage, $smaxage) = @_;
70 $params->{time} = time;
71 $params->{args
} = {%_};
72 bless {template
=> $template, title
=> $title, params
=> $params, maxage
=> ($maxage // 3600), maybe smaxage
=> $smaxage}, __PACKAGE__
.'::Response'
77 $privacy = 'private' if $condition;
78 return if !$condition || admin
;
79 unwind
$env->{authcomplex
}->unauthorized, SUB UP
87 sub (/robots
.txt
) { NOT_FOUND
},
90 my $job = db
->select(jobs
=> '*', {id
=> $_{job
}})->hash;
91 return NOT_FOUND
if !$job;
92 my $isowner = remote_user
&& remote_user
->{id
} eq $job->{owner
};
93 my $contest = $job->{contest
} && contest_entry
$job->{contest
};
94 my $private = $job->{private
} || $contest && ($contest->{started
} && !$contest->{finished
});
95 forbid
!$isowner && $private;
96 my $privacy = $private ?
'private' : 'public';
97 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => 'text/plain');
98 push @headers, (Vary
=> 'Authorization') if $private;
99 [200, \
@headers, [$job->{source
}]]
103 my $format = lc ($_{format
} // '');
106 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
107 my $smaxage = $r->{smaxage
} ?
", s-maxage=$r->{smaxage}" : '';
108 my @hdrs = ('Cache-Control' => "$privacy$smaxage, max-age=$r->{maxage}");
109 push @hdrs, 'X-Forever' => 1 unless $smaxage;
110 push @hdrs, Vary
=> 'Authorization' if $privacy eq 'private';
111 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json
$r->{params
}]] if $format eq 'json';
112 my $ret = render
$r->{template
}, 'en', title
=> $r->{title
}, %{$r->{params
}};
113 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode
'UTF-8', $ret]]
118 my @pb = map { [$_->{id
}, $_->{name
}] } sort { $a->{value
} <=> $b->{value
} } @
{problem_list contest
=> $_{contest
}};
119 response st
=> 'Standings', {problems
=> \
@pb, st
=> standings
$_{contest
}}, 10
123 my $contest = db
->select(contests
=> '*', {id
=> $_{contest
}})->hash;
124 forbid
time < $contest->{stop
};
125 my $pblist = problem_list contest
=> $_{contest
}, solution
=> 1;
126 response ed
=> 'Editorial of ' . $contest->{name
}, {pb
=> $pblist, editorial
=> $contest->{editorial
}}
131 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary
=> 'Authorization'], [$env->{REMOTE_USER
}]]
134 sub (/ct/:contest
/log/st
) { redirect
"/st/$_{contest}" },
136 sub (/us/) { response us
=> 'Users', {us
=> user_list
} },
137 sub (/ct/ + ?
:owner
~) {
138 my $cts = contest_list
(%_);
139 my $first_event = min
140 map ({ $_->{start
} } grep { !$_->{started
} } @
$cts),
141 map ({ $_->{stop
} } grep { $_->{started
} && !$_->{finished
}} @
$cts);
142 response ct
=> 'Contests', {ct
=> $cts}, 300, $first_event ?
($first_event - time) : ();
144 sub (/log/ + ?
:contest
~&:owner
~&:page
~&:problem
~&:private
~&:result
~) {
146 my ($jobs, $pageinfo) = job_list
(%_);
147 response
log => 'Job log', {log => $jobs, %$pageinfo}, 5
149 sub (/pb/ + ?
:owner
~&:contest
~&:private
~) {
151 my $pending = $_{contest
} && !contest_entry
($_{contest
})->{started
};
153 response pb
=> 'Problems', {pb
=> problem_list
%_}
157 my $user = user_entry
$_{user
};
158 response us_entry
=> $user->{name
}, $user
161 my $contest = contest_entry
$_{contest
};
163 $smaxage = $contest->{start
} - time if !$contest->{started
};
164 $smaxage = $contest->{stop
} - time if $contest->{started
} && !$contest->{finished
};
165 response ct_entry
=> $contest->{name
}, $contest, 60, $smaxage ?
($smaxage) : ()
168 my $job = job_entry
$_{job
};
169 forbid
$job->{private
};
170 response log_entry
=> "Job $_{job}", $job, 10
172 sub (/pb/:problem
+ ?contest
~) {
173 my (undef, undef, $contest) = @_;
174 $_{contest
} = $contest;
175 $contest = $contest && contest_entry
$_{contest
};
176 return NOT_FOUND
if $contest && !contest_has_problem
$_{contest
}, $_{problem
};
177 my $problem = problem_entry
$_{problem
}, $_{contest
};
178 forbid
$problem->{private
} && !$contest;
180 return redirect
"/pb/$_{problem}" if !$contest->{started
} || $contest->{finished
};
182 $privacy = 'private';
184 response pb_entry
=> $problem->{name
}, $problem, $_{contest
} ?
10 : ();
186 sub (/sol/:problem
) {
187 my $problem = problem_entry
$_{problem
};
188 forbid
$problem->{private
};
189 response sol
=> 'Solution of ' . $problem->{name
}, {solution
=> $problem->{solution
}};
192 sub (/) { redispatch_to '/index
' },
193 sub (/favicon.ico) { redirect '/static
/favicon
.ico
' },
194 sub (/:article) { [200, ['Content
-Type
' => 'text
/html
; charset
=utf
-8', 'Cache
-Control
' => 'public
, max
-age
=3600', 'X
-Forever
' => 1], [render_article $_{article}, 'en
']] }
198 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
199 my (undef, undef, $prog) = @_;
201 my $problem = problem_entry $_{problem};
202 my $private = $problem->{private} ? 1 : 0;
205 my $contest = contest_entry $_{contest};
206 return reply 'This contest has
not yet started
' if !$contest->{started};
207 return reply 'This contest has finished
' if $contest->{finished};
208 return reply 'This problem is private
' if !admin && $private;
209 return reply 'This problem does
not belong to this contest
' unless contest_has_problem $_{contest}, $_{problem};
211 return reply 'Maximum source size is
10KB
' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
212 return reply 'You must
wait 30 seconds between jobs
' if !admin && time <= remote_user->{lastjob} + 30;
214 my $source = $prog ? read_file $prog->path : $_{source_code};
215 unlink $prog->path if $prog;
217 maybe contest => $_{contest},
220 extension => FORMAT_EXTENSION->{$_{prog_format}},
221 format => $_{prog_format},
222 problem => $_{problem},
224 owner => remote_user->{id},
227 [303, [Location => '/log/' . $id], []]