| 1 | package App::MadEye::Plugin::Notify::IKC; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base qw/App::MadEye::Plugin::Base/; | ||
| 5 | use Text::Truncate qw/truncstr/; | ||
| 6 | use POE::Component::IKC::ClientLite; | ||
| 7 | |||
| 8 | sub notify :Hook { | ||
| 9 | my ($self, $context, $args) = @_; | ||
| 10 | |||
| 11 | my $config = $self->config->{config}; | ||
| 12 | |||
| 13 | my $client = POE::Component::IKC::ClientLite->create_ikc_client( | ||
| 14 | ip => $config->{daemon_ip}, | ||
| 15 | port => $config->{daemon_port}, | ||
| 16 | name => "AppMadEye$$", | ||
| 17 | timeout => $config->{timeout}, | ||
| 18 | ) or die "cannot connect to $config->{daemon_ip}:$config->{daemon_port}"; | ||
| 19 | |||
| 20 | for my $line (split /\n/, _format($args, $config->{cutoff_length})) { | ||
| 21 | $client->post($config->{key}, $line); | ||
| 22 | } | ||
| 23 | $client->post($config->{key}, "> all"); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub _format { | ||
| 27 | my ($args, $cutoff_length) = @_; | ||
| 28 | |||
| 29 | my $text = ''; | ||
| 30 | while (my ($module, $targets) = each %$args) { | ||
| 31 | $module = _moniker($module); | ||
| 32 | $text .= "= $module\n" . truncstr(_format_target($targets), $cutoff_length) . "\n"; | ||
| 33 | } | ||
| 34 | $text; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _format_target { | ||
| 38 | my $targets = shift; | ||
| 39 | |||
| 40 | my $text = ''; | ||
| 41 | for my $target (@$targets) { | ||
| 42 | $text .= "- $target->{target}\n"; | ||
| 43 | $text .= "$target->{message}\n"; | ||
| 44 | } | ||
| 45 | $text; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub _moniker { | ||
| 49 | my $module = shift; | ||
| 50 | $module =~ s/.+:://; | ||
| 51 | $module; | ||
| 52 | } | ||
| 53 | |||
| 54 | 1; | ||
| 55 | __END__ |