use strict;
use Irssi;
use Irssi::Irc;
use Mail::Sendmail;
use vars qw($VERSION %IRSSI);

$VERSION = "0.3";
%IRSSI = (
    "authors"       =>  "Andy Smith",
    "contact"       =>  "irssi_pl\@anarkystic.com",
    "name"          =>  "prax0r",
    "description"   =>  "While connected through the proxy you are considered non-away, while disconnected you are considered away. While away any highlighted words or private messages result in an email, ideally to your phone. Watch out, no throttling (but feel free to send me a patch)",
    "url"           =>  "http://an9.org/w/Prax0r_Pl",
    "license"       =>  "MIT",
    "changed"       =>  "2005-08-23"
);


# The clients-connected threshold for setting you away
# If you set this to 0 then you are away when no clients are connected via the
# proxy. Increase this if you have other clients always connected to the proxy.
Irssi::settings_add_int('prax0r', 'away_threshold', '0');

# The reason given for your awayness if somebody were to look
Irssi::settings_add_str('prax0r', 'away_reason', 'probably not watching');

# The email address you want messages sent to
Irssi::settings_add_str('prax0r', 'away_email_to', '');

# The email address you want messages sent 'from'
Irssi::settings_add_str('prax0r', 'away_email_from', 'irssi@localhost');

# The truncation length of the email, useful for sms
Irssi::settings_add_int('prax0r', 'away_email_length', '140');


my $client_count = 0;

# Called on proxy client connect
sub client_connect {
    my (@servers) = Irssi::servers;
    my $nick = "";
    $client_count++;

    # We only have to call it on one server, sets unaway on all of them
    my $server = $servers[0];
    if ($server->{'usermode_away'} == "1") {
        $server->command('away');
    }
}

# Called on proxy client disconnect
sub client_disconnect {
    my (@servers) = Irssi::servers;

    $client_count--;
    if ($client_count < 0) { 
        $client_count = 0;
    }
    
    my $away_threshold = Irssi::settings_get_int("away_threshold");
    my $away_reason = Irssi::settings_get_str("away_reason");
    
    if ($client_count <= $away_threshold) {
        # again, we only need to touch one server
        my $server = $servers[0];
        if ($server->{'usermode_away'} == "0") {
            $server->command('away '.$away_reason);
        }
    }
}

# Called pretty much every time anything shows up on the screen
sub sig_printtext {
    my ($dest, $text, $stripped) = @_;
    
    my $away_email_to = Irssi::settings_get_str('away_email_to');
    my $away_email_from = Irssi::settings_get_str('away_email_from');
    my $away_email_length = Irssi::settings_get_int('away_email_length');
    
    if (($dest->{'level'} & (MSGLEVEL_HILIGHT|MSGLEVEL_MSGS))
        && ($dest->{'level'} & MSGLEVEL_NOHILIGHT) == 0) {
        
        my $m_stripped = $stripped;
        $m_stripped =~ s/^\[.*\] //;
        
        #This may be extremely t-mobile specific...
        if (length($m_stripped) + length($dest->{'target'}) + length($away_email_from) + 4 > $away_email_length) {
            $m_stripped = substr $m_stripped, 0, ($away_email_length - (length($dest->{'target'}) + length($away_email_from) + 7) ); 
            $m_stripped .= "...";
        }
        if ($dest->{'server'}->{'usermode_away'} == "1") {
            my %mail = (
                To => $away_email_to,
                From => $away_email_from,
                Subject => $dest->{'target'},
                Body => $m_stripped
            );
            sendmail(%mail);
        }
    }
}

# Register the callbacks
Irssi::signal_add ({
    'print text' => 'sig_printtext',
    'proxy client connected' => 'client_connect',
    'proxy client disconnected' => 'client_disconnect'
});
