Message/Object.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Toolkit/CTCP.pm ↑ Summary Toolkit/ISupport.pm »
1 package IRC::Message::Object;
2 $IRC::Message::Object::VERSION = '0.091001';
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 use List::Objects::Types -all;
8 use Types::Standard -all;
9  
10 use POE::Filter::IRCv3;
11  
12 sub ircmsg { __PACKAGE__->new(@_) }
13 our @EXPORT = our @EXPORT_OK = 'ircmsg';
14  
15  
16 use Moo;
17 extends 'Exporter::Tiny';
18  
19 has colonify => (
20 lazy => 1,
21 is => 'ro',
22 predicate => 1,
23 default => sub { 0 },
24 );
25  
26 has command => (
27 required => 1,
28 is => 'ro',
29 );
30  
31  
32 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 POE::Filter::IRCv3->new( colonify => (defined $_[1] ? $_[1] : 0) )
42 }
43  
44  
45 has prefix => (
46 is => 'ro',
47 lazy => 1,
48 predicate => 1,
49 default => sub { '' },
50 );
51  
52 has params => (
53 is => 'ro',
54 lazy => 1,
55 isa => ArrayObj,
56 coerce => 1,
57 predicate => 1,
58 default => sub { array },
59 );
60  
61 has raw_line => (
62 is => 'ro',
63 lazy => 1,
64 predicate => 1,
65 default => sub {
66 my ($self) = @_;
67 my %opts;
68 for (qw/prefix command params tags/) {
69 my $pred = "has_".$_;
70 $opts{$_} = $self->$_ if $self->$pred;
71 }
72 $self->filter->put([ \%opts ])->[0]
73 },
74 );
75  
76 has tags => (
77 lazy => 1,
78 is => 'ro',
79 isa => HashObj,
80 coerce => 1,
81 predicate => 'has_tags',
82 default => sub { hash },
83 );
84  
85 sub BUILDARGS {
86 my $class = shift;
87 my %params = @_ > 1 ? @_ : (raw_line => $_[0]) ;
88  
89 if (! defined $params{command}) {
90 if (defined $params{raw_line}) {
91 ## Try to create self from raw_line instead:
92 my $filt = $params{filter} ?
93 $params{filter} : $class->__build_filter($params{colonify});
94 my $refs = $filt->get( [$params{raw_line}] );
95 %params = %{ $refs->[0] } if @$refs;
96 } else {
97 confess "Bad params; a command or a raw_line must be specified in new()"
98 }
99 }
100  
101 \%params
102 }
103  
104 sub get_tag {
105 my ($self, $tag) = @_;
106 return unless $self->has_tags;
107 ## A tag might have an undef value ...
108 ## ... see has_tag
109 $self->tags->{$tag}
110 }
111  
112 sub has_tag {
113 my ($self, $tag) = @_;
114 return unless $self->has_tags;
115 exists $self->tags->{$tag}
116 }
117  
118 sub tags_as_array {
119 my ($self) = @_;
120 return array unless $self->has_tags;
121  
122 my @tag_array;
123 for ($self->tags->kv->all) {
124 my ($thistag, $thisval) = @$_;
125 push @tag_array,
126 defined $thisval ? join '=', $thistag, $thisval
127 : $thistag
128 };
129  
130 array @tag_array
131 }
132  
133 sub tags_as_string {
134 my ($self) = @_;
135 return unless $self->has_tags;
136  
137 my $str;
138 my $kv = $self->tags->kv;
139  
140 TAG: {
141 my $nxt = $kv->shift || last TAG;
142 my ($thistag, $thisval) = @$nxt;
143 $str .= $thistag . ( defined $thisval ? '='.$thisval : '' );
144 if ($kv->has_any) {
145 $str .= ';';
146 redo TAG
147 }
148 }
149  
150 $str
151 }
152  
153 sub truncate {
154 my ($self) = @_;
155  
156 my $new;
157 my $current = $self->raw_line;
158  
159 ## TODO check for CTCP first
160 ## if so, set flag, consider and readd trailing \001 ?
161  
162 if ($self->has_tags) {
163 my $tagstr = '@' . $self->tags_as_string;
164 my $trunc = substr $current, (length($tagstr) + 1), 510;
165 $new = join ' ', $tagstr, $trunc;
166 } else {
167 ## No tags, truncate to 510
168 $new = length $current <= 510 ? $current : substr $current, 0, 510 ;
169 }
170  
171 (ref $self)->new(raw_line => $new)
172 }
173  
174 sub TO_JSON {
175 my ($self) = @_;
176 +{
177 command => $self->command,
178 prefix => $self->prefix,
179 params => $self->params,
180 ( $self->has_tags ? (tags => $self->tags) : () ),
181 }
182 }
183  
184 print
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 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
« Toolkit/CTCP.pm ↑ Summary Toolkit/ISupport.pm »