<?

/************************************************************************/
/* 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.       */
/************************************************************************/

function smiley($message) {
  global $DbSmiles, $prefix, $smilesdir, $dbhost, $dbname, $dbuname, $dbpass;

  $patterns = array();
  $replace = array();
  $DbSmiles->query("SELECT code, file, emotion, length(code) as length FROM ".$prefix."_4nchatsmile ORDER BY length DESC");
  if($DbSmiles->num_rows() > 0) {
    while(list($code, $filename, $emotion, $length) = $DbSmiles->next_record()) {
      $patterns[] = '/([\n\\ \\.])'.preg_quote("$code").'/si';
      $replace[]  = ' <img src="'.$smilesdir.'/'.$filename.'" border="0" alt="'.$code.'" align="absmiddle">';
    }
  }
  $DbSmiles->clean_results();
  $patterns[] = "/(http\:\/\/[^\s]+)/i";
        $patterns[] = "/(ftp\:\/\/[^\s]+)/i";
        $patterns[] = "/(mailto\:([^\s]+))/i";
        $patterns[] = "/\*(.+)\*/i";
        $patterns[] = "/\_(.+)\_/i";
        $patterns[] = "/\r/i";
  $replace[]  = "<a href=\"\\1\" target=\"_blank\">\\1</a>";
        $replace[]  = "<a href=\"\\1\" target=\"_blank\">\\1</a>";
        $replace[]  = "<a href=\"\\1\" target=\"_blank\">\\2</a>";
        $replace[]  = "<b>\\1</b>";
        $replace[]  = "<i>\\1</i>";
        $replace[]  = "<br>\n";

  $message = ' '.$message;
  $message = preg_replace($patterns,$replace,$message);
  $message = substr($message,1);
  return($message);
}
?>