lib/Horris/Connection.pm

filter: Statementstmt 41.5% 27 / 65 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 5 / 15 Podpod 20.0% 1 / 5 totaltotal 32.3% 33 / 102 CC 10 SCAR 22.9
CC
10
Cov
34%
occur_event
30.0
__ANON__
12.0
run
6.0
CRAP 9.9
« lib/Horris/Connection/Plugin/Eval.pm ↑ Summary blib/lib/Horris.pm »
1 package Horris::Connection;
2 # ABSTRACT: Single IRC Connection
3  
4  
5 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use AnyEvent::IRC::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Horris::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use namespace::clean -except => qw/meta/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 with 'MooseX::Role::Pluggable';
12  
13 5 const my $IRC_DEFAULT_PORT => 6667;
14  
15 5 has irc => (
16 is => 'rw',
17 isa => 'AnyEvent::IRC::Client',
18 handles => {
19 send_srv => 'send_srv',
20 }
21 );
22  
23 5 has nickname => (
24 is => 'ro',
25 isa => 'Str',
26 required => 1
27 );
28  
29 5 has password => (
30 is => 'ro',
31 isa => 'Str',
32 );
33  
34 5 has port => (
35 is => 'ro',
36 isa => 'Str',
37 default => $IRC_DEFAULT_PORT,
38 );
39  
40 5 has server => (
41 is => 'ro',
42 isa => 'Str',
43 required => 1
44 );
45  
46 5 has username => (
47 is => 'ro',
48 isa => 'Str',
49 lazy_build => 1
50 );
51  
52 5 has channels => (
53 is => 'ro',
54 isa => 'ArrayRef[Str]',
55 );
56  
57 # plugin's preference from configfle,
58 # Horris::Connection::Plugin using it when all the plugins are initialize.
59 5 has 'plugin' => (
60 traits => ['Hash'],
61 is => 'ro',
62 isa => 'HashRef',
63 handles => {
64 get_args => 'get',
65 },
66 );
67  
68 0 ▶sub _build_username { $_[0]->nickname }
Subroutine _build_username: not called CC=1 SCAR=6.9
69  
70 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod undocumented
71 0 my $self = shift;
72 0 foreach my $plugin (@{ $self->plugin_list }) { # WARNING: DO NOT CHANGE: plugin_list is lazy_build. it means initialize all the plugins at here.
73 0 $plugin->init($self);
74 }
75  
76 0 my $irc = AnyEvent::IRC::Client->new();
77 0 $self->irc($irc);
78  
79 0 ▶ $irc->reg_cb(disconnect => sub { $self->occur_event('on_disconnect'); });
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 0 ▶ $irc->reg_cb(connect => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
81 0 my ($con, $err) = @_;
82 0 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 0 0
83 0 warn "connect error: $err\n";
84 0 return;
85 }
86  
87 0 ▶ warn "connected to: " . $self->server . ":" . $self->port if $Horris::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Horris::DEBUG 0 0 0
88 0 $self->occur_event('on_connect');
89 0 });
90  
91 0 ▶ $irc->reg_cb(irc_privmsg => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
92 0 my ($con, $raw) = @_;
93 my $message = Horris::Message->new(
94 channel => $raw->{params}->[0],
95 message => $raw->{params}->[1],
96 from => $raw->{prefix}
97 0 );
98  
99 0 ▶ $self->occur_event('irc_privmsg', $message) if $message->from->nickname ne $self->nickname; # loop guard
Branch: 1 branch
BranchTrueFalseTotal
if $message->from->nickname ne $self->nickname 0 0 0
100 0 });
101  
102 0 ▶ $irc->reg_cb(privatemsg => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
103 0 my ($con, $nick, $raw) = @_;
104 my $message = Horris::Message->new(
105 channel => '',
106 message => $raw->{params}->[1],
107 0 ▶ from => defined $raw->{prefix} ? $raw->{prefix} : '',
Branch: 1 branch
BranchTrueFalseTotal
defined $raw->{'prefix'} ? : 0 0 0
108 );
109 0 $self->occur_event('on_privatemsg', $nick, $message);
110 0 });
111  
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 }
119  
120 0 ▶sub irc_notice {
Subroutine irc_notice: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my ($self, $args) = @_;
122 0 $self->send_srv(NOTICE => $args->{channel} => $args->{message});
123 }
124  
125 0 ▶sub irc_privmsg {
Subroutine irc_privmsg: not called CC=1 SCAR=6.9
Pod documented
126 0 my ($self, $args) = @_;
127 0 $self->send_srv(PRIVMSG => $args->{channel} => $args->{message});
128 }
129  
130 0 ▶sub irc_mode {
Subroutine irc_mode: not called CC=1 SCAR=6.9
Pod undocumented
131 0 my ($self, $args) = @_;
132 0 $self->send_srv(MODE => $args->{channel} => $args->{mode}, $args->{who});
133 }
134  
135 0 ▶sub occur_event {
Subroutine occur_event: not called CC=5 SCAR=34.0
Pod undocumented
136 0 my ($self, $event, @args) = @_;
137 0 my $plugins = $self->plugin_hash;
138 0 foreach my $plugin_name (@{ $self->plugins }) {
139 0 my $plugin = $plugins->{$plugin_name};
140 0 ▶ my $rev = $plugin->$event(@args) if $plugin->can($event);
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->can($event) 0 0 0
141  
142 # Don't try next plugin for $event if current plugin returns true
143 0 ▶ last if defined $rev and $rev;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rev and $rev 0 0 0
Condition0%0/3
defined $rev and $rev
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rev and $rev
defined $rev $rev
Truth table0%0/3
A and B
A= defined $revB= $rev
ABresult
0X0
100
111
144 }
145 }
146  
147 5 __PACKAGE__->meta->make_immutable();
148  
149 5 1;
150  
151 __END__
« lib/Horris/Connection/Plugin/Eval.pm ↑ Summary blib/lib/Horris.pm »