A simple perl module to parse E2's chatterbox XML ticker. It's used with the XML::Parser module. POD (with a short example) is at the bottom. It converts the href's to square brackets, just like the nodelet. If you use it, remember to escape the '&' in the chatterbox XML ticker output, otherwise it dies, saying that the xml is not well-formed.

There's also a Perl E2 private message XML ticker parser.

package E2Chatterbox::ChatterParser;

use strict;

########################################################################
########################################################################
###
### XML subs
###
########################################################################
########################################################################
sub Init {
  my $expat = shift();

  $expat->{speaker} = undef;
  $expat->{message} = undef;
  $expat->{chatter} = [];
  $expat->{time} = undef;
  $expat->{room} = undef;
}


sub Char {
  my ( $expat, $string ) = @_;

  if ( defined($expat->{speaker}) ) {
    $string =~ s/\n//g;

    $expat->{message} .= $string;
  }
}

sub Start {
  my ( $expat, $element, %attr ) = @_;
  $element = lc( $element );

  if ( $element eq 'message' ) {
    $expat->{speaker} = $attr{author};
    $expat->{time}    = $attr{time};
  }
  elsif ( $element eq 'a' ) {
    $expat->{message} .= '[';
  }
  elsif ( $element eq 'info' ) {
    $expat->{room} = $attr{room};
  }
}

sub End {
  my ( $expat, $element ) = @_;
  $element = lc( $element );

  if ( $element eq 'message' ) {
    if ( $expat->{time} > $expat->{last_time} ) {
      push( @{$expat->{chatter}},
        [ $expat->{time}, $expat->{speaker}, $expat->{message} ] );
      $expat->{last_time} = $expat->{time};
    }

    $expat->{speaker} = undef;
    $expat->{message} = undef;
    $expat->{time} = undef;
  }
  elsif ( $element eq 'a' ) {
    $expat->{message} .= ']';
  }
}

sub Final {
  my $expat = shift();

  return ( $expat->{chatter}, $expat->{last_time} );
}


1;

=pod

=head1 NAME

E2ChatterboxCParser - XML parser for chatterbox XML ticker

=head1 SYNOPSIS

 use XML::Parser;

 my $parser = new XML::Parser( 
   Style => "E2Chatterbox::ChatterParser" 
  );

 my ($chatter_log, $last_time) = $parser->parse( $xml, 'last_time' => $time );

 foreach my $line ( @$chatter_log ) {
   my ( $time, $speaker, $speech ) = @$line;
   print "[$time] $speaker: $speech\n";
 }

=head1 DESCRIPTION

This is a style for the XML::Parser perl module that deals
with E2's xml chatterbox ticker (/?node=chatterbox+XML+ticker).
It requires two arguments (the xml from the ticker and the
time to start reading), and returns two values (a reference
to an array of arrays and the time of the last message read.)

=cut
Unfortunately, now that I have all of the modules, it still doesn't work. Here are the errors I print out:

[Shan@ragu] (~)$ ./e2chatterbox.pl shanoyu *CENSORED*
Subroutine Init redefined at E2ChatterboxCParser.pm line 72.
Subroutine Char redefined at E2ChatterboxCParser.pm line 83.
Subroutine Start redefined at E2ChatterboxCParser.pm line 93.
Bareword "sage" not allowed while "strict subs" in use at E2ChatterboxCParser.pm line 137.
Unquoted string "sage" may clash with future reserved word at E2ChatterboxCParser.pm line 137.
"use" not allowed in expression at E2ChatterboxCParser.pm line 63, at end of line
syntax error at E2ChatterboxCParser.pm line 63, near "use strict"
syntax error at E2ChatterboxCParser.pm line 137, near "} ="
syntax error at E2ChatterboxCParser.pm line 140, near "elsif"
BEGIN failed--compilation aborted at E2Chatterbox2.pm line 14.
BEGIN failed--compilation aborted at ./e2chatterbox.pl line 5.


...elsif?

Log in or register to write something here or to contact authors.