html/autoaway.pl


   1 # /AUTOAWAY <n> - Mark user away after <n> seconds of inactivity
   2 # /AWAY - play nice with autoaway
   3 # New, brighter, whiter version of my autoaway script. Actually works :)
   4 # (c) 2000 Larry Daffner (vizzie@airmail.net)
   5 #     You may freely use, modify and distribute this script, as long as
   6 #      1) you leave this notice intact
   7 #      2) you don't pretend my code is yours
   8 #      3) you don't pretend your code is mine
   9 #
  10 # share and enjoy!
  11 
  12 # A simple script. /autoaway <n> will mark you as away automatically if
  13 # you have not typed any commands in <n> seconds. (<n>=0 disables the feature)
  14 # It will also automatically unmark you away the next time you type a command.
  15 # Note that using the /away command will disable the autoaway mechanism, as
  16 # well as the autoreturn. (when you unmark yourself, the autoaway wil
  17 # restart again)
  18 
  19 # Thanks to Adam Monsen for multiserver and config file fix
  20 
  21 use Irssi;
  22 use Irssi::Irc;
  23 
  24 use vars qw($VERSION %IRSSI);
  25 $VERSION = "0.3";
  26 %IRSSI = (
  27     authors => 'Larry "Vizzie" Daffner',
  28     contact => 'vizzie@airmail.net',
  29     name => 'Automagic away setting',
  30     description => 'Automatically goes  away after defined inactivity',
  31     license => 'BSD',
  32     url => 'http://www.flamingpackets.net/~vizzie/irssi/',
  33     changed => 'Tue Oct 19 14:41:15 CDT 2010',
  34     changes => 'Applied multiserver/store config patch from Adam Monsen'
  35 );
  36 
  37 my ($autoaway_sec, $autoaway_to_tag, $autoaway_state);
  38 $autoaway_state = 0;
  39 
  40 #
  41 # /AUTOAWAY - set the autoaway timeout
  42 #
  43 sub cmd_autoaway {
  44   my ($data, $server, $channel) = @_;
  45   
  46   if (!($data =~ /^[0-9]+$/)) {
  47     Irssi::print("autoaway: usage: /autoaway <seconds>");
  48     return 1;
  49   }
  50   
  51   $autoaway_sec = $data;
  52   
  53   if ($autoaway_sec) {
  54     Irssi::settings_set_int("autoaway_timeout", $autoaway_sec);
  55     Irssi::print("autoaway timeout set to $autoaway_sec seconds");
  56   } else {
  57     Irssi::print("autoway disabled");
  58   }
  59   
  60   if (defined($autoaway_to_tag)) {
  61     Irssi::timeout_remove($autoaway_to_tag);
  62     $autoaway_to_tag = undef;
  63   }
  64 
  65   if ($autoaway_sec) {
  66     $autoaway_to_tag =
  67       Irssi::timeout_add($autoaway_sec*1000, "auto_timeout", "");
  68   }
  69 }
  70 
  71 #
  72 # away = Set us away or back, within the autoaway system
  73 sub cmd_away {
  74   my ($data, $server, $channel) = @_;
  75   
  76   if ($data eq "") {
  77     $autoaway_state = 0;
  78     # If $autoaway_state is 2, we went away by typing /away, and need
  79     # to restart autoaway ourselves. Otherwise, we were autoaway, and
  80     # we'll let the autoaway return take care of business.
  81 
  82     if ($autoaway_state eq 2) {
  83       if ($autoaway_sec) {
  84 	$autoaway_to_tag =
  85 	  Irssi::timeout_add($autoaway_sec*1000, "auto_timeout", "");
  86       }
  87     }
  88   } else {
  89     if ($autoaway_state eq 0) {
  90       Irssi::timeout_remove($autoaway_to_tag);
  91       $autoaway_to_tag = undef;
  92       $autoaway_state = 2;
  93     }
  94   }
  95 }
  96 
  97 sub auto_timeout {
  98   my ($data, $server) = @_;
  99 
 100   # we're in the process.. don't touch anything.
 101   $autoaway_state = 3;
 102   foreach my $server (Irssi::servers()) {
 103       $server->command("/AWAY autoaway after $autoaway_sec seconds");
 104   }
 105 
 106   Irssi::timeout_remove($autoaway_to_tag);
 107   $autoaway_state = 1;
 108 }
 109 
 110 sub reset_timer {
 111    if ($autoaway_state eq 1) {
 112      $autoaway_state = 3;
 113      foreach my $server (Irssi::servers()) {
 114          $server->command("/AWAY");
 115      }
 116      
 117      $autoaway_state = 0;
 118    } 
 119   if ($autoaway_state eq 0) {
 120     if (defined($autoaway_to_tag)) {
 121       Irssi::timeout_remove($autoaway_to_tag);
 122       $autoaway_to_tag = undef();
 123     }
 124     if ($autoaway_sec) {
 125       $autoaway_to_tag = Irssi::timeout_add($autoaway_sec*1000
 126 					    , "auto_timeout", "");
 127     }
 128   }
 129 }
 130 
 131 Irssi::settings_add_int("misc", "autoaway_timeout", 0);
 132 
 133 $autoaway_default = Irssi::settings_get_int("autoaway_timeout");
 134 if ($autoaway_default) {
 135   $autoaway_to_tag =
 136     Irssi::timeout_add($autoaway_default*1000, "auto_timeout", "");
 137 
 138 }
 139 
 140 Irssi::command_bind('autoaway', 'cmd_autoaway');
 141 Irssi::command_bind('away', 'cmd_away');
 142 Irssi::signal_add('send command', 'reset_timer');