<?

/************************************************************************/
/* XChat Version 0.9.8                                                   */
/* ==================================================================== */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

?>
<html>
<head>
<style>
BODY {
  font-family: Verdana, sans-serif;
  color: #000055;
  font-size: 14px;
  font-weight: bold;
}
</style>
<script>
<!--
<?
  require("./inc/config.php");
  require("./inc/mysql.lib.php");
  require("./inc/smiley.php");

  set_magic_quotes_runtime(0);

  $DbLink = new DB($dbhost, $dbname, $dbuname, $dbpass);

  // let's make sure this user is indeed logged in on chat, drop otherwise
  $DbLink->query("select name from ".$prefix."_4nchatuser where uid = $uid");
  if(!$DbLink->num_rows()) {
    $DbLink->clean_results();
    $DbLink->close();
    ?>
    top.location="quit.php?rid=<?=$rid ?>&uid=<?=$uid ?>&reason=1&lang=<?=$lang ?>";
    </script>
    </head>
    </html>
    <?
    exit;
  }
  $DbLink->clean_results();

  $DbSmiles = new DB($dbhost, $dbname, $dbuname, $dbpass);  // we'll need this for the smiley function
  $RCPT_ID = 0;

  $txt = htmlspecialchars(rawurldecode($txt));
  if (isset($txt) && trim($txt) != "") {
    if(preg_match("/^\/(\w+)\s+([^\s]+)\s+(.+)/i", $txt, $parts) || preg_match("/^\/(\w+)\s+([^\s]+)/i", $txt, $parts) || preg_match("/^\/(\w+)/i", $txt, $parts)) {
      if($parts[1] == 'msg') {
        if($parts[2] != '') {
          $txt = smiley(' '.$parts[3]);
                $DbLink->query("select uid from ".$prefix."_4nchatuser where name = '$parts[2]'");
          if($DbLink->num_rows() > 0) {
                  list($RCPT_ID) = $DbLink->next_record();
                  $cmd = 'MSG';
                } else {
                        $RCPT_ID = $uid;
                        $txt = '/'.$parts[1].' '.$parts[2].' - Recipient not found';
                        $cmd = 'USERERR';
                }
                $DbLink->clean_results();
              } else {
                $RCPT_ID = $uid;
                $txt = '/'.$parts[1].' - Recipient name missing';
                $cmd = 'USERERR';
              }
            } else {
              // test for sound commands
              $DbSounds = new DB($dbhost, $dbname, $dbuname, $dbpass);
              $DbSounds->query("select cmd, file, saytext from ".$prefix."_4nchatsound where cmd = '$parts[1]' LIMIT 1");
              if($DbSounds->num_rows() > 0) {
                list($cmd, $sndfile, $sndtext) = $DbSounds->next_record();
          if($sndtext != '')
            $txt = $sndtext.' to';
          else
            $txt = 'sending a '.$parts[1].' to';
          if($parts[2] != '' && $parts[2] != 'all') {
            $DbLink->query("select uid from ".$prefix."_4nchatuser where name = '$parts[2]'");
            if($DbLink->num_rows() > 0) {
                    list($RCPT_ID) = $DbLink->next_record();
                  } else {
                    $RCPT_ID = $uid;
                    $txt = $parts[1].' '.$parts[2].' - Recipient not found';
                          $cmd = 'USERERR';
                         }
                         $DbLink->clean_results();
                }
              } else {
          $txt = 'Unknown command: /'.$parts[1];
          $RCPT_ID = $uid;
          $cmd = 'USERERR';
              }
              $DbSounds->clean_results();
              $DbSounds->close();
            }
    }

    if($RCPT_ID != 0)
      $DbLink->query("select name from ".$prefix."_4nchatuser where uid = $RCPT_ID LIMIT 1");
    else
      $DbLink->query("select name from ".$prefix."_4nchatuser where uid = $uid LIMIT 1");
    if($DbLink->num_rows() > 0)
      list($name) = $DbLink->next_record();
    $DbLink->clean_results();

    if($tf==12) {
        $timestamp = '<span class="timestamp">'.date("h:i:sa", (time() + (doubleval($tz)) * 60 * 60)).'&nbsp;</span>';
        } else {
        $timestamp = '<span class="timestamp">'.date("H:i:s", (time() + (doubleval($tz)) * 60 * 60)).'&nbsp;</span>';
        }

    $message = rawurldecode(addslashes(smiley($txt)));  /* decode smiled msg for displaying */

    // check for system messages foist
    if(isset($cmd)) {
      switch($cmd) {
        case 'MSG':
          if($RCPT_ID == $uid)
            $toOut .= '<div class="message">'.$timestamp.'<font color="#BB6655"><b>(to you)</b></font> '.$message.'</div>';
          else
            $toOut .= '<div class="message">'.$timestamp.'<font color="#BB6655"><b>(to '.$name.')</b></font> '.$message.'</div>';
          break;
        case 'USERERR':
          $toOut .= '<div class="message">'.$timestamp.'<font color="red"><b>Error:</b></font> '.$message.'</div>';
          break;
        default:
          if($RCPT_ID != 0)
            $toOut .= '<div class="sysmessage">'.$timestamp.'<font color="#BB6655"><b>('.$message.' '.$name.')</b></font></div>';
          else
            $toOut .= '<div class="sysmessage">'.$timestamp.'<font color="#BB6655"><b>('.$message.' all users)</b></font></div>';
          break;
      }
    }

    // standard messages
    elseif($RCPT_ID == 0)
      $toOut .= '<div class="message">'.$timestamp.'<font color="#288F9B"><b>'.$name.'&gt;</b></font>&nbsp;'.$message.'</div>';
    elseif($RCPT_ID != $uid)
      $toOut .= '<div class="message">'.$timestamp.'<font color="red"><b>'.$name.'&gt;</b></font> '.$message.'</div>';
    elseif($RCPT_ID == $uid)
        $toOut .= '<div class="message">'.$timestamp.'<font color="#BB6655"><b>(to you)</b></font> '.$message.'</div>';

    if($cmd != 'USERERR') {
      $txt = rawurlencode(addslashes($txt));  /* slash specialchars and encode for posting */
      $DbLink->query("insert into ".$prefix."_4nchatmessage values (".time().", $rid, $uid, $RCPT_ID, '$cmd', '$txt')");
    }
  }

  $DbLink->close();
  $DbSmiles->close();
  $chatroom = stripslashes($chatroom);
  if($toOut != '' ) {
?>
  toOut = '<?=$toOut ?>';
  if (document.all) {
    top.wOut.document.body.innerHTML+= toOut;
  } else {
    top.wOut.document.write("<html><style>BODY {font-family: Verdana, sans-serif; color: #000055; font-size: 12px;} </style><body bgcolor='#FFFFFF'>");
          top.wOut.document.write(toOut);
  }
<?
  }
?>
//-->
</script>
</head>
<body bgcolor="#C0C0C0" topmargin="0" rightmargin="4">
<table border="0" width="100%">
<tr><td align="center" valign="middle"><font size="+0"><b><?=$chatroom ?></b></font></td>
</tr>
</table>
<form name="form2" method="post" action="send.php?rid=<?=$rid ?>&uid=<?=$uid ?>&tz=<?=$tz ?>&chatroom=<?=$chatroom ?>&lang=<?=$lang ?>">
<input type="hidden" name="txt">
</form>
</body>
</html>