###################################################################

package KoLBot::ChatMessage;
 
# Copyright (c) 2004 Alan W. Mahoney. All rights reserved. 
# This program is free software.  You may modify and/or 
# distribute it under the same terms as Perl itself.  
# This copyright notice must remain attached to the file.  
#

use warnings;
use strict;

BEGIN 
{
  use Exporter ();
  our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $ID);

  $VERSION     = 0.5;
  $ID          = '$Id: ChatMessage.pm 173 2006-10-30 17:29:34Z alan $';

  @ISA         = qw(Exporter);
  @EXPORT      = qw( );
  %EXPORT_TAGS = ( );

  @EXPORT_OK   = qw();
}

our @EXPORT_OK;

##########################################
## Overloads
##########################################

use overload q("") => \&as_string;

##########################################
## element access
##########################################

# legal type:
#
# brick
# toilet_paper

for my $field (qw( channel
		   type
		   message
		   user
		   user_id
		   )) {
  no strict "refs";
  *$field = sub {
    my $self = shift;
    $self->{$field} = shift if @_;
    return $self->{$field};
  }
}

##########################################
## Constructor:
##########################################

sub new {
  my $invocant = shift;
  my $class = ref($invocant) || $invocant;
  my $bot = { @_ };
  bless $bot, $class;
}

##########################################
## The rest
##########################################

sub as_string {
  my $message = shift;

  my $str = '';

  $message->{user} ||= '';
  if($message->type eq 'normal') {
    $str .= '[' . $message->channel . '] ' . $message->user
      . ': ' . $message->message;
  } elsif ($message->type eq 'emote') {
    $str .= '[' . $message->channel . '] ' . $message->user
      . ' ' . $message->message;
  } elsif ($message->type eq 'private') {
    $str .= 'private: '
      . $message->user . ': '
      . $message->message;
  } else {
    $str .= '[' . $message->channel . '] {'
      . $message->type . '} '
      . $message->user . ' ('
      . $message->user_id . ') : '
      . $message->message;
  }
  return $str;
}

END {} ;

1;
