<commit_before>import hexchat
import re

__module_name__ = "AdFilter"
__module_author__ = "PDog"
__module_version__ = "0.1"
__module_description__ = "Move fserve advertisements to a separate tab"

# Customize the name of the tab to your liking
tab_name = "(Ads)"

omenserve_regex = re.compile(".*?Type:\s+\W[\w-]+\s+For\sMy\sList\sOf:\s+[\d,]+\s+Files\s+.*?Slots:\s+\d+/\d+\s+.*?Queued:\s+\d+\s+.*?Speed:\s+[\d,]+cps\s+.*?Next:\s+\w+\s+.*?Served:\s+[\d,]+\s+.*?List:\s+[A-Z][a-z]+\s+\w+\s+.*?Search:\s+[A-Z]{2,3}\s+.*?Mode:\s+\w+\s+.*?$")
bwi_regex = re.compile("^(\[BWI\])\sType\s+\W[\w-]+\s+to\sget\sthe\slist\sof\s+[\d,]+\s+files\s\([\d\.]+\s+[A-Z]+\)\.\s+Updated\son\s+[\d+-]+\s+[\d:]+\s+Total\sSent\(channel\):\s+[\d,]+\s+\([\d\.]+\s+[A-Z]+(\))$")

ad_lst = [omenserve_regex, bwi_regex]

def find_adtab():
	context = hexchat.find_context(channel=tab_name)
	if context == None:
		hexchat.command("NEWSERVER -noconnect {0}".format(tab_name))
		return hexchat.find_context(channel=tab_name)
	else:
		return context
		
def adfilter_cb(word, word_eol, userdata):

	word = [(word[i] if len(word) > i else "") for i in range(4)]

	global ad_lst
	channel = hexchat.get_info("channel")
	stripped_msg = hexchat.strip(word[1], -1, 3)
	
	for ad in ad_lst:
		if ad.match(stripped_msg):
			ad_context = find_adtab()
			ad_context.prnt("{0}\t\00318<{4}{3}{1}>\00399 {2}".format(channel, *word))
			return hexchat.EAT_ALL
		else:
			return hexchat.EAT_NONE
			
def unload_cb(userdata):
	for chan in hexchat.get_list("channels"):
		if chan.type == 1 and chan.channel == tab_name:
			ad_context = hexchat.find_context(channel=tab_name)
			ad_context.command("CLOSE")
	hexchat.prnt(__module_name__ + " version " + __module_version__ + " unloaded")

hexchat.hook_print("Channel Message", adfilter_cb)
hexchat.hook_unload(unload_cb)

hexchat.prnt(__module_name__ + " version " + __module_version__ + " loaded")<commit_msg>Update adfilter.py<commit_after>import hexchat
import re

__module_name__ = "AdFilter"
__module_author__ = "PDog"
__module_version__ = "0.1.1"
__module_description__ = "Move fserve advertisements to a separate tab"

# Add channels from which you would like to filter ads, e.g. channels = ["#freenode", "#defocus", "##linux"]
channels = ["#ebooks"]

# Customize the name of the tab to your liking
tab_name = "(Ads)"

omenserve_regex = re.compile(".*?Type:\s+\W[\w-]+\s+For\sMy\sList\sOf:\s+[\d,]+\s+Files\s+.*?Slots:\s+\d+/\d+\s+.*?Queued:\s+\d+\s+.*?Speed:\s+[\d,]+cps\s+.*?Next:\s+\w+\s+.*?Served:\s+[\d,]+\s+.*?List:\s+[A-Z][a-z]+\s+\w+\s+.*?Search:\s+[A-Z]{2,3}\s+.*?Mode:\s+\w+\s+.*?$")
bwi_regex = re.compile("^(\[BWI\])\sType\s+\W[\w-]+\s+to\sget\sthe\slist\sof\s+[\d,]+\s+files\s\([\d\.]+\s+[A-Z]+\)\.\s+Updated\son\s+[\d+-]+\s+[\d:]+\s+Total\sSent\(channel\):\s+[\d,]+\s+\([\d\.]+\s+[A-Z]+(\))$")

ad_lst = [omenserve_regex, bwi_regex]

def find_adtab():
	context = hexchat.find_context(channel=tab_name)
	if context == None:
		hexchat.command("NEWSERVER -noconnect {0}".format(tab_name))
		return hexchat.find_context(channel=tab_name)
	else:
		return context
		
def adfilter_cb(word, word_eol, userdata):

	word = [(word[i] if len(word) > i else "") for i in range(4)]

	global channels
	global ad_lst
	channel = hexchat.get_info("channel")
	stripped_msg = hexchat.strip(word[1], -1, 3)
	
	for ad in ad_lst:
		if ad.match(stripped_msg) and channel in channels:
			ad_context = find_adtab()
			ad_context.prnt("{0}\t\00318<{4}{3}{1}>\00399 {2}".format(channel, *word))
			return hexchat.EAT_ALL
			
def unload_cb(userdata):
	for chan in hexchat.get_list("channels"):
		if chan.type == 1 and chan.channel == tab_name:
			ad_context = hexchat.find_context(channel=tab_name)
			ad_context.command("CLOSE")
	hexchat.prnt(__module_name__ + " version " + __module_version__ + " unloaded")

hexchat.hook_print("Channel Message", adfilter_cb)
hexchat.hook_unload(unload_cb)

hexchat.prnt(__module_name__ + " version " + __module_version__ + " loaded")
{'fraction_non_alphanumeric': 0.22309247094492168, 'fraction_numerical': 0.012885295603840323, 'mean_word_length': 4.779562043795621, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5354290', 'n_tokens_mistral': 1755, 'n_tokens_neox': 1673, 'n_words': 252}