Bot-BasicBot-Pluggable-Module-Notify

 view release on metacpan or  search on metacpan

lib/Bot/BasicBot/Pluggable/Module/Notify.pm  view on Meta::CPAN


    my $fn = $self->store->get( 'notify', 'notifications' ) or return 0;
    return 0 unless(-r $fn); # file must be readable

    my $mod = (stat($fn))[9];
    return 1 if($mod <= $load_time && keys %emails); # don't reload if not modified

    my $fh = IO::File->new($fn,'r') or return 0;
    (%settings,%emails) = ();
    while(<$fh>) {
        s/\s+$//;
        next if(/^#/ || /^$/);
        my ($nick,$ident,$email) = split(/,/,$_,3);
        
        if($nick eq 'CONFIG') {
            $settings{$ident} = $email;
            next;
        }

        $emails{$nick}{email} = $email;
        $emails{$nick}{ident} = $ident if($ident);
    }

    $fh->close;
    $load_time = $mod;

    for my $key (keys %defaults) {
        $settings{$key} ||= $defaults{$key};
    }

    return 0    unless($settings{smtp});
    return 1    if(keys %emails);
    return 0;
}

sub _match_user {
    my ($self,$user,$nicks) = @_;

    # matches a known user
    return $user if($emails{$user});

    # see if idents match
    for my $ident (keys %emails) {
        next    unless($emails{$ident}{ident});

        for my $nick (keys %$nicks) {
            next    unless($user eq $nick);

            return $ident if($nicks->{$nick}->{Real}      =~ /\Q$emails{$ident}{ident}\E/);
            return $ident if($nicks->{$nick}->{User}      =~ /\Q$emails{$ident}{ident}\E/);
            return $ident if($nicks->{$nick}->{Userhost}  =~ /\Q$emails{$ident}{ident}\E/);
        }
    }

    return;
}

sub _sendmail {
    my ($self,%hash) = @_;

    MIME::Lite->send('smtp', $settings{smtp}, Timeout=>60);

    my $mail = MIME::Lite->new(
        'Reply-To'  => $settings{replyto},
        'From'      => $settings{from},

        'Subject'   => $hash{subject},
        'To'        => $hash{to},
        'Data'      => $hash{body}
    );

    eval { $mail->send };
    if($@) {
        print "MailError: eval=[$@]\n";
        return;
    }

    return 1;
}

 
1;
 
__END__

#----------------------------------------------------------------------------

=head1 NAME
 
Bot::BasicBot::Pluggable::Module::Notify - runs a IRC offline notification service
 
=head1 DESCRIPTION

When you have been away from IRC for more than 15 minutes, and someone posts a 
message mentioning you, this module will detect this, and send you a short 
email notification, detailing the sendee, the message, the channel and the time
sent. 

In addition to specific user mentions, the abillity to send to @here (active in
the last hour, but not in the last 15 minutes) or @all (all connected users, 
but not active in the last 15 minutes)

These latter two special cases are shortcuts to enable urgent or group wide 
messages to reach their intended recipients. 

Only users which have email addresses in the notification configuration file 
are alerted.

If a user leaves the channel within the minimum activity period (defaul 15 
minutes), and they are explicitly mentioned in the message, they are also 
notified.

=head1 SYNOPSIS

    my $bot = Bot::BasicBot::Pluggable->new(
        ... # various settings
    }; 

    $bot->store->set( 'notify', 'notifications', '/path/to/my/configuration.csv' },
    $bot->load('Seen');     # must be loaded to use Noify effectively
    $bot->load('Notify');



( run in 1.092 second using v1.01-cache-2.11-cpan-39bf76dae61 )