| 1 | package POE::Component::IRC::Plugin::Blowfish; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use POE; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use POE::Component::IRC::Plugin qw( :ALL ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Crypt::Blowfish_PP; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Carp qw/croak/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | $VERSION = '0.01'; | |||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | use constant B64 => | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | './0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |||||||||
| 15 | |||||||||||
| 16 | 2 | ▶ | use constant states => qw/set_blowfish_key del_blowfish_key/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 19 | 1 | my $package = shift; | |||||||||
| 20 | 1 | ▶ | croak "Plugin requires an even number of parameters" if @_ % 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | |||||||||||
| 22 | 1 | my $self = { targets => {@_} }; | |||||||||
| 23 | 1 | bless $self, $package; | |||||||||
| 24 | |||||||||||
| 25 | 1 | foreach my $chan ( keys %{ $self->{targets} } ) { | |||||||||
| 26 | 0 | $self->_set_key( $chan, $self->{targets}->{$chan} ); | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 1 | return $self; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 1 | ▶ | sub PCI_register { | ||||||||
|
Subroutine
PCI_register: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 33 | 1 | my ( $self, $irc ) = splice @_, 0, 2; | |||||||||
| 34 | |||||||||||
| 35 | 1 | $irc->plugin_register( $self, 'SERVER', qw(public 001) ); | |||||||||
| 36 | 1 | $irc->plugin_register( $self, 'USER', qw(privmsg) ); | |||||||||
| 37 | |||||||||||
| 38 | $self->{session_id} = | ||||||||||
| 39 | POE::Session->create( object_states => | ||||||||||
| 40 | 1 | [ $self => [ qw/_shutdown _start/, map { "_$_" } states ], ], )->ID; | |||||||||
| 41 | |||||||||||
| 42 | 1 | $poe_kernel->state( $_ => $self ) for states; | |||||||||
| 43 | |||||||||||
| 44 | 1 | return 1; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 1 | ▶ | sub PCI_unregister { | ||||||||
|
Subroutine
PCI_unregister: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 48 | 1 | my ( $self, $irc ) = splice @_, 0, 2; | |||||||||
| 49 | |||||||||||
| 50 | 1 | $poe_kernel->state($_) for states; | |||||||||
| 51 | 1 | $poe_kernel->call( $self->{session_id} => '_shutdown' ); | |||||||||
| 52 | 1 | return 1; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 1 | ▶ | sub _start { | ||||||||
|
Subroutine
_start: called CC=1 SCAR=0.0
| |||||||||||
| 56 | 1 | my ( $kernel, $self ) = @_[ KERNEL, OBJECT ]; | |||||||||
| 57 | 1 | $self->{session_id} = $_[SESSION]->ID(); | |||||||||
| 58 | 1 | $kernel->refcount_increment( $self->{session_id}, __PACKAGE__ ); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 1 | ▶ | sub _shutdown { | ||||||||
|
Subroutine
_shutdown: called CC=1 SCAR=0.0
| |||||||||||
| 62 | 1 | my ( $kernel, $self ) = @_[ KERNEL, OBJECT ]; | |||||||||
| 63 | 1 | $kernel->alarm_remove_all(); | |||||||||
| 64 | 1 | $kernel->refcount_decrement( $self->{session_id}, __PACKAGE__ ); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | sub set_blowfish_key { | ||||||||
|
Subroutine
set_blowfish_key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 68 | 0 | my ( $kernel, $self, $sender ) = @_[ KERNEL, OBJECT, SENDER ]; | |||||||||
| 69 | 0 | $kernel->post( $self->{session_id}, '_set_blowfish_key', $sender, | |||||||||
| 70 | @_[ ARG0 .. $#_ ] ); | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | sub del_blowfish_key { | ||||||||
|
Subroutine
del_blowfish_key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 74 | 0 | my ( $kernel, $self, $sender ) = @_[ KERNEL, OBJECT, SENDER ]; | |||||||||
| 75 | 0 | $kernel->post( $self->{session_id}, '_del_blowfish_key', $sender, | |||||||||
| 76 | @_[ ARG0 .. $#_ ] ); | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 0 | ▶ | sub _set_blowfish_key { | ||||||||
|
Subroutine
_set_blowfish_key: not called CC=2 SCAR=17.9
| |||||||||||
| 80 | 0 | my ( $kernel, $self, $sender, $chan, $key ) = | |||||||||
| 81 | @_[ KERNEL, OBJECT, ARG0 .. ARG2 ]; | ||||||||||
| 82 | 0 | my $old_key; | |||||||||
| 83 | $old_key = $self->{targets}->{$chan}->[1] | ||||||||||
| 84 | 0 | ▶ | if defined $self->{targets}->{$chan}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | $self->_set_key( $chan, $key ); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | sub _del_blowfish_key { | ||||||||
|
Subroutine
_del_blowfish_key: not called CC=2 SCAR=17.9
| |||||||||||
| 89 | 0 | my ( $kernel, $self, $sender, $chan ) = @_[ KERNEL, OBJECT, ARG0, ARG1 ]; | |||||||||
| 90 | 0 | ▶ | delete $self->{targets}->{$chan} if defined $self->{targets}->{$chan}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | sub S_001 { | ||||||||
|
Subroutine
S_001: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 94 | 0 | my ( $self, $irc ) = splice @_, 0, 2; | |||||||||
| 95 | |||||||||||
| 96 | # get this plugin to the pole position | ||||||||||
| 97 | 0 | ▶ | $irc->pipeline->bump_up($self) while $irc->pipeline->get_index($self) > 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | |||||||||||
| 99 | 0 | return PCI_EAT_NONE; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 0 | ▶ | sub U_privmsg { | ||||||||
|
Subroutine
U_privmsg: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 103 | 0 | my ( $self, $irc ) = splice @_, 0, 2; | |||||||||
| 104 | |||||||||||
| 105 | 0 | my $line = ${ $_[0] }; | |||||||||
| 106 | 0 | my ( $target, $msg ) = $line =~ /PRIVMSG (.*?) :(.*?)$/; | |||||||||
| 107 | |||||||||||
| 108 | 0 | ▶ | return PCI_EAT_NONE unless defined $self->{targets}->{$target}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | |||||||||||
| 110 | $msg = sprintf '+OK %s', | ||||||||||
| 111 | 0 | $self->_encrypt( $msg, $self->{targets}->{$target}->[0] ); | |||||||||
| 112 | |||||||||||
| 113 | 0 | ${ $_[0] } = sprintf 'PRIVMSG %s :%s', $target, $msg; | |||||||||
| 114 | |||||||||||
| 115 | 0 | return PCI_EAT_NONE; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 0 | ▶ | sub S_public { | ||||||||
|
Subroutine
S_public: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 119 | 0 | my ( $self, $irc ) = splice @_, 0, 2; | |||||||||
| 120 | |||||||||||
| 121 | 0 | my ($nick) = ( split /!/, ${ $_[0] } )[0]; | |||||||||
| 122 | 0 | my ($target) = ${ $_[1] }->[0]; | |||||||||
| 123 | 0 | my ($msg) = ${ $_[2] }; | |||||||||
| 124 | |||||||||||
| 125 | 0 | ▶ | if ( defined $self->{targets}->{$target} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | ▶ | if ($msg =~ s/^\+OK //) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 0 | $msg = $self->_decrypt( $msg, $self->{targets}->{$target}->[0] ); | |||||||||
| 128 | 0 | $msg =~ s/\0//g; | |||||||||
| 129 | 0 | ${ $_[2] } = $msg; | |||||||||
| 130 | } | ||||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 0 | return PCI_EAT_NONE; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 0 | ▶ | sub _encrypt { | ||||||||
|
Subroutine
_encrypt: not called CC=2 SCAR=17.9
| |||||||||||
| 137 | 0 | my ( $self, $text, $key ) = @_; | |||||||||
| 138 | |||||||||||
| 139 | 0 | $text =~ s/(.{8})/$1\n/g; | |||||||||
| 140 | 0 | my $result = ''; | |||||||||
| 141 | 0 | my $cipher = new Crypt::Blowfish_PP $key; | |||||||||
| 142 | 0 | foreach ( split /\n/, $text ) { | |||||||||
| 143 | 0 | $result .= $self->_inflate( $cipher->encrypt($_) ); | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | 0 | return $result; | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 0 | ▶ | sub _decrypt { | ||||||||
|
Subroutine
_decrypt: not called CC=2 SCAR=17.9
| |||||||||||
| 150 | 0 | my ( $self, $text, $key ) = @_; | |||||||||
| 151 | |||||||||||
| 152 | 0 | $text =~ s/(.{12})/$1\n/g; | |||||||||
| 153 | 0 | my $result = ''; | |||||||||
| 154 | 0 | my $cipher = new Crypt::Blowfish_PP $key; | |||||||||
| 155 | 0 | foreach ( split /\n/, $text ) { | |||||||||
| 156 | 0 | $result .= $cipher->decrypt( $self->_deflate($_) ); | |||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | 0 | return $result; | |||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | 0 | ▶ | sub _set_key { | ||||||||
|
Subroutine
_set_key: not called CC=4 SCAR=30.0
| |||||||||||
| 163 | 0 | my ( $self, $chan, $key ) = @_; | |||||||||
| 164 | |||||||||||
| 165 | 0 | $self->{targets}->{$chan} = [ $key, $key ]; | |||||||||
| 166 | |||||||||||
| 167 | 0 | my $l = length($key); | |||||||||
| 168 | |||||||||||
| 169 | 0 | ▶ | if ( $l < 8 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 0 | my $longkey = ''; | |||||||||
| 171 | 0 | my $i = 8 / $l; | |||||||||
| 172 | 0 | ▶ | $i = $1 + 1 if $i =~ /(\d+)\.\d+/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 0 | ▶ | while ( $i > 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 0 | $longkey .= $key; | |||||||||
| 175 | 0 | $i--; | |||||||||
| 176 | } | ||||||||||
| 177 | 0 | $self->{targets}->{$chan} = [ $longkey, $key ]; | |||||||||
| 178 | } | ||||||||||
| 179 | } | ||||||||||
| 180 | |||||||||||
| 181 | 0 | ▶ | sub _inflate { | ||||||||
|
Subroutine
_inflate: not called CC=6 SCAR=37.4
| |||||||||||
| 182 | 0 | my ( $self, $text ) = @_; | |||||||||
| 183 | 0 | my $result = ''; | |||||||||
| 184 | 0 | my $k = -1; | |||||||||
| 185 | |||||||||||
| 186 | 0 | ▶ | while ( $k < ( length($text) - 1 ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 187 | 0 | my ( $l, $r ) = ( 0, 0 ); | |||||||||
| 188 | 0 | for ( $l, $r ) { | |||||||||
| 189 | 0 | foreach my $i ( 24, 16, 8 ) { | |||||||||
| 190 | 0 | $_ += ord( substr( $text, ++$k, 1 ) ) << $i; | |||||||||
| 191 | } | ||||||||||
| 192 | 0 | $_ += ord( substr( $text, ++$k, 1 ) ); | |||||||||
| 193 | } | ||||||||||
| 194 | 0 | for ( $r, $l ) { | |||||||||
| 195 | 0 | foreach my $i ( 0 .. 5 ) { | |||||||||
| 196 | 0 | $result .= substr( B64, $_ & 0x3F, 1 ); | |||||||||
| 197 | 0 | $_ = $_ >> 6; | |||||||||
| 198 | } | ||||||||||
| 199 | } | ||||||||||
| 200 | } | ||||||||||
| 201 | 0 | return $result; | |||||||||
| 202 | } | ||||||||||
| 203 | |||||||||||
| 204 | 0 | ▶ | sub _deflate { | ||||||||
|
Subroutine
_deflate: not called CC=6 SCAR=37.4
| |||||||||||
| 205 | 0 | my ( $self, $text ) = @_; | |||||||||
| 206 | 0 | my $result = ''; | |||||||||
| 207 | 0 | my $k = -1; | |||||||||
| 208 | |||||||||||
| 209 | 0 | ▶ | while ( $k < ( length($text) - 1 ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 210 | 0 | my ( $l, $r ) = ( 0, 0 ); | |||||||||
| 211 | 0 | for ( $r, $l ) { | |||||||||
| 212 | 0 | foreach my $i ( 0 .. 5 ) { | |||||||||
| 213 | 0 | $_ |= index( B64, substr( $text, ++$k, 1 ) ) << ( $i * 6 ); | |||||||||
| 214 | } | ||||||||||
| 215 | } | ||||||||||
| 216 | 0 | for ( $l, $r ) { | |||||||||
| 217 | 0 | foreach my $i ( 0 .. 3 ) { | |||||||||
| 218 | 0 | $result .= | |||||||||
| 219 | chr( ( $_ & ( 0xFF << ( ( 3 - $i ) * 8 ) ) ) | ||||||||||
| 220 | >> ( ( 3 - $i ) * 8 ) ); | ||||||||||
| 221 | } | ||||||||||
| 222 | } | ||||||||||
| 223 | } | ||||||||||
| 224 | |||||||||||
| 225 | 0 | return $result; | |||||||||
| 226 | } | ||||||||||
| 227 | |||||||||||
| 228 | 2 | 1; | |||||||||
| 229 | __END__ | ||||||||||