# /AUTOAWAY - Mark user away after seconds of inactivity # /AWAY - play nice with autoaway # New, brighter, whiter version of my autoaway script. Actually works :) # (c) 2000 Larry Daffner (vizzie@airmail.net) # You may freely use, modify and distribute this script, as long as # 1) you leave this notice intact # 2) you don't pretend my code is yours # 3) you don't pretend your code is mine # # portions (c) 2010 Tamara Temple # -- verbose away - gives reason and an extra message # -- resets nick so it shows you're away # # share and enjoy! # A simple script. /autoaway will mark you as away automatically if # you have not typed any commands in seconds. (=0 disables the feature) # It will also automatically unmark you away the next time you type a command. # Note that using the /away command will disable the autoaway mechanism, as # well as the autoreturn. (when you unmark yourself, the auutoaway wil # restart again) use Irssi; use Irssi::Irc; use Data::Dumper::Names; use vars qw($VERSION %IRSSI); $VERSION = "1.0"; %IRSSI = ( authors => 'Larry "Vizzie" Daffner, modified by Tamara Temple', contact => 'vizzie@airmail.net, tamouse@gmail.com', name => 'Automagic away setting', description => 'Automatically goes away after defined inactivity', license => 'BSD', url => 'http://www.flamingpackets.net/~vizzie/irssi/, http://public.tamaratemple.com/irssi/', changed => '2010-06-06', changes => 'Get rid of afk spam' ); my ( $autoaway_sec, $autoaway_to_tag, $autoaway_state, $away_time_texts, $away_time, $nick_prefix, $nick_suffix, %oldnick, $away_message ); $autoaway_state = 0; $away_already_loaded = Irssi::settings_get_int('autoaway_already_loaded_once'); unless ($away_already_loaded) { $away_already_loaded = 1; Irssi::settings_add_str('misc','autoaway_already_loaded_once', $away_already_loaded); $away_time_texts = "wk,wks,day,days,hr,hrs,min,mins,sec,secs"; Irssi::settings_add_str('misc','away_time_texts',$away_time_texts); $autoaway_sec = 60 * 60; # go away after sixty minutes of inactivity Irssi::settings_add_int('misc', 'autoaway_timeout', $autoaway_sec); $nick_prefix = "AFK|"; Irssi::settings_add_str("misc", "away_nick_prefix", $nick_prefix); $nick_suffix = ""; Irssi::settings_add_str("misc", "away_nick_suffix", $nick_suffix); $away_message = 'Contact me at gtalk:tamouse@gmail.com'; Irssi::settings_add_str("misc", "away_message", $away_message); } $away_time = 0; $Debug = 0; sub cmd_setDebug { my ($server, $data, $target) = @_; $Debug = $Debug ? 0 : 1; # Toggle Debug variable Irssi::print("%Y[$IRSSI{name}]%n Debug set to $Debug"); } Irssi::command_bind('autoawaysetdebug','cmd_setDebug'); sub DebugPrint { my ($debugmsg) = @_; Irssi::print("%Y[$IRSSI{name}]%n " . $debugmsg) if $Debug; } #-------------------------------------------------------------------- # Translate the number of seconds to a human readable format. #-------------------------------------------------------------------- sub secs2text { $away_time_texts = Irssi::settings_get_str('away_time_texts'); my ($secs) = @_; my ($wk_,$wks_,$day_,$days_,$hr_,$hrs_,$min_,$mins_,$sec_,$secs_) = (0,1,2,3,4,5,6,7,8,9,10); my @texts = split(/,/,$away_time_texts); my $mins=int($secs/60); $secs -= ($mins*60); my $hrs=int($mins/60); $mins -= ($hrs*60); my $days=int($hrs/24); $hrs -= ($days*24); my $wks=int($days/7); $days -= ($wks*7); my $text = (($wks>0) ? (($wks>1) ? "$wks $texts[$wks_] " : "$wks $texts[$wk_] ") : "" ); $text .= (($days>0) ? (($days>1) ? "$days $texts[$days_] " : "$days $texts[$day_] ") : "" ); $text .= (($hrs>0) ? (($hrs>1) ? "$hrs $texts[$hrs_] " : "$hrs $texts[$hr_] ") : "" ); $text .= (($mins>0) ? (($mins>1) ? "$mins $texts[$mins_] " : "$mins $texts[$min_] ") : "" ); $text .= (($secs>0) ? (($secs>1) ? "$secs $texts[$secs_] " : "$secs $texts[$sec_] ") : "" ); $text =~ s/ $//; return $text; } #-------------------------------------------------------------------- # Output the public away on all channels. #-------------------------------------------------------------------- sub away_describe_pub_channels { my ($server, $text) = @_; if ($server->{connected}){ foreach my $chan ($server->channels) { sleep 1; DebugPrint("sending ACTION $chan->{name} $text"); $server->command("ACTION $chan->{name} $text"); } } } #--------------------------------------------------------------------- # Come back from away state #--------------------------------------------------------------------- sub comeback { DebugPrint("in comeback"); my (@servers) = Irssi::servers(); my $duration = time - $away_time; my $backmsg = "Returned after " . secs2text($duration); # Server away command only needs to be sent once DebugPrint("sending AWAY"); $servers[0]->command("AWAY"); foreach my $server (@servers) { if ($server->{connected} && $server->{usermode_away}) { sleep 1; DebugPrint("sending NICK $oldnick{$server->{chatnet}}"); $server->command("NICK $oldnick{$server->{chatnet}}"); # away_describe_pub_channels($server, $backmsg); } } } #--------------------------------------------------------------------- # Go Away #--------------------------------------------------------------------- sub goaway { DebugPrint("in goaway"); my ($reason, $server) = @_; my (@servers) = Irssi::servers(); my $nick; Irssi::timeout_remove($autoaway_to_tag); $nick_prefix = Irssi::settings_get_str('away_nick_prefix'); $nick_suffix = Irssi::settings_get_str('away_nick_suffix'); $away_message = Irssi::settings_get_str('away_message'); # AWAY server command only needs to be sent once -- it affects all servers DebugPrint("sending AWAY $reason - $away_message"); $servers[0]->command("AWAY $reason - $away_message"); foreach my $server (@servers) { if ($server->{connected} && !$server->{usermode_away}) { $nick = $server->{nick}; $oldnick{$server->{chatnet}} = $nick; $nick = $nick_prefix . $nick . $nick_suffix; DebugPrint("sending NICK $nick"); $server->command("NICK $nick"); # away_describe_pub_channels($server, "$reason - $away_message"); } } $away_time = time; } # # /AUTOAWAY - set the autoaway timeout # sub cmd_autoaway { my ( $data, $server, $channel ) = @_; DebugPrint("in cmd_autoaway"); if ( $data =~ /^[0-9]+m$/ ) { $autoaway_sec = $data * 60; } elsif ( $data =~ /^([0-9]+)s?$/ ) { $autoaway_sec = $1; } else { Irssi::print("autoaway: usage: /autoaway [[s] | m]"); return; } Irssi::settings_set_int("autoaway_timeout", $autoaway_sec); if ($autoaway_sec) { Irssi::print("autoaway timeout set to $autoaway_sec seconds"); } else { Irssi::print("autoaway 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 ) = @_; DebugPrint("in cmd_away, autoaway_state=$autoaway_state"); if ( $data eq "" ) { # If $autoaway_state is 2, we went away by typing /afk, 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 ) { DebugPrint("manually set away, now coming back"); $autoaway_state = 0; comeback(); if ($autoaway_sec) { $autoaway_to_tag = Irssi::timeout_add( $autoaway_sec * 1000, "auto_timeout", "" ); } } } else { if ( $autoaway_state eq 0 ) { DebugPrint("set away manually, disable autoreturn"); Irssi::timeout_remove($autoaway_to_tag); $autoaway_to_tag = undef; $autoaway_state = 2; goaway($data); } } } sub auto_timeout { my ( $data, $server ) = @_; DebugPrint("in auto_timeout. autoaway_state=$autoaway_state"); DebugPrint(Dumper($data)); DebugPrint(Dumper($server)); # we're in the process.. don't touch anything. $autoaway_state = 3; goaway("autoaway after $autoaway_sec seconds."); $autoaway_state = 1; } sub reset_timer { my ($server, $msg, $target) = @_; DebugPrint("in reset_timer. autoaway_state=$autoaway_state"); if ( $autoaway_state eq 1 ) { $autoaway_state = 3; comeback(); $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", "" ); } } } # Initialize autoaway if ($autoaway_sec) { $autoaway_to_tag = Irssi::timeout_add( $autoaway_sec * 1000, "auto_timeout", "" ); } Irssi::command_bind( 'autoaway', 'cmd_autoaway' ); Irssi::command_bind( 'afk', 'cmd_away' ); Irssi::command_bind('bak', 'cmd_away'); Irssi::signal_add( 'message own_public', 'reset_timer' ); Irssi::signal_add( 'message own_private', 'reset_timer' );