App-MonM-Notifier

 view release on metacpan or  search on metacpan

lib/App/MonM/Notifier.pm  view on Meta::CPAN

        File /tmp/monotifier.db
        Expires 1h
        MaxTime 1m
    </MoNotifier>

=head1 HISTORY

See C<Changes> file

=head1 DEPENDENCIES

L<App::MonM::QNotifier>

=head1 TO DO

See C<TODO> file

=head1 BUGS

* none noted

=head1 SEE ALSO

L<App::MonM::QNotifier>

=head1 AUTHOR

Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>

=head1 COPYRIGHT

Copyright (C) 1998-2022 D&D Corporation. All Rights Reserved

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See C<LICENSE> file and L<https://dev.perl.org/licenses/>

=cut

use vars qw/$VERSION/;
$VERSION = '1.04';

use parent qw/App::MonM::QNotifier/;

use CTK::ConfGenUtil;

use App::MonM::Util qw/getExpireOffset parsewords merge/;
use App::MonM::Notifier::Store;

use constant {
    NODE_NAME           => 'notifier',
    NODE_NAME_ALIAS     => 'monotifier',
};

sub new {
    my $class = shift;
    my %args = @_;
    my $self = $class->SUPER::new(%args);

    # Store
    my $store_conf = hash($self->config->conf(NODE_NAME) || $self->config->conf(NODE_NAME_ALIAS));
    $store_conf->{expires} = getExpireOffset(lvalue($store_conf, "expires") || lvalue($store_conf, "expire") || 0);
    $store_conf->{maxtime} = getExpireOffset(lvalue($store_conf, "maxtime") || 0);
    my $store = App::MonM::Notifier::Store->new(%$store_conf);
    $self->{store} = $store;
    #print App::MonM::Util::explain($store);

    return $self;
}
sub store {
    my $self = shift;
    return $self->{store};
}
sub notify { # send message to recipients list
    my $self = shift;
    my %args = @_;
    $self->error("");
    my $before = $args{before}; # The callback for before sending
    my $after = $args{after}; # The callback for after sending
    my @channels = $self->getChanelsBySendTo(array($args{to}));
    my $store = $self->store;

    # Create messages and send its
    foreach my $ch (@channels) {
        #print App::MonM::Util::explain($ch);
        my $message = App::MonM::Message->new(
            to          => lvalue($ch, "to"),
            cc          => lvalue($ch, "cc"),
            bcc         => lvalue($ch, "bcc"),
            from        => lvalue($ch, "from"),
            subject     => $args{subject} // '', # Message subject
            body        => $args{message} // '', # Message body
            headers     => hash($ch, "headers"),
            contenttype => lvalue($ch, "contenttype"), # optional
            charset     => lvalue($ch, "charset"), # optional
            encoding    => lvalue($ch, "encoding"), # optional
            attachment  => node($ch, "attachment"),
        );

        # Enqueue
        my $newid = $store->enqueue(
            to      => lvalue($ch, "to") || lvalue($ch, "recipient") || "anonymous",
            channel => $ch->{chname},
            subject => $args{subject} // '',
            message => $args{message} // '',
            attributes => $ch, # Channel attributes
        );
        $self->error($store->error);

        # Run before callback
        if (ref($before) eq 'CODE') {
            &$before($self, $message) or next;
        }

        # Send message
        my $sent = $self->channel->sendmsg($message, $ch);

        # ReQueue or DeQueue



( run in 0.969 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )