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