1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
package POE::Filter::IRCD;
{
$POE::Filter::IRCD::VERSION = '2.44';
}
#ABSTRACT: A POE-based parser for the IRC protocol
use strict;
use warnings;
use Carp;
use base qw[POE::Filter];
sub _PUT_LITERAL () { 1 }
# Probably some other stuff should go here.
my $g = {
space => qr/\x20+/o,
trailing_space => qr/\x20*/o,
};
my $irc_regex = qr/^
(?:
\x40 # '@'-prefixed IRCv3.2 messsage tags.
(\S+) # [tags] Semi-colon delimited key=value list
$g->{space}
)?
(?:
\x3a # : comes before hand
(\S+) # [prefix]
$g->{'space'} # Followed by a space
)? # but is optional.
(
\d{3}|[a-zA-Z]+ # [command]
) # required.
(?:
$g->{'space'} # Strip leading space off [middle]s
( # [middle]s
(?:
[^\x00\x0a\x0d\x20\x3a]
[^\x00\x0a\x0d\x20]*
) # Match on 1 of these,
(?:
$g->{'space'}
[^\x00\x0a\x0d\x20\x3a]
[^\x00\x0a\x0d\x20]*
)* # then match as many of these as possible
)
)? # otherwise dont match at all.
(?:
$g->{'space'}\x3a # Strip off leading spacecolon for [trailing]
([^\x00\x0a\x0d]*) # [trailing]
)? # [trailing] is not necessary.
$g->{'trailing_space'}
$/x;
sub new {
my $type = shift;
croak "$type requires an even number of parameters" if @_ % 2;
my $buffer = { @_ };
$buffer->{uc $_} = delete $buffer->{$_} for keys %{ $buffer };
$buffer->{BUFFER} = [];
return bless $buffer, $type;
}
sub debug {
my $self = shift;
my $value = shift;
if ( defined $value ) {
$self->{DEBUG} = $value;
return $self->{DEBUG};
}
$self->{DEBUG} = $value;
}
sub get {
my ($self, $raw_lines) = @_;
my $events = [];
foreach my $raw_line (@$raw_lines) {
warn "->$raw_line \n" if $self->{DEBUG};
if ( my($tags, $prefix, $command, $middles, $trailing) = $raw_line =~ m/$irc_regex/ ) {
my $event = { raw_line => $raw_line };
if ($tags) {
for my $tag_pair (split /;/, $tags) {
my ($thistag, $thisval) = split /=/, $tag_pair;
$event->{tags}->{$thistag} = $thisval
}
}
$event->{'prefix'} = $prefix if $prefix;
$event->{'command'} = uc $command;
$event->{'params'} = [] if defined ( $middles ) || defined ( $trailing );
push @{$event->{'params'}}, (split /$g->{'space'}/, $middles) if defined $middles;
push @{$event->{'params'}}, $trailing if defined $trailing;
push @$events, $event;
}
else {
warn "Received line $raw_line that is not IRC protocol\n";
}
}
return $events;
}
sub get_one_start {
my ($self, $raw_lines) = @_;
push @{ $self->{BUFFER} }, $_ for @$raw_lines;
}
sub get_one {
my $self = shift;
my $events = [];
if ( my $raw_line = shift ( @{ $self->{BUFFER} } ) ) {
warn "->$raw_line \n" if $self->{DEBUG};
if ( my($tags, $prefix, $command, $middles, $trailing) = $raw_line =~ m/$irc_regex/ ) {
my $event = { raw_line => $raw_line };
if ($tags) {
for my $tag_pair (split /;/, $tags) {
my ($thistag, $thisval) = split /=/, $tag_pair;
$event->{tags}->{$thistag} = $thisval
}
}
$event->{'prefix'} = $prefix if $prefix;
$event->{'command'} = uc $command;
$event->{'params'} = [] if defined ( $middles ) || defined ( $trailing );
push @{$event->{'params'}}, (split /$g->{'space'}/, $middles) if defined $middles;
push @{$event->{'params'}}, $trailing if defined $trailing;
push @$events, $event;
}
else {
warn "Received line $raw_line that is not IRC protocol\n";
}
}
return $events;
}
sub get_pending {
return;
}
sub put {
my ($self, $events) = @_;
my $raw_lines = [];
foreach my $event (@$events) {
if (ref $event eq 'HASH') {
my $colonify = ( defined $event->{colonify} ? $event->{colonify} : $self->{COLONIFY} );
if ( _PUT_LITERAL || _checkargs($event) ) {
my $raw_line = '';
if ( ref $event->{tags} eq 'HASH' && keys %{ $event->{tags} } ) {
$raw_line .= '@';
my @tags = %{ $event->{tags} };
while (my ($thistag, $thisval) = splice @tags, 0, 2) {
$raw_line .= $thistag . ( defined $thisval ? '='.$thisval : '' );
$raw_line .= ';' if @tags;
}
$raw_line .= ' ';
}
$raw_line .= (':' . $event->{'prefix'} . ' ') if exists $event->{'prefix'};
$raw_line .= $event->{'command'};
if ( $event->{'params'} and ref $event->{'params'} eq 'ARRAY' ) {
my $params = [ @{ $event->{'params'} } ];
$raw_line .= ' ';
my $param = shift @$params;
while (@$params) {
$raw_line .= $param . ' ';
$param = shift @$params;
}
$raw_line .= ':' if $param =~ m/\x20/ or $colonify;
$raw_line .= $param;
}
push @$raw_lines, $raw_line;
warn "<-$raw_line \n" if $self->{DEBUG};
}
else {
next;
}
}
else {
warn __PACKAGE__ . " non hashref passed to put(): \"$event\"\n";
push @$raw_lines, $event if ref $event eq 'SCALAR';
}
}
return $raw_lines;
}
sub clone {
my $self = shift;
my $nself = { };
$nself->{$_} = $self->{$_} for keys %{ $self };
$nself->{BUFFER} = [ ];
return bless $nself, ref $self;
}
# This thing is far from correct, dont use it.
sub _checkargs {
my $event = shift || return;
warn("Invalid characters in prefix: " . $event->{'prefix'} . "\n")
if ($event->{'prefix'} =~ m/[\x00\x0a\x0d\x20]/);
warn("Undefined command passed.\n")
unless ($event->{'command'} =~ m/\S/o);
warn("Invalid command: " . $event->{'command'} . "\n")
unless ($event->{'command'} =~ m/^(?:[a-zA-Z]+|\d{3})$/o);
foreach my $middle (@{$event->{'middles'}}) {
warn("Invalid middle: $middle\n")
unless ($middle =~ m/^[^\x00\x0a\x0d\x20\x3a][^\x00\x0a\x0d\x20]*$/);
}
warn("Invalid trailing: " . $event->{'trailing'} . "\n")
unless ($event->{'trailing'} =~ m/^[\x00\x0a\x0d]*$/);
}
1;
__END__
=pod
=head1 NAME
POE::Filter::IRCD - A POE-based parser for the IRC protocol
=head1 VERSION
version 2.44
=head1 SYNOPSIS
use POE::Filter::IRCD;
my $filter = POE::Filter::IRCD->new( debug => 1, colonify => 0 );
my $arrayref = $filter->get( [ $hashref ] );
my $arrayref2 = $filter->put( $arrayref );
use POE qw(Filter::Stackable Filter::Line Filter::IRCD);
my ($filter) = POE::Filter::Stackable->new();
$filter->push( POE::Filter::Line->new( InputRegexp => '\015?\012', OutputLiteral => "\015\012" ),
POE::Filter::IRCD->new(), );
=head1 DESCRIPTION
POE::Filter::IRCD provides a convenient way of parsing and creating IRC protocol
lines. It provides the parsing engine for L<POE::Component::Server::IRC> and L<POE::Component::IRC>.
A standalone version exists as L<Parse::IRC>.
=head1 CONSTRUCTOR
=over
=item C<new>
Creates a new POE::Filter::IRCD object. Takes two optional arguments:
'debug', which will print all lines received to STDERR;
'colonify', set to 1 to force the filter to always colonify the last param passed in a put(),
default is 0. See below for more detail.
=back
=head1 METHODS
=over
=item C<get_one_start>
=item C<get_one>
=item C<get_pending>
=item C<get>
Takes an arrayref which is contains lines of IRC formatted input. Returns an arrayref of hashrefs
which represents the lines. The hashref contains the following fields:
prefix
command
params ( this is an arrayref )
raw_line
For example, if the filter receives the following line, the following hashref is produced:
LINE: ':moo.server.net 001 lamebot :Welcome to the IRC network lamebot'
HASHREF: {
prefix => ':moo.server.net',
command => '001',
params => [ 'lamebot', 'Welcome to the IRC network lamebot' ],
raw_line => ':moo.server.net 001 lamebot :Welcome to the IRC network lamebot',
}
=item C<put>
Takes an arrayref containing hashrefs of IRC data and returns an arrayref containing IRC formatted lines.
Optionally, one can specify 'colonify' to override the global colonification option.
eg.
$hashref = {
command => 'PRIVMSG',
prefix => 'FooBar!foobar@foobar.com',
params => [ '#foobar', 'boo!' ],
colonify => 1, # Override the global colonify option for this record only.
};
$filter->put( [ $hashref ] );
=item C<clone>
Makes a copy of the filter, and clears the copy's buffer.
=item C<debug>
With a true or false argument, enables or disables debug output respectively. Without an argument the behaviour is to toggle the debug status.
=back
=head1 SEE ALSO
L<POE>
L<POE::Filter>
L<POE::Filter::Stackable>
L<POE::Component::Server::IRC>
L<POE::Component::IRC>
L<Parse::IRC>
=head1 AUTHOR
Chris Williams <chris@bingosnet.co.uk>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2013 by Chris Williams and Jonathan Steinert.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|