File Coverage

blib/lib/Bot/IRC/Functions.pm
Criterion Covered Total %
statement 28 28 100.0
branch 9 12 75.0
condition 3 6 50.0
subroutine 4 4 100.0
pod 0 1 0.0
total 44 51 86.2


line stmt bran cond sub pod time code
1             # ABSTRACT: Bot::IRC add maybe helpful functions to the bot
2              
3             use 5.014;
4 1     1   1630 use exact;
  1         3  
5 1     1   14  
  1         2  
  1         4  
6             our $VERSION = '1.39'; # VERSION
7              
8             my ($bot) = @_;
9              
10 1     1 0 4568 my $alphabet = 'abcdefghijklmnopqrstuvwxyz';
11             my $start = uc($alphabet) . $alphabet;
12 1         1 my @alphabet = split( '', $alphabet );
13 1         4  
14 1         6 $bot->hook(
15             {
16             command => 'PRIVMSG',
17             text => qr/^(?<function>ord|chr|ascii|rot\d+|crypt)\s+(?<input>.+)/i,
18             },
19             sub {
20             my ( $bot, $in, $m ) = @_;
21              
22 4     4   1286 my $function = lc( $m->{function} );
23             my $text = '';
24 4         8  
25 4         5 if ( $function eq 'ord' ) {
26             $text =
27 4 100 33     24 "\"$m->{input}\" has a numerical value of " .
    100 66        
    100          
    50          
28             join( ' ', map { ord($_) } split( '', $m->{input} ) ) . '.';
29             }
30 1         4 elsif ( $function eq 'chr' or $function eq 'ascii' and $m->{input} =~ /^\d+$/ ) {
  1         12  
31             $text =
32             "$m->{input} has a character value of \"" .
33             chr( $m->{input} ) . '".';
34             }
35 1         5 elsif ( $function =~ /rot(\d+)/ ) {
36             my $rot = $1;
37             my @this_alphabet = @alphabet;
38 1         2 push( @this_alphabet, splice( @this_alphabet, 0, $rot ) );
39 1         5 my $end = uc( join( '', @this_alphabet ) ) . join( '', @this_alphabet );
40 1         3 ( $text = $m->{input} ) =~ tr/$start/$end/;
41 1         6 $text = "The ROT$rot of your input is \"$text\".";
42 1         3 }
43 1         4 elsif ( $function eq 'crypt' ) {
44             my $salt = ( $m->{input} =~ s/(\w+)\s+(\w{2})\s*$/$1/ )
45             ? $2
46             : join( '', map { $alphabet[ int( rand() * @alphabet ) ] } 0 .. 1 );
47             $text = 'The crypt value of your input is "' . crypt( $m->{input}, $salt ) . '".';
48 1 50       4 }
  2         8  
49 1         390  
50             $bot->reply($text) if ($text);
51             },
52 4 50       16 );
53              
54 1         10 $bot->helps( functions =>
55             'A set of maybe useful functions. ' .
56 1         13 'Usage: ord <character>; (chr|ascii) <number>; rot<number> <string>; crypt <string> [<salt>].'
57             );
58             }
59              
60             1;
61              
62              
63             =pod
64              
65             =encoding UTF-8
66              
67             =head1 NAME
68              
69             Bot::IRC::Functions - Bot::IRC add maybe helpful functions to the bot
70              
71             =head1 VERSION
72              
73             version 1.39
74              
75             =head1 SYNOPSIS
76              
77             use Bot::IRC;
78              
79             Bot::IRC->new(
80             connect => { server => 'irc.perl.org' },
81             plugins => ['Functions'],
82             )->run;
83              
84             =head1 DESCRIPTION
85              
86             This L<Bot::IRC> plugin adds what might be helpful functions to the bot.
87             Commands include:
88              
89             =head2 ord <character>
90              
91             Convert a character into its ASCII number equivalent.
92              
93             =head2 (chr|ascii) <number>
94              
95             Convert a number into its ASCII character equivalent.
96              
97             =head2 rot<number> <string>
98              
99             Inspired by ROT13, this function will transpose letters based on the sequence
100             of the alphabet and by the number provided.
101              
102             rot13 hello
103             rot42 hello again
104              
105             ROT13 is a simple letter substitution cipher that replaces a letter with the
106             letter 13 letters after it in the alphabet.
107              
108             =head2 crypt <string> [<salt>]
109              
110             This method will encrypt using C<crypt> a string. If the salt is not provided,
111             it will be randomly generated.
112              
113             =head2 SEE ALSO
114              
115             L<Bot::IRC>
116              
117             =for Pod::Coverage init
118              
119             =head1 AUTHOR
120              
121             Gryphon Shafer <gryphon@cpan.org>
122              
123             =head1 COPYRIGHT AND LICENSE
124              
125             This software is Copyright (c) 2016-2050 by Gryphon Shafer.
126              
127             This is free software, licensed under:
128              
129             The Artistic License 2.0 (GPL Compatible)
130              
131             =cut