From fa2beb76767d124d4f1ef59c0d86b78005402371 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Tue, 7 May 2019 01:19:29 -0400 Subject: [PATCH] screen_away --- weechat/.weechat/irc.conf | 8 +- weechat/.weechat/plugins.conf | 8 +- .../.weechat/python/autoload/screen_away.py | 1 + weechat/.weechat/python/screen_away.py | 244 ++++++++++++++++++ weechat/.weechat/weechat.conf | 8 +- 5 files changed, 256 insertions(+), 13 deletions(-) create mode 120000 weechat/.weechat/python/autoload/screen_away.py create mode 100644 weechat/.weechat/python/screen_away.py diff --git a/weechat/.weechat/irc.conf b/weechat/.weechat/irc.conf index bb2ca34..5e773d7 100644 --- a/weechat/.weechat/irc.conf +++ b/weechat/.weechat/irc.conf @@ -181,7 +181,7 @@ tilde.local_hostname tilde.usermode tilde.command = "/oper root ${sec.data.tildenetoper}; /msg operserv login ${sec.data.pass}" tilde.command_delay -tilde.autojoin = "#chaos,#meta,#secret-sudoers,#opers,#team,#sudoers,#YourTilde,#bots,#music,#politics,#gopher,#tildeverse,#idlerpg,#tilderadio,#minecraft,#tildelinux,#slbr,#cosmic,#institute,#.tilde,#tildetel,#dcss,#center,#thunix,#theasylum,#minetest,#aussie,#uucp,#projects,#hamradio,#vim,#black,#pink,#modded,#red,#admin,#vh7,#cervezafria,#~ 57:60,10:20" +tilde.autojoin = "#meta,#chaos,#secret-sudoers,#opers,#team,#sudoers,#YourTilde,#bots,#music,#politics,#gopher,#tildeverse,#idlerpg,#tilderadio,#minecraft,#tildelinux,#slbr,#cosmic,#institute,#.tilde,#tildetel,#dcss,#center,#thunix,#theasylum,#minetest,#aussie,#uucp,#projects,#hamradio,#vim,#black,#pink,#modded,#red,#admin,#vh7,#cervezafria,#~ :10:20,:57:60" tilde.autorejoin tilde.autorejoin_delay tilde.connection_timeout @@ -222,7 +222,7 @@ hashbang.local_hostname hashbang.usermode hashbang.command = "/oper benharri x" hashbang.command_delay -hashbang.autojoin = "#!git,#!support,#!politics,#!os,#!social,#!,#!opers" +hashbang.autojoin = "#!,#!social,#!os,#!politics,#!support,#!git,#!opers" hashbang.autorejoin hashbang.autorejoin_delay hashbang.connection_timeout @@ -345,7 +345,7 @@ sdf.local_hostname sdf.usermode sdf.command sdf.command_delay -sdf.autojoin = "#helpdesk,#anonradio,#gopher,#sdf" +sdf.autojoin = "#sdf,#gopher,#anonradio,#helpdesk" sdf.autorejoin sdf.autorejoin_delay sdf.connection_timeout @@ -427,7 +427,7 @@ oftc.local_hostname oftc.usermode oftc.command oftc.command_delay -oftc.autojoin = "#fish,#debian-devel" +oftc.autojoin = "#debian-devel,#fish" oftc.autorejoin oftc.autorejoin_delay oftc.connection_timeout diff --git a/weechat/.weechat/plugins.conf b/weechat/.weechat/plugins.conf index e367023..e1aac68 100644 --- a/weechat/.weechat/plugins.conf +++ b/weechat/.weechat/plugins.conf @@ -94,10 +94,12 @@ python.screen_away.away_suffix = "" python.screen_away.command_on_attach = "" python.screen_away.command_on_detach = "" python.screen_away.ignore = "" -python.screen_away.ignore_relays = "off" +python.screen_away.ignore_relays = "on" python.screen_away.interval = "5" -python.screen_away.message = "Detached from screen" +python.screen_away.message = "detached from byobu" +python.screen_away.no_output = "off" python.screen_away.set_away = "on" +python.screen_away.socket_file = "" python.screen_away.time_format = "since %Y-%m-%d %H:%M:%S%z" python.topicdiff_alt.color_del = "darkgray" python.topicdiff_alt.color_ins = "lightcyan" @@ -157,7 +159,9 @@ python.screen_away.ignore = "Comma-separated list of servers to ignore." python.screen_away.ignore_relays = "Only check screen status and ignore relay interfaces" python.screen_away.interval = "How often in seconds to check screen status" python.screen_away.message = "Away message" +python.screen_away.no_output = "no detach/attach information will be displayed in buffer" python.screen_away.set_away = "Set user as away." +python.screen_away.socket_file = "Socket file to use (leave blank to auto-detect)" python.screen_away.time_format = "time format append to away message" python.urlserver.buffer_short_name = "use buffer short name on dedicated buffer (default: "off")" python.urlserver.color = "color for urls displayed after message (default: "darkgray")" diff --git a/weechat/.weechat/python/autoload/screen_away.py b/weechat/.weechat/python/autoload/screen_away.py new file mode 120000 index 0000000..e485b70 --- /dev/null +++ b/weechat/.weechat/python/autoload/screen_away.py @@ -0,0 +1 @@ +../screen_away.py \ No newline at end of file diff --git a/weechat/.weechat/python/screen_away.py b/weechat/.weechat/python/screen_away.py new file mode 100644 index 0000000..23fa206 --- /dev/null +++ b/weechat/.weechat/python/screen_away.py @@ -0,0 +1,244 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2009 by xt +# Copyright (c) 2009 by penryu +# Copyright (c) 2010 by Blake Winton +# Copyright (c) 2010 by Aron Griffis +# Copyright (c) 2010 by Jani Kesänen +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +# +# (this script requires WeeChat 0.3.0 or newer) +# +# History: +# 2019-03-04, Germain Z. +# version 0.16: add option "socket_file", for use with e.g. dtach +# : code reformatting for consistency/PEP8 +# 2017-11-20, Nils Görs +# version 0.15: make script python3 compatible +# : fix problem with empty "command_on_*" options +# : add option "no_output" +# 2014-08-02, Nils Görs +# version 0.14: add time to detach message. (idea by Mikaela) +# 2014-06-19, Anders Bergh +# version 0.13: Fix a simple typo in an option description. +# 2014-01-12, Phyks (Lucas Verney) +# version 0.12: Added an option to check status of relays to set unaway in +# case of a connected relay. +# 2013-08-30, Anders Einar Hilden +# version: 0.11: Fix reading of set_away +# 2013-06-16, Renato Botelho +# version 0.10: add option to don't set away, only change nick +# allow multiple commands on attach/dettach +# do not add suffix if nick already have it +# 2012-12-29, David Flatz +# version 0.9: add option to ignore servers and don't set away status for them +# add descriptions to config options +# 2010-08-07, Filip H.F. "FiXato" Slagter +# version 0.8: add command on attach feature +# 2010-05-07, Jani Kesänen +# version 0.7: add command on detach feature +# 2010-03-07, Aron Griffis +# version 0.6: move socket check to register, +# add hook_config for interval, +# reduce default interval from 60 to 5 +# 2010-02-19, Blake Winton +# version 0.5: add option to change nick when away +# 2010-01-18, xt +# version 0.4: only update servers that are connected +# 2009-11-30, xt +# version 0.3: do not touch servers that are manually set away +# 2009-11-27, xt +# version 0.2: code for TMUX from penryu +# 2009-11-27, xt +# version 0.1: initial release + +import os +import re +import time +import weechat as w + + +SCRIPT_NAME = 'screen_away' +SCRIPT_AUTHOR = 'xt ' +SCRIPT_VERSION = '0.16' +SCRIPT_LICENSE = 'GPL3' +SCRIPT_DESC = 'Set away status on screen detach' + +SETTINGS = { + 'message': ('Detached from screen', 'Away message'), + 'time_format': ('since %Y-%m-%d %H:%M:%S%z', + 'time format append to away message'), + 'interval': ('5', 'How often in seconds to check screen status'), + 'away_suffix': ('', 'What to append to your nick when you\'re away.'), + 'command_on_attach': ('', + ('Commands to execute on attach, separated by ' + 'semicolon')), + 'command_on_detach': ('', + ('Commands to execute on detach, separated by ' + 'semicolon')), + 'ignore': ('', 'Comma-separated list of servers to ignore.'), + 'set_away': ('on', 'Set user as away.'), + 'ignore_relays': ('off', + 'Only check screen status and ignore relay interfaces'), + 'no_output': ('off', + 'no detach/attach information will be displayed in buffer'), + 'socket_file': ('', 'Socket file to use (leave blank to auto-detect)'), +} + +TIMER = None +SOCK = None +AWAY = False +CONNECTED_RELAY = False + + +def set_timer(): + '''Update timer hook with new interval.''' + global TIMER + if TIMER: + w.unhook(TIMER) + TIMER = w.hook_timer(int(w.config_get_plugin('interval')) * 1000, 0, 0, + 'screen_away_timer_cb', '') + + +def screen_away_config_cb(data, option, value): + '''Update timer / sock file on config changes.''' + global SOCK + if SOCK and option.endswith('.interval'): + set_timer() + elif option.endswith('.socket_file'): + SOCK = value + if not SOCK: + SOCK = get_sock() + if SOCK: + set_timer() + elif TIMER: + w.unhook(TIMER) + return w.WEECHAT_RC_OK + + +def get_servers(): + '''Get the servers that are not away, or were set away by this script.''' + ignores = w.config_get_plugin('ignore').split(',') + infolist = w.infolist_get('irc_server', '', '') + buffers = [] + while w.infolist_next(infolist): + if (not w.infolist_integer(infolist, 'is_connected') == 1 or + w.infolist_string(infolist, 'name') in ignores): + continue + if (not w.config_string_to_boolean(w.config_get_plugin('set_away')) or + not w.infolist_integer(infolist, 'is_away') or + w.config_get_plugin('message') in w.infolist_string( + infolist, 'away_message')): + buffers.append((w.infolist_pointer(infolist, 'buffer'), + w.infolist_string(infolist, 'nick'))) + w.infolist_free(infolist) + return buffers + + +def screen_away_timer_cb(buffer, args): + '''Check if screen is attached and update awayness.''' + global AWAY, SOCK, CONNECTED_RELAY + + set_away = w.config_string_to_boolean(w.config_get_plugin('set_away')) + check_relays = not w.config_string_to_boolean( + w.config_get_plugin('ignore_relays')) + suffix = w.config_get_plugin('away_suffix') + attached = os.access(SOCK, os.X_OK) # X bit indicates attached. + + # Check wether a client is connected on relay or not. + CONNECTED_RELAY = False + if check_relays: + infolist = w.infolist_get('relay', '', '') + if infolist: + while w.infolist_next(infolist): + status = w.infolist_string(infolist, 'status_string') + if status == 'connected': + CONNECTED_RELAY = True + break + w.infolist_free(infolist) + + if ((attached and AWAY) or + (check_relays and CONNECTED_RELAY and not attached and AWAY)): + if not w.config_string_to_boolean(w.config_get_plugin('no_output')): + w.prnt('', '{}: Screen attached. Clearing away status'.format( + SCRIPT_NAME)) + for server, nick in get_servers(): + if set_away: + w.command(server, '/away') + if suffix and nick.endswith(suffix): + nick = nick[:-len(suffix)] + w.command(server, '/nick {}'.format(nick)) + AWAY = False + if w.config_get_plugin('command_on_attach'): + for cmd in w.config_get_plugin('command_on_attach').split(';'): + w.command('', cmd) + + elif not attached and not AWAY: + if not CONNECTED_RELAY: + if (not w.config_string_to_boolean( + w.config_get_plugin('no_output'))): + w.prnt('', '{}: Screen detached. Setting away status'.format( + SCRIPT_NAME)) + for server, nick in get_servers(): + if suffix and not nick.endswith(suffix): + w.command(server, '/nick {}{}'.format(nick, suffix)) + if set_away: + w.command(server, '/away {} {}'.format( + w.config_get_plugin('message'), + time.strftime(w.config_get_plugin('time_format')))) + AWAY = True + if w.config_get_plugin('command_on_detach'): + for cmd in w.config_get_plugin('command_on_detach').split(';'): + w.command('', cmd) + + return w.WEECHAT_RC_OK + + +def get_sock(): + '''Try to get the appropriate sock file for screen/tmux.''' + sock = None + if 'STY' in os.environ.keys(): + # We are running under screen. + cmd_output = os.popen('env LC_ALL=C screen -ls').read() + match = re.search(r'Sockets? in (/.+)\.', cmd_output) + if match: + sock = os.path.join(match.group(1), os.environ['STY']) + + if not sock and 'TMUX' in os.environ.keys(): + # We are running under tmux. + socket_data = os.environ['TMUX'] + sock = socket_data.rsplit(',', 2)[0] + return sock + + +if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, + SCRIPT_DESC, '', ''): + version = w.info_get('version_number', '') or 0 + for option, default_desc in SETTINGS.items(): + if not w.config_is_set_plugin(option): + w.config_set_plugin(option, default_desc[0]) + if int(version) >= 0x00030500: + w.config_set_desc_plugin(option, default_desc[1]) + + SOCK = w.config_get_plugin('socket_file') + if not SOCK: + SOCK = get_sock() + + if SOCK: + set_timer() + w.hook_config('plugins.var.python.{}.*'.format(SCRIPT_NAME), + 'screen_away_config_cb', '') diff --git a/weechat/.weechat/weechat.conf b/weechat/.weechat/weechat.conf index dc2f271..36f747d 100644 --- a/weechat/.weechat/weechat.conf +++ b/weechat/.weechat/weechat.conf @@ -351,13 +351,7 @@ default.window = "3;1;0;0;core;weechat" default.current = on [notify] -irc.darwin = message -irc.freenode.##csharp = highlight -irc.freenode.#vim = highlight -irc.hashbang = message -irc.sdf = message -irc.tilde = message -irc.town = message +irc.bitlbee = highlight [filter] irc_smart = on;irc.freenode.#git;irc_smart_filter;*