Rewrite valid_room_name helper to return a hashref

master
Daniel Berteaud 10 years ago
parent 88315e6423
commit bdb86d4973
  1. 1
      lib/Vroom/I18N/en.pm
  2. 1
      lib/Vroom/I18N/fr.pm
  3. 70
      vroom.pl

@ -8,6 +8,7 @@ our %Lexicon = (
"VROOM_IS_FREE_SOFTWARE" => "VROOM is a free software, released under the MIT licence", "VROOM_IS_FREE_SOFTWARE" => "VROOM is a free software, released under the MIT licence",
"POWERED_BY" => "Proudly powered by", "POWERED_BY" => "Proudly powered by",
"ERROR_NAME_INVALID" => "This name is not valid", "ERROR_NAME_INVALID" => "This name is not valid",
"ERROR_NAME_RESERVED" => "This name is reserved and cannot be used",
"ERROR_NAME_CONFLICT" => "A room with this name already exists, please choose another one", "ERROR_NAME_CONFLICT" => "A room with this name already exists, please choose another one",
"ERROR_ROOM_s_DOESNT_EXIST" => "The room %s doesn't exist", "ERROR_ROOM_s_DOESNT_EXIST" => "The room %s doesn't exist",
"ERROR_ROOM_s_LOCKED" => "The room %s is locked, you cannot join it", "ERROR_ROOM_s_LOCKED" => "The room %s is locked, you cannot join it",

@ -10,6 +10,7 @@ our %Lexicon = (
"VROOM_IS_FREE_SOFTWARE" => "VROOM est un logiciel libre diffusé sous licence MIT", "VROOM_IS_FREE_SOFTWARE" => "VROOM est un logiciel libre diffusé sous licence MIT",
"POWERED_BY" => "Fièrement propulsé par", "POWERED_BY" => "Fièrement propulsé par",
"ERROR_NAME_INVALID" => "Ce nom n'est pas valide", "ERROR_NAME_INVALID" => "Ce nom n'est pas valide",
"ERROR_NAME_RESERVED" => "Ce nom est réservé et ne peut être utilisé",
"ERROR_NAME_CONFLICT" => "Ce nom est déjà pris, choisissez en un autre", "ERROR_NAME_CONFLICT" => "Ce nom est déjà pris, choisissez en un autre",
"ERROR_ROOM_s_DOESNT_EXIST" => "Le salon %s n'existe pas", "ERROR_ROOM_s_DOESNT_EXIST" => "Le salon %s n'existe pas",
"ERROR_ROOM_s_LOCKED" => "Le salon %s est verrouillé, vous ne pouvez pas le rejoindre", "ERROR_ROOM_s_LOCKED" => "Le salon %s est verrouillé, vous ne pouvez pas le rejoindre",

@ -81,6 +81,36 @@ plugin mail => {
type => 'text/html', type => 'text/html',
}; };
##########################
# Validation helpers #
##########################
# take a string as argument and check if it's a valid room name
helper valid_room_name => sub {
my $self = shift;
my ($name) = @_;
my $ret = {status => undef, msg => undef};
# A few names are reserved
my @reserved = qw(about help feedback feedback_thanks goodbye admin create localize action
missing dies password kicked invitation js css img fonts snd);
if ($name !~ m/^[\w\-]{1,49}$/){
$ret->{msg} = 'ERROR_NAME_INVALID';
}
elsif (grep { $name eq $_ } @reserved){
$ret->{msg} = 'ERROR_NAME_RESERVED';
}
else{
$ret->{status} = 1;
$ret->{msg} = 'OK'
}
return $ret;
};
##########################
# Various helpers #
##########################
# Create a cookie based session # Create a cookie based session
helper login => sub { helper login => sub {
my $self = shift; my $self = shift;
@ -133,7 +163,7 @@ helper create_room => sub {
$name = lc $name; $name = lc $name;
} }
# Exit if the name isn't valid or already taken # Exit if the name isn't valid or already taken
return undef if ($self->get_room_by_name($name) || !$self->valid_room_name($name)); return undef if ($self->get_room_by_name($name) || !$self->valid_room_name($name)->{status});
my $sth = eval { my $sth = eval {
$self->db->prepare('INSERT INTO `rooms` $self->db->prepare('INSERT INTO `rooms`
(`name`,`create_date`,`last_activity`,`owner`,`token`,`realm`) (`name`,`create_date`,`last_activity`,`owner`,`token`,`realm`)
@ -492,20 +522,6 @@ helper ping_room => sub {
return 1; return 1;
}; };
# Check if this name is a valid room name
helper valid_room_name => sub {
my $self = shift;
my ($name) = @_;
my $ret = undef;
# A few names are reserved
my @reserved = qw(about help feedback feedback_thanks goodbye admin create localize action
missing dies password kicked invitation js css img fonts snd);
if ($name =~ m/^[\w\-]{1,49}$/ && !grep { $name eq $_ } @reserved){
$ret = 1;
}
return $ret;
};
# Generate a random token # Generate a random token
helper get_random => sub { helper get_random => sub {
my $self = shift; my $self = shift;
@ -982,10 +998,10 @@ post '/create' => sub {
my $msg = $self->l('ERROR_OCCURRED'); my $msg = $self->l('ERROR_OCCURRED');
# Cleanup unused rooms before trying to create it # Cleanup unused rooms before trying to create it
$self->delete_rooms; $self->delete_rooms;
my $res = $self->valid_room_name($name);
if (!$self->valid_room_name($name)){ if (!$res->{status}){
$err = 'ERROR_NAME_INVALID'; $err = $res->{msg};
$msg = $self->l('ERROR_NAME_INVALID'); $msg = $self->l($res->{msg});
} }
elsif ($self->get_room_by_name($name)){ elsif ($self->get_room_by_name($name)){
$err = 'ERROR_NAME_CONFLICT'; $err = 'ERROR_NAME_CONFLICT';
@ -1077,10 +1093,11 @@ get '/(*room)' => sub {
} }
$self->delete_rooms; $self->delete_rooms;
$self->delete_invitations; $self->delete_invitations;
unless ($self->valid_room_name($room)){ my $res = $self->valid_room_name($room);
if (!$res->{status}){
return $self->render('error', return $self->render('error',
msg => $self->l('ERROR_NAME_INVALID'), msg => $self->l($res->{msg}),
err => 'ERROR_NAME_INVALID', err => $res->{msg},
room => $room room => $room
); );
} }
@ -1167,12 +1184,15 @@ post '/*action' => [action => [qw/action admin\/action/]] => sub {
); );
} }
# Sanity check on the room name # Sanity check on the room name
return $self->render( my $res = $self->valid_room_name($room);
if (!$res->{status}){
return $self->render(
json => { json => {
msg => sprintf ($self->l("ERROR_NAME_INVALID"), $room), msg => $self->l($res->{msg}),
status => 'error' status => 'error'
}, },
) unless ($self->valid_room_name($room)); );
}
# Push the room name to the stash, just in case # Push the room name to the stash, just in case
$self->stash(room => $room); $self->stash(room => $room);
# Gather room info from the DB # Gather room info from the DB

Loading…
Cancel
Save