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