Skip to content

Instantly share code, notes, and snippets.

@hagb4rd
Last active April 9, 2019 23:17
Show Gist options
  • Save hagb4rd/32ef361b371ea65f33567775fe0befe8 to your computer and use it in GitHub Desktop.
Save hagb4rd/32ef361b371ea65f33567775fe0befe8 to your computer and use it in GitHub Desktop.
A hexchat addon providing a way to whitelist users (/add nick). anyone else will be muted in channels added to (/safe) is muted.
# 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 2 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Copyright (C) 2009 Steven Pigeon,
#
# based on earlier version by
#
# Copyright (C) 2004 Marduk Enterprises <[email protected]>
#
# minor fixes (in addition to changing message type from
# msg highlight to local console print)
#
# Copyright (C) 2019 earendel
#
# whitelist
import xchat
import anydbm as gdbm
import os
from itertools import dropwhile
import string
import re
__module_name__ = 'hexchat-whitelist'
__module_version__ = '0.951'
__module_description__ = """With this plug-in, you can prevent some people
from getting your attention."""
xchat.prnt('%(name)s, version %(version)s' % {'name': __module_name__,
'version': __module_version__})
DBFILE = os.environ['HOME'] + '/Documents/hexchat-whitelist.anydb'
SEPARATOR=','
# loads the database
db = gdbm.open(DBFILE,'c')
try:
nicks = db['soft-ignore'].split(SEPARATOR)
except KeyError:
db['soft-ignore'] = ''
nicks = []
try:
CHANNELS = db['channels-ignore'].split(SEPARATOR)
except KeyError:
db['channels-ignore'] = ''
CHANNELS = []
def change_mode(word, word_eol, userdata):
print(userdata)
if xchat.get_info('channel') in CHANNELS:
CHANNELS.remove(xchat.get_info('channel'))
db['channels-ignore']=SEPARATOR.join(CHANNELS)
print('%(channel)s removed from safe list.' % {'channel': xchat.get_info('channel')})
else:
CHANNELS.append(xchat.get_info('channel'))
db['channels-ignore']=SEPARATOR.join(CHANNELS)
print('%(channel)s added to safe list.' % {'channel': xchat.get_info('channel')})
# this function strops the colors
# from the nicknames in a safe(r) fashion
def remove_color( nick ):
# 0 is x3
# then follows 1 or 2 numbers
return "".join(list(dropwhile( lambda x : x.isdigit(), nick[1:] )))
# commit changes to database
#
def save_nicks():
nicks.sort()
db['soft-ignore'] = SEPARATOR.join(nicks)
# adds a pattern/nick to the list, and
# prints the list if no pattern/nick is
# supplied.
#
def add_nick(word, word_eol, userdata):
if len(word) == 1:
return white_list(word, word_eol, userdata)
for nick in word[1:]:
if nick not in nicks:
try:
re.compile(nick)
except:
xchat.prnt('\x032* \x034 %s \x032 is not a valid PCRE' % nick)
else:
nicks.append(nick)
xchat.prnt('\x032* %s will be whitelisted' % nick)
else:
xchat.prnt('\x032* %s is already being whitelisted' % nick)
save_nicks()
return xchat.EAT_XCHAT
# shows soft-ignore-list
#
def white_list(word, word_eol, userdata):
xchat.prnt('\x032Currently %d users whitelisted.' % len(nicks) )
for nick in nicks:
xchat.prnt('\x032 --- %s' % nick)
xchat.prnt('\x032* End of whitelist')
return xchat.EAT_XCHAT
# removes a pattern/nick from the list
# or prints a colorful error if pattern/nick
# is not in the list
#
def delete_nick(word, word_eol, userdata):
for nick in word[1:]:
if nick in nicks:
nicks.remove(nick)
xchat.prnt('\x032* %s is removed from whitelist' % nick)
else:
xchat.prnt('\x032* \x034 %s \x032 is not in whitelist' % nick)
save_nicks()
return xchat.EAT_XCHAT
# filter message according to pattern/nicks
#
def ignore_message(word, word_eol, userdata):
if xchat.get_info('channel') in CHANNELS:
nick = remove_color(word[0]).lower().split("!")[0] # skips the initial coloring
#print('[ nick: %s ]' % nick)
if(nick in nicks):
#print('[ nick: %s ]' % nick)
#should be more or less like the normal 'Channel Message' display
#print('[ OK ]')
#xchat.prnt('\x0314 <%s>\t%s' % (nick,word[1]))
#return xchat.EAT_XCHAT
return xchat.EAT_NONE
else:
#print('[ NO ]')
return xchat.EAT_ALL
else:
return xchat.EAT_NONE
xchat.hook_command('add', add_nick)
xchat.hook_command('whitelist', white_list)
xchat.hook_command('del', delete_nick)
xchat.hook_command('safe', change_mode)
# here we wanna hook in to the channel message event
xchat.hook_server("PRIVMSG", ignore_message)
#xchat.hook_print("Channel Message", ignore_message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment