comparison .irssi/scripts/tmux_away.pl @ 304:139f30ce463a

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