Filter/CTCP/Hybrid.pm

filter: Statementstmt 15.3% 14 / 91 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 4 / 12 Podpod 100% 4 / 4 totaltotal 14.2% 22 / 154 CC 27 SCAR 43.7
CC
27
Cov
10%
get
132.0
_ctcp_dequote
132.0
_low_quote
12.0
CRAP 79.2
« Filter/IRC/P10.pm ↑ Summary Filter/CTCP/P10.pm »
1 # $Id: Filter-CTCP.pm,v 1.3 1999/12/12 11:48:07 dennis Exp $
2 #
3 # POE::Filter::CTCP, by Dennis Taylor <dennis@funkplanet.com>
4 #
5 # This module may be used, modified, and distributed under the same
6 # terms as Perl itself. Please see the license that came with your Perl
7 # distribution for details.
8 #
9  
10 package POE::Filter::CTCP::Hybrid;
11  
12 1 push (@INC,".");
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use POE::Filter::IRC::Hybrid;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 # Create a new, empty POE::Filter::CTCP object.
20 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
21 0 my $class = shift;
22 0 my %args = @_;
23  
24 0 my $self = { 'irc_filter' => POE::Filter::IRC::Hybrid->new() };
25 0 bless $self, $class;
26 }
27  
28  
29 # Set/clear the 'debug' flag.
30 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
31 0 my $self = shift;
32 0 ▶ $self->{'debug'} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
33 0 return( $self->{'debug'} );
34 }
35  
36  
37 # For each line of raw CTCP input data that we're fed, spit back the
38 # appropriate CTCP and normal message events.
39 0 ▶sub get {
Subroutine get: not called CC=11 SCAR=48.8
Pod documented
40 0 my ($self, $lineref) = @_;
41 0 my ($who, $type, $where, $ctcp, $text, $name, $args);
42 0 my $events = [];
43  
44 LINE:
45 0 foreach my $line (@$lineref) {
46 0 ($who, $type, $where, $ctcp, $text) = _ctcp_dequote( $line );
47  
48 0 foreach (@$ctcp) {
49 ($name, $args) = $_ =~ /^(\w+)(?: (.*))?/
50 0 ▶ or do { warn "Received malformed CTCP message: \"$_\""; next LINE; };
Branch: 1 branch
BranchTrueFalseTotal
() = $_ =~ /^(\w+)(?: (.*))?/ or do { warn qq[Received malformed CTCP message: "$_"]; next LINE } 0 0 0
51 0 ▶ if (lc $name eq 'dcc') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $name eq 'dcc') { } 0 0 0
52 $args =~ /^(\w+) (\S+) (\d+) (\d+)(?: (\d+))?$/
53 0 ▶ or do { warn "Received malformed DCC request: \"$_\""; next LINE; };
Branch: 1 branch
BranchTrueFalseTotal
$args =~ /^(\w+) (\S+) (\d+) (\d+)(?: (\d+))?$/ or do { warn qq[Received malformed DCC request: "$_"]; next LINE } 0 0 0
54 0 my $basename = File::Basename::basename( $2 );
55 0 push @$events, { name => 'dcc_request',
56 args => [ $who, uc $1, $4, { open => undef,
57 nick => $who,
58 type => uc $1,
59 file => $basename,
60 size => $5,
61 done => 0,
62 addr => $3,
63 port => $4,
64 }, $basename, $5 ]
65 };
66  
67 } else {
68 0 ▶ push @$events, { name => $type . '_' . lc $name,
Branch: 1 branch
BranchTrueFalseTotal
defined $args ? : 0 0 0
69 args => [ $who, [split /,/, $where],
70 (defined $args ? $args : '') ]
71 };
72 }
73 }
74  
75 0 ▶ if ($text and @$text > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text and @$text > 0) 0 0 0
Condition0%0/3
$text and @$text > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$text and @$text > 0
$text @$text > 0
Truth table0%0/3
A and B
A= $textB= @$text > 0
ABresult
0X0
100
111
76 0 ▶ $line =~ /^(:\S+ +\w+ +\S+ +)/ or warn "What the heck? \"$line\"";
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^(:\S+ +\w+ +\S+ +)/ or warn qq[What the heck? "$line"] 0 0 0
77 0 $text = $1 . ':' . join '', @$text;
78 0 $text =~ s/\cP/^P/g;
79 0 ▶ warn "CTCP: $text\n" if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
80 0 push @$events, @{$self->{irc_filter}->get( [$text] )};
81 }
82 }
83  
84 0 return $events;
85 }
86  
87  
88 # For each line of text we're fed, spit back a CTCP-quoted version of
89 # that line.
90 0 ▶sub put {
Subroutine put: not called CC=2 SCAR=17.9
Pod documented
91 0 my ($self, $lineref) = @_;
92 0 my $quoted = [];
93  
94 0 foreach my $line (@$lineref) {
95 0 push @$quoted, _ctcp_quote( $line );
96 }
97  
98 0 return $quoted;
99 }
100  
101  
102 # Quotes a string in a low-level, protocol-safe, utterly brain-dead
103 # fashion. Returns the quoted string.
104 0 ▶sub _low_quote {
Subroutine _low_quote: not called CC=3 SCAR=24.8
105 0 my $line = shift;
106 0 my %enquote = ("\012" => 'n', "\015" => 'r', "\0" => '0', "\cP" => "\cP");
107  
108 0 ▶ unless (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 0 0
109 0 die "Not enough arguments to POE::Filter::CTCP->_low_quote";
110 }
111  
112 0 ▶ if ($line =~ tr/[\012\015\0\cP]//) { # quote \n, \r, ^P, and \0.
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ tr/\000\n\r\cP[]//) 0 0 0
113 0 $line =~ s/([\012\015\0\cP])/\cP$enquote{$1}/g;
114 }
115  
116 0 return $line;
117 }
118  
119  
120 # Does low-level dequoting on CTCP messages. I hate this protocol.
121 # Yes, I copied this whole section out of Net::IRC.
122 0 ▶sub _low_dequote {
Subroutine _low_dequote: not called CC=3 SCAR=24.8
123 0 my $line = shift;
124 0 my %dequote = (n => "\012", r => "\015", 0 => "\0", "\cP" => "\cP");
125  
126 0 ▶ unless (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 0 0
127 0 die "Not enough arguments to POE::Filter::CTCP->_low_dequote";
128 }
129  
130 # Thanks to Abigail (abigail@foad.org) for this clever bit.
131 0 ▶ if ($line =~ tr/\cP//) { # dequote \n, \r, ^P, and \0.
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ tr/\cP//) 0 0 0
132 0 $line =~ s/\cP([nr0\cP])/$dequote{$1}/g;
133 }
134  
135 0 return $line;
136 }
137  
138  
139 # Properly CTCP-quotes a message. Whoop.
140 0 ▶sub _ctcp_quote {
Subroutine _ctcp_quote: not called CC=1 SCAR=6.9
141 0 my $line = shift;
142  
143 0 $line = _low_quote( $line );
144 # $line =~ s/\\/\\\\/g;
145 0 $line =~ s/\001/\\a/g;
146  
147 0 return "\001" . $line . "\001";
148 }
149  
150  
151 # Splits a message into CTCP and text chunks. This is gross. Most of
152 # this is also stolen from Net::IRC, but I wrote that too, so it's
153 # used with permission. ;-)
154 0 ▶sub _ctcp_dequote {
Subroutine _ctcp_dequote: not called CC=11 SCAR=48.8
155 0 my $line = shift;
156 0 my (@chunks, $ctcp, $text, $who, $type, $where, $msg);
157  
158 # CHUNG! CHUNG! CHUNG!
159  
160 0 ▶ unless (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 0 0
161 0 die "Not enough arguments to POE::Filter::CTCP->_ctcp_dequote";
162 }
163  
164 # Strip out any low-level quoting in the text.
165 0 $line = _low_dequote( $line );
166  
167 # Filter misplaced \001s before processing... (Thanks, tchrist!)
168 0 ▶ substr($line, rindex($line, "\001"), 1) = '\\a'
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ tr/\cA// % 2 == 0 0 0 0
169 unless ($line =~ tr/\001//) % 2 == 0;
170  
171 0 ▶ return unless $line =~ tr/\001//;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ tr/\cA// 0 0 0
172  
173 0 ▶ ($who, $type, $where, $msg) = ($line =~ /^:(\S+) +(\w+) +(\S+) +:?(.*)$/)
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /^:(\S+) +(\w+) +(\S+) +:?(.*)$/ or return 0 0 0
174 or return;
175 0 @chunks = split /\001/, $msg;
176 0 ▶ shift @chunks unless length $chunks[0]; # FIXME: Is this safe?
Branch: 1 branch
BranchTrueFalseTotal
unless length $chunks[0] 0 0 0
177  
178 0 foreach (@chunks) {
179 # Dequote unnecessarily quoted chars, and convert escaped \'s and ^A's.
180 0 s/\\([^\\a])/$1/g;
181 0 s/\\\\/\\/g;
182 0 s/\\a/\001/g;
183 }
184  
185 # If the line begins with a control-A, the first chunk is a CTCP
186 # message. Otherwise, it starts with text and alternates with CTCP
187 # messages. Really stupid protocol.
188  
189 0 ▶ if ($msg =~ /^\001/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg =~ /^\001/) 0 0 0
190 0 push @$ctcp, shift @chunks;
191 }
192  
193 0 ▶ while (@chunks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chunks) 0 0 0
194 0 push @$text, shift @chunks;
195 0 ▶ push @$ctcp, shift @chunks if @chunks;
Branch: 1 branch
BranchTrueFalseTotal
if @chunks 0 0 0
196 }
197  
198 # Is this a CTCP request or reply?
199 0 ▶ if ($type eq 'PRIVMSG') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'PRIVMSG') { } 0 0 0
200 0 $type = 'ctcp';
201 } else {
202 0 $type = 'ctcpreply';
203 }
204  
205 0 return ($who, $type, $where, $ctcp, $text);
206 }
207  
208  
209 1 1;
210  
211 __END__
« Filter/IRC/P10.pm ↑ Summary Filter/CTCP/P10.pm »