#!/usr/local/bin/perl -w
###########################################
# pounce-sound - Play a configured sound
#            for an event caused by a buddy
# 2009, Mike Schilli <m@perlmeister.com>
###########################################
use strict;
use YAML qw(LoadFile);
use Data::Throttler;

my($proto, $buddy) = @ARGV;

die "usage: $0 proto:recv buddy" 
  if !defined $buddy;

my($home) = glob "~";
my $path = "$home/.pidgin-pounce";

my $yaml = LoadFile( "$path/pounce.yml" );

my $throttler = Data::Throttler->new(
    max_items => 1,
    interval  => $yaml->{interval},
    backend   => "YAML",
    backend_options => {
        db_file => "$path/throttle.yml",
    },
);

if(! $throttler->try_push(
      key => "$proto:$buddy")) {
    # rate limit reached, skip it
    exit 0;
}

if(exists 
 $yaml->{sound_map}->{$proto}->{$buddy}) {
  my $sound = 
    $yaml->{sound_map}->{$proto}->{$buddy};
  system("play $path/sounds/$sound");
}
