5 use vars qw($VERSION %IRSSI);
9 authors => 'Andreas \'ads\' Scherbaum <ads@wars-nicht.de>',
10 name => 'screen_away',
11 description => 'set (un)away, if screen is attached/detached',
16 # screen_away irssi module
18 # written by Andreas 'ads' Scherbaum <ads@ufp.de>
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>
33 # put this script into your autorun directory and/or load it with
36 # there are 5 settings available:
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>
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
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
60 my $timer_name = undef;
66 Irssi::theme_register(
69 '{line_start}{hilight ' . $IRSSI{'name'} . ':} $0'
73 my $screen_away_used = 0;
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() . "): $!");
84 my ($socket_name, $socket_path);
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
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`;
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;
108 Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
109 "error reading screen informations from:");
110 Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
117 if ($screen_away_used == 0) {
118 # we will never be called again
122 # build complete socket name
123 $socket = $socket_path . "/" . $socket_name;
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', "");
137 # check, set or reset the away status
144 my ($away, @screen, $screen);
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)");
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
160 # execute permissions, Attached
164 # check if status has changed
165 if ($away == 1 and $away_status != 1) {
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'));
171 Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
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 ...";
179 foreach $server (Irssi::servers()) {
180 if (!$server->{usermode_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}) {
188 $old_nicks{$server->{'chatnet'}} = $server->{nick};
190 $server->command("NICK " . Irssi::settings_get_str($IRSSI{'name'} . '_nick'));
194 # user is already away, remember this
195 $away{$server->{'chatnet'}} = 1;
198 $away_status = $away;
199 } elsif ($away == 2 and $away_status != 2) {
201 Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
204 foreach $server (Irssi::servers()) {
205 if ($away{$server->{'chatnet'}} == 1) {
206 # user was already away, dont reset away
207 $away{$server->{'chatnet'}} = 0;
210 $server->command('AWAY -one') if ($server->{usermode_away});
211 if (defined($old_nicks{$server->{'chatnet'}}) and length($old_nicks{$server->{'chatnet'}}) > 0) {
213 $server->command("NICK " . $old_nicks{$server->{'chatnet'}});
214 $old_nicks{$server->{'chatnet'}} = "";
217 $away_status = $away;
220 # but everytimes install a new timer
221 register_screen_away_timer();
225 # register_screen_away_timer()
227 # remove old timer and install a new one
233 sub register_screen_away_timer {
234 if (defined($timer_name)) {
235 # remove old timer, if defined
236 Irssi::timeout_remove($timer_name);
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', '');