http://perl.lt/autoaway :: /Kodas/autoaway.pl - irssi Away sistema atsižvelgianti į laiką./


#######################################################################
# autoaway.pl
# - Smart Auto Away -
# Author: Saulius <saulius@tornado.ktu.lt>
# thanx to: Larry Daffner (vizzie@airmail.net)
#######################################################################

# identify.pl uses AUTOAWAY.pl (auto 30m)

# /autoaway <n> (sec, 0-disable)
# /autoaway [<secs>[s] | <mins>m]
# /away - will disable
use Irssi;
use vars qw($VERSION %IRSSI);
$VERSION = '1.00';
%IRSSI   = (
    author      => 'Saulius',
    contact     => 'saulius@tornado.ktu.lt',
    name        => 'Automagic away system',
    description => 'Smart auto-away after defined inactivity',
    license     => 'Free',
    changed     => '2003-11-07',
);

my ( $autoaway_sec, $autoaway_to_tag, $autoaway_state );
$autoaway_state = 0;

#
# /AUTOAWAY - set the autoaway timeout
#
sub cmd_autoaway {
    my ( $data, $server, $channel ) = @_;

    if ( $data =~ /^[0-9]+m$/ ) {
        $autoaway_sec = $data * 60;
    }
    elsif ( $data =~ /^([0-9]+)s?$/ ) {
        $autoaway_sec = $1;
    }
    else {
        Irssi::print("autoaway: usage: /autoaway [<secs>[s] | <mins>m]");
        return;
    }

    if ($autoaway_sec) {
        Irssi::print("autoaway timeout set to $autoaway_sec seconds");
    }
    else {
        Irssi::print("autoway disabled");
    }

    if ( defined($autoaway_to_tag) ) {
        Irssi::timeout_remove($autoaway_to_tag);
        $autoaway_to_tag = undef;
    }

    if ($autoaway_sec) {
        $autoaway_to_tag =
          Irssi::timeout_add( $autoaway_sec * 1000, "auto_timeout", "" );
    }
}

#
# away = Set us away or back, within the autoaway system
sub cmd_away {
    my ( $data, $server, $channel ) = @_;

    if ( $data eq "" ) {
        $autoaway_state = 0;

        # If $autoaway_state is 2, we went away by typing /away, and need
        # to restart autoaway ourselves. Otherwise, we were autoaway, and
        # we'll let the autoaway return take care of business.

        if ( $autoaway_state eq 2 ) {
            if ($autoaway_sec) {
                $autoaway_to_tag =
                  Irssi::timeout_add( $autoaway_sec * 1000, "auto_timeout",
                    "" );
            }
        }
    }
    else {
        if ( $autoaway_state eq 0 ) {
            Irssi::timeout_remove($autoaway_to_tag);
            $autoaway_to_tag = undef;
            $autoaway_state  = 2;
        }
    }
}

# -----------------<<<<<<<< PURE MAGIC >>>>>>>>>>>-----------------
sub away_reason {
    my ( $sec, $min, $val, $day, $men, $met, $sdn ) =
      ( localtime(time) )[ 0, 1, 2, 3, 4, 5, 6 ];
    $met += 1900;
    $men++;

    # savaitgalis:
    if ( ( $sdn == 6 ) || ( $sdn == 0 ) ) {
        return "savaitgalis... matyt kazkur dingau";
    }

    # naktis:
    if ( ( $val >= 2 ) && ( $val <= 10 ) ) {
        return "naktis... turbut dar miegu";
    }

    # paskaitos:
    if ( ( $val >= 11 ) && ( $val <= 15 ) ) { return "univiere or smth"; }

    # mieste:
    if ( ( $val >= 16 ) && ( $val <= 18 ) ) {
        return "kazkur barake, arba mieste arba belenkur";
    }

    # barake:
    return "barake arba nezinia kur...";
}

# ----------------->>>>>>>>
sub auto_timeout {
    my ( $data, $server ) = @_;
    my (@servers) = Irssi::servers();

    # we're in the process.. don't touch anything.
    $autoaway_state = 3;
    $server         = $servers[0];

    # Intelligent Auto-away
    my $away_msg = away_reason();
    $server->command("/AWAY nera: $away_msg");
    Irssi::timeout_remove($autoaway_to_tag);
    $autoaway_state = 1;
}

sub reset_timer {
    if ( $autoaway_state eq 1 ) {
        my (@servers) = Irssi::servers();

        $autoaway_state = 3;
        $server         = $servers[0];
        $server->command("/AWAY");

        $autoaway_state = 0;
    }
    if ( $autoaway_state eq 0 ) {
        if ( defined($autoaway_to_tag) ) {
            Irssi::timeout_remove($autoaway_to_tag);
            $autoaway_to_tag = undef();
        }
        if ($autoaway_sec) {
            $autoaway_to_tag =
              Irssi::timeout_add( $autoaway_sec * 1000, "auto_timeout", "" );
        }
    }
}

Irssi::settings_add_int( "misc", "autoaway_timeout", 0 );

$autoaway_default = Irssi::settings_get_int("autoaway_timeout");
if ($autoaway_default) {
    $autoaway_to_tag =
      Irssi::timeout_add( $autoaway_default * 1000, "auto_timeout", "" );

}

Irssi::command_bind( 'autoaway', 'cmd_autoaway' );
Irssi::command_bind( 'away',     'cmd_away' );
Irssi::signal_add( 'send command', 'reset_timer' );


2005.04.11 - 2012.05.18 © algirdas@perl.lt