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
=> {
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) = @_;
64 $params->{time} = time;
65 $params->{args
} = {%_};
66 bless {template
=> $template, title
=> $title, params
=> $params, maxage
=> ($maxage // 3600)}, __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 @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
102 push @hdrs, Vary
=> 'Authorization' if $privacy eq 'private';
103 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json
$r->{params
}]] if $format eq 'json';
104 my $ret = render
$r->{template
}, 'en', title
=> $r->{title
}, %{$r->{params
}};
105 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode
'UTF-8', $ret]]
110 my @pb = map { [$_->{id
}, $_->{name
}] } sort { $a->{value
} <=> $b->{value
} } @
{problem_list contest
=> $_{contest
}};
111 response st
=> 'Standings', {problems
=> \
@pb, st
=> standings
$_{contest
}}, 10
115 my $contest = db
->select(contests
=> '*', {id
=> $_{contest
}})->hash;
116 forbid
time < $contest->{stop
};
117 my $pblist = problem_list contest
=> $_{contest
}, solution
=> 1;
118 response ed
=> 'Editorial of ' . $contest->{name
}, {pb
=> $pblist, editorial
=> $contest->{editorial
}}
123 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary
=> 'Authorization'], [$env->{REMOTE_USER
}]]
126 sub (/ct/:contest
/log/st
) { redirect
"/st/$_{contest}" },
128 sub (/us/) { response us
=> 'Users', {us
=> user_list
} },
129 sub (/ct/ + ?
:owner
~) { response ct
=> 'Contests', {ct
=> contest_list
(%_)}, 300 },
130 sub (/log/ + ?
:contest
~&:owner
~&:page
~&:problem
~&:private
~&:result
~) {
132 my ($jobs, $pageinfo) = job_list
(%_);
133 response
log => 'Job log', {log => $jobs, %$pageinfo}, 5
135 sub (/pb/ + ?
:owner
~&:contest
~&:private
~) {
137 my $pending = $_{contest
} && !contest_entry
($_{contest
})->{started
};
139 response pb
=> 'Problems', {pb
=> problem_list
%_}
143 my $user = user_entry
$_{user
};
144 response us_entry
=> $user->{name
}, $user
147 my $contest = contest_entry
$_{contest
};
148 response ct_entry
=> $contest->{name
}, $contest, 60
151 my $job = job_entry
$_{job
};
152 forbid
$job->{private
};
153 response log_entry
=> "Job $_{job}", $job, 10
155 sub (/pb/:problem
+ ?contest
~) {
156 my (undef, undef, $contest) = @_;
157 $_{contest
} = $contest;
158 $contest = $contest && contest_entry
$_{contest
};
159 return NOT_FOUND
if $contest && !contest_has_problem
$_{contest
}, $_{problem
};
160 my $problem = problem_entry
$_{problem
}, $_{contest
};
161 forbid
$problem->{private
} && !$contest;
163 return redirect
"/pb/$_{problem}" if !$contest->{started
} || $contest->{finished
};
165 $privacy = 'private';
167 response pb_entry
=> $problem->{name
}, $problem, $_{contest
} ?
10 : ();
169 sub (/sol/:problem
) {
170 my $problem = problem_entry
$_{problem
};
171 forbid
$problem->{private
};
172 response sol
=> 'Solution of ' . $problem->{name
}, {solution
=> $problem->{solution
}};
175 sub (/) { redispatch_to '/index
' },
176 sub (/favicon.ico) { redirect '/static
/favicon
.ico
' },
177 sub (/:article) { [200, ['Content
-Type
' => 'text
/html
; charset
=utf
-8', 'Cache
-Control
' => 'public
, max
-age
=3600', 'X
-Forever
' => 1], [render_article $_{article}, 'en
']] }
181 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
182 my (undef, undef, $prog) = @_;
184 my $problem = problem_entry $_{problem};
185 my $private = $problem->{private} ? 1 : 0;
188 my $contest = contest_entry $_{contest};
189 return reply 'This contest has
not yet started
' if !$contest->{started};
190 return reply 'This contest has finished
' if $contest->{finished};
191 return reply 'This problem is private
' if !admin && $private;
192 return reply 'This problem does
not belong to this contest
' unless contest_has_problem $_{contest}, $_{problem};
194 return reply 'Maximum source size is
10KB
' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
195 return reply 'You must
wait 30 seconds between jobs
' if !admin && time <= remote_user->{lastjob} + 30;
197 my $source = $prog ? read_file $prog->path : $_{source_code};
198 unlink $prog->path if $prog;
200 maybe contest => $_{contest},
203 extension => FORMAT_EXTENSION->{$_{prog_format}},
204 format => $_{prog_format},
205 problem => $_{problem},
207 owner => remote_user->{id},
210 [303, [Location => '/log/' . $id], []]