Changeset 454


Ignore:
Timestamp:
07/10/12 17:09:38 (12 years ago)
Author:
nanardon
Message:
  • add bot administration functions
File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/trunk/web/lib/Sophie/Controller/Chat/Cmd.pm

    r451 r454  
    1212=head1 DESCRIPTION 
    1313 
    14 Catalyst Controller. 
     14This module handle chat command. 
    1515 
    1616=head1 METHODS 
     
    5555    my $message =  $c->stash->{xmlrpc}; 
    5656 
    57     my @backup = @{ $message->{message} }; 
     57    my @backup = @{ $message->{message} || []}; 
    5858    my $needpaste = 0; 
    5959 
    60     if (@{ $message->{message} } > ($reqspec->{max_line})) { 
     60    if ($message->{message} && @{ $message->{message} } > ($reqspec->{max_line})) { 
    6161        @{ $message->{message} } =  
    6262            # -2 because line 0 and we remove one for paste url 
     
    220220 
    221221sub set : XMLRPC { 
    222     my ( $self, $c, $reqspec, $var, $val ) = @_; 
     222    my ( $self, $c, $reqspec, @args ) = @_; 
    223223     
     224    my $for; 
     225    my ($var, $val) = @{ $c->forward('_getopt', [ 
     226        { 
     227            'for=s' => \$for, 
     228        }, @args ]) }; 
     229    if ($for && !$c->forward('_check_auth', [ $reqspec ])) { 
     230        return $c->stash->{xmlrpc} = { 
     231            private_reply => 1, 
     232            error => 'You must be authenticated to change settings for someone else', 
     233        }; 
     234    } 
     235    $for ||= $reqspec->{from}; 
     236 
    224237    # if there is no variable to fix, Sophie ask and stop 
    225238    if (!$var) { 
     
    232245        } 
    233246    }  
     247 
    234248     
    235249    # if the variable is not 'distribution', 'distrib', 'release' or 'arch', Sophie 
     
    260274        if ($var ne "distribution") { 
    261275            # the message is built using the user's pref 
    262             my $res = $c->forward('/user/fetchdata', [ $reqspec->{from}, ]); 
     276            my $res = $c->forward('/user/fetchdata', [ $for, ]); 
    263277            my $owndistrib = $res->{"distribution"} || '(none)'; 
    264278            if ($var eq "release" && $owndistrib eq '(none)') { 
     
    290304    }  
    291305     
    292     $c->forward('/user/update_data', [ $reqspec->{from}, { $var => $val } ]); 
     306    $c->forward('/user/update_data', [ $for, { $var => $val } ]); 
    293307 
    294308    return $c->stash->{xmlrpc} = { 
     
    297311            "$var set to: " . ($val || '(none)'), 
    298312            ($c->forward('/distrib/exists', [ 
    299                     $c->forward('/user/fetchdata', [ $reqspec->{from}, ]) ]) 
     313                    $c->forward('/user/fetchdata', [ $for, ]) ]) 
    300314                ? () 
    301315                : ("warning: your setting does not match any distribution") 
     
    312326 
    313327sub unset : XMLRPC { 
    314     my ( $self, $c, $reqspec, $var ) = @_; 
     328    my ( $self, $c, $reqspec, @args ) = @_; 
     329 
     330    my $for; 
     331    my ($var) = @{ $c->forward('_getopt', [ 
     332        { 
     333            'for=s' => \$for, 
     334        }, @args ]) }; 
     335    if ($for && !$c->forward('_check_auth', [ $reqspec ])) { 
     336        return $c->stash->{xmlrpc} = { 
     337            private_reply => 1, 
     338            error => 'You must be authenticated to change settings for someone else', 
     339        }; 
     340    } 
     341    $for ||= $reqspec->{from}; 
    315342 
    316343    # if there is no variable to fix, Sophie ask and stop 
     
    342369    } 
    343370 
    344     $c->forward('/user/update_data', [ $reqspec->{from}, { $var => undef } ]); 
     371    $c->forward('/user/update_data', [ $for, { $var => undef } ]); 
    345372     
    346373    return $c->stash->{xmlrpc} = { 
     
    13391366} 
    13401367 
     1368=head2 auth 
     1369 
     1370Authentify yourself to have access to bot admin command 
     1371 
     1372=cut 
     1373 
     1374sub auth : XMLRPC { 
     1375    my ($self, $c, $reqspec, $password) = @_; 
     1376 
     1377    my $config = $c->forward('/user/fetchdata', [ 'botconfig' ]) || {}; 
     1378 
     1379    my $from = lc($reqspec->{from}); 
     1380 
     1381    require Data::Dumper; 
     1382    print Data::Dumper::Dumper( $config ); 
     1383 
     1384    if ($config 
     1385        && (ref $config->{admin} eq 'HASH') 
     1386        && (my $pass = $config->{admin}{$from})) { 
     1387        if ($pass eq crypt($password, $pass)) { 
     1388            $c->session->{botauth}{$from} = time; 
     1389            return $c->{stash}->{xmlrpc} = { 
     1390                message => [ 'You are now authenticated', ], 
     1391            }; 
     1392        } 
     1393    } 
     1394 
     1395    return $c->{stash}->{xmlrpc} = { 
     1396        error => 'Cannot authenticated you !', 
     1397    }; 
     1398} 
     1399 
     1400=head2 chpwd 
     1401 
     1402Change your admin password if you have one. 
     1403 
     1404=cut 
     1405 
     1406sub chpwd : XMLRPC { 
     1407    my ( $self, $c, $reqspec, $password ) = @_; 
     1408     
     1409    if (!$c->forward('_check_auth', [ $reqspec ])) { 
     1410        return $c->stash->{xmlrpc} = { 
     1411            private_reply => 1, 
     1412            error => 'You must be authenticated to change your password', 
     1413        }; 
     1414    } 
     1415 
     1416    my $from = lc($reqspec->{from}); 
     1417 
     1418    # the message is built using the user's pref 
     1419    my $res = $c->forward('/user/fetchdata', [ 'botconfig', ]); 
     1420 
     1421    my @char = ('a' .. 'z', 'A' .. 'Z', 0 .. 9); 
     1422    $res->{admin}{$from} = crypt( 
     1423        $password, 
     1424        '$1$' . join('', map{ $char[rand(scalar(@char))] } (0 .. 5)) 
     1425    ); 
     1426     
     1427    $c->forward('/user/setdata', [ 'botconfig', $res ]); 
     1428 
     1429    return $c->stash->{xmlrpc} = { 
     1430        private_reply => 1, 
     1431        message => [ 'Your new password has been stored' ], 
     1432    }; 
     1433} 
     1434 
     1435=head2 bye 
     1436 
     1437Leave admin permission 
     1438 
     1439=cut 
     1440 
     1441sub bye : XMLRPC { 
     1442    my ($self, $c, $reqspec) = @_; 
     1443 
     1444    my $from = lc($reqspec->{from}); 
     1445 
     1446    if ($c->forward('_check_auth', [ $reqspec ])) { 
     1447        delete($c->session->{botauth}{$from}); 
     1448        return $c->{stash}->{xmlrpc} = { 
     1449            message => [ 'See you soon' ], 
     1450        }; 
     1451    } else { 
     1452        return $c->{stash}->{xmlrpc} = { 
     1453            message => [ 'It seems you were not login' ], 
     1454        }; 
     1455    } 
     1456} 
     1457 
     1458sub _check_auth : Private { 
     1459    my ( $self, $c, $reqspec ) = @_; 
     1460 
     1461    my $from = lc($reqspec->{from}); 
     1462 
     1463    if ($c->session->{botauth}{$from} 
     1464        && $c->session->{botauth}{$from} > time - 3600) { # One hour session 
     1465        $c->session->{botauth}{$from} = time; 
     1466        return 1; 
     1467    } else { 
     1468        delete($c->session->{botauth}{$from}); 
     1469        return; 
     1470    } 
     1471} 
     1472 
    13411473_to_list_functions('try me'); 
    13421474 
     
    13721504} 
    13731505 
     1506=head1 USER VARIABLE 
     1507 
     1508=head2 botconfig 
     1509 
     1510Contains basic data for bot startup and managment 
     1511 
     1512  server: 
     1513    - irc.domain.org 
     1514  admin: 
     1515    - nick: crypt_password 
     1516      nick2: crypt_password 
     1517 
     1518 
     1519 
    13741520=head1 AUTHOR 
    13751521 
Note: See TracChangeset for help on using the changeset viewer.