lib/Morris/Connection.pm

filter: Statementstmt 31.4% 22 / 70 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 22.2% 4 / 18 Podpod 0.0% 0 / 7 totaltotal 23.6% 26 / 110 CC 9 SCAR 23.3
CC
9
Cov
26%
new_from_config
30.0
call_hook
20.0
register_hook
6.0
CRAP 10.3
« blib/lib/Morris/Plugin/Reputation.pm ↑ Summary blib/lib/Morris/Plugin/QOTD.pm »
1 package Morris::Connection;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use AnyEvent::IRC::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Morris::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use namespace::clean -except => qw(meta);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 has hooks => (
8 traits => ['Hash'],
9 is => 'ro',
10 isa => 'HashRef',
11 lazy_build => 1,
12 handles => {
13 get_hook => 'get',
14 }
15 );
16  
17 2 has irc => (
18 is => 'rw',
19 isa => 'AnyEvent::IRC::Client',
20 handles => {
21 send_srv => 'send_srv',
22 }
23 );
24  
25 2 has nickname => (
26 is => 'ro',
27 isa => 'Str',
28 required => 1
29 );
30  
31 2 has password => (
32 is => 'ro',
33 isa => 'Str',
34 );
35  
36 2 has plugins => (
37 is => 'ro',
38 isa => 'ArrayRef',
39 writer => 'set_plugins'
40 );
41  
42 2 has port => (
43 is => 'ro',
44 isa => 'Str',
45 default => 6667,
46 required => 1
47 );
48  
49 2 has server => (
50 is => 'ro',
51 isa => 'Str',
52 required => 1
53 );
54  
55 2 has username => (
56 is => 'ro',
57 isa => 'Str',
58 lazy_build => 1,
59 );
60  
61 0 ▶sub _build_hooks { {} }
Subroutine _build_hooks: not called CC=1 SCAR=6.9
62 0 ▶sub _build_username { $_[0]->nickname }
Subroutine _build_username: not called CC=1 SCAR=6.9
63  
64 0 ▶sub new_from_config {
Subroutine new_from_config: not called CC=5 SCAR=34.0
Pod undocumented
65 0 my ($class, $config) = @_;
66  
67 0 my $plugins = delete $config->{plugin};
68 0 my $self = $class->new(%$config);
69 0 my @plugins;
70 0 ▶ while ( my ($class, $p) = each %$plugins ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$plugins) 0 0 0
71 0 ▶ if ($class !~ s/^\+//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class =~ s/^\+//) 0 0 0
72 0 $class = "Morris::Plugin::$class";
73 }
74 0 ▶ if (! Class::MOP::is_class_loaded( $class) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Class::MOP::is_class_loaded($class)) 0 0 0
75 0 Class::MOP::load_class( $class );
76 }
77  
78 0 my $plugin = $class->new_from_config( $p );
79 0 $plugin->register( $self );
80 0 push @plugins, $plugin;
81 }
82 0 ▶ $self->set_plugins(\@plugins) if @plugins;
Branch: 1 branch
BranchTrueFalseTotal
if @plugins 0 0 0
83 0 return $self;
84 }
85  
86 0 ▶sub call_hook {
Subroutine call_hook: not called CC=4 SCAR=30.0
Pod undocumented
87 0 my ($self, $name, @args) = @_;
88  
89 0 ▶warn "Calling hooks for $name" if Morris::DEBUG();
Branch: 1 branch
BranchTrueFalseTotal
if &Morris::DEBUG() 0 0 0
90  
91 0 my $hooks = $self->get_hook( $name );
92 0 ▶ return unless $hooks;
Branch: 1 branch
BranchTrueFalseTotal
unless $hooks 0 0 0
93  
94 0 foreach my $hook (@$hooks) {
95 0 $hook->( @args );
96 }
97 }
98  
99 0 ▶sub register_hook {
Subroutine register_hook: not called CC=2 SCAR=17.9
Pod undocumented
100 0 my ($self, $name, $code) = @_;
101  
102 0 ▶ $self->hooks->{$name} ||= [];
Condition0%0/2
$self->hooks->{$name} ||= []
l!l
00
MC/DC0%0/1
$self->hooks->{$name} ||= []
$self->hooks->{$name}
Truth table0%0/2
A
A= $self->hooks->{$name}
Aresult
11
00
103 0 my $list = $self->hooks->{$name};
104 0 push @$list, $code;
105 }
106  
107 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
108 0 my $self = shift;
109  
110 0 my $irc = AnyEvent::IRC::Client->new();
111 0 $self->irc($irc);
112 0 $irc->connect( $self->server, $self->port, {
113 nick => $self->nickname,
114 user => $self->username,
115 password => $self->password,
116 timeout => 1,
117 } );
118 $irc->reg_cb(
119 0 ▶ connect => sub { $self->call_hook( 'server.connected', @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 ▶ disconnect => sub { $self->call_hook( 'server.disconnect', @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 0 ▶ irc_privmsg => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 my ($nick, $raw) = @_;
123 my $message = Morris::Message->new(
124 channel => $raw->{params}->[0],
125 message => $raw->{params}->[1],
126 from => $raw->{prefix},
127 0 );
128 0 $self->call_hook( 'chat.privmsg', $message )
129 },
130  
131 # XXX - we want the /full/ details of this user, not his nick
132 # so we override the original irc_join callback
133 0 ▶ irc_join => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 my $object = shift;
135 0 $object->AnyEvent::IRC::Client::join_cb(@_);
136 # and /THEN/ call our callback
137 # fix the param thing to be just a simple 'channel' parameter
138 0 my $channel = $_[0]->{params}->[0];
139 0 my $addr = Morris::Message::Address->new( $_[0]->{prefix} );
140 0 $self->call_hook( 'channel.joined', $channel, $addr );
141 },
142 0 ▶ registered => sub { $self->call_hook( 'server.registered', @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 );
144 }
145  
146 0 ▶sub irc_notice {
Subroutine irc_notice: not called CC=1 SCAR=6.9
Pod undocumented
147 0 my ($self, $args) = @_;
148 0 $self->send_srv(NOTICE => $args->{channel} => $args->{message});
149 }
150  
151 0 ▶sub irc_privmsg {
Subroutine irc_privmsg: not called CC=1 SCAR=6.9
Pod undocumented
152 0 my ($self, $args) = @_;
153 0 $self->send_srv(PRIVMSG => $args->{channel} => $args->{message});
154 }
155  
156 0 ▶sub irc_mode {
Subroutine irc_mode: not called CC=1 SCAR=6.9
Pod undocumented
157 0 my ($self, $args) = @_;
158 0 $self->send_srv(MODE => $args->{channel} => $args->{mode}, $args->{who});
159 }
160  
161 2 __PACKAGE__->meta->make_immutable();
162  
163 2 1;
164  
165 __END__
« blib/lib/Morris/Plugin/Reputation.pm ↑ Summary blib/lib/Morris/Plugin/QOTD.pm »