File Coverage

blib/lib/Bot/IRC/Join.pm
Criterion Covered Total %
statement 22 59 37.2
branch 0 18 0.0
condition 0 5 0.0
subroutine 4 10 40.0
pod 0 1 0.0
total 26 93 27.9


line stmt bran cond sub pod time code
1             package Bot::IRC::Join;
2             # ABSTRACT: Bot::IRC join and part channels and remember channels state
3              
4 1     1   2308 use 5.014;
  1         5  
5 1     1   17 use exact;
  1         3  
  1         6  
6              
7             our $VERSION = '1.38'; # VERSION
8              
9             sub init {
10 1     1 0 6450 my ($bot) = @_;
11 1         6 $bot->load('Store');
12              
13             $bot->hook(
14             {
15             to_me => 1,
16             text => qr/^join\s+(?<channel>\S+)/i,
17             },
18             sub {
19 0     0   0 my ( $bot, $in, $m ) = @_;
20              
21 0 0       0 if ( $m->{channel} =~ /^#\w+$/ ) {
22 0         0 $bot->reply( 'I will attempt to join: ' . $m->{channel} );
23 0         0 $bot->join( $m->{channel} );
24             }
25             else {
26 0         0 $bot->reply_to( '"' . $m->{channel} . q{" doesn't appear to be a channel I can join.} );
27             }
28             },
29 1         12 );
30              
31             $bot->hook(
32             {
33             to_me => 1,
34             text => qr/^(?:part|leave)\s+(?<channel>\S+)/i,
35             },
36             sub {
37 0     0   0 my ( $bot, $in, $m ) = @_;
38              
39 0 0       0 if ( $m->{channel} =~ /^#\w+$/ ) {
40 0         0 $bot->reply_to( 'I will depart: ' . $m->{channel} );
41 0         0 $bot->part( $m->{channel} );
42             }
43             else {
44 0         0 $bot->reply_to( '"' . $m->{channel} . q{" doesn't appear to be a valid channel name.} );
45             }
46             },
47 1         21 );
48              
49             $bot->hook(
50             {
51             to_me => 1,
52             text => qr/^channels\b/i,
53             },
54             sub {
55 0     0   0 my ($bot) = @_;
56 0 0       0 my @channels = @{ $bot->store->get('channels') || [] };
  0         0  
57              
58             $bot->reply_to(
59             (@channels)
60             ? 'I am currently in the following channels: ' .
61 0 0       0 $bot->list( ', ', 'and', sort { $a cmp $b } @channels ) . '.'
  0         0  
62             : 'I am currently not in any channels.'
63             );
64             },
65 1         22 );
66              
67 1         16 $bot->helps( join => 'Join and part channels. Usage: join <channel>, part <channel>, channels.' );
68              
69             {
70 1     1   1415 no strict 'refs';
  1         2  
  1         630  
  1         2  
71 1         3 for ( qw( join part ) ) {
72 2         6 my $name = ref($bot) . '::' . $_;
73 2         6 *{ $name . '_super' } = *$name{CODE};
  2         9  
74             }
75             }
76              
77             $bot->subs(
78             join => sub {
79 0     0   0 my $bot = shift;
80 0         0 my @channels = @_;
81 0 0       0 my %channels = map { $_ => 1 } @{ $bot->store->get('channels') || [] };
  0         0  
  0         0  
82 0 0       0 @channels = keys %channels unless (@channels);
83              
84 0         0 my $join = $bot->settings('connect')->{join};
85 0 0 0     0 if ( not @channels and $join ) {
86 0 0       0 @channels = ( ref $join eq 'ARRAY' ) ? @$join : $join
87             }
88              
89 0         0 $bot->join_super(@channels);
90              
91 0         0 $channels{$_} = 1 for (@channels);
92 0         0 $bot->store->set( 'channels' => [ keys %channels ] );
93              
94 0         0 return $bot;
95             },
96 1         7 );
97              
98             $bot->subs(
99             part => sub {
100 0     0   0 my $bot = shift;
101 0 0       0 my %channels = map { $_ => 1 } @{ $bot->store->get('channels') || [] };
  0         0  
  0         0  
102              
103 0         0 $bot->part_super(@_);
104              
105 0         0 delete $channels{$_} for (@_);
106 0         0 $bot->store->set( 'channels' => [ keys %channels ] );
107              
108 0         0 return $bot;
109             },
110 1         6 );
111              
112             $bot->subs(
113             channels => sub {
114 0   0 0     return shift->store->get('channels') || [];
115             },
116 1         4 );
117             }
118              
119             1;
120              
121             __END__
122              
123             =pod
124              
125             =encoding UTF-8
126              
127             =head1 NAME
128              
129             Bot::IRC::Join - Bot::IRC join and part channels and remember channels state
130              
131             =head1 VERSION
132              
133             version 1.38
134              
135             =head1 SYNOPSIS
136              
137             use Bot::IRC;
138              
139             Bot::IRC->new(
140             connect => { server => 'irc.perl.org' },
141             plugins => ['Join'],
142             )->run;
143              
144             =head1 DESCRIPTION
145              
146             This L<Bot::IRC> plugin handles messages instructing the bot to join or
147             part channels. Tell the bot to join and part channels as such:
148              
149             =head2 join <channel>
150              
151             Join a given channel.
152              
153             =head2 part <channel>
154              
155             Depart a given channel.
156              
157             =head2 SEE ALSO
158              
159             L<Bot::IRC>
160              
161             =for Pod::Coverage init
162              
163             =head1 AUTHOR
164              
165             Gryphon Shafer <gryphon@cpan.org>
166              
167             =head1 COPYRIGHT AND LICENSE
168              
169             This software is Copyright (c) 2016-2021 by Gryphon Shafer.
170              
171             This is free software, licensed under:
172              
173             The Artistic License 2.0 (GPL Compatible)
174              
175             =cut