Compare commits
3 Commits
1abe0f5723
...
971452640a
Author | SHA1 | Date |
---|---|---|
Brian S. Stephan | 971452640a | |
Brian S. Stephan | f3cfac7759 | |
Brian S. Stephan | 7bd6bca58a |
|
@ -0,0 +1,108 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2016 Brian S. Stephan <bss@incorporeal.org>
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
# Convenience commands for appending/prepending text to the channel topic
|
||||
# or other buffer title.
|
||||
#
|
||||
# 2016-05-03 v0.1
|
||||
# initial script
|
||||
|
||||
SCRIPT_NAME = "email-notify"
|
||||
SCRIPT_AUTHOR = "bss <bss@incorporeal.org>"
|
||||
SCRIPT_VERSION = "0.1"
|
||||
SCRIPT_LICENSE = "GPL3"
|
||||
SCRIPT_DESC = "Send an email on highlight and privmsg when away"
|
||||
|
||||
try:
|
||||
import weechat
|
||||
except:
|
||||
print("This script must be run under WeeChat.")
|
||||
print("Get WeeChat now at: http://www.weechat.org/")
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
MAIL = '/usr/bin/mail'
|
||||
|
||||
settings = {
|
||||
'to' : '',
|
||||
'ignore' : '',
|
||||
}
|
||||
|
||||
last_buffer = ''
|
||||
|
||||
|
||||
def get_nick(s):
|
||||
"""Strip nickmodes and prefix, suffix."""
|
||||
if not s: return ''
|
||||
# prefix and suffix
|
||||
prefix = weechat.config_string(weechat.config_get('irc.look.nick_prefix'))
|
||||
suffix = weechat.config_string(weechat.config_get('irc.look.nick_suffix'))
|
||||
if s[0] == prefix:
|
||||
s = s[1:]
|
||||
if s[-1] == suffix:
|
||||
s = s[:-1]
|
||||
# nick mode
|
||||
modes = '~+@!%'
|
||||
s = s.lstrip(modes)
|
||||
return s
|
||||
|
||||
|
||||
def away_cb(data, buffer, time, tags, display, hilight, prefix, msg):
|
||||
global last_buffer
|
||||
|
||||
# Check if we are away
|
||||
if not weechat.buffer_get_string(buffer, 'localvar_away'):
|
||||
return weechat.WEECHAT_RC_OK
|
||||
|
||||
if (hilight == 1 or 'notify_private' in tags) and display == 1:
|
||||
if hilight == 1:
|
||||
subj = weechat.buffer_get_string(buffer, 'short_name')
|
||||
else:
|
||||
subj = prefix
|
||||
|
||||
ignores = weechat.config_get_plugin('ignore').split(',')
|
||||
if subj not in ignores:
|
||||
prefix = get_nick(prefix)
|
||||
last_buffer = weechat.buffer_get_string(buffer, 'plugin') + '.' + weechat.buffer_get_string(buffer, 'name')
|
||||
|
||||
send_mail("IRC: {0:s}".format(subj), "<{0:s}> {1:s}".format(prefix, msg))
|
||||
|
||||
return weechat.WEECHAT_RC_OK
|
||||
|
||||
|
||||
def send_mail(subject, body):
|
||||
to_addr = weechat.config_get_plugin('to')
|
||||
|
||||
mail = subprocess.Popen([MAIL, '-s', subject, to_addr], stdin=subprocess.PIPE)
|
||||
mail.stdin.write(bytes(body, 'UTF-8'))
|
||||
mail.stdin.close()
|
||||
mail.wait()
|
||||
|
||||
|
||||
def info_hook_cb(data, info_name, arguments):
|
||||
global last_buffer
|
||||
return last_buffer
|
||||
|
||||
|
||||
if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', ''):
|
||||
for opt, val in settings.items():
|
||||
if not weechat.config_is_set_plugin(opt):
|
||||
weechat.config_set_plugin(opt, val)
|
||||
|
||||
weechat.hook_print('', '', '', 1, 'away_cb', '')
|
||||
weechat.hook_info('%s_buffer' %SCRIPT_NAME, '', '', 'info_hook_cb', '')
|
|
@ -0,0 +1,271 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (c) 2009 by xt <xt@bash.no>
|
||||
# Copyright (c) 2009 by penryu <penryu@gmail.com>
|
||||
# Copyright (c) 2010 by Blake Winton <bwinton@latte.ca>
|
||||
# Copyright (c) 2010 by Aron Griffis <agriffis@n01se.net>
|
||||
# Copyright (c) 2010 by Jani Kesänen <jani.kesanen@gmail.com>
|
||||
# Copyright (c) 2020 by Brian S. Stephan <bss@incorporeal.org>
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
#
|
||||
# (this script requires WeeChat 0.3.0 or newer)
|
||||
#
|
||||
# History:
|
||||
# 2020-07-11, Brian S. Stephan <bss@incorporeal.org>
|
||||
# version 0.17: fork from screen_away, detection of tmux focus
|
||||
# 2019-03-04, Germain Z. <germanosz@gmail.com>
|
||||
# version 0.16: add option "socket_file", for use with e.g. dtach
|
||||
# : code reformatting for consistency/PEP8
|
||||
# 2017-11-20, Nils Görs <weechatter@arcor.de>
|
||||
# version 0.15: make script python3 compatible
|
||||
# : fix problem with empty "command_on_*" options
|
||||
# : add option "no_output"
|
||||
# 2014-08-02, Nils Görs <weechatter@arcor.de>
|
||||
# version 0.14: add time to detach message. (idea by Mikaela)
|
||||
# 2014-06-19, Anders Bergh <anders1@gmail.com>
|
||||
# version 0.13: Fix a simple typo in an option description.
|
||||
# 2014-01-12, Phyks (Lucas Verney) <phyks@phyks.me>
|
||||
# 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 <hildenae@gmail.com>
|
||||
# version: 0.11: Fix reading of set_away
|
||||
# 2013-06-16, Renato Botelho <rbgarga@gmail.com>
|
||||
# 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 <david@upcs.at>
|
||||
# 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 <fixato@gmail.com>
|
||||
# version 0.8: add command on attach feature
|
||||
# 2010-05-07, Jani Kesänen <jani.kesanen@gmail.com>
|
||||
# version 0.7: add command on detach feature
|
||||
# 2010-03-07, Aron Griffis <agriffis@n01se.net>
|
||||
# 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 <bwinton@latte.ca>
|
||||
# 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 <xt@bash.no>
|
||||
# version 0.3: do not touch servers that are manually set away
|
||||
# 2009-11-27, xt <xt@bash.no>
|
||||
# version 0.2: code for TMUX from penryu
|
||||
# 2009-11-27, xt <xt@bash.no>
|
||||
# version 0.1: initial release
|
||||
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import time
|
||||
import weechat as w
|
||||
|
||||
|
||||
SCRIPT_NAME = 'tmux_smart_away'
|
||||
SCRIPT_AUTHOR = 'Brian S. Stephan <bss@incorporeal.org>'
|
||||
SCRIPT_VERSION = '0.17'
|
||||
SCRIPT_LICENSE = 'GPL3'
|
||||
SCRIPT_DESC = 'Set away status on tmux detach or lack of tmux focus'
|
||||
|
||||
SETTINGS = {
|
||||
'message': ('Not watching IRC', '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 tmux attached 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 tmux 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)'),
|
||||
'weechat_window_regex': ('weechat',
|
||||
'Regex matching the tmux window to count as focus (via list-windows)'),
|
||||
}
|
||||
|
||||
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,
|
||||
'tmux_smart_away_timer_cb', '')
|
||||
|
||||
|
||||
def tmux_smart_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 tmux_smart_away_timer_cb(buffer, args):
|
||||
'''Check if weechat's window in tmux is focused and update awayness.'''
|
||||
global AWAY, SOCK, CONNECTED_RELAY
|
||||
|
||||
set_away = w.config_string_to_boolean(w.config_get_plugin('set_away'))
|
||||
window_regex = w.config_get_plugin('weechat_window_regex')
|
||||
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 whether attached sessions are focusing on the window
|
||||
focused = False
|
||||
if attached:
|
||||
list_sessions = subprocess.Popen(['tmux', 'list-sessions'], stdout=subprocess.PIPE)
|
||||
active_sessions = subprocess.Popen(['grep', '-E', '\(attached\)$'],
|
||||
stdin=list_sessions.stdout, stdout=subprocess.PIPE)
|
||||
active_session_names = subprocess.Popen(['grep', '-o', '^[^:]\+'], stdin=active_sessions.stdout,
|
||||
stdout=subprocess.PIPE)
|
||||
list_sessions.stdout.close()
|
||||
active_sessions.stdout.close()
|
||||
out, err = active_session_names.communicate()
|
||||
tmux_sessions = out.decode('utf-8').split('\n')
|
||||
for tmux_session in tmux_sessions:
|
||||
session_name = tmux_session
|
||||
if session_name != '':
|
||||
window_list = subprocess.Popen(['tmux', 'list-windows', '-t', session_name],
|
||||
stdout=subprocess.PIPE)
|
||||
irc_window = subprocess.Popen(['grep', '-E', window_regex], stdin=window_list.stdout,
|
||||
stdout=subprocess.PIPE)
|
||||
active_window = subprocess.Popen(['grep', '-E', '\(active\)$'], stdin=irc_window.stdout,
|
||||
stdout=subprocess.PIPE)
|
||||
window_list.stdout.close()
|
||||
irc_window.stdout.close()
|
||||
out, err = active_window.communicate()
|
||||
if out:
|
||||
focused = True
|
||||
|
||||
# Check whether 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 ((focused and AWAY) or
|
||||
(check_relays and CONNECTED_RELAY and not focused and AWAY)):
|
||||
if not w.config_string_to_boolean(w.config_get_plugin('no_output')):
|
||||
w.prnt('', '{}: IRC focused. 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 focused and not AWAY:
|
||||
if not CONNECTED_RELAY:
|
||||
if (not w.config_string_to_boolean(
|
||||
w.config_get_plugin('no_output'))):
|
||||
w.prnt('', '{}: IRC defocused. 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 tmux.'''
|
||||
sock = None
|
||||
if '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),
|
||||
'tmux_smart_away_config_cb', '')
|
|
@ -0,0 +1,64 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2013 Brian S. Stephan <bss@incorporeal.org>
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
# Convenience commands for appending/prepending text to the channel topic
|
||||
# or other buffer title.
|
||||
#
|
||||
# 2013-02-16 v0.1
|
||||
# initial script, /topicpre
|
||||
|
||||
try:
|
||||
import weechat
|
||||
except:
|
||||
print("This script must be run under WeeChat.")
|
||||
print("Get WeeChat now at: http://www.weechat.org/")
|
||||
|
||||
SCRIPT_NAME = "topicutil"
|
||||
SCRIPT_AUTHOR = "Brian S. Stephan <bss@incorporeal.org>"
|
||||
SCRIPT_VERSION = "0.1"
|
||||
SCRIPT_LICENSE = "GPL3"
|
||||
SCRIPT_DESC = "Easily add text to the start of a channel topic"
|
||||
|
||||
settings = {
|
||||
'splitter': " | ",
|
||||
}
|
||||
|
||||
def topic_prepend_cb(data, buffer, args):
|
||||
"""Prepend some text to the current buffer's topic."""
|
||||
|
||||
if args:
|
||||
splitter = weechat.config_get_plugin('splitter')
|
||||
topic = weechat.buffer_get_string(buffer, 'title')
|
||||
if topic != "":
|
||||
new_topic = "{0:s}{1:s}{2:s}".format(''.join(args), splitter,
|
||||
topic)
|
||||
else:
|
||||
new_topic = "{0:s}".format(' '.join(args))
|
||||
|
||||
weechat.command(buffer, '/topic {0:s}'.format(new_topic))
|
||||
return weechat.WEECHAT_RC_OK
|
||||
|
||||
if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
|
||||
SCRIPT_DESC, "", ""):
|
||||
# initialize default settings
|
||||
for option, default in settings.items():
|
||||
if weechat.config_get_plugin(option) == "":
|
||||
weechat.config_set_plugin(option, default)
|
||||
|
||||
hook = weechat.hook_command("topicpre", topic_prepend_cb.__doc__,
|
||||
"<text>", "text: data to add to topic",
|
||||
'', 'topic_prepend_cb', '')
|
Loading…
Reference in New Issue