| 1 | package Monitoring::Spooler::Cmd::Command::email; | ||
| 2 | $Monitoring::Spooler::Cmd::Command::email::VERSION = '0.05'; | ||
| 3 | BEGIN { | ||
| 4 | $Monitoring::Spooler::Cmd::Command::email::AUTHORITY = 'cpan:TEX'; | ||
| 5 | } | ||
| 6 | # ABSTRACT: enqueue a new notification from an email | ||
| 7 | |||
| 8 | use 5.010_000; | ||
| 9 | use mro 'c3'; | ||
| 10 | use feature ':5.10'; | ||
| 11 | |||
| 12 | use Moose; | ||
| 13 | use namespace::autoclean; | ||
| 14 | |||
| 15 | # use IO::Handle; | ||
| 16 | # use autodie; | ||
| 17 | # use MooseX::Params::Validate; | ||
| 18 | # use Carp; | ||
| 19 | # use English qw( -no_match_vars ); | ||
| 20 | # use Try::Tiny; | ||
| 21 | |||
| 22 | # extends ... | ||
| 23 | extends 'Monitoring::Spooler::Cmd::Command::create'; | ||
| 24 | # has ... | ||
| 25 | has '+group_id' => ( | ||
| 26 | 'is' => 'ro', | ||
| 27 | 'isa' => 'Int', | ||
| 28 | 'required' => 0, | ||
| 29 | ); | ||
| 30 | |||
| 31 | has '+type' => ( | ||
| 32 | 'is' => 'ro', | ||
| 33 | 'isa' => 'Str', | ||
| 34 | 'required' => 0, | ||
| 35 | ); | ||
| 36 | |||
| 37 | has '+message' => ( | ||
| 38 | 'is' => 'ro', | ||
| 39 | 'isa' => 'Str', | ||
| 40 | 'required' => 0, | ||
| 41 | ); | ||
| 42 | # with ... | ||
| 43 | # initializers ... | ||
| 44 | |||
| 45 | # your code here ... | ||
| 46 | sub execute { | ||
| 47 | my $self = shift; | ||
| 48 | |||
| 49 | my @lines = <STDIN>; | ||
| 50 | my ($message, $group_id, $type) = $self->_parse_mail(\@lines); | ||
| 51 | return unless ($message && $group_id && $type); | ||
| 52 | |||
| 53 | # the superclass accessors are ro, so go directly to the guts | ||
| 54 | $self->{'message'} = $message; | ||
| 55 | $self->{'group_id'} = $group_id; | ||
| 56 | $self->{'type'} = $type; | ||
| 57 | |||
| 58 | return $self->SUPER::execute(); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub _parse_mail { | ||
| 62 | my $self = shift; | ||
| 63 | my $line_ref = shift; | ||
| 64 | |||
| 65 | my %props = (); | ||
| 66 | my $is_header = 1; | ||
| 67 | foreach my $line (@{$line_ref}) { | ||
| 68 | $is_header = 0 if $line =~ m/^$/; | ||
| 69 | next if $is_header; # ignore header | ||
| 70 | # parse body | ||
| 71 | if($line =~ m/^(\w+):(.*)$/) { | ||
| 72 | my ($key, $value) = ($1, $2); | ||
| 73 | $props{$key} = $value; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | return unless ($props{'id'} && $props{'status'} && $props{'name'}); | ||
| 77 | |||
| 78 | $props{'message'} = $props{'id'}.q{ }.$props{'status'}.q{ }.$props{'name'}; | ||
| 79 | return @props{qw(message group_id type)}; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub abstract { | ||
| 83 | return "Place a new message in the notification queue"; | ||
| 84 | } | ||
| 85 | |||
| 86 | no Moose; | ||
| 87 | __PACKAGE__->meta->make_immutable; | ||
| 88 | |||
| 89 | 1; | ||
| 90 | |||
| 91 | __END__ |