Created
July 20, 2015 01:50
-
-
Save Raimondi/d74ab85adbba81b0cce2 to your computer and use it in GitHub Desktop.
Keep a naughty list with weechat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# | |
# Copyright (c) 2015 by Israel Chauca F. <[email protected]> | |
# Copyright (c) 2014 by Filip H.F. "FiXato" Slagter <[email protected]> | |
# | |
# Naughty: a quick WeeChat script to mark users as "naughty" and display a | |
# warning behind what they post. | |
# | |
# 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/>. | |
# | |
# 2015-07-31: 0.1 initial release | |
# | |
# requires: WeeChat version 0.3.6 or higher | |
# | |
# Based on https://github.com/FiXato/weechat_scripts/blob/master/shutup.py | |
try: | |
import weechat,re | |
from random import choice | |
except Exception: | |
print "This script must be run under WeeChat." | |
print "Get WeeChat now at: http://www.weechat.org/" | |
quit() | |
SCRIPT_NAME = "naughty" | |
SCRIPT_AUTHOR = 'Israel Chauca F. <[email protected]>' | |
SCRIPT_VERSION = "0.1" | |
SCRIPT_LICENSE = "GPL" | |
SCRIPT_DESC = "Keep a naughty list to keep an eye on troublesome users. Message from users on the list will be marked with a tag." | |
OPTIONS = { | |
'tag' : ('naughty','Text used to mark messages as naughty.'), | |
'masks' : ('','Space-separated regular expressions that will be matched against the [email protected]. Any user matching will get their message marked as naughty. Can also include a comma-separated list of channels for every regular expression separated from the regexp by a colon. Prefix regexp with (?i) if you want it to be case insensitive. Example: "@\S+\.aol\.com$:#comcast,#AT&T (?i)!root@\S+" would mark messages in channels #comcast and #AT&T from users whose hosts end in *.aol.com, as well as all users who have any case variation of root as ident regardless of channel.'), | |
} | |
DEBUG = True | |
def get_tag(): | |
global OPTIONS | |
if OPTIONS['tag'] == '': | |
return "naughty" | |
return OPTIONS['tag'] | |
# Easily use weechat colors in the script | |
# text = substitute_colors('my text ${color:yellow}yellow${color:default} colored.') | |
# eval_expression(): to match ${color:nn} tags | |
regex_color=re.compile('\$\{color:([^\{\}]+)\}') | |
def substitute_colors(text): | |
global regex_color | |
if int(version) >= 0x00040200: | |
return weechat.string_eval_expression(text,{},{},{}) | |
# substitute colors in output | |
return re.sub(regex_color, lambda match: weechat.color(match.group(1)), text) | |
# ===================[ weechat options & description ]=================== | |
def init_options(): | |
for option,value in OPTIONS.items(): | |
if not weechat.config_is_set_plugin(option): | |
weechat.config_set_plugin(option, value[0]) | |
toggle_refresh(None, 'plugins.var.python.' + SCRIPT_NAME + '.' + option, value[0]) | |
else: | |
toggle_refresh(None, 'plugins.var.python.' + SCRIPT_NAME + '.' + option, weechat.config_get_plugin(option)) | |
weechat.config_set_desc_plugin(option, '%s (default: "%s")' % (value[1], value[0])) | |
def debug(str): | |
if DEBUG: | |
weechat.prnt("", str) | |
def update_naughty_masks(masks): | |
global naughty_masks | |
naughty_masks = [] | |
for mask in masks.split(): | |
if '#' in mask: | |
mask, chan = mask.split(':',1) | |
channels = [channel.lower() for channel in chan.split(',')] | |
else: | |
channels = [] | |
naughty_masks.append([mask, re.compile(mask), channels]) | |
debug('naughty masks: %s' % naughty_masks) | |
def toggle_refresh(pointer, name, value): | |
global OPTIONS | |
option = name[len('plugins.var.python.' + SCRIPT_NAME + '.'):] # get optionname | |
OPTIONS[option] = value # save new value | |
if option == 'masks': | |
update_naughty_masks(value) | |
return weechat.WEECHAT_RC_OK | |
def naughty_cb(data, modifier, modifier_data, string): | |
dict_in = { "message": string } | |
message_ht = weechat.info_get_hashtable("irc_message_parse", dict_in) | |
hostmask = message_ht['host'] | |
arguments = message_ht['arguments'] | |
channel = message_ht['channel'] | |
new_arguments = re.sub(r'^%s :' % channel, lambda x: '%s :%s' % (channel, "<naughty_marker>"), arguments) | |
new_string = re.sub(r'%s$' % re.escape(arguments), lambda x: new_arguments, string) | |
for key, mask_regexp, channels in naughty_masks: | |
debug('regexp is : %s' % mask_regexp.pattern) | |
# If there is one or more channels listed for this mask regexp, and none of them match the current channel, continue to the next mute mask | |
if len(channels) > 0 and channel.lower() not in channels: | |
debug("%s doesn't match any of the listed channels: %s" % (channel, channels)) | |
continue | |
# If the hostmask matches the mask regular expression, return the new, manipulated, string. | |
debug("comparing %s to %s" % (mask_regexp.pattern, hostmask)) | |
if mask_regexp.search(hostmask): | |
debug(" %s matches %s" % (mask_regexp.pattern, hostmask)) | |
#return new_string | |
return new_string | |
# Nothing matches, so return the original, unmodified, string | |
return string | |
def naughty_colorize_cb(data, modifier, modifier_data, string): | |
return re.sub(r'<naughty_marker>', '%s%s%s ' % (weechat.color("yellow"), get_tag(), weechat.color("chat")), string) | |
def naughty_command_cb(data, buffer, args): | |
debug("args: %s" % args) | |
masks = weechat.config_get_plugin('masks') | |
new_masks = '%s %s' % (masks, args) | |
debug('new_masks: %s' % new_masks) | |
rc = weechat.config_set_plugin('masks', new_masks) | |
if rc == weechat.WEECHAT_CONFIG_OPTION_SET_OK_CHANGED: | |
debug('option set ok changed') | |
elif rc == weechat.WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE: | |
debug('option set ok same value') | |
elif rc == weechat.WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND: | |
debug('option set option not found') | |
elif rc == weechat.WEECHAT_CONFIG_OPTION_SET_ERROR: | |
debug('option set error') | |
return weechat.WEECHAT_RC_OK | |
# ================================[ main ]=============================== | |
if __name__ == "__main__": | |
if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', ''): | |
version = weechat.info_get("version_number", "") or 0 | |
if int(version) >= 0x00030600: | |
# init options from your script | |
init_options() | |
# create a hook for your options | |
weechat.hook_config( 'plugins.var.python.' + SCRIPT_NAME + '.*', 'toggle_refresh', '' ) | |
else: | |
weechat.prnt("","%s%s %s" % (weechat.prefix("error"),SCRIPT_NAME,": needs version 0.3.6 or higher")) | |
weechat.command("","/wait 1ms /python unload %s" % SCRIPT_NAME) | |
hook = weechat.hook_modifier("irc_in_privmsg", "naughty_cb", "") | |
hook = weechat.hook_modifier("weechat_print", "naughty_colorize_cb", "") | |
hook = weechat.hook_command("naughty", "Add an item to the naughty list.", | |
"pattern[:channel1[,channel2[,...]]]", | |
"A pattern with an optional comma delimited list of channel names", | |
"%(nick)", "naughty_command_cb", "") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment