logo

scripts

A bunch of scripts, some to be moved to their own repository git clone https://hacktivis.me/git/scripts.git

screen_away.pl (6491B)


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