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']];
44 sub db
{ $env->{'gruntmaster.dbic'} }
47 unless ($env->{'gruntmaster.user'}) {
48 my $user = $env->{REMOTE_USER
};
49 $user &&= db
->user_entry($user);
50 $env->{'gruntmaster.user'} = $user;
52 $env->{'gruntmaster.user'}
55 sub admin
{ remote_user
&& remote_user
->{admin
} }
57 sub redirect
{ [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] }
58 sub reply
{ [200, ['Content-Type' => 'text/plain; charset=utf-8'], \
@_] }
60 my ($template, $title, $params, $maxage) = @_;
65 $params->{time} = time;
66 $params->{args
} = {%_};
67 bless {template
=> $template, title
=> $title, params
=> $params, maxage
=> ($maxage // 3600)}, __PACKAGE__
.'::Response'
72 $privacy = 'private' if $condition;
73 return if !$condition || admin
;
74 unwind
$env->{authcomplex
}->unauthorized, SUB UP
82 sub (/robots
.txt
) { NOT_FOUND
},
85 my $job = db
->job_full($_{job
});
86 return NOT_FOUND
if !$job;
87 my $isowner = remote_user
&& remote_user
->{id
} eq $job->{owner
};
88 my $contest = $job->{contest
} && db
->contest_entry($job->{contest
});
89 my $private = $job->{private
} || $contest && ($contest->{started
} && !$contest->{finished
});
90 forbid
!$isowner && $private;
91 my $privacy = $private ?
'private' : 'public';
92 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => 'text/plain');
93 push @headers, (Vary
=> 'Authorization') if $private;
94 [200, \
@headers, [$job->{source
}]]
98 my $format = lc ($_{format
} // '');
101 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
102 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
103 push @hdrs, Vary
=> 'Authorization' if $privacy eq 'private';
104 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json
$r->{params
}]] if $format eq 'json';
105 my $ret = render
$r->{template
}, 'en', title
=> $r->{title
}, %{$r->{params
}};
106 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode
'UTF-8', $ret]]
110 sub (/st/:contest
) { response st
=> 'Standings', db
->standings($_{contest
}), 10 },
113 my $contest = db
->contest_full($_{contest
});
114 forbid
!$contest->{finished
};
115 my $pblist = db
->problem_list(contest
=> $_{contest
}, solution
=> 1);
116 response ed
=> 'Editorial of ' . $contest->{name
}, {%$pblist, editorial
=> $contest->{editorial
}}
121 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary
=> 'Authorization'], [$env->{REMOTE_USER
}]]
124 sub (/ct/:contest
/log/st
) { redirect
"/st/$_{contest}" },
126 sub (/us/) { response us
=> 'Users', {us
=> db
->user_list} },
127 sub (/ct/ + ?
:owner
~) { response ct
=> 'Contests', db
->contest_list(%_), 300 },
128 sub (/log/ + ?
:contest
~&:owner
~&:page
~&:problem
~&:private
~&:result
~) {
130 response
log => 'Job list', db
->job_list(%_), 5
132 sub (/pb/ + ?
:owner
~&:contest
~&:private
~) {
134 my $pending = $_{contest
} && !db
->contest_entry($_{contest
})->{started
};
136 response pb
=> 'Problems', db
->problem_list(%_)
140 my $user = db
->user_entry($_{user
});
141 response us_entry
=> $user->{name
}, $user
144 my $contest = db
->contest_entry($_{contest
});
145 response ct_entry
=> $contest->{name
}, $contest, 60
148 my $job = db
->job_entry($_{job
});
149 forbid
$job->{private
};
150 response log_entry
=> "Job $_{job}", $job, 10
152 sub (/pb/:problem
+ ?contest
~) {
153 my (undef, undef, $contest) = @_;
154 $_{contest
} = $contest;
155 $contest = $contest && db
->contest_entry($_{contest
});
156 return NOT_FOUND
if $contest && !db
->contest_has_problem($_{contest
}, $_{problem
});
157 my $problem = db
->problem_entry($_{problem
}, $_{contest
});
158 forbid
$problem->{private
} && !$contest;
160 return redirect
"/pb/$_{problem}" if !$contest->{started
} || $contest->{finished
};
162 $privacy = 'private';
164 response pb_entry
=> $problem->{name
}, $problem, $_{contest
} ?
10 : ();
166 sub (/sol/:problem
) {
167 my $problem = db
->problem_entry($_{problem
});
168 forbid
$problem->{private
};
169 response sol
=> 'Solution of ' . $problem->{name
}, {solution
=> $problem->{solution
}};
172 sub (/) { redispatch_to '/index
' },
173 sub (/favicon.ico) { redirect '/static
/favicon
.ico
' },
174 sub (/:article) { [200, ['Content
-Type
' => 'text
/html
; charset
=utf
-8', 'Cache
-Control
' => 'public
, max
-age
=3600', 'X
-Forever
' => 1], [render_article $_{article}, 'en
']] }
178 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
179 my (undef, undef, $prog) = @_;
181 my $problem = db->problem_entry($_{problem});
182 my $private = $problem->{private} ? 1 : 0;
185 my $contest = db->contest_entry($_{contest});
186 return reply 'This contest has
not yet started
' if !$contest->{started};
187 return reply 'This contest has finished
' if $contest->{finished};
188 return reply 'This problem is private
' if !admin && $private;
189 return reply 'This problem does
not belong to this contest
' unless db->contest_has_problem($_{contest}, $_{problem});
191 return reply 'Maximum source size is
10KB
' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
192 return reply 'You must
wait 30 seconds between jobs
' if !admin && time <= remote_user->{lastjob} + 30;
194 my $source = $prog ? read_file $prog->path : $_{source_code};
195 unlink $prog->path if $prog;
196 my $id = db->create_job(
197 maybe contest => $_{contest},
200 extension => FORMAT_EXTENSION->{$_{prog_format}},
201 format => $_{prog_format},
202 problem => $_{problem},
204 owner => remote_user->{id},
207 [303, [Location => '/log/' . $id], []]