<?php
/*
 * This script converts IRC colours to HTML.
 * Copyright (C) 2005  BarkerJr <http://barkerjr.net>
 *
 * 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, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

/* Configure two variables, below. */

function Control2Html($text)
{
  
/* Set these to your page's background and foreground colours.  This is used
     when the reverse code is encountered. */

  
$pageBGcolour 'black';
  
$pageFGcolour 'silver';

  
/* No more configuration needed below. */

  
$bold false;
  
$colour false;
  
$reverse false;
  
$underline false;
  
$newtext '';
  for (
$i 0$i strlen($text); $i++)
    switch (
$text[$i])
    {
      case 
chr(2):
        if (
$bold)
        {
          
$newtext .= '</b>';
          
$bold false;
        }
        else
        {
          
$newtext .= '<b>';
          
$bold true;
        }
        break;
      case 
chr(3):
        if (
$colour)
        {
          
$newtext .= '</span>';
          
$colour false;
        }
        
$forcolour '';
        
$backcolour '';
        if ((
$text[$i+1] >= '0') && ($text[$i+1] <= '9'))
        {
          
$colour true;
          if ((
$text[++$i+1] >= '0') && ($text[$i+1] <= '9'))
            
$forcolour getColor($text[$i] * 10 $text[++$i]);
          else
            
$forcolour getColor($text[$i]);
          if ((
$text[$i+1] == ',') && ($text[++$i+1] >= '0') && ($text[$i+1] <= '9'))
            if ((
$text[++$i+1] >= '0') && ($text[$i+1] <= '9'))
              
$background getColor($text[$i] * 10 $text[++$i]);
            else
              
$backcolour getColor($text[$i]);
        }
        if (
$forcolour)
        {
          
$newtext .= '<span style="color:' $forcolour;
          if (
$backcolour$newtext .= ';background-color:' $backcolour;
          
$newtext .= '">';
        }
        break;
      case 
chr(15):
        if (
$bold)
        {
          
$newtext .= '</b>';
          
$bold false;
        }
        if (
$colour)
        {
          
$newtext .= '</span>';
          
$colour false;
        }
        if (
$reverse)
        {
          
$newtext .= '</span>';
          
$colour false;
        }
        if (
$underline)
        {
          
$newtext .= '</u>';
          
$underline false;
        }
        break;
      case 
chr(22):
        if (
$reverse)
        {
          
$newtext .= '</span>';
          
$reverse false;
        }
        else
        {
          
$newtext .= '<span style="color:' $pageBGcolour ';background-color:' $pageFGcolour '">';
          
$reverse true;
        }
        break;
      case 
chr(31):
        if (
$underline)
        {
          
$newtext .= '</u>';
          
$underline false;
        }
        else
        {
          
$newtext .= '<u>';
          
$underline true;
        }
        break;
      default: 
$newtext .= $text[$i];
    }
  if (
$bold$newtext .= '</b>';
  if (
$colour$newtext .= '</span>';
  if (
$reverse$newtext .= '</span>';
  if (
$underline$newtext .= '</u>';
  return 
$newtext;
}

function 
getColor($numeric)
{
  switch (
$numeric)
  {
    case 
0: return 'white';
    case 
1: return 'black';
    case 
2: return 'navy';
    case 
3: return 'green';
    case 
4: return 'red';
    case 
5: return 'maroon';
    case 
6: return 'purple';
    case 
7: return 'olive';
    case 
8: return 'yellow';
    case 
9: return 'lime';
    case 
10: return 'teal';
    case 
11: return 'aqua';
    case 
12: return 'blue';
    case 
13: return 'fuchsia';
    case 
14: return 'gray';
    default: return 
'silver';
  }
}
?>