Changeset 454
- Timestamp:
- 07/10/12 17:09:38 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
server/trunk/web/lib/Sophie/Controller/Chat/Cmd.pm
r451 r454 12 12 =head1 DESCRIPTION 13 13 14 Catalyst Controller.14 This module handle chat command. 15 15 16 16 =head1 METHODS … … 55 55 my $message = $c->stash->{xmlrpc}; 56 56 57 my @backup = @{ $message->{message} };57 my @backup = @{ $message->{message} || []}; 58 58 my $needpaste = 0; 59 59 60 if ( @{ $message->{message} } > ($reqspec->{max_line})) {60 if ($message->{message} && @{ $message->{message} } > ($reqspec->{max_line})) { 61 61 @{ $message->{message} } = 62 62 # -2 because line 0 and we remove one for paste url … … 220 220 221 221 sub set : XMLRPC { 222 my ( $self, $c, $reqspec, $var, $val) = @_;222 my ( $self, $c, $reqspec, @args ) = @_; 223 223 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 224 237 # if there is no variable to fix, Sophie ask and stop 225 238 if (!$var) { … … 232 245 } 233 246 } 247 234 248 235 249 # if the variable is not 'distribution', 'distrib', 'release' or 'arch', Sophie … … 260 274 if ($var ne "distribution") { 261 275 # 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, ]); 263 277 my $owndistrib = $res->{"distribution"} || '(none)'; 264 278 if ($var eq "release" && $owndistrib eq '(none)') { … … 290 304 } 291 305 292 $c->forward('/user/update_data', [ $ reqspec->{from}, { $var => $val } ]);306 $c->forward('/user/update_data', [ $for, { $var => $val } ]); 293 307 294 308 return $c->stash->{xmlrpc} = { … … 297 311 "$var set to: " . ($val || '(none)'), 298 312 ($c->forward('/distrib/exists', [ 299 $c->forward('/user/fetchdata', [ $ reqspec->{from}, ]) ])313 $c->forward('/user/fetchdata', [ $for, ]) ]) 300 314 ? () 301 315 : ("warning: your setting does not match any distribution") … … 312 326 313 327 sub 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}; 315 342 316 343 # if there is no variable to fix, Sophie ask and stop … … 342 369 } 343 370 344 $c->forward('/user/update_data', [ $ reqspec->{from}, { $var => undef } ]);371 $c->forward('/user/update_data', [ $for, { $var => undef } ]); 345 372 346 373 return $c->stash->{xmlrpc} = { … … 1339 1366 } 1340 1367 1368 =head2 auth 1369 1370 Authentify yourself to have access to bot admin command 1371 1372 =cut 1373 1374 sub 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 1402 Change your admin password if you have one. 1403 1404 =cut 1405 1406 sub 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 1437 Leave admin permission 1438 1439 =cut 1440 1441 sub 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 1458 sub _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 1341 1473 _to_list_functions('try me'); 1342 1474 … … 1372 1504 } 1373 1505 1506 =head1 USER VARIABLE 1507 1508 =head2 botconfig 1509 1510 Contains 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 1374 1520 =head1 AUTHOR 1375 1521
Note: See TracChangeset
for help on using the changeset viewer.