initial checkin
[grml-etc.git] / etc / skel / .irssi / scripts / screen_away.pl
1 use Irssi;
2 use strict;
3 use FileHandle;
4
5 use vars qw($VERSION %IRSSI);
6
7 $VERSION = "0.9.6";
8 %IRSSI = (
9     authors     => 'Andreas \'ads\' Scherbaum <ads@wars-nicht.de>',
10     name        => 'screen_away',
11     description => 'set (un)away, if screen is attached/detached',
12     license     => 'GPL v2',
13     url         => 'none',
14 );
15
16 # screen_away irssi module
17 #
18 # written by Andreas 'ads' Scherbaum <ads@ufp.de>
19 #
20 # changes:
21 #  07.02.2004 fix error with away mode
22 #             thanks to Michael Schiansky for reporting and fixing this one
23 #  07.08.2004 new function for changing nick on away
24 #  24.08.2004 fixing bug where the away nick was not storedcorrectly
25 #             thanks for Harald Wurpts for help debugging this one
26 #  17.09.2004 rewrote init part to use $ENV{'STY'}
27 #  05.12.2004 add patch for remember away state
28 #             thanks to Jilles Tjoelker <jilles@stack.nl>
29 #
30 #
31 # usage:
32 #
33 # put this script into your autorun directory and/or load it with
34 #  /SCRIPT LOAD <name>
35 #
36 # there are 5 settings available:
37 #
38 # /set screen_away_active ON/OFF/TOGGLE
39 # /set screen_away_repeat <integer>
40 # /set screen_away_message <string>
41 # /set screen_away_window <string>
42 # /set screen_away_nick <string>
43 #
44 # active means, that you will be only set away/unaway, if this
45 #   flag is set, default is ON
46 # repeat is the number of seconds, after the script will check the
47 #   screen status again, default is 5 seconds
48 # message is the away message sent to the server, default: not here ...
49 # window is a window number or name, if set, the script will switch
50 #   to this window, if it sets you away, default is '1'
51 # nick is the new nick, if the script goes away
52 #   will only be used it not empty
53 #
54 # normal you should be able to rename the script to something other
55 # than 'screen_away' (as example, if you dont like the name) by simple
56 # changing the 'name' parameter in the %IRSSI hash at the top of this script
57
58
59 # variables
60 my $timer_name = undef;
61 my $away_status = 0;
62 my %old_nicks = ();
63 my %away = ();
64
65 # Register formats
66 Irssi::theme_register(
67 [
68  'screen_away_crap', 
69  '{line_start}{hilight ' . $IRSSI{'name'} . ':} $0'
70 ]);
71
72 # if we are running
73 my $screen_away_used = 0;
74
75 # try to find out, if we are running in a screen
76 # (see, if $ENV{STY} is set
77 if (!defined($ENV{STY})) {
78   # just return, we will never be called again
79   Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
80     "could not open status file for parent process (pid: " . getppid() . "): $!");
81   return;
82 }
83
84 my ($socket_name, $socket_path);
85
86 # search for socket
87 # normal we could search the socket file, ... if we know the path
88 # but so we have to call one time the screen executable
89 # disable locale
90 # the quotes around C force perl 5.005_03 to use the shell
91 # thanks to Jilles Tjoelker <jilles@stack.nl> for pointing this out
92 my $socket = `LC_ALL="C" screen -ls`;
93
94
95
96 my $running_in_screen = 0;
97 # locale doesnt seems to be an problem (yet)
98 if ($socket !~ /^No Sockets found/s) {
99   # ok, should have only one socket
100   $socket_name = $ENV{'STY'};
101   $socket_path = $socket;
102   $socket_path =~ s/^.+\d+ Sockets? in ([^\n]+)\.\n.+$/$1/s;
103   if (length($socket_path) != length($socket)) {
104     # only activate, if string length is different
105     # (to make sure, we really got a dir name)
106     $screen_away_used = 1;
107   } else {
108     Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
109       "error reading screen informations from:");
110     Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
111       "$socket");
112     return;
113   }
114 }
115
116 # last check
117 if ($screen_away_used == 0) {
118   # we will never be called again
119   return;
120 }
121
122 # build complete socket name
123 $socket = $socket_path . "/" . $socket_name;
124
125 # register config variables
126 Irssi::settings_add_bool('misc', $IRSSI{'name'} . '_active', 1);
127 Irssi::settings_add_int('misc', $IRSSI{'name'} . '_repeat', 5);
128 Irssi::settings_add_str('misc', $IRSSI{'name'} . '_message', "not here ...");
129 Irssi::settings_add_str('misc', $IRSSI{'name'} . '_window', "1");
130 Irssi::settings_add_str('misc', $IRSSI{'name'} . '_nick', "");
131
132 # init process
133 screen_away();
134
135 # screen_away()
136 #
137 # check, set or reset the away status
138 #
139 # parameter:
140 #   none
141 # return:
142 #   0 (OK)
143 sub screen_away {
144   my ($away, @screen, $screen);
145
146   # only run, if activated
147   if (Irssi::settings_get_bool($IRSSI{'name'} . '_active') == 1) {
148     if ($away_status == 0) {
149       # display init message at first time
150       Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
151         "activating $IRSSI{'name'} (interval: " . Irssi::settings_get_int($IRSSI{'name'} . '_repeat') . " seconds)");
152     }
153     # get actual screen status
154     my @screen = stat($socket);
155     # 00100 is the mode for "user has execute permissions", see stat.h
156     if (($screen[2] & 00100) == 0) {
157       # no execute permissions, Detached
158       $away = 1;
159     } else {
160       # execute permissions, Attached
161       $away = 2;
162     }
163
164     # check if status has changed
165     if ($away == 1 and $away_status != 1) {
166       # set away
167       if (length(Irssi::settings_get_str($IRSSI{'name'} . '_window')) > 0) {
168         # if length of window is greater then 0, make this window active
169         Irssi::command('window goto ' . Irssi::settings_get_str($IRSSI{'name'} . '_window'));
170       }
171       Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
172         "Set away");
173       my $message = Irssi::settings_get_str($IRSSI{'name'} . '_message');
174       if (length($message) == 0) {
175         # we have to set a message or we wouldnt go away
176         $message = "not here ...";
177       }
178       my ($server);
179       foreach $server (Irssi::servers()) {
180         if (!$server->{usermode_away}) {
181           # user isnt yet away
182           $away{$server->{'chatnet'}} = 0;
183           $server->command("AWAY -one $message") if (!$server->{usermode_away});
184           if (length(Irssi::settings_get_str($IRSSI{'name'} . '_nick')) > 0) {
185             # only change, if actual nick isnt already the away nick
186             if (Irssi::settings_get_str($IRSSI{'name'} . '_nick') ne $server->{nick}) {
187               # keep old nick
188               $old_nicks{$server->{'chatnet'}} = $server->{nick};
189               # set new nick
190               $server->command("NICK " . Irssi::settings_get_str($IRSSI{'name'} . '_nick'));
191             }
192           }
193         } else {
194           # user is already away, remember this
195           $away{$server->{'chatnet'}} = 1;
196         }
197       }
198       $away_status = $away;
199     } elsif ($away == 2 and $away_status != 2) {
200       # unset away
201       Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
202         "Reset away");
203       my ($server);
204       foreach $server (Irssi::servers()) {
205         if ($away{$server->{'chatnet'}} == 1) {
206           # user was already away, dont reset away
207           $away{$server->{'chatnet'}} = 0;
208           next;
209         }
210         $server->command('AWAY -one') if ($server->{usermode_away});
211         if (defined($old_nicks{$server->{'chatnet'}}) and length($old_nicks{$server->{'chatnet'}}) > 0) {
212           # set old nick
213           $server->command("NICK " . $old_nicks{$server->{'chatnet'}});
214           $old_nicks{$server->{'chatnet'}} = "";
215         }
216       }
217       $away_status = $away;
218     }
219   }
220   # but everytimes install a new timer
221   register_screen_away_timer();
222   return 0;
223 }
224
225 # register_screen_away_timer()
226 #
227 # remove old timer and install a new one
228 #
229 # parameter:
230 #   none
231 # return:
232 #   none
233 sub register_screen_away_timer {
234   if (defined($timer_name)) {
235     # remove old timer, if defined
236     Irssi::timeout_remove($timer_name);
237   }
238   # add new timer with new timeout (maybe the timeout has been changed)
239   $timer_name = Irssi::timeout_add(Irssi::settings_get_int($IRSSI{'name'} . '_repeat') * 1000, 'screen_away', '');
240 }
241