AnySan/Provider/IRC.pm untested

filter: Statementstmt 0.0% 0 / 114 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 203 CC 18 SCAR 39.4
CC
18
Cov
0%
irc
132.0
event_callback
20.0
_run
20.0
CRAP 51.4
1 package AnySan::Provider::IRC;
2 use strict;
3 use warnings;
4 use base 'AnySan::Provider';
5 our @EXPORT = qw(irc);
6 use AnySan;
7 use AnySan::Receive;
8 use AnyEvent::IRC::Client;
9 use AnyEvent::IRC::Util qw/mk_msg/;
10 use Encode;
11  
12 sub irc {
13 my($host, %config) = @_;
14  
15 my $self = __PACKAGE__->new(
16 client => undef,
17 config => \%config,
18 LAST_SEND_TIME => 0,
19 SEND_QUEUE => [],
20 SEND_TIMER => 0,
21 );
22 $self->{config}{wait_queue_size} ||= 100;
23  
24 my $port = $config{port} || 6667;
25 my $nickname = $config{nickname};
26 my $instance_key = $config{key} || "$host:$port";
27 $self->{config}{interval} = defined $config{interval} ? $config{interval} : 2;
28 $self->{config}{interval} = 2 unless $self->{config}{interval} =~ /\A[0-9]+\z/;
29  
30 my %recive_commands = map {
31 uc($_) => 1,
32 } @{ $config{recive_commands} || [ 'PRIVMSG' ] };
33  
34 my $con = AnyEvent::IRC::Client->new;
35 $self->{client} = $con;
36  
37 my $on_connect = $config{on_connect} ||= sub {
38 my ($con, $err) = @_;
39 if (defined $err) {
40 warn "connect error: $err\n";
41 return;
42 }
43 };
44 $con->reg_cb( connect => sub {
45 my ($con, $err) = @_;
46 $on_connect->($con, $err);
47 return if defined $err;
48  
49 # join channels
50 my @channels = keys %{ $config{channels} };
51 if ( @channels ) {
52 my $join_on_connect; $join_on_connect = AnyEvent->timer(
53 after => $self->{config}{interval},
54 interval => $self->{config}{interval},
55 cb => sub {
56 my $channel = shift @channels;
57 warn "join channel: $channel";
58 $self->join_channel( $channel, $config{channels}->{$channel}->{key} );
59 if ( !@channels ) {
60 undef $join_on_connect;
61 }
62 }
63 );
64 }
65 } );
66 if ( $config{on_disconnect} ) {
67 $con->reg_cb( disconnect => $config{on_disconnect} );
68 }
69  
70 $con->reg_cb (
71 'irc_*' => sub {
72 my(undef, $param) = @_;
73 return if $param->{command} =~ /\A[0-9]+\z/;
74 return unless $recive_commands{uc($param->{command})};
75 my($channel, $message) = @{ $param->{params} };
76 my($nickname, ) = split '!', ($param->{prefix} || '');
77  
78 my $receive; $receive = AnySan::Receive->new(
79 provider => 'irc',
80 event => 'privmsg',
81 message => $message,
82 nickname => $config{nickname},
83 from_nickname => $nickname,
84 attribute => {
85 channel => $channel,
86 command => $param->{command},
87 raw_params => $param,
88 },
89 cb => sub { $self->event_callback($receive, @_) },
90 );
91 AnySan->broadcast_message($receive);
92 }
93 );
94  
95 $con->enable_ssl if $config{enable_ssl}; # enable ssl
96  
97 # connect server
98 $con->connect ($host, $port, {
99 nick => $nickname,
100 user => $config{user},
101 password => $config{password},
102 });
103  
104 return $self;
105 }
106  
107  
108 sub event_callback {
109 my($self, $receive, $type, @args) = @_;
110  
111 if ($type eq 'reply') {
112 my $cmd = $receive->attribute('send_command') || 'NOTICE';
113 my $send = '';
114 my $msg = $args[0];
115 $msg = encode( utf8 => $msg ) if Encode::is_utf8($msg);
116 if ($receive->nickname eq $receive->attribute('channel')) {
117 $send = mk_msg undef, $cmd => $receive->from_nickname, $msg;
118 } else {
119 $send = mk_msg undef, $cmd => $receive->attribute('channel'), $msg;
120 }
121 $self->_send_raw($send);
122 }
123 }
124  
125 sub _run {
126 my($self, $cb) = @_;
127 if (scalar(@{ $self->{SEND_QUEUE} }) >= $self->{config}{wait_queue_size}) {
128 return;
129 }
130 if (time() - $self->{LAST_SEND_TIME} <= 0 || $self->{SEND_TIMER}) {
131 $self->{SEND_TIMER} ||= AnyEvent->timer(
132 after => 1,
133 interval => $self->{config}{interval},
134 cb => sub {
135 (shift @{ $self->{SEND_QUEUE} })->();
136 $self->{LAST_SEND_TIME} = time();
137 $self->{SEND_TIMER} = undef unless @{ $self->{SEND_QUEUE} };
138 },
139 );
140 push @{ $self->{SEND_QUEUE} }, $cb;
141 return;
142 }
143 $cb->();
144 $self->{LAST_SEND_TIME} = time();
145 }
146  
147 sub _send_raw {
148 my($self, $send, %args) = @_;
149 $self->_run(sub {
150 $self->{client}->send_raw($send);
151 });
152 }
153  
154 sub send_message {
155 my($self, $message, %args) = @_;
156 $self->_run(sub {
157 my $type = $args{privmsg} ? 'PRIVMSG' : 'NOTICE';
158 $self->{client}->send_chan(
159 $args{channel},
160 $type,
161 $args{channel},
162 $message,
163 );
164 });
165 }
166  
167 sub join_channel {
168 my($self, $channel, $key) = @_;
169 $self->{client}->send_srv( JOIN => $channel, $key );
170 }
171  
172 sub leave_channel {
173 my($self, $channel) = @_;
174 $self->{client}->send_srv( PART => $channel );
175 }
176  
177 1;
178 __END__