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/;
11 use Scope
::Upper qw
/unwind SUB UP/;
14 use Gruntmaster
::Data
;
15 use Plack
::App
::Gruntmaster
::HTML
;
17 use warnings NONFATAL
=> 'all';
18 no warnings
'illegalproto';
20 ##################################################
22 use constant USER_REGEX
=> qr/^\w{2,20}$/a;
24 use constant FORMAT_EXTENSION
=> {
40 use constant NOT_FOUND
=> [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
45 unless ($env->{'gruntmaster.user'}) {
46 my $user = $env->{REMOTE_USER
};
47 $user &&= user_entry
$user;
48 $env->{'gruntmaster.user'} = $user;
50 $env->{'gruntmaster.user'}
53 sub admin
{ remote_user
&& remote_user
->{admin
} }
55 sub redirect
{ [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] }
56 sub reply
{ [200, ['Content-Type' => 'text/plain; charset=utf-8'], \
@_] }
58 my ($template, $title, $params, $maxage) = @_;
63 $params->{time} = time;
64 $params->{args
} = {%_};
65 bless {template
=> $template, title
=> $title, params
=> $params, maxage
=> ($maxage // 3600)}, __PACKAGE__
.'::Response'
70 $privacy = 'private' if $condition;
71 return if !$condition || admin
;
72 unwind
$env->{authcomplex
}->unauthorized, SUB UP
80 sub (/robots
.txt
) { NOT_FOUND
},
83 my $job = job_full
$_{job
};
84 return NOT_FOUND
if !$job;
85 my $isowner = remote_user
&& remote_user
->{id
} eq $job->{owner
};
86 my $contest = $job->{contest
} && contest_entry
$job->{contest
};
87 my $private = $job->{private
} || $contest && ($contest->{started
} && !$contest->{finished
});
88 forbid
!$isowner && $private;
89 my $privacy = $private ?
'private' : 'public';
90 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => 'text/plain');
91 push @headers, (Vary
=> 'Authorization') if $private;
92 [200, \
@headers, [$job->{source
}]]
96 my $format = lc ($_{format
} // '');
99 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
100 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
101 push @hdrs, Vary
=> 'Authorization' if $privacy eq 'private';
102 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json
$r->{params
}]] if $format eq 'json';
103 my $ret = render
$r->{template
}, 'en', title
=> $r->{title
}, %{$r->{params
}};
104 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode
'UTF-8', $ret]]
108 sub (/st/:contest
) { response st
=> 'Standings', standings
($_{contest
}), 10 },
111 my $contest = contest_full
$_{contest
};
112 forbid
!$contest->{finished
};
113 my $pblist = problem_list contest
=> $_{contest
}, solution
=> 1;
114 response ed
=> 'Editorial of ' . $contest->{name
}, {%$pblist, editorial
=> $contest->{editorial
}}
119 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary
=> 'Authorization'], [$env->{REMOTE_USER
}]]
122 sub (/ct/:contest
/log/st
) { redirect
"/st/$_{contest}" },
124 sub (/us/) { response us
=> 'Users', {us
=> user_list
} },
125 sub (/ct/ + ?
:owner
~) { response ct
=> 'Contests', contest_list
(%_), 300 },
126 sub (/log/ + ?
:contest
~&:owner
~&:page
~&:problem
~&:private
~&:result
~) {
128 response
log => 'Job list', job_list
(%_), 5
130 sub (/pb/ + ?
:owner
~&:contest
~&:private
~) {
132 my $pending = $_{contest
} && !contest_entry
($_{contest
})->{started
};
134 response pb
=> 'Problems', problem_list
%_
138 my $user = user_entry
$_{user
};
139 response us_entry
=> $user->{name
}, $user
142 my $contest = contest_entry
$_{contest
};
143 response ct_entry
=> $contest->{name
}, $contest, 60
146 my $job = job_entry
$_{job
};
147 forbid
$job->{private
};
148 response log_entry
=> "Job $_{job}", $job, 10
150 sub (/pb/:problem
+ ?contest
~) {
151 my (undef, undef, $contest) = @_;
152 $_{contest
} = $contest;
153 $contest = $contest && contest_entry
$_{contest
};
154 return NOT_FOUND
if $contest && !contest_has_problem
$_{contest
}, $_{problem
};
155 my $problem = problem_entry
$_{problem
}, $_{contest
};
156 forbid
$problem->{private
} && !$contest;
158 return redirect
"/pb/$_{problem}" if !$contest->{started
} || $contest->{finished
};
160 $privacy = 'private';
162 response pb_entry
=> $problem->{name
}, $problem, $_{contest
} ?
10 : ();
164 sub (/sol/:problem
) {
165 my $problem = problem_entry
$_{problem
};
166 forbid
$problem->{private
};
167 response sol
=> 'Solution of ' . $problem->{name
}, {solution
=> $problem->{solution
}};
170 sub (/) { redispatch_to '/index
' },
171 sub (/favicon.ico) { redirect '/static
/favicon
.ico
' },
172 sub (/:article) { [200, ['Content
-Type
' => 'text
/html
; charset
=utf
-8', 'Cache
-Control
' => 'public
, max
-age
=3600', 'X
-Forever
' => 1], [render_article $_{article}, 'en
']] }
176 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
177 my (undef, undef, $prog) = @_;
179 my $problem = problem_entry $_{problem};
180 my $private = $problem->{private} ? 1 : 0;
183 my $contest = contest_entry $_{contest};
184 return reply 'This contest has
not yet started
' if !$contest->{started};
185 return reply 'This contest has finished
' if $contest->{finished};
186 return reply 'This problem is private
' if !admin && $private;
187 return reply 'This problem does
not belong to this contest
' unless contest_has_problem $_{contest}, $_{problem};
189 return reply 'Maximum source size is
10KB
' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
190 return reply 'You must
wait 30 seconds between jobs
' if !admin && time <= remote_user->{lastjob} + 30;
192 my $source = $prog ? read_file $prog->path : $_{source_code};
193 unlink $prog->path if $prog;
195 maybe contest => $_{contest},
198 extension => FORMAT_EXTENSION->{$_{prog_format}},
199 format => $_{prog_format},
200 problem => $_{problem},
202 owner => remote_user->{id},
205 [303, [Location => '/log/' . $id], []]