use strict;
use vars qw($VERSION %IRSSI);
use Irssi;
#use Data::Dumper;

$VERSION = '1.0';
%IRSSI = (
    authors	=> 'Remco B. Brink',
    contact	=> 'remco@rc6.org',
    name	=> 'BitlBee_quietness',
    description	=> 'Makes your BitlBee channel quite a bit more quiet.',
    license	=> 'GPLv2',
    url		=> 'http://brink.st/config/files/irssi/scripts',
    changed	=> '2005-11-22',
);

# Set the default BitlBee information
my $bitlbee_channel = "&bitlbee";
my $bitlbee_server_tag = "bitlbee";

sub message{
  # Immediately exit if we are not enabled
  if (!Irssi::settings_get_bool('bitlbee_quietness')) {
    return;
  }

  # Check if the user has configured an MSN address
  my $msn_address = Irssi::settings_get_str('bitlbee_quietness_msn');
  if($msn_address eq 'john@doe.net'){
    Irssi::print("This script is \x02disabled\x02 until you configure your MSN address.");
    Irssi::settings_set_bool('bitlbee_quietness' => 0);
    return;
  }

  my ($server, $msg, $nick, $address, $target) = @_;
  if($server->{tag} eq $bitlbee_server_tag){
    # Get the users MSN address
    my $msn_address = Irssi::settings_get_str('bitlbee_quietness_msn');

    # All MSN status messages we care about look like:
    #   MSN(john@doe.net) - 
    if($msg =~ /MSN\($msn_address\).*/){
      # General messages:
      #   Not downloading buddy image from john@doe.net because we already have it cached!
      #   My buddy image successfully sent to john@doe.net
      #   john@doe.net downloaded my buddy image!
      #   Saving john@doe.net's buddy image to: /path/to/file.png
      #   File /path/to/file.png received successfully!
      # MSN status messages
      #   Contact john@doe.net is in your buddy list but you don't appear in his/her one (yet?)!
      my $msnmessage = $msg;
      if($msnmessage =~ /Not downloading buddy image from .* because we already have it cached\!/){
        Irssi::signal_stop() if Irssi::settings_get_bool('bitlbee_quietness_msnimage');
      } elsif ($msnmessage =~ /My buddy image successfully sent to .*/){
        Irssi::signal_stop() if Irssi::settings_get_bool('bitlbee_quietness_msnimage');
      } elsif ($msnmessage =~ /.* downloaded my buddy image\!/){
        Irssi::signal_stop() if Irssi::settings_get_bool('bitlbee_quietness_msnimage');
      } elsif ($msnmessage =~ /Saving .*s buddy image to: .*/){
        Irssi::signal_stop() if Irssi::settings_get_bool('bitlbee_quietness_msnimage');
      } elsif ($msnmessage =~ /File .* received successfully\!/){
        Irssi::signal_stop() if Irssi::settings_get_bool('bitlbee_quietness_msnimage');
      } elsif ($msnmessage =~ /Contact .* is in your buddy list but you don\'t appear in his\/her one \(yet\?\)\!/){
        Irssi::signal_stop() if Irssi::settings_get_bool('bitlbee_quietness_msncontact');
      }
    }
  }    
}

Irssi::signal_add('message public', 'message');

# Core settings
Irssi::settings_add_str ('bitlbee','bitlbee_quietness_msn','john@doe.net');
Irssi::settings_add_bool('bitlbee','bitlbee_quietness' => 0);
Irssi::settings_add_bool('bitlbee','bitlbee_quietness_msnimage' => 1);
Irssi::settings_add_bool('bitlbee','bitlbee_quietness_msncontact' => 1);

# Greet the user
Irssi::print("BitlBee Quietness v$VERSION loaded");

