dotfiles/weechat/.weechat/python/autojoin.py

207 lines
6.7 KiB
Python
Raw Normal View History

2018-07-22 19:39:58 +00:00
# -*- coding: utf-8 -*-
#
# Copyright (c) 2009 by xt <xt@bash.no>
#
# 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:
# 2009-06-18, xt <xt@bash.no>
# version 0.1: initial release
#
# 2009-10-18, LBo <leon@tim-online.nl>
# version 0.2: added autosaving of join channels
# /set plugins.var.python.autojoin.autosave 'on'
#
# 2009-10-19, LBo <leon@tim-online.nl>
# version 0.2.1: now only responds to part messages from self
# find_channels() only returns join'ed channels, not all the buffers
# updated description and docs
#
# 2009-10-20, LBo <leon@tim-online.nl>
# version 0.2.2: fixed quit callback
# removed the callbacks on part & join messages
#
# 2012-04-14, Filip H.F. "FiXato" Slagter <fixato+weechat+autojoin@gmail.com>
# version 0.2.3: fixed bug with buffers of which short names were changed.
# Now using 'name' from irc_channel infolist.
# version 0.2.4: Added support for key-protected channels
#
# 2014-05-22, Nathaniel Wesley Filardo <PADEBR2M2JIQN02N9OO5JM0CTN8K689P@cmx.ietfng.org>
# version 0.2.5: Fix keyed channel support
#
2018-08-09 00:20:19 +00:00
# 2016-01-13, The fox in the shell <KellerFuchs@hashbang.sh>
# version 0.2.6: Support keeping chan list as secured data
#
2018-08-15 04:59:51 +00:00
# 2018-08-09, Julien Palard <julien@palard.fr>
# version 0.3.0: Support for Python 3
2019-10-24 00:49:15 +00:00
#
# 2019-09-28, fructose
# version 0.3.1: Error on invalid arguments
#
2018-07-22 19:39:58 +00:00
# @TODO: add options to ignore certain buffers
# @TODO: maybe add an option to enable autosaving on part/join messages
import weechat as w
import re
SCRIPT_NAME = "autojoin"
SCRIPT_AUTHOR = "xt <xt@bash.no>"
2019-10-24 00:49:15 +00:00
SCRIPT_VERSION = "0.3.1"
2018-07-22 19:39:58 +00:00
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC = "Configure autojoin for all servers according to currently joined channels"
SCRIPT_COMMAND = "autojoin"
# script options
settings = {
"autosave": "off",
}
if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""):
w.hook_command(SCRIPT_COMMAND,
SCRIPT_DESC,
"[--run]",
" --run: actually run the commands instead of displaying\n",
"--run",
"autojoin_cb",
"")
2019-10-24 00:49:15 +00:00
# w.hook_signal('*,irc_in2_join', 'autosave_channels_on_activity', '')
# w.hook_signal('*,irc_in2_part', 'autosave_channels_on_activity', '')
2018-07-22 19:39:58 +00:00
w.hook_signal('quit', 'autosave_channels_on_quit', '')
# Init everything
for option, default_value in settings.items():
if w.config_get_plugin(option) == "":
w.config_set_plugin(option, default_value)
2019-10-24 00:49:15 +00:00
2018-07-22 19:39:58 +00:00
def autosave_channels_on_quit(signal, callback, callback_data):
''' Autojoin current channels '''
if w.config_get_plugin(option) != "on":
return w.WEECHAT_RC_OK
items = find_channels()
# print/execute commands
2018-08-15 04:59:51 +00:00
for server, channels in items.items():
2018-08-09 00:20:19 +00:00
process_server(server, channels)
2018-07-22 19:39:58 +00:00
return w.WEECHAT_RC_OK
def autosave_channels_on_activity(signal, callback, callback_data):
''' Autojoin current channels '''
if w.config_get_plugin(option) != "on":
return w.WEECHAT_RC_OK
items = find_channels()
# print/execute commands
2018-08-15 04:59:51 +00:00
for server, channels in items.items():
2018-07-22 19:39:58 +00:00
nick = w.info_get('irc_nick', server)
pattern = "^:%s!.*(JOIN|PART) :?(#[^ ]*)( :.*$)?" % nick
match = re.match(pattern, callback_data)
2019-10-24 00:49:15 +00:00
if match: # check if nick is my nick. In that case: save
2018-08-09 00:20:19 +00:00
process_server(server, channels)
2019-10-24 00:49:15 +00:00
else: # someone else: ignore
2018-07-22 19:39:58 +00:00
continue
return w.WEECHAT_RC_OK
2019-10-24 00:49:15 +00:00
def autojoin_cb(data, buffer, args):
2018-08-09 00:20:19 +00:00
if args == '--run':
run = True
2019-10-24 00:49:15 +00:00
elif args != '':
w.prnt('', 'Unexpected argument: %s' % args)
return w.WEECHAT_RC_ERROR
2018-08-09 00:20:19 +00:00
else:
run = False
2019-10-24 00:49:15 +00:00
# Old behaviour: doesn't save empty channel list
# In fact should also save open buffers with a /part'ed channel
# But I can't believe somebody would want that behaviour
items = find_channels()
2018-07-22 19:39:58 +00:00
# print/execute commands
2018-08-15 04:59:51 +00:00
for server, channels in items.items():
2018-08-09 00:20:19 +00:00
process_server(server, channels, run)
return w.WEECHAT_RC_OK
2019-10-24 00:49:15 +00:00
2018-08-09 00:20:19 +00:00
def process_server(server, channels, run=True):
2019-10-24 00:49:15 +00:00
option = "irc.server.%s.autojoin" % server
channels = channels.rstrip(',')
oldchans = w.config_string(w.config_get(option))
if not channels: # empty channel list
return
# Note: re already caches the result of regexp compilation
sec = re.match('^\${sec\.data\.(.*)}$', oldchans)
if sec:
secvar = sec.group(1)
command = "/secure set %s %s" % (secvar, channels)
else:
command = "/set irc.server.%s.autojoin '%s'" % (server, channels)
if run:
w.command('', command)
else:
w.prnt('', command)
2018-07-22 19:39:58 +00:00
def find_channels():
"""Return list of servers and channels"""
2019-10-24 00:49:15 +00:00
# TODO: make it return a dict with more options like "nicks_count etc."
2018-07-22 19:39:58 +00:00
items = {}
infolist = w.infolist_get('irc_server', '', '')
# populate servers
while w.infolist_next(infolist):
items[w.infolist_string(infolist, 'name')] = ''
w.infolist_free(infolist)
# populate channels per server
for server in items.keys():
keys = []
keyed_channels = []
unkeyed_channels = []
2019-10-24 00:49:15 +00:00
items[server] = '' # init if connected but no channels
2018-07-22 19:39:58 +00:00
infolist = w.infolist_get('irc_channel', '', server)
while w.infolist_next(infolist):
if w.infolist_integer(infolist, 'nicks_count') == 0:
2019-10-24 00:49:15 +00:00
# parted but still open in a buffer: bit hackish
2018-07-22 19:39:58 +00:00
continue
if w.infolist_integer(infolist, 'type') == 0:
key = w.infolist_string(infolist, "key")
if len(key) > 0:
keys.append(key)
keyed_channels.append(w.infolist_string(infolist, "name"))
2019-10-24 00:49:15 +00:00
else:
2018-07-22 19:39:58 +00:00
unkeyed_channels.append(w.infolist_string(infolist, "name"))
items[server] = ','.join(keyed_channels + unkeyed_channels)
if len(keys) > 0:
items[server] += ' %s' % ','.join(keys)
w.infolist_free(infolist)
return items