Brandan

RP2Serv

Jun 25th, 2012
277
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. #use strict;
  5. use IO::Socket::INET;
  6. use IO::Select
  7. $| = 1;
  8. $filename = "/var/www/serverlist.php";
  9. @filelist = ($filename,"/var/www/database.txt");
  10. unlink @filelist;
  11. $socket = new IO::Socket::INET (
  12.     LocalHost => '0.0.0.0',
  13.     LocalPort => '666',
  14.     Proto => 'tcp',
  15.     Listen => 100,
  16.     Reuse => 1
  17.     ) or die "ERROR in Socket Creation : $!\n";
  18.  
  19. $select = IO::Select->new($socket) or die "IO::Select $!";
  20.  
  21. $SIG{PIPE} =  sub
  22. {
  23.     if(defined $current_client) {
  24.     $select->remove($current_client);
  25.     $current_client->close;
  26.     }
  27. };
  28.  
  29. my %nickhash = ();
  30.  
  31. while(1) {
  32.     @ready_clients = $select->can_read(0);
  33.     foreach my $fh (@ready_clients)  {
  34.     print $fh "";
  35.     if($fh == $socket)       {
  36.         my $new = $socket->accept();
  37.         $select->add($new);
  38.     } else {
  39.         my $data;
  40.         $fh->recv($data, 1024 * 1024);
  41.         my $unique = ($fh->peerhost() || ""). ":" . ($fh->peerport() || "");
  42.         my $ip = $fh->peerhost();
  43.  
  44.  
  45.  
  46.      if($data && $data =~ /add:=/) {        
  47.         @servername = split(/\|\|/, $data);
  48.         $data =~ s/add:=//gi;
  49.         $data =~ s/\{IP\}/$ip/gi;
  50.         open CHK_ARRAY, $filename;
  51.         my @chk_array = <CHK_ARRAY>;
  52.         close CHK_ARRAY;
  53.         if (grep(/$servername[0] . "\|\|"/,@chk_array) eq 0) {
  54.             open (RP2DATA, '>>',$filename);
  55.              print RP2DATA "\n" . $data;
  56.              close (RP2DATA);    
  57.              $fh->send("Server Added Successfully! " . $ip . "\n");
  58.         } else {
  59.             $fh->send("Error: Server Already Exists! " . $ip. "\n");
  60.         }
  61.      }
  62.         elsif($data && $data =~ /remove:=/) {
  63.            $data =~ s/remove:=//gi;
  64.            $arg1 = $data;
  65.            chomp $arg1;
  66.            open IN, '<', $filename;
  67.          my @contents = <IN>;
  68.          close IN;
  69.          @contents = grep !/$arg1/, @contents;
  70.          open OUT, '>', $filename;
  71.          @constents =  map { chomp $_; $_; } @constents;
  72.          print OUT @contents;
  73.          close OUT;
  74.            
  75.              $fh->send("Server \"$arg1\" Removed Successfully! " . $ip. "\n");
  76.        }
  77.         elsif($data && $data =~ /update:=/) {
  78.         # Removing
  79.          $data =~ s/update:=//gi;
  80.          @servername = split(/\|\|/, $data);
  81.          $arg1 = $servername[0];
  82.          chomp $arg1;
  83.            open IN, '<', $filename;
  84.          my @contents = <IN>;
  85.          close IN;
  86.          @contents = grep !/$arg1/, @contents;
  87.          open OUT, '>', $filename;
  88.          @constents =  map { chomp $_; $_; } @constents;
  89.          print OUT @contents;
  90.          close OUT;
  91.         # Adding
  92.         $data =~ s/\{IP\}/$ip/gi;
  93.         open CHK_ARRAY, $filename;
  94.         my @chk_array = <CHK_ARRAY>;
  95.         close CHK_ARRAY;
  96.         if (grep(/$servername[0] . "\|\|"/,@chk_array) eq 0) {
  97.             open (RP2DATA, '>>',$filename);
  98.              print RP2DATA "\n" . $data;
  99.              @servername = split(/\|\|/, $data);
  100.              close (RP2DATA);      
  101.              $fh->send("Server Updated Successfully! " . $ip . "\n");
  102.         } else {
  103.             $fh->send("Error: Server Already Exists! " . $ip. "\n");
  104.        }
  105.        } elsif ($data && $data =~ /ls/) {
  106.       $fh->send("Current Servers Online " . $ip. ":\n");
  107.        open (MYFILE, $filename);
  108.        while (<MYFILE>) {
  109.          chomp;
  110.          $fh->send("$_ \n");
  111.        }
  112.         $fh->send("End Of List \n");
  113.       close (MYFILE);
  114.      } elsif ($data && $data =~ /format/) {
  115.       $fh->send("{SERVNAME}||{MAP}||{TIME}||{PLAYERS}||{NOTHING}||{PORT}||" . $ip . "\n");
  116.      } elsif ($data && $data =~ /help/) {
  117.       $fh->send("Roleplay 2 Server API:\n");
  118.       $fh->send("\*\"add:=\" -- This allows a server to be added to the network\n");
  119.       $fh->send("\*\"remove:=\" -- This allows a server to be removed to the network\n");
  120.       $fh->send("\*\"update:=\" -- This allows a server to be updated on the network\n");
  121.       $fh->send("\*\"ls\" -- This lists a servers on the network\n");
  122.       $fh->send("\*\"help\" -- How did you get to this point\?\n");
  123.      }
  124.  }
  125. }
RAW Paste Data