| 1 | package IRC::Message::Object; | ||||||||||||
| 2 | 1 | $IRC::Message::Object::VERSION = '0.092001'; | |||||||||||
| 3 | 5 | ▶ | use strictures 2; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 5 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 5 | ▶ | use List::Objects::WithUtils; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use List::Objects::Types -all; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use Types::Standard -all; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | ▶ | use POE::Filter::IRCv3; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 6 | ▶ | sub ircmsg { __PACKAGE__->new(@_) } | ||||||||||
|
Subroutine
ircmsg: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 13 | 1 | our @EXPORT = our @EXPORT_OK = 'ircmsg'; | |||||||||||
| 14 | |||||||||||||
| 15 | |||||||||||||
| 16 | 1 | ▶ | use Moo; use MooX::TypeTiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 1 | extends 'Exporter::Tiny'; | |||||||||||
| 18 | |||||||||||||
| 19 | has colonify => ( | ||||||||||||
| 20 | lazy => 1, | ||||||||||||
| 21 | is => 'ro', | ||||||||||||
| 22 | predicate => 1, | ||||||||||||
| 23 | 0 | ▶ | default => sub { 0 }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 24 | 1 | ); | |||||||||||
| 25 | |||||||||||||
| 26 | 1 | has command => ( | |||||||||||
| 27 | required => 1, | ||||||||||||
| 28 | is => 'ro', | ||||||||||||
| 29 | ); | ||||||||||||
| 30 | |||||||||||||
| 31 | |||||||||||||
| 32 | 1 | has filter => ( | |||||||||||
| 33 | is => 'rw', | ||||||||||||
| 34 | isa => HasMethods[qw/get put/], | ||||||||||||
| 35 | lazy => 1, | ||||||||||||
| 36 | predicate => 1, | ||||||||||||
| 37 | builder => '__build_filter', | ||||||||||||
| 38 | ); | ||||||||||||
| 39 | |||||||||||||
| 40 | sub __build_filter { | ||||||||||||
| 41 | 3 | ▶ | POE::Filter::IRCv3->new( colonify => (defined $_[1] ? $_[1] : 0) ) | ||||||||||
|
Subroutine
__build_filter: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||
| 42 | } | ||||||||||||
| 43 | |||||||||||||
| 44 | |||||||||||||
| 45 | has prefix => ( | ||||||||||||
| 46 | is => 'ro', | ||||||||||||
| 47 | lazy => 1, | ||||||||||||
| 48 | predicate => 1, | ||||||||||||
| 49 | 0 | ▶ | default => sub { '' }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 50 | 1 | ); | |||||||||||
| 51 | |||||||||||||
| 52 | has params => ( | ||||||||||||
| 53 | is => 'ro', | ||||||||||||
| 54 | lazy => 1, | ||||||||||||
| 55 | isa => ArrayObj, | ||||||||||||
| 56 | coerce => 1, | ||||||||||||
| 57 | predicate => 1, | ||||||||||||
| 58 | 0 | ▶ | default => sub { array }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 59 | 1 | ); | |||||||||||
| 60 | |||||||||||||
| 61 | has raw_line => ( | ||||||||||||
| 62 | is => 'ro', | ||||||||||||
| 63 | lazy => 1, | ||||||||||||
| 64 | predicate => 1, | ||||||||||||
| 65 | default => sub { | ||||||||||||
| 66 | 0 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||
| 67 | 0 | my %opts; | |||||||||||
| 68 | 0 | for (qw/prefix command params tags/) { | |||||||||||
| 69 | 0 | my $pred = "has_".$_; | |||||||||||
| 70 | 0 | ▶ | $opts{$_} = $self->$_ if $self->$pred; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | } | ||||||||||||
| 72 | 0 | $self->filter->put([ \%opts ])->[0] | |||||||||||
| 73 | }, | ||||||||||||
| 74 | 1 | ); | |||||||||||
| 75 | |||||||||||||
| 76 | has tags => ( | ||||||||||||
| 77 | lazy => 1, | ||||||||||||
| 78 | is => 'ro', | ||||||||||||
| 79 | isa => HashObj, | ||||||||||||
| 80 | coerce => 1, | ||||||||||||
| 81 | predicate => 'has_tags', | ||||||||||||
| 82 | 0 | ▶ | default => sub { hash }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 83 | 1 | ); | |||||||||||
| 84 | |||||||||||||
| 85 | sub BUILDARGS { | ||||||||||||
| 86 | 6 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
BUILDARGS: called CC=6 SCAR=18.8
Pod
undocumented
| |||||||||||||
| 87 | 6 | ▶ | my %params = @_ > 1 ? @_ : (raw_line => $_[0]) ; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | |||||||||||||
| 89 | 6 | ▶ | if (! defined $params{command}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 3 | ▶ | if (defined $params{raw_line}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | ## Try to create self from raw_line instead: | ||||||||||||
| 92 | my $filt = $params{filter} ? | ||||||||||||
| 93 | 3 | ▶ | $params{filter} : $class->__build_filter($params{colonify}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | 3 | my $refs = $filt->get( [$params{raw_line}] ); | |||||||||||
| 95 | 3 | ▶ | %params = %{ $refs->[0] } if @$refs; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | } else { | ||||||||||||
| 97 | 0 | confess "Bad params; a command or a raw_line must be specified in new()" | |||||||||||
| 98 | } | ||||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | 6 | \%params | |||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | sub get_tag { | ||||||||||||
| 105 | 0 | ▶ | my ($self, $tag) = @_; | ||||||||||
|
Subroutine
get_tag: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 106 | 0 | ▶ | return unless $self->has_tags; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | ## A tag might have an undef value ... | ||||||||||||
| 108 | ## ... see has_tag | ||||||||||||
| 109 | 0 | $self->tags->{$tag} | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | sub has_tag { | ||||||||||||
| 113 | 0 | ▶ | my ($self, $tag) = @_; | ||||||||||
|
Subroutine
has_tag: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 114 | 0 | ▶ | return unless $self->has_tags; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | 0 | exists $self->tags->{$tag} | |||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | sub tags_as_array { | ||||||||||||
| 119 | 0 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
tags_as_array: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 120 | 0 | ▶ | return array unless $self->has_tags; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | |||||||||||||
| 122 | 0 | my @tag_array; | |||||||||||
| 123 | 0 | for ($self->tags->kv->all) { | |||||||||||
| 124 | 0 | my ($thistag, $thisval) = @$_; | |||||||||||
| 125 | 0 | ▶ | push @tag_array, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 126 | defined $thisval ? join '=', $thistag, $thisval | ||||||||||||
| 127 | : $thistag | ||||||||||||
| 128 | }; | ||||||||||||
| 129 | |||||||||||||
| 130 | 0 | array @tag_array | |||||||||||
| 131 | } | ||||||||||||
| 132 | |||||||||||||
| 133 | sub tags_as_string { | ||||||||||||
| 134 | 0 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
tags_as_string: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 135 | 0 | ▶ | return unless $self->has_tags; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 136 | |||||||||||||
| 137 | 0 | my $str; | |||||||||||
| 138 | 0 | my $kv = $self->tags->kv; | |||||||||||
| 139 | |||||||||||||
| 140 | TAG: { | ||||||||||||
| 141 | 0 | ▶ | my $nxt = $kv->shift || last TAG; | ||||||||||
|
Condition0%0/2
$kv->shift || (last TAG)
MC/DC0%0/1
$kv->shift || (last TAG)
$kv->shift
Truth table0%0/2
A
A= $kv->shift
| |||||||||||||
| 142 | 0 | my ($thistag, $thisval) = @$nxt; | |||||||||||
| 143 | 0 | ▶ | $str .= $thistag . ( defined $thisval ? '='.$thisval : '' ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 144 | 0 | ▶ | if ($kv->has_any) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 145 | 0 | $str .= ';'; | |||||||||||
| 146 | redo TAG | ||||||||||||
| 147 | 0 | } | |||||||||||
| 148 | } | ||||||||||||
| 149 | |||||||||||||
| 150 | $str | ||||||||||||
| 151 | 0 | } | |||||||||||
| 152 | |||||||||||||
| 153 | sub truncate { | ||||||||||||
| 154 | 0 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
truncate: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 155 | |||||||||||||
| 156 | 0 | my $new; | |||||||||||
| 157 | 0 | my $current = $self->raw_line; | |||||||||||
| 158 | |||||||||||||
| 159 | ## TODO check for CTCP first | ||||||||||||
| 160 | ## if so, set flag, consider and readd trailing \001 ? | ||||||||||||
| 161 | |||||||||||||
| 162 | 0 | ▶ | if ($self->has_tags) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 163 | 0 | my $tagstr = '@' . $self->tags_as_string; | |||||||||||
| 164 | 0 | my $trunc = substr $current, (length($tagstr) + 1), 510; | |||||||||||
| 165 | 0 | $new = join ' ', $tagstr, $trunc; | |||||||||||
| 166 | } else { | ||||||||||||
| 167 | ## No tags, truncate to 510 | ||||||||||||
| 168 | 0 | ▶ | $new = length $current <= 510 ? $current : substr $current, 0, 510 ; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 169 | } | ||||||||||||
| 170 | |||||||||||||
| 171 | 0 | (ref $self)->new(raw_line => $new) | |||||||||||
| 172 | } | ||||||||||||
| 173 | |||||||||||||
| 174 | sub TO_JSON { | ||||||||||||
| 175 | 0 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
TO_JSON: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 176 | +{ | ||||||||||||
| 177 | 0 | ▶ | command => $self->command, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 178 | prefix => $self->prefix, | ||||||||||||
| 179 | params => $self->params, | ||||||||||||
| 180 | ( $self->has_tags ? (tags => $self->tags) : () ), | ||||||||||||
| 181 | } | ||||||||||||
| 182 | } | ||||||||||||
| 183 | |||||||||||||
| 184 | 1 | ▶ | |||||||||||
|
Branch: 1 branch
| |||||||||||||
| 185 | qq[<rnowak> fine, be rude like that\n], | ||||||||||||
| 186 | qq[<Perihelion> SORRY I WAS DISCUSSING THE ABILITY TO], | ||||||||||||
| 187 | qq[ PUT AN IRCD ON A ROOMBA\n] | ||||||||||||
| 188 | 1 | unless caller; 1; | |||||||||||
| 189 | |||||||||||||
| 190 | =pod | ||||||||||||
| 191 | |||||||||||||
| 192 | =for Pod::Coverage BUILDARGS TO_JSON has_\w+ | ||||||||||||
| 193 | |||||||||||||
| 194 | =head1 NAME | ||||||||||||
| 195 | |||||||||||||
| 196 | IRC::Message::Object - Incoming or outgoing IRC events | ||||||||||||
| 197 | |||||||||||||
| 198 | =head1 SYNOPSIS | ||||||||||||
| 199 | |||||||||||||
| 200 | ## Feed me some parameters: | ||||||||||||
| 201 | my $event = IRC::Message::Object->new( | ||||||||||||
| 202 | command => '001', | ||||||||||||
| 203 | prefix => ':some.server.org', | ||||||||||||
| 204 | params => [ 'user', 'Welcome to IRC' ], | ||||||||||||
| 205 | ); | ||||||||||||
| 206 | |||||||||||||
| 207 | ## ... or import and use the 'ircmsg()' shortcut: | ||||||||||||
| 208 | use IRC::Message::Object 'ircmsg'; | ||||||||||||
| 209 | my $event = ircmsg( | ||||||||||||
| 210 | command => '001', | ||||||||||||
| 211 | prefix => ':some.server.org', | ||||||||||||
| 212 | params => [ 'user', 'Welcome to IRC' ], | ||||||||||||
| 213 | ); | ||||||||||||
| 214 | |||||||||||||
| 215 | ## ... or take a raw IRC line (and parse it): | ||||||||||||
| 216 | $event = ircmsg( | ||||||||||||
| 217 | raw_line => ':some.server.org 001 user :Welcome to IRC' | ||||||||||||
| 218 | ); | ||||||||||||
| 219 | |||||||||||||
| 220 | ## ... or feed from POE::Filter::IRCD or POE::Filter::IRCv3: | ||||||||||||
| 221 | $event = ircmsg( %$ref_from_filter ); | ||||||||||||
| 222 | |||||||||||||
| 223 | ## ... retrieve useful bits later (see Methods): | ||||||||||||
| 224 | my $cmd = $event->command; | ||||||||||||
| 225 | my $line = $event->raw_line; | ||||||||||||
| 226 | if ($event->has_tag('monkeys')) { | ||||||||||||
| 227 | ... | ||||||||||||
| 228 | } | ||||||||||||
| 229 | |||||||||||||
| 230 | =head1 DESCRIPTION | ||||||||||||
| 231 | |||||||||||||
| 232 | These objects represent incoming or outgoing IRC messages (events); they can | ||||||||||||
| 233 | be created from either named parameters or a raw IRC line and provide | ||||||||||||
| 234 | accessors with automatic parsing magic. | ||||||||||||
| 235 | |||||||||||||
| 236 | =head2 Functions | ||||||||||||
| 237 | |||||||||||||
| 238 | =head3 ircmsg | ||||||||||||
| 239 | |||||||||||||
| 240 | Create a new B<IRC::Message::Object>; | ||||||||||||
| 241 | shortcut for C<< IRC::Message::Object->new >>. | ||||||||||||
| 242 | |||||||||||||
| 243 | This module uses L<Exporter::Tiny>, so you can rename the exported constructor | ||||||||||||
| 244 | if you like: | ||||||||||||
| 245 | |||||||||||||
| 246 | use IRC::Message::Object ircmsg => { -as => 'irc_ev' }; | ||||||||||||
| 247 | |||||||||||||
| 248 | =head2 Attributes and Methods | ||||||||||||
| 249 | |||||||||||||
| 250 | =head3 raw_line | ||||||||||||
| 251 | |||||||||||||
| 252 | The raw IRC line. The line is generated via the current | ||||||||||||
| 253 | L</filter> if the message object wasn't constructed with one. | ||||||||||||
| 254 | |||||||||||||
| 255 | predicate: C<has_raw_line> | ||||||||||||
| 256 | |||||||||||||
| 257 | =head3 command | ||||||||||||
| 258 | |||||||||||||
| 259 | The parsed command received. | ||||||||||||
| 260 | |||||||||||||
| 261 | Note that if the C<command> is set at construction time, | ||||||||||||
| 262 | no case-folding takes place. | ||||||||||||
| 263 | However, specifying a C<raw_line> at construction feeds | ||||||||||||
| 264 | L<POE::Filter::IRCv3>, which will uppercase commands. | ||||||||||||
| 265 | |||||||||||||
| 266 | =head3 params | ||||||||||||
| 267 | |||||||||||||
| 268 | A L<List::Objects::WithUtils::Array> containing the parameters attached to the | ||||||||||||
| 269 | message. | ||||||||||||
| 270 | |||||||||||||
| 271 | predicate: C<has_params> | ||||||||||||
| 272 | |||||||||||||
| 273 | =head3 prefix | ||||||||||||
| 274 | |||||||||||||
| 275 | The origin prefix. | ||||||||||||
| 276 | |||||||||||||
| 277 | predicate: C<has_prefix> | ||||||||||||
| 278 | |||||||||||||
| 279 | =head3 colonify | ||||||||||||
| 280 | |||||||||||||
| 281 | Passed through to L<POE::Filter::IRCv3/"colonify">; see the | ||||||||||||
| 282 | L<POE::Filter::IRCv3> documentation for details. | ||||||||||||
| 283 | |||||||||||||
| 284 | Defaults to true. | ||||||||||||
| 285 | |||||||||||||
| 286 | =head3 filter | ||||||||||||
| 287 | |||||||||||||
| 288 | Can be used to change the L<POE::Filter> used to transform a raw line into a | ||||||||||||
| 289 | HASH and vice-versa. | ||||||||||||
| 290 | |||||||||||||
| 291 | Defaults to a L<POE::Filter::IRCv3> instance with C<< colonify => 0 >> set. | ||||||||||||
| 292 | |||||||||||||
| 293 | =head3 get_tag | ||||||||||||
| 294 | |||||||||||||
| 295 | Retrieve a specific IRCv3.2 message tag's value. | ||||||||||||
| 296 | |||||||||||||
| 297 | This only works for tags with a defined value; see L</has_tag> to discover if | ||||||||||||
| 298 | a tag exists. | ||||||||||||
| 299 | |||||||||||||
| 300 | =head3 has_tag | ||||||||||||
| 301 | |||||||||||||
| 302 | Takes a tag identifier; returns true if the tag exists. | ||||||||||||
| 303 | |||||||||||||
| 304 | This is useful for finding out about tags that have no defined value. | ||||||||||||
| 305 | |||||||||||||
| 306 | =head3 has_tags | ||||||||||||
| 307 | |||||||||||||
| 308 | Returns true if there are tags present. | ||||||||||||
| 309 | |||||||||||||
| 310 | =head3 tags | ||||||||||||
| 311 | |||||||||||||
| 312 | IRCv3.2 message tags, as a L<List::Objects::WithUtils::Hash> of key-value pairs. | ||||||||||||
| 313 | |||||||||||||
| 314 | =head3 tags_as_array | ||||||||||||
| 315 | |||||||||||||
| 316 | IRCv3.2 message tags, as a L<List::Objects::WithUtils::Array> of tags in the | ||||||||||||
| 317 | form of 'key=value' | ||||||||||||
| 318 | |||||||||||||
| 319 | =head3 tags_as_string | ||||||||||||
| 320 | |||||||||||||
| 321 | IRCv3.2 message tags as a specification-compliant string. | ||||||||||||
| 322 | |||||||||||||
| 323 | =head3 truncate | ||||||||||||
| 324 | |||||||||||||
| 325 | Truncates the raw line to 510 characters, excluding message tags (per the | ||||||||||||
| 326 | specification), and returns a new L<IRC::Message::Object>. | ||||||||||||
| 327 | |||||||||||||
| 328 | =head1 AUTHOR | ||||||||||||
| 329 | |||||||||||||
| 330 | Jon Portnoy <avenj@cobaltirc.org> | ||||||||||||
| 331 | |||||||||||||
| 332 | =cut | ||||||||||||