| 1 | package Bot::IRC::History; | ||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Bot::IRC selected channel history dumped to email | ||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use 5.016; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use exact; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Date::Format 'time2str'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Date::Parse 'str2time'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Email::Mailer; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Email::Valid; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use File::Grep 'fgrep'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||
| 13 | 2 | our $VERSION = '1.49'; # VERSION | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | sub init { | ||||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 16 | 1 | my ($bot) = @_; | |||||||||||||||||||||||||||||
| 17 | 1 | my $vars = $bot->vars; | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | my @filter = ( ref $vars->{filter} ) ? @{ $vars->{filter} } : ( $vars->{filter} ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 19 | 1 | my $stdout_file = $bot->settings('daemon')->{stdout_file}; | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||
| 21 | $bot->hook( | ||||||||||||||||||||||||||||||
| 22 | { | ||||||||||||||||||||||||||||||
| 23 | to_me => 1, | ||||||||||||||||||||||||||||||
| 24 | text => qr/ | ||||||||||||||||||||||||||||||
| 25 | ^\s*history\s+( | ||||||||||||||||||||||||||||||
| 26 | (?: | ||||||||||||||||||||||||||||||
| 27 | (?<type>on)\s+(?<date>.+?) | ||||||||||||||||||||||||||||||
| 28 | ) | | ||||||||||||||||||||||||||||||
| 29 | (?: | ||||||||||||||||||||||||||||||
| 30 | (?<type>from)\s+(?<date>.+?)\s+to\s+(?<date2>.+?) | ||||||||||||||||||||||||||||||
| 31 | ) | | ||||||||||||||||||||||||||||||
| 32 | (?: | ||||||||||||||||||||||||||||||
| 33 | (?<type>matching)\s+(?<string>.+?) | ||||||||||||||||||||||||||||||
| 34 | ) | ||||||||||||||||||||||||||||||
| 35 | )\s+(?:to\s+)?(?<email>\S+)\s*$ | ||||||||||||||||||||||||||||||
| 36 | /ix, | ||||||||||||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||||
| 39 | 0 | my ( $bot, $in, $m ) = @_; | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | if ( not $in->{forum} ) { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 42 | 0 | $bot->reply_to(q{Ask me from within a specific channel.}); | |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||
| 44 | 0 | elsif ( grep { lc( $in->{forum} ) eq lc($_) } @filter ) { | |||||||||||||||||||||||||||||
| 45 | 0 | $bot->reply_to(q{I'm not allowed to return history for this channel.}); | |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||
| 47 | elsif ( not Email::Valid->address( $m->{email} ) ) { | ||||||||||||||||||||||||||||||
| 48 | 0 | $bot->reply_to('The email address you provided does not appear to be valid.'); | |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||
| 50 | elsif ( not -f $stdout_file ) { | ||||||||||||||||||||||||||||||
| 51 | 0 | $bot->reply_to(q{Sorry. I can't seem to access a log file right now.}); | |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||
| 53 | ▶ | elsif ( $m->{date} and not $m->{time_date} = str2time( $m->{date} ) ) { | |||||||||||||||||||||||||||||
|
Condition0%0/3
$m->{"date"} and not $m->{"time_date"} = str2time($m->{"date"})
MC/DC0%0/2
$m->{"date"} and not $m->{"time_date"} = str2time($m->{"date"})
$m->{"date"}
not $m->{"time_date"} = str2time($m->{"date"})
Truth table0%0/3
A and not(B)
A= $m->{"date"}B= not $m->{"time_date"} = str2time($m->{"date"})
| |||||||||||||||||||||||||||||||
| 54 | 0 | $bot->reply_to(qq{I don't understand "$m->{date}" as a date or date/time.}); | |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||
| 56 | ▶ | elsif ( $m->{date2} and not $m->{time_date2} = str2time( $m->{date2} ) ) { | |||||||||||||||||||||||||||||
|
Condition0%0/3
$m->{"date2"} and not $m->{"time_date2"} = str2time($m->{"date2"})
MC/DC0%0/2
$m->{"date2"} and not $m->{"time_date2"} = str2time($m->{"date2"})
$m->{"date2"}
not $m->{"time_date2"} = str2time($m->{"date2"})
Truth table0%0/3
A and not(B)
A= $m->{"date2"}B= not $m->{"time_date2"} = str2time($m->{"date2"})
| |||||||||||||||||||||||||||||||
| 57 | 0 | $bot->reply_to(qq{I don't understand "$m->{date2}" as a date or date/time.}); | |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||||
| 60 | 0 | $bot->reply_to('Searching history...'); | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||
| 62 | my @matches = | ||||||||||||||||||||||||||||||
| 63 | map { | ||||||||||||||||||||||||||||||
| 64 | 0 | my $matches = $_->{matches}; | |||||||||||||||||||||||||||||
| 65 | 0 | map { $matches->{$_} } sort { $a <=> $b } keys %$matches; | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | } fgrep { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 67 | 0 | /^\[[^\]]*\]\s\S+\sPRIVMSG\s$in->{forum}/ | |||||||||||||||||||||||||||||
| 68 | 0 | } $stdout_file; | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||
| 70 | 0 | my $subject; | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ( lc $m->{type} eq 'on' ) { | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 72 | 0 | my $date = time2str( '%d/%b/%Y', $m->{time_date} ); | |||||||||||||||||||||||||||||
| 73 | 0 | my $re = qr/^\[$date/; | |||||||||||||||||||||||||||||
| 74 | 0 | @matches = grep { $_ =~ $re } @matches; | |||||||||||||||||||||||||||||
| 75 | 0 | $subject = "on date $m->{date}"; | |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||
| 77 | elsif ( lc $m->{type} eq 'from' ) { | ||||||||||||||||||||||||||||||
| 78 | @matches = | ||||||||||||||||||||||||||||||
| 79 | 0 | map { $_->{text} } | |||||||||||||||||||||||||||||
| 80 | grep { | ||||||||||||||||||||||||||||||
| 81 | $_->{time} >= $m->{time_date} and | ||||||||||||||||||||||||||||||
| 82 | $_->{time} <= $m->{time_date2} | ||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | } | ||||||||||||||||||||||||||||
|
Condition0%0/3
$_->{'time'} >= $m->{'time_date'} && $_->{'time'} <= $m->{'time_date2'}
MC/DC0%0/2
$_->{'time'} >= $m->{'time_date'} && $_->{'time'} <= $m->{'time_date2'}
$_->{'time'} >= $m->{'time_date'}
$_->{'time'} <= $m->{'time_date2'}
Truth table0%0/3
A && B
A= $_->{'time'} >= $m->{'time_date'}B= $_->{'time'} <= $m->{'time_date2'}
| |||||||||||||||||||||||||||||||
| 84 | map { | ||||||||||||||||||||||||||||||
| 85 | 0 | /^\[([^\]]+)\]\s/; | |||||||||||||||||||||||||||||
| 86 | +{ | ||||||||||||||||||||||||||||||
| 87 | 0 | time => str2time($1), | |||||||||||||||||||||||||||||
| 88 | text => $_, | ||||||||||||||||||||||||||||||
| 89 | }; | ||||||||||||||||||||||||||||||
| 90 | } @matches; | ||||||||||||||||||||||||||||||
| 91 | 0 | $subject = "from $m->{date} to $m->{date2}"; | |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||
| 93 | elsif ( lc $m->{type} eq 'matching' ) { | ||||||||||||||||||||||||||||||
| 94 | 0 | @matches = grep { /$m->{string}/i } @matches; | |||||||||||||||||||||||||||||
| 95 | 0 | $subject = "matching $m->{string}"; | |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if ( not @matches ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 99 | 0 | $bot->reply_to(q{I didn't find any history matching what you requested.}); | |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||||
| 102 | my $html = join( "\n", map { | ||||||||||||||||||||||||||||||
| 103 | 0 | /^\[(?<timestamp>[^\]]+)\]\s(?:\:(?<nick>[^!]+)!)?.*?PRIVMSG\s$in->{forum}\s:(?<text>.+)$/; | |||||||||||||||||||||||||||||
| 104 | 0 | my $parts = {%+}; | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $parts->{nick} //= 'ME'; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$parts->{"nick"} //= "ME"
MC/DC0%0/1
$parts->{"nick"} //= "ME"
$parts->{"nick"}
Truth table0%0/2
A
A= $parts->{"nick"}
| |||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||
| 107 | 0 | qq{ | |||||||||||||||||||||||||||||
| 108 | <p style="text-indent: -3em; margin: 0; margin-left: 3em"> | ||||||||||||||||||||||||||||||
| 109 | <i>$parts->{timestamp}</i> | ||||||||||||||||||||||||||||||
| 110 | <b>$parts->{nick}</b> | ||||||||||||||||||||||||||||||
| 111 | $parts->{text} | ||||||||||||||||||||||||||||||
| 112 | </p> | ||||||||||||||||||||||||||||||
| 113 | }; | ||||||||||||||||||||||||||||||
| 114 | } @matches ); | ||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | 0 | $html =~ s|(\w+://[\w\-\.!@#$%^&*-_+=;:,]+)|<a href="$1">$1</a>|g; | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||
| 118 | Email::Mailer->send( | ||||||||||||||||||||||||||||||
| 119 | to => $m->{email}, | ||||||||||||||||||||||||||||||
| 120 | from => $m->{email}, | ||||||||||||||||||||||||||||||
| 121 | 0 | subject => "IRC $in->{forum} history $subject", | |||||||||||||||||||||||||||||
| 122 | html => $html, | ||||||||||||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||
| 125 | $bot->reply_to( | ||||||||||||||||||||||||||||||
| 126 | 0 | 'OK. I just sent ' . $m->{email} . ' an email with ' . | |||||||||||||||||||||||||||||
| 127 | scalar(@matches) . ' matching history lines.' | ||||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||
| 131 | }, | ||||||||||||||||||||||||||||||
| 132 | 1 | ); | |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||
| 134 | 1 | $bot->helps( history => | |||||||||||||||||||||||||||||
| 135 | 'Dump selected channel history to email. ' . | ||||||||||||||||||||||||||||||
| 136 | 'Usage: "history on DATE EMAIL" or "history from DATE to DATE EMAIL" or "history matching STRING EMAIL".' | ||||||||||||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||
| 140 | 2 | 1; | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||
| 142 | __END__ | ||||||||||||||||||||||||||||||