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
=> {
41 use constant NOT_FOUND
=> [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
46 unless ($env->{'gruntmaster.user'}) {
47 my $user = $env->{REMOTE_USER
};
48 $user &&= user_entry
$user;
49 $env->{'gruntmaster.user'} = $user;
51 $env->{'gruntmaster.user'}
54 sub admin
{ remote_user
&& remote_user
->{admin
} }
56 sub redirect
{ [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] }
57 sub reply
{ [200, ['Content-Type' => 'text/plain; charset=utf-8'], \
@_] }
59 my ($template, $title, $params, $maxage, $smaxage) = @_;
64 $params->{time} = time;
65 $params->{args
} = {%_};
66 bless {template
=> $template, title
=> $title, params
=> $params, maxage
=> ($maxage // 3600), maybe smaxage
=> $smaxage}, __PACKAGE__
.'::Response'
71 $privacy = 'private' if $condition;
72 return if !$condition || admin
;
73 unwind
$env->{authcomplex
}->unauthorized, SUB UP
81 sub (/robots
.txt
) { NOT_FOUND
},
84 my $job = db
->select(jobs
=> '*', {id
=> $_{job
}})->hash;
85 return NOT_FOUND
if !$job;
86 my $isowner = remote_user
&& remote_user
->{id
} eq $job->{owner
};
87 my $contest = $job->{contest
} && contest_entry
$job->{contest
};
88 my $private = $job->{private
} || $contest && ($contest->{started
} && !$contest->{finished
});
89 forbid
!$isowner && $private;
90 my $privacy = $private ?
'private' : 'public';
91 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => 'text/plain');
92 push @headers, (Vary
=> 'Authorization') if $private;
93 [200, \
@headers, [$job->{source
}]]
97 my $format = lc ($_{format
} // '');
100 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
101 my $smaxage = $r->{smaxage
} ?
", s-maxage=$r->{smaxage}" : '';
102 my @hdrs = ('Cache-Control' => "$privacy$smaxage, max-age=$r->{maxage}");
103 push @hdrs, 'X-Forever' => 1 unless $smaxage;
104 push @hdrs, Vary
=> 'Authorization' if $privacy eq 'private';
105 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json
$r->{params
}]] if $format eq 'json';
106 my $ret = render
$r->{template
}, 'en', title
=> $r->{title
}, %{$r->{params
}};
107 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode
'UTF-8', $ret]]
112 my @pb = map { [$_->{id
}, $_->{name
}] } sort { $a->{value
} <=> $b->{value
} } @
{problem_list contest
=> $_{contest
}};
113 response st
=> 'Standings', {problems
=> \
@pb, st
=> standings
$_{contest
}}, 10
117 my $contest = db
->select(contests
=> '*', {id
=> $_{contest
}})->hash;
118 forbid
time < $contest->{stop
};
119 my $pblist = problem_list contest
=> $_{contest
}, solution
=> 1;
120 response ed
=> 'Editorial of ' . $contest->{name
}, {pb
=> $pblist, editorial
=> $contest->{editorial
}}
125 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary
=> 'Authorization'], [$env->{REMOTE_USER
}]]
128 sub (/ct/:contest
/log/st
) { redirect
"/st/$_{contest}" },
130 sub (/us/) { response us
=> 'Users', {us
=> user_list
} },
131 sub (/ct/ + ?
:owner
~) {
132 my $cts = contest_list
(%_);
133 my $first_event = min
134 map ({ $_->{start
} } grep { !$_->{started
} } @
$cts),
135 map ({ $_->{stop
} } grep { $_->{started
} && !$_->{finished
}} @
$cts);
136 response ct
=> 'Contests', {ct
=> $cts}, 300, $first_event ?
($first_event - time) : ();
138 sub (/log/ + ?
:contest
~&:owner
~&:page
~&:problem
~&:private
~&:result
~) {
140 my ($jobs, $pageinfo) = job_list
(%_);
141 response
log => 'Job log', {log => $jobs, %$pageinfo}, 5
143 sub (/pb/ + ?
:owner
~&:contest
~&:private
~) {
145 my $pending = $_{contest
} && !contest_entry
($_{contest
})->{started
};
147 response pb
=> 'Problems', {pb
=> problem_list
%_}
151 my $user = user_entry
$_{user
};
152 response us_entry
=> $user->{name
}, $user
155 my $contest = contest_entry
$_{contest
};
157 $smaxage = $contest->{start
} - time if !$contest->{started
};
158 $smaxage = $contest->{stop
} - time if $contest->{started
} && !$contest->{finished
};
159 response ct_entry
=> $contest->{name
}, $contest, 60, $smaxage ?
($smaxage) : ()
162 my $job = job_entry
$_{job
};
163 forbid
$job->{private
};
164 response log_entry
=> "Job $_{job}", $job, 10
166 sub (/pb/:problem
+ ?contest
~) {
167 my (undef, undef, $contest) = @_;
168 $_{contest
} = $contest;
169 $contest = $contest && contest_entry
$_{contest
};
170 return NOT_FOUND
if $contest && !contest_has_problem
$_{contest
}, $_{problem
};
171 my $problem = problem_entry
$_{problem
}, $_{contest
};
172 forbid
$problem->{private
} && !$contest;
174 return redirect
"/pb/$_{problem}" if !$contest->{started
} || $contest->{finished
};
176 $privacy = 'private';
178 response pb_entry
=> $problem->{name
}, $problem, $_{contest
} ?
10 : ();
180 sub (/sol/:problem
) {
181 my $problem = problem_entry
$_{problem
};
182 forbid
$problem->{private
};
183 response sol
=> 'Solution of ' . $problem->{name
}, {solution
=> $problem->{solution
}};
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], [render_article $_{article}, 'en
']] }
192 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
193 my (undef, undef, $prog) = @_;
195 my $problem = problem_entry $_{problem};
196 my $private = $problem->{private} ? 1 : 0;
199 my $contest = contest_entry $_{contest};
200 return reply 'This contest has
not yet started
' if !$contest->{started};
201 return reply 'This contest has finished
' if $contest->{finished};
202 return reply 'This problem is private
' if !admin && $private;
203 return reply 'This problem does
not belong to this contest
' unless contest_has_problem $_{contest}, $_{problem};
205 return reply 'Maximum source size is
10KB
' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
206 return reply 'You must
wait 30 seconds between jobs
' if !admin && time <= remote_user->{lastjob} + 30;
208 my $source = $prog ? read_file $prog->path : $_{source_code};
209 unlink $prog->path if $prog;
211 maybe contest => $_{contest},
214 extension => FORMAT_EXTENSION->{$_{prog_format}},
215 format => $_{prog_format},
216 problem => $_{problem},
218 owner => remote_user->{id},
221 [303, [Location => '/log/' . $id], []]