<?php

/* TEST LOGIN, ENTER, CHAT, AUTO WRITE, AND RESPONSE */

session_start();

require "func.php";

$guser = trim($_GET['user']);
$gpass = trim($_GET['pass']);
$gadmin = trim($_GET['admin']);
$groom = trim($_GET['room']);

// SETTINGS 
$user = "masihrimbun";
$pass = "sukirmankartodiharjo27091983";
$room = "emerland";
if ($groom)
 $room = $groom;
$rooms = "|".$room; //separated by |
$interval = 5; // menit
$status = "away";
$admin = "errotix";
$writeText = "/me : jam";
$txt = "horreeey aku ol :-D";

$body = "";

if ($guser && $gpass) {
 $user = $guser;
 $pass = $gpass;
 if ($gadmin)
  $admin = $gadmin;
}

if ($user) {
 if ($status == "on" || $status == "hidup")
  $stts = 1;
 else if ($status == "off" || $status == "mati")
  $stts = 2;
 else if ($status == "busy" || $status == "sibuk")
  $stts = 3;
 else 
  $stts = 4;
  // try to logining
 $sock = konek($user, $pass, $stts);
}
 
$find = time();
$auto = time();

$i = 0;
$maxloop = 2000;
while ($i < $maxloop) {
 if ($user != "") {
  $hasil = $mig->packet2array(read($sock));
  
  if (preg_match('#failed#', implode('', $hasil)) && $i < 6) {
   socket_close($sock);
   $i = $maxloop;
   die(implode("<br>", $hasil));
  }
  
  if ($i == 8) {
   if ($room != "") {
    $send = $mig->EnterRoom(3, $room);
    socket_write( $sock, $send, strlen($send) );
    flush();
    @ob_flush();
   }
  }

  if ($i == 10) {
   $send = $mig->chat(4, $txt, $admin, $user, 1);
   socket_write( $sock, $send, strlen($send) );
   flush();
   @ob_flush();
  }

  $type = hexdec(bin2hex($hasil[3]));
  
  if (time() > $auto + 60 * $interval) {
   $text = str_replace("jam", gmdate("g:i A", time()+7*3600), $writeText);
   $text = str_replace("time", "[".gmdate("H:i:s", time()+7*3600)."]", $text);
   $text = str_replace(array("tanggal", "tgl"), gmdate("d-m-Y", time()+7*3600), $text);
   $text = str_replace("date", gmdate("l, d F Y", time()+7*3600), $text);
   if ($rooms){
    $rum = explode("|", $rooms);
    for ($i = 1; $i < count($rum); $i++){
     $send = $mig->chat(6, $text, $rum[$i], $user);
     socket_write($sock, $send, strlen($send));
     flush();
     @ob_flush();
    }
   }
   $auto = time();
  }

  if ($hasil[0] == hexdec('0201F4')) {
   $cek = strtolower($hasil[8]);
   $target = $type == 1 ? $hasil[2] : $hasil[4];
   $isMe = $hasil[2] == $user;
   $isAdmin = $hasil[2] == $admin;
   $isRoom = $hasil[2] == $hasil[4];

   // .go
   if (strpos($cek, " .go") !== false || preg_match('#^\.go#', $cek)) {
    if ($isAdmin) {
     $cekRoom = strtolower($hasil[4]);
     $rum = trim(substr($hasil[8], strpos($cek, ".go") + 3));
     if ($rum AND strlen($rum) < 16) {
      if ($cekRoom == strtolower($rum)) {
       $text = "am here (hee)";
       $send = $mig->chat(3 + $type, $text, $target, $user, $type);
       socket_write($sock, $send, strlen($send));
      }
      else {
       $room = $rum;
       $rooms = str_replace("|".strtolower($room), "", strtolower($rooms));
       $rooms .= "|".$room;
       $text = "am in going to ".unDc($room);
       $send = $mig->chat(3 + $type, $text, $target, $user, $type);
       socket_write($sock, $send, strlen($send));
       $eRoom = $mig->EnterRoom(3, $room);
       socket_write($sock, $eRoom, strlen($eRoom));
      }
     }
        flush();
     @ob_flush();
    }
   }

   // .left
   else if (strpos($cek, " .left") !== false || preg_match('#^\.left#', $cek)) {
    if ($isAdmin) {
     $leftRoom = trim(substr($hasil[8], strpos($cek, ".left") + 6));
     if ($leftRoom == "")
      $leftRoom = $hasil[4];
     $text = "by by ".unDc($leftRoom)." :D";
     $rooms = str_replace("|".strtolower($leftRoom), "", strtolower($rooms));
     $room = end(explode("|", $rooms));
     $send = $mig->chat(6, $text, $leftRoom, $user, 3);
     socket_write($sock, $send, strlen($send));
     $left = $mig->LeaveRoom(7, $leftRoom);
     socket_write($sock, $left, strlen($left));
       flush();
     @ob_flush();
    }
   }

   // .tgl .date .jam .time
   else if (strpos($cek, " .tgl") !== false || preg_match('#^\.tgl#', $cek) ||
    strpos($cek, " .date") !== false || preg_match('#^\.date#', $cek) ||
    strpos($cek, " .jam") !== false || preg_match('#^\.jam#', $cek) ||
    strpos($cek, " .time") !== false || preg_match('#^\.time#', $cek)) {
    if ($isAdmin) {
     // .tgl
     if (strpos($cek, " .tgl") !== false || preg_match('#^\.tgl#', $cek)) {
      $text = "Sekarang tanggal " . gmdate("d M Y", time()+7*3600) . " gan";
     }
     // .date
     else if (strpos($cek, " .date") !== false || preg_match('#^\.date#', $cek)) {
      $text = gmdate("l, d F Y", time()+7*3600);
     }
     // .jam
     else if (strpos($cek, " .jam") !== false || preg_match('#^\.jam#', $cek)) {
      $text = "Sekarang jam " . gmdate("g:i A", time()+7*3600) . " gan";
     }
     // .time
     else {
      $text = "[".gmdate("H:i:s", time()+7*3600)."]";
     }
     $send = $mig->chat(3 + $type, $text, $target, $user, $type);
     socket_write($sock, $send, strlen($send));
       flush();
     @ob_flush();
    }
   }

   // .ival
   else if (strpos($cek, " .i") !== false || preg_match('#^\.ival#', $cek)) {
    if ($isAdmin) {
     $interval = (int)trim(substr($hasil[8], strpos($cek, ".ival") + 5));
     if ($interval == 0)
      $interval = 1;
     $text = "Intrval Auto: " . $interval . " minutes";
     $send = $mig->chat(3 + $type, $text, $target, $user, $type);
     socket_write($sock, $send, strlen($send));
       flush();
     @ob_flush();
    }
   }

   // VOTE
   else if (strpos($cek, "vote to kick") !== false && strpos($hasil[8], $user) !== false) {
    if ($isRoom) {
     $left = $mig->LeaveRoom(7, $hasil[4]);
     socket_write($sock, $left, strlen($left));
     flush();
     @ob_flush();
    }
   }

   else {
   }
  }

  if (time() > $find + 300) {
   socket_write( $sock, $mig->refresh(), strlen($mig->refresh()) );
   flush();
   @ob_flush();
   $find = time();
  }

 }
 $i++;
}


?>