html/away.pl


   1 # $Id: away.pl,v 1.6 2003/02/25 08:48:56 nemesis Exp $
   2 
   3 use Irssi 20020121.2020 ();
   4 $VERSION = "0.23";
   5 %IRSSI = (
   6 	  authors     => 'Jean-Yves Lefort, Larry "Vizzie" Daffner, Kees Cook',
   7 	  contact     => 'jylefort@brutele.be, vizzie@airmail.net, kc@outflux.net',
   8 	  name        => 'away',
   9 	  description => 'Away with reason, unaway, and autoaway',
  10 	  license     => 'BSD',
  11 	  changed     => '$Date: 2003/02/25 08:48:56 $ ',
  12 );
  13 
  14 # /SET
  15 #
  16 #	away_reason		if you are not away and type /AWAY without
  17 #				arguments, this string will be used as
  18 #				your away reason
  19 #
  20 #       autoaway                number of seconds before marking away,
  21 #                               only actions listed in "autounaway_level"
  22 #                               will reset the timeout.
  23 #
  24 #	autounaway_level	if you are away and you type a message
  25 #				belonging to one of these levels, you'll be
  26 #				automatically unmarked away
  27 #
  28 #				levels considered:
  29 #
  30 #				DCC		a dcc chat connection has
  31 #						been established
  32 #				PUBLICS		a public message from you
  33 #				MSGS		a private message from you
  34 #				ACTIONS		an action from you
  35 #				NOTICES		a notice from you
  36 #
  37 # changes:
  38 #       2003-02-24
  39 #                       0.23?
  40 #                       merged with autoaway script
  41 #
  42 #	2003-01-09	release 0.22
  43 #			* command char independed
  44 #
  45 #	2002-07-04	release 0.21
  46 #			* signal_add's uses a reference instead of a string
  47 #
  48 # todo:
  49 #
  50 #	* rewrite the away command to support -one and -all switches
  51 #       * make auto-away stuff sane for multiple servers
  52 #       * make auto-away reason configurable
  53 #
  54 # (c) 2003 Jean-Yves Lefort (jylefort@brutele.be)
  55 #
  56 # (c) 2000 Larry Daffner (vizzie@airmail.net)
  57 #     You may freely use, modify and distribute this script, as long as
  58 #      1) you leave this notice intact
  59 #      2) you don't pretend my code is yours
  60 #      3) you don't pretend your code is mine
  61 #
  62 # (c) 2003 Kees Cook (kc@outflux.net)
  63 #      merged 'autoaway.pl' and 'away.pl'
  64 #
  65 # BUGS:
  66 #  - This only works for the first server
  67 
  68 use strict;
  69 use Irssi;
  70 use Irssi::Irc;			# for DCC object
  71 
  72 my ($autoaway_sec, $autoaway_to_tag, $am_away);
  73 
  74 sub away {
  75   my ($args, $server, $item) = @_;
  76 
  77   if ($server)
  78   {
  79     if (!$server->{usermode_away})
  80     {
  81       # go away
  82       $am_away=1;
  83 
  84       # stop autoaway
  85       if (defined($autoaway_to_tag)) {
  86         Irssi::timeout_remove($autoaway_to_tag);
  87         $autoaway_to_tag = undef();
  88       }
  89 
  90       if (!defined($args))
  91       {
  92         $server->command("AWAY " . Irssi::settings_get_str("away_reason"));
  93         Irssi::signal_stop();
  94       }
  95     }
  96     else
  97     {
  98       # come back
  99       $am_away=0;
 100       reset_timer();
 101     }
 102 
 103   }
 104 }
 105 
 106 sub cond_unaway {
 107   my ($server, $level) = @_;
 108   if (Irssi::level2bits(Irssi::settings_get_str("autounaway_level")) & $level)
 109   {
 110     #if ($server->{usermode_away})
 111     if ($am_away)
 112     {
 113       # come back from away
 114       $server->command("AWAY");
 115     }
 116     else
 117     {
 118       # bump the autoaway timeout
 119       reset_timer();
 120     }
 121   }
 122 }
 123 
 124 sub dcc_connected {
 125   my ($dcc) = @_;
 126   cond_unaway($dcc->{server}, MSGLEVEL_DCC) if ($dcc->{type} eq "CHAT");
 127 }
 128 
 129 sub message_own_public {
 130   my ($server, $msg, $target) = @_;
 131   cond_unaway($server, MSGLEVEL_PUBLIC);
 132 }
 133 
 134 sub message_own_private {
 135   my ($server, $msg, $target, $orig_target) = @_;
 136   cond_unaway($server, MSGLEVEL_MSGS);
 137 }
 138 
 139 sub message_irc_own_action {
 140   my ($server, $msg, $target) = @_;
 141   cond_unaway($server, MSGLEVEL_ACTIONS);
 142 }
 143 
 144 sub message_irc_own_notice {
 145   my ($server, $msg, $target) = @_;
 146   cond_unaway($server, MSGLEVEL_NOTICES);
 147 }
 148 
 149 #
 150 # /AUTOAWAY - set the autoaway timeout
 151 #
 152 sub away_setupcheck {
 153   $autoaway_sec = Irssi::settings_get_int("autoaway");
 154   reset_timer();
 155 }
 156 
 157 
 158 sub auto_timeout {
 159   my ($data, $server) = @_;
 160   my $msg = "autoaway after $autoaway_sec seconds";
 161 
 162   Irssi::timeout_remove($autoaway_to_tag);
 163   $autoaway_to_tag=undef;
 164 
 165   Irssi::print($msg);
 166 
 167   $am_away=1;
 168 
 169   my (@servers) = Irssi::servers();
 170   $servers[0]->command("AWAY $msg");
 171 }
 172 
 173 sub reset_timer {
 174     if (defined($autoaway_to_tag)) {
 175       Irssi::timeout_remove($autoaway_to_tag);
 176       $autoaway_to_tag = undef;
 177     }
 178     if ($autoaway_sec) {
 179       $autoaway_to_tag = Irssi::timeout_add($autoaway_sec*1000,
 180                                                 "auto_timeout", "");
 181     }
 182 }
 183 
 184 Irssi::settings_add_str("misc", "away_reason", "not here");
 185 Irssi::settings_add_str("misc", "autounaway_level", "PUBLIC MSGS ACTIONS DCC");
 186 Irssi::settings_add_int("misc", "autoaway", 0);
 187 
 188 Irssi::signal_add("dcc connected",          \&dcc_connected);
 189 Irssi::signal_add("message own_public",     \&message_own_public);
 190 Irssi::signal_add("message own_private",    \&message_own_private);
 191 Irssi::signal_add("message irc own_action", \&message_irc_own_action);
 192 Irssi::signal_add("message irc own_notice", \&message_irc_own_notice);
 193 Irssi::signal_add("setup changed"       =>  \&away_setupcheck);
 194 
 195 Irssi::command_bind("away",     "away");
 196 
 197 away_setupcheck();
 198