<?php
/* (C) 2002 Peter Salanki, The Natasha Project */
include("settings.inc.php");

function dbconnect() {
  global $db, $mysql_server, $mysql_user, $mysql_password, $mysql_database;

  $db = mysql_connect($mysql_server, $mysql_user, $mysql_password);
  mysql_select_db($mysql_database, $db);
  echo mysql_error();
}

function request($chan, $nick, $bot) {
  global $db, $max_channels, $max_queue, $request_server, $request_port;

  if(!((isset($chan)) && (isset($nick)) && (isset($bot)) && (eregi("^.{2,16}$", $nick)) && (eregi("^#[^ ]+$", $chan)))) {
    echo "Incomplete or invalid data in input fields.";
    return;
  }

  $strQuery = "SELECT count(*) FROM `request` WHERE status = '0'";
  $qri = mysql_query($strQuery, $db);
  $qr = mysql_fetch_array($qri);

  if($qr[0] >= $max_queue) {
    echo "Queue is full, please try again later.";
    return;
  }

 $strQuery = "SELECT count(*) FROM `request` WHERE `channel` LIKE '$chan' AND `status` = '0' ";
 $qri = mysql_query($strQuery, $db);
 $qr = mysql_fetch_array($qri);
 if($qr[0] >= 1) {
    echo "You already have a request pending for that channel.";
    return;
  }

 $strQuery = "SELECT count(*) FROM `channels` WHERE `name` LIKE '$chan'";
 $qri = mysql_query($strQuery, $db);
 $qr = mysql_fetch_array($qri);
 if($qr[0] >= 1) {
    echo "You already have a bot on that channel.";
    return;
 }

  $strQuery = "SELECT `channels`,`status` FROM `arms` WHERE `nick` = '$bot'";
  $qri = mysql_query($strQuery, $db);
  $qr = mysql_fetch_array($qri);

  if($qr[0] >= $max_channels) {
    echo "Bot has become full, request another bot.";
    return;
  }

  if($qr[1] == 0) {
    echo "Bot has gone down, request another bot.";
    return;
  }

 $strQuery = "SELECT count(*) FROM `badchan` WHERE  '$chan' REGEXP `channel` AND `active` = '1'";
 $qri = mysql_query($strQuery, $db);
 $qr = mysql_fetch_array($qri);

  if($qr[0] >= 1) {
    echo "Your channel is banned from request, please head to #BotService to get more information.";
    return;
  }

  $socket = socket_create (AF_INET, SOCK_STREAM, 0);

  $server = gethostbyname ($request_server);
  $result = socket_connect ($socket, $server, $request_port);

  $send = "REQUEST $chan $nick $bot\n";
  socket_write ($socket, $send, strlen($send));


  $out = '';
  $out = socket_read ($socket, 1);

  socket_close ($socket);

  /* Some nice responses */

  /* Return codes: 0 = banned 1 = Not on chan/no op 2 = No L, placed in queue 3 = Auto accepted 4 = Not auth'd 5 = No such user 6 = Internal error 7 = Not requestok */

  if($out == 0) {
    $status = 0;
    $reason = "The bot can't get in to the channel (invite, key, limit, banned).";
  } else if($out == 1) {
    $status = 0;
    $reason = "You do NOT have channel operator status.";
  } else if($out == 2) {
    $status = 1;
    $reason = "You have met the requirements, your request is placed in queue. In a short time, an admin will handle your
request. Please stay on IRC with your current nickname.";
  } else if($out == 3) {
    $status = 2;
    $reason = "You have met requirements the for automatic acceptation. The bot will now join, and you will be given the channel \
owner
status.<br><b>Important:</b> The bot needs +ao in Q/L. Type i.e: <b>/msg L chanlev $chan $bot +ao</b>";
  } else if($out == 4) {
    $status = 0;
    $reason = "You are not AUTH'd with Q. If you think this is wrong, try requesting again. If it
still not works, come to #BotService and notify the admins.";
  } else if($out == 5) {
    $status = 0;
    $reason = "There is no such nick currently active on Quakenet. If you think this is wrong, try requesting again. If it
still not works, come to #BotService and notify the admins.";
  } else if($out == 6) {
    $status = 0;
    $reason = "An internal error has occured, please try again.";
  } else if($out == 7) {
    $status = 0;
    $reason = "You are not in request acceptance mode, please do <b>/msg Request-dev ACCEPT</b>";
  } else {
    $satus = 0;
    $reason = "Error in the request process, please report the bug to us and try tro request again.";
  }

  if($status == 0) echo "<b>Request failed</b>: ";
  else if ($status == 1) echo "<b>Request placed in queue</b>: ";
  else if ($status == 2) echo "<b>Request accepted</b>: ";
  echo $reason;
}

function login($handle, $password) {
  global $db;

  if(!((isset($handle)) && isset($password))) {
    echo "Incomplete fields.";
    return 0;
  }

  $strQuery = "SELECT admins.dispnick, users.auth, users.id FROM admins INNER JOIN users ON users.id = admins.uid WHERE users.handle = '$handle' AND admins.password = '$password'";
  $qri = mysql_query($strQuery, $db);
  $qr = mysql_fetch_array($qri);
    
  if($qr == NULL) {
    echo "Login failed.";
    return 0;
  }

  $_SESSION['auth'] = $qr[1];
  $_SESSION['dispnick'] = $qr[0];
  $_SESSION['id'] = $qr[2];
     
  return 1;
}

function auth_to_string($level) {
   if ($level == 0) { return "No access"; }
   if ($level == 1) { return "Normal user"; }
   if ($level == 7) { return "Helper"; }
   if ($level == 8) { return "Queue Worker"; }
   if ($level == 10) { return "Global Master"; }
   if ($level == 20) { return "Technician"; }
   if ($level == 21) { return "Master Technician"; }
   if ($level == 22) { return "IRC Operator"; }
}

function addadmin ($hand, $level, $password, $comments, $realname, $dispnick) {
  global $db;

  if(!((isset($hand)) && (isset($level)) && (isset($password)) && (isset($comments)) && (isset($realname)) && (isset($dispnick)))) {
    echo "Incomplete fields.";
    return;
  }

  $strQuery = "UPDATE `users` SET `auth` = '$level' WHERE `handle` = '$hand'";
  mysql_query($strQuery, $db);
  
  $strQuery = "SELECT `id` FROM `users` WHERE `handle` = '$hand'";
  $qri = mysql_query($strQuery, $db);
  $qr = mysql_fetch_array($qri);

  if($qr[0] == "") {
    echo "Error";
    return;
  }

  $strQuery = "INSERT INTO `admins` (uid, dispnick, realname, comments, password)   VALUES ('$qr[0]','$dispnick','$realname', '$comments', '$password')";
  mysql_query($strQuery, $db);

  echo "Done.";
  return;
}

function handle_accept($huid, $rid, $channel, $requester, $bot, $ruid) {
  global $db, $request_server, $request_port;

  $query = "UPDATE `request` SET `status` = '1', htime = UNIX_TIMESTAMP(), `huid` = '$huid' WHERE id = '$rid'";    
  mysql_query($query, $db);
  echo mysql_error();

  $socket = socket_create (AF_INET, SOCK_STREAM, 0);

  $server = gethostbyname ($request_server);
  $result = socket_connect ($socket, $server, $request_port);

  $send = "HANDLE $_POST[chan] $_POST[nick] $_POST[bot] $_POST[uid]\n";
  socket_write ($socket, $send, strlen($send));

  socket_close ($socket);

  echo "Request accepted.";
}

function handle_decline($huid, $rid, $channel, $requester, $bot, $ruid, $reason) {
  global $db, $request_server, $request_port;

  $query = "UPDATE `request` SET `status` = '2', htime = UNIX_TIMESTAMP(), `huid` = '$huid', `reason` = '$reason' WHERE id = '$rid'";    
  mysql_query($query, $db);

  $socket = socket_create (AF_INET, SOCK_STREAM, 0);

  $server = gethostbyname ($request_server);
  $result = socket_connect ($socket, $server, $request_port);

  $send = "DECLINE $_POST[chan] $_POST[nick] $_POST[bot] $_POST[uid]\n";
  socket_write ($socket, $send, strlen($send));

  socket_close ($socket);

  echo "Request declined.";
}
?>
