| 1 | package MYDan::Util::Contact; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | use YAML::XS; | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use MYDan::Util::Contact; | ||
| 10 | my $contact = MYDan::Util::Contact->new(); | ||
| 11 | my $email = $contact->contact( 'user1' => 'email' ); | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | use MYDan::Util::OptConf; | ||
| 16 | use MYDan::Oncall::Policy; | ||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | our ( %util, %oncall ); | ||
| 21 | BEGIN{ | ||
| 22 | my $opt = MYDan::Util::OptConf->load(); | ||
| 23 | %util = $opt->dump( 'util' ); | ||
| 24 | %oncall = $opt->dump( 'oncall' ); | ||
| 25 | }; | ||
| 26 | |||
| 27 | sub new | ||
| 28 | { | ||
| 29 | my ( $class, %this ) = @_; | ||
| 30 | |||
| 31 | map{ | ||
| 32 | my $file = "$util{conf}/$_"; | ||
| 33 | $this{$_} = eval{ YAML::XS::LoadFile -f "$file.private" ? "$file.private" : $file }; | ||
| 34 | die "load config error:$@" if $@; | ||
| 35 | }qw( contact team ); | ||
| 36 | |||
| 37 | bless \%this, ref $class || $class; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub contact | ||
| 41 | { | ||
| 42 | my ( $this, $user, $type, $depth ) = @_; | ||
| 43 | |||
| 44 | $depth ++; | ||
| 45 | return () if $depth > 5; | ||
| 46 | my %c; | ||
| 47 | if ( $user =~ /(.+):(.+)/ ) | ||
| 48 | { | ||
| 49 | |||
| 50 | my $time = time; | ||
| 51 | my $who = MYDan::Oncall::Policy->new( "$oncall{data}/$1" ) | ||
| 52 | ->set( $time - MYDan::Oncall::HOUR, $time + MYDan::Oncall::HOUR ) | ||
| 53 | ->get( $time, $2 ); | ||
| 54 | map{ $c{$_} ++ }$this->contact( $who->{item}, $type, $depth ) | ||
| 55 | |||
| 56 | |||
| 57 | } | ||
| 58 | elsif( $user =~ s/^@// ) | ||
| 59 | { | ||
| 60 | return () unless my $user = $this->{team}{$user}; | ||
| 61 | map{ $c{$_} ++ }map{ $this->contact( $_, $type, $depth )}@$user; | ||
| 62 | } | ||
| 63 | else | ||
| 64 | { | ||
| 65 | return () unless my $c = $this->{contact}{$user}{$type}; | ||
| 66 | return $c; | ||
| 67 | } | ||
| 68 | return keys %c; | ||
| 69 | } | ||
| 70 | |||
| 71 | 1; |