Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extract handling of challenge selection into Intercensor::User

  • Loading branch information...
commit 21c20f699252164d665958c66d6a2dd54e1db2d5 1 parent 870ed48
@mxey mxey authored
Showing with 27 additions and 19 deletions.
  1. +24 −0 lib/Intercensor/User.pm
  2. +3 −19 webif/app.pl
View
24 lib/Intercensor/User.pm
@@ -7,6 +7,8 @@ use Carp;
use DateTime;
use DBI;
use Intercensor::Challenge;
+use Intercensor::Util::Conntrack qw(delete_conntrack_states);
+use Intercensor::Util::IPSet qw(find_ipset add_to_ipset delete_from_ipset);
use List::MoreUtils qw(uniq);
use Set::Object qw(set);
@@ -75,6 +77,27 @@ sub address {
return $self->{address};
}
+sub current_challenge {
+ my ($self) = @_;
+ my $cid = find_ipset($self->address);
+ return Intercensor::Challenge->get($cid) if $cid;
+ return undef;
+}
+
+sub stop_challenge {
+ my ($self) = @_;
+ if ($self->current_challenge) {
+ delete_from_ipset($self->current_challenge->id, $self->address);
+ }
+}
+
+sub start_challenge {
+ my ($self, $challenge) = @_;
+ $self->stop_challenge();
+ add_to_ipset($challenge->id, $self->address);
+ delete_conntrack_states($self->address);
+}
+
sub solve_challenge {
my ($self, $challenge) = @_;
$dbh->do(
@@ -86,6 +109,7 @@ sub solve_challenge {
$challenge->id,
DateTime->now(),
);
+ $self->stop_challenge();
}
sub solved_challenges {
View
22 webif/app.pl
@@ -5,8 +5,6 @@
use lib '../lib';
use Intercensor::Challenge;
use Intercensor::User;
-use Intercensor::Util::Conntrack qw(delete_conntrack_states);
-use Intercensor::Util::IPSet qw(find_ipset add_to_ipset delete_from_ipset);
use IPC::System::Simple qw(capture);
use POSIX qw(strftime);
@@ -91,14 +89,7 @@
$user->address($self->tx->remote_address);
$self->stash(current_username => $self->session('user')->{name});
-
- my $cid = find_ipset($self->tx->remote_address);
- if ($cid) {
- $self->stash(current_challenge => Intercensor::Challenge->get($cid));
- }
- else {
- $self->stash(current_challenge => undef);
- }
+ $self->stash(current_challenge => $user->current_challenge);
my @latest_challenges = $self->session('user')->latest_solved_challenges();
$self->stash(latest_challenges => \@latest_challenges);
@@ -165,11 +156,7 @@
#debug sprintf('User %s starting challenge %s', $self->session('user')->{name}, $self->param('id'));
my $addr = $self->tx->remote_address;
- delete_from_ipset($self->stash('current_challenge')->id, $addr)
- if $self->stash('current_challenge');
- add_to_ipset($c->id, $addr);
- delete_conntrack_states($addr);
-
+ $self->session('user')->start_challenge($c);
$self->redirect_to('/challenge/' . $c->id);
}
else {
@@ -187,8 +174,7 @@
if ($c) {
#debug sprintf('User %s stopping challenge %s', $self->session('user')->{name}, $self->param('id'));
- delete_from_ipset($self->stash('current_challenge')->id,
- $self->tx->remote_address);
+ $self->session('user')->stop_challenge();
$self->redirect_to('/challenges');
}
else {
@@ -210,8 +196,6 @@
if ($c->verify_answer($self->session('user')->{id}, $a)) {
$self->session('user')->solve_challenge($c);
- delete_from_ipset($self->stash('current_challenge')->id,
- $self->tx->remote_address);
#debug sprintf('User %s solved challenge %s', $self->session('user')->{name}, $self->param('id'));
Please sign in to comment.
Something went wrong with that request. Please try again.