Brandan

Untitled

Jun 26th, 2012
195
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.         $data =~ s/add:=//gi;
  48.         $data =~ s/\{IP\}/$ip/gi;
  49.         $data =~ s/\{\r \n \s}//gi;
  50.         @servername = split(/\|\|/, $data);
  51.         open CHK_ARRAY, $filename;
  52.         my @chk_array = <CHK_ARRAY>;
  53.         close CHK_ARRAY;
  54.         if (grep(/$servername[0]/,@chk_array) eq 0) {
  55.             open (RP2DATA, '>>',$filename);
  56.              print RP2DATA "\n" . $data;
  57.              close (RP2DATA);    
  58.              $fh->send("Server Added Successfully! " . $ip . "\n");
  59.         } else {
  60.             $fh->send("Error: Server Already Exists! " . $ip. "\n");
  61.         }
  62.      }
  63.         elsif($data && $data =~ /remove:=/) {
  64.            $data =~ s/remove:=//gi;
  65.            $arg1 = $data;
  66.            chomp $arg1;
  67.            open $IN, '<', $filename;
  68.          my @contents = <$IN>;
  69.          close $IN;
  70.          @contents = grep !/$arg1/, @contents;
  71.          open $OUT, '>', $filename;
  72.          @contents =  map { chomp $_; $_; } @constents;
  73.          print $OUT @contents;
  74.          close $OUT;
  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.        }
  106.         elsif ($data && $data =~ /ls/) {
  107.        $fh->send("Current Servers Online " . $ip. ":\n");
  108.        open (MYFILE, $filename);
  109.        while (<MYFILE>) {
  110.          chomp;
  111.          $fh->send("$_ \n");
  112.        }
  113.         $fh->send("End Of List \n");
  114.       close (MYFILE);
  115.      } elsif ($data && $data =~ /format/) {
  116.       $fh->send("{SERVNAME}||{MAP}||{TIME}||{PLAYERS}||{NOTHING}||{PORT}||" . $ip . "\n");
  117.      } elsif ($data && $data =~ /help/) {
  118.       $fh->send("Roleplay 2 Server API:\n");
  119.       $fh->send("\*\"add:=\" -- This allows a server to be added to the network\n");
  120.       $fh->send("\*\"remove:=\" -- This allows a server to be removed to the network\n");
  121.       $fh->send("\*\"update:=\" -- This allows a server to be updated on the network\n");
  122.       $fh->send("\*\"ls\" -- This lists a servers on the network\n");
  123.       $fh->send("\*\"help\" -- How did you get to this point\?\n");
  124.      }
  125.     }
  126.  }
  127. }
RAW Paste Data