]>
git.sthu.org Git - shutils.git/blob - dotfiles/irssi/scripts/screen_away.pl
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>
29 # change "chatnet" to "tag"
30 # 18.05.2007 fix '-one' for SILC networks
35 # put this script into your autorun directory and/or load it with
38 # there are 5 settings available:
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>
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
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
62 my $timer_name = undef;
68 Irssi::theme_register(
71 '{line_start}{hilight ' . $IRSSI{'name'} . ':} $0'
75 my $screen_away_used = 0;
77 # try to find out, if we are running in a screen
78 # (see, if $ENV{STY} is set
79 if (!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() . "): $!");
86 my ($socket_name, $socket_path);
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
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
94 my $socket = `LC_ALL="C" screen -ls`;
98 my $running_in_screen = 0;
99 # locale doesnt seems to be an problem (yet)
100 if ($socket !~ /^No Sockets found/s) {
101 # ok, should have only one socket
102 $socket_name = $ENV{'STY'};
103 $socket_path = $socket;
104 $socket_path =~ s/^.+\d+ Sockets? in ([^\n]+)\.\n.+$/$1/s;
105 if (length($socket_path) != length($socket)) {
106 # only activate, if string length is different
107 # (to make sure, we really got a dir name)
108 $screen_away_used = 1;
110 Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
111 "error reading screen informations from:");
112 Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
119 if ($screen_away_used == 0) {
120 # we will never be called again
124 # build complete socket name
125 $socket = $socket_path . "/" . $socket_name;
127 # register config variables
128 Irssi::settings_add_bool('misc', $IRSSI{'name'} . '_active', 1);
129 Irssi::settings_add_int('misc', $IRSSI{'name'} . '_repeat', 5);
130 Irssi::settings_add_str('misc', $IRSSI{'name'} . '_message', "not here ...");
131 Irssi::settings_add_str('misc', $IRSSI{'name'} . '_window', "1");
132 Irssi::settings_add_str('misc', $IRSSI{'name'} . '_nick', "");
139 # check, set or reset the away status
146 my ($away, @screen, $screen);
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)");
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
162 # execute permissions, Attached
166 # check if status has changed
167 if ($away == 1 and $away_status != 1) {
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'));
173 Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
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 ...";
181 foreach $server (Irssi::servers()) {
182 if (!$server->{usermode_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}) {
190 $old_nicks{$server->{'tag'}} = $server->{nick};
192 $server->command("NICK " . Irssi::settings_get_str($IRSSI{'name'} . '_nick'));
196 # user is already away, remember this
197 $away{$server->{'tag'}} = 1;
200 $away_status = $away;
201 } elsif ($away == 2 and $away_status != 2) {
203 Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'screen_away_crap',
206 foreach $server (Irssi::servers()) {
207 if ($away{$server->{'tag'}} == 1) {
208 # user was already away, dont reset away
209 $away{$server->{'tag'}} = 0;
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) {
215 $server->command("NICK " . $old_nicks{$server->{'tag'}});
216 $old_nicks{$server->{'tag'}} = "";
219 $away_status = $away;
222 # but everytimes install a new timer
223 register_screen_away_timer();
227 # register_screen_away_timer()
229 # remove old timer and install a new one
235 sub register_screen_away_timer {
236 if (defined($timer_name)) {
237 # remove old timer, if defined
238 Irssi::timeout_remove($timer_name);
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', '');