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
21use strict;
22use Irssi;
23use Irssi::Irc;
24
25use vars qw($VERSION %IRSSI);
26$VERSION = "0.4";
27%IRSSI = (
28    authors => 'Larry "Vizzie" Daffner',
29    contact => 'vizzie@airmail.net',
30    name => 'Automagic away setting',
31    description => 'Automatically goes  away after defined inactivity',
32    license => 'BSD',
33    url => 'http://www.flamingpackets.net/~vizzie/irssi/',
34    changed => 'Tue Apr 26 19:30:00 CDT 2016',
35    changes => 'Applied multiserver/store config patch from Adam Monsen'
36);
37
38my ($autoaway_sec, $autoaway_to_tag, $autoaway_state);
39$autoaway_state = 0;
40
41#
42# /AUTOAWAY - set the autoaway timeout
43#
44sub cmd_autoaway {
45  my ($data, $server, $channel) = @_;
46
47  if (!($data =~ /^[0-9]+$/)) {
48    Irssi::print("autoaway: usage: /autoaway <seconds>");
49    return 1;
50  }
51
52  $autoaway_sec = $data;
53
54  if ($autoaway_sec) {
55    Irssi::settings_set_int("autoaway_timeout", $autoaway_sec);
56    Irssi::print("autoaway timeout set to $autoaway_sec seconds");
57  } else {
58    Irssi::print("autoway disabled");
59  }
60
61  if (defined($autoaway_to_tag)) {
62    Irssi::timeout_remove($autoaway_to_tag);
63    $autoaway_to_tag = undef;
64  }
65
66  if ($autoaway_sec) {
67    $autoaway_to_tag =
68      Irssi::timeout_add($autoaway_sec*1000, "auto_timeout", "");
69  }
70}
71
72#
73# away = Set us away or back, within the autoaway system
74sub cmd_away {
75  my ($data, $server, $channel) = @_;
76
77  if ($data eq "") {
78    $autoaway_state = 0;
79  } else {
80    if ($autoaway_state eq 0) {
81      Irssi::timeout_remove($autoaway_to_tag);
82      $autoaway_to_tag = undef;
83      $autoaway_state = 2;
84    }
85  }
86}
87
88sub auto_timeout {
89  my ($data, $server) = @_;
90
91  # we're in the process.. don't touch anything.
92  $autoaway_state = 3;
93  foreach my $server (Irssi::servers()) {
94      $server->command("/AWAY autoaway after $autoaway_sec seconds");
95  }
96
97  Irssi::timeout_remove($autoaway_to_tag);
98  $autoaway_state = 1;
99}
100
101sub reset_timer {
102   if ($autoaway_state eq 1) {
103     $autoaway_state = 3;
104     foreach my $server (Irssi::servers()) {
105         $server->command("/AWAY");
106     }
107
108     $autoaway_state = 0;
109   }
110  if ($autoaway_state eq 0) {
111    if (defined($autoaway_to_tag)) {
112      Irssi::timeout_remove($autoaway_to_tag);
113      $autoaway_to_tag = undef();
114    }
115    if ($autoaway_sec) {
116      $autoaway_to_tag = Irssi::timeout_add($autoaway_sec*1000
117					    , "auto_timeout", "");
118    }
119  }
120}
121
122Irssi::settings_add_int("misc", "autoaway_timeout", 0);
123
124my $autoaway_default = Irssi::settings_get_int("autoaway_timeout");
125if ($autoaway_default) {
126  $autoaway_to_tag =
127    Irssi::timeout_add($autoaway_default*1000, "auto_timeout", "");
128
129}
130
131Irssi::command_bind('autoaway', 'cmd_autoaway');
132Irssi::command_bind('away', 'cmd_away');
133Irssi::signal_add('send command', 'reset_timer');
134