Simple return value of valid_name

master
root 10 years ago committed by Daniel Berteaud
parent effaf249ad
commit b7924cbf2f
  1. 45
      vroom.pl

@ -93,13 +93,10 @@ helper valid_room_name => sub {
# A few names are reserved # A few names are reserved
my @reserved = qw(about help feedback feedback_thanks goodbye admin create localize action my @reserved = qw(about help feedback feedback_thanks goodbye admin create localize action
missing dies password kicked invitation js css img fonts snd); missing dies password kicked invitation js css img fonts snd);
if ($name !~ m/^[\w\-]{1,49}$/){ if ($name !~ m/^[\w\-]{1,49}$/ || grep { $name eq $_ } @reserved){
return {msg => 'ERROR_NAME_INVALID'}; return 0;
} }
elsif (grep { $name eq $_ } @reserved){ return 1;
return {msg => 'ERROR_NAME_RESERVED'};
}
return {ok => 1};
}; };
# Check id arg is a valid ID number # Check id arg is a valid ID number
@ -157,9 +154,8 @@ helper create_room => sub {
$name = lc $name; $name = lc $name;
} }
# Check if the name is valid # Check if the name is valid
$res = $self->valid_room_name($name); if (!$self->valid_room_name($name)){
if (!$res->{ok}){ return 0;
return $res;
} }
$res = $self->get_room_by_name($name); $res = $self->get_room_by_name($name);
if ($res->{data}){ if ($res->{data}){
@ -206,8 +202,8 @@ helper get_room_by_name => sub {
my $self = shift; my $self = shift;
my ($name) = @_; my ($name) = @_;
my $res = $self->valid_room_name($name); my $res = $self->valid_room_name($name);
if (!$res->{ok}){ if (!$self->valid_room_name($name)){
return $res; return 0;
} }
my $sth = eval { my $sth = eval {
$self->db->prepare('SELECT * $self->db->prepare('SELECT *
@ -262,9 +258,8 @@ helper modify_room => sub {
if (!$res->{ok}){ if (!$res->{ok}){
return $res; return $res;
} }
$res = $self->valid_room_name($room->{name}); if (!$self->valid_room_name($room->{name})){
if (!$res->{ok}){ return 0;
return $res;
} }
if (($room->{locked} && $room->{locked} !~ m/^0|1$/) || if (($room->{locked} && $room->{locked} !~ m/^0|1$/) ||
($room->{ask_for_name} && $room->{ask_for_name} !~ m/^0|1$/) || ($room->{ask_for_name} && $room->{ask_for_name} !~ m/^0|1$/) ||
@ -1135,10 +1130,9 @@ post '/create' => sub {
}; };
# Cleanup unused rooms before trying to create it # Cleanup unused rooms before trying to create it
$self->purge_rooms; $self->purge_rooms;
my $res = $self->valid_room_name($name); if (!$self->valid_room_name($name)){
if (!$res->{ok}){ $json->{err} = 'ERROR_NAME_INVALID';
$json->{err} = $res->{msg}; $json->{msg} = $self->l('ERROR_NAME_INVALID');
$json->{msg} = $self->l($res->{msg});
return $self->render(json => $json); return $self->render(json => $json);
} }
elsif ($self->get_room_by_name($name)->{data}){ elsif ($self->get_room_by_name($name)->{data}){
@ -1146,7 +1140,7 @@ post '/create' => sub {
$json->{msg} = $self->l('ERROR_NAME_CONFLICT'); $json->{msg} = $self->l('ERROR_NAME_CONFLICT');
return $self->render(json => $json); return $self->render(json => $json);
} }
$res = $self->create_room($name,$self->session('name')); my $res = $self->create_room($name,$self->session('name'));
if (!$res->{ok}){ if (!$res->{ok}){
$json->{err} = $res->{msg}; $json->{err} = $res->{msg};
$json->{msg} = $self->l($res->{msg}); $json->{msg} = $self->l($res->{msg});
@ -1233,10 +1227,10 @@ get '/(*room)' => sub {
$self->purge_rooms; $self->purge_rooms;
$self->delete_invitations; $self->delete_invitations;
my $res = $self->valid_room_name($room); my $res = $self->valid_room_name($room);
if (!$res->{ok}){ if (!$self->valid_room_name($room)){
return $self->render('error', return $self->render('error',
msg => $self->l($res->{msg}), msg => $self->l('ERROR_NAME_INVALID'),
err => $res->{msg}, err => 'ERROR_NAME_INVALID',
room => $room room => $room
); );
} }
@ -1327,11 +1321,10 @@ post '/*action' => [action => [qw/action admin\/action/]] => sub {
); );
} }
# Sanity check on the room name # Sanity check on the room name
my $res = $self->valid_room_name($room); if (!$self->valid_room_name($room)){
if (!$res->{ok}){
return $self->render( return $self->render(
json => { json => {
msg => $self->l($res->{msg}), msg => $self->l('ERROR_NAME_INVALID'),
status => 'error' status => 'error'
}, },
); );
@ -1339,7 +1332,7 @@ post '/*action' => [action => [qw/action admin\/action/]] => sub {
# 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
$res = $self->get_room_by_name($room); my $res = $self->get_room_by_name($room);
# Stop here if the room doesn't exist # Stop here if the room doesn't exist
if (!$res->{ok}){ if (!$res->{ok}){
return $self->render( return $self->render(

Loading…
Cancel
Save