App-MonM

 view release on metacpan or  search on metacpan

lib/App/MonM/Channel/Command.pm  view on Meta::CPAN

Defines type of channel. MUST BE set to "Command" value

=back

About common directives see L<App::MonM::Channel/CONFIGURATION DIRECTIVES>

=head1 HISTORY

See C<Changes> file

=head1 TO DO

See C<TODO> file

=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.00';

use CTK::Util qw/ execute dformat date_time2dig date2dig /;
use CTK::ConfGenUtil;
use App::MonM::Util qw/ set2attr getTimeOffset run_cmd /;

use constant {
    TIMEOUT         => 20, # 20 sec timeout
};

sub sendmsg {
    my $self = shift;
    return $self->maybe::next::method() unless $self->type eq 'command';
    my $message = $self->message;

    # Options
    my $options = set2attr($self->chconf) || {};
    #print App::MonM::Util::explain($options);

    # Get command string
    my $command = lvalue($self->chconf, "command") || lvalue($self->chconf, "script");
    unless ($command) {
        $self->error("Command string incorrect");
        return 0;
    }

    my $phone = $self->message->recipient;
    my $command_res = dformat($command, {
            ID      => $self->message->msgid,
            TO      => $self->message->to,
            RCPT    => $phone, RECIPIENT => $phone,
            PHONE   => $phone, NUM => $phone, TEL => $phone, NUMBER => $phone, MSISDN => $phone,
            TIME    => time(), DATETIME => date_time2dig(), DATE => date2dig(),
            SUBJECT => $self->message->subject, SUBJ => $self->message->subject, SBJ => $self->message->subject,
            MESSAGE => $self->message->subject, MSG => $self->message->subject,
        });

    # Get content body
    my $ct_type = lc(lvalue($self->chconf, "content") || "body");
    my $content = undef;
    if ($ct_type eq 'body') {
        $content = $message->email->body;
    } elsif ($ct_type eq 'email' or $ct_type eq 'mail') {
        $content = $message->email->as_string;
    }

    #print App::MonM::Util::explain($self->chconf);
    #print App::MonM::Util::explain(\$body);

    # Get timeout
    my $timeout = getTimeOffset(lvalue($self->chconf, "timeout") // TIMEOUT);

    # Run command
    my $r = run_cmd($command_res, $timeout, $content);
    $self->{exitval} = $r->{code};
    $self->{content} = $r->{stdout};
    $self->error($r->{stderr});

    return $r->{status};
}

1;

__END__



( run in 1.525 second using v1.01-cache-2.11-cpan-ceb78f64989 )