#!/usr/bin/perl -w

=head1 NAME

octo_sender - Octopussy Sender program

=head1 SYNOPSIS

octo_sender 

=head1 DESCRIPTION

octo_sender is the program used by the Octopussy Project to send messages

=cut

use strict;
use warnings;
use Readonly;

use AAT::NSCA;
use AAT::SMTP;
use AAT::Syslog;
use AAT::Utils qw( ARRAY );
use AAT::XMPP;
use AAT::Zabbix;

use Octopussy;
use Octopussy::Alert;
use Octopussy::Cache;
use Octopussy::Contact;

Readonly my $PROG_NAME => 'octo_sender';

exit if (!Octopussy::Valid_User($PROG_NAME));

my %contact  = ();
my $file_pid = Octopussy::PID_File($PROG_NAME);

=head1 FUNCTIONS

=head2 Contact_Configuration()

Loads Contact Configuration

=cut

sub Contact_Configuration
{
    foreach my $c (keys %contact)
    {
        delete $contact{$c};
    }
    foreach my $c (Octopussy::Contact::Configurations('cid'))
    {
        $contact{$c->{cid}} = $c;
    }
    my $nb_contacts = scalar(keys %contact);
    AAT::Syslog::Message($PROG_NAME, 'LOAD_CONTACTS_CONFIG', $nb_contacts);

    return ($nb_contacts);
}

=head2 Get_IM_Addresses($action)

Returns list of IM addresses from Contacts

=cut

sub Get_IM_Addresses
{
    my $action = shift;
    my @ims    = ();

    foreach my $c (ARRAY($action->{contacts}))
    {
        push @ims, $contact{$c}->{im}
            if (defined $contact{$c}->{im});
    }

    return (@ims);
}

=head2 Get_Mail_Addresses($action)

Returns list of mail addresses from Contacts

=cut

sub Get_Mail_Addresses
{
    my $action = shift;
    my @mails  = ();

    foreach my $c (ARRAY($action->{contacts}))
    {
    	push @mails, $contact{$c}->{email}
			if (defined $contact{$c}->{email});
 	}

    return (@mails);
}

#
# MAIN
#

$SIG{HUP} = \&Contact_Configuration;

Contact_Configuration();
my $cache = Octopussy::Cache::Init($PROG_NAME);
while (1)
{
    my @keys = $cache->get_keys();
    foreach my $k (sort @keys)
    {
        my $msg    = $cache->get($k);
        my $action = $msg->{action};
        my ($subject, $body, $action_host, $action_service, $action_body) =
            Octopussy::Alert::Message_Building($action, $msg->{device},
            $msg->{data}, $msg->{msg});

        if (defined $action->{action_jabber}
            && AAT::XMPP::Configured('Octopussy'))
        {
            my @ims = Get_IM_Addresses($action);
            AAT::XMPP::Send_Message('Octopussy', $subject, @ims);
            foreach my $line (split(/\n/, $body))
            {
                AAT::XMPP::Send_Message('Octopussy', $line, @ims);
            }
        }
        if (defined $action->{action_mail})
        {
            my @mails = Get_Mail_Addresses($action);
            AAT::SMTP::Send_Message('Octopussy',
                {subject => $subject, body => $body, dests => \@mails});
        }
        AAT::NSCA::Send('Octopussy', (($action->{level} =~ /Warning/i) ? 1 : 2),
            $action_body, $action_host, $action_service)
            if (defined $action->{action_nsca});
        AAT::Zabbix::Send('Octopussy', $action_body, $action_host,
            $action_service)
            if (defined $action->{action_zabbix});

        $cache->remove($k);
    }
    sleep 1;
}

1;

=head1 AUTHOR

Sebastien Thebert <octo.devel@gmail.com>

=head1 SEE ALSO

octo_commander, octo_extractor, octo_parser, octo_uparser, octo_reporter, octo_rrd, 
octo_scheduler

=cut
