2021-06-19 12:38:43 +02:00
|
|
|
###
|
|
|
|
# Copyright (c) 2021, mogad0n
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
from supybot import utils, plugins, ircutils, callbacks, ircdb, conf, log, world, ircmsgs
|
|
|
|
from supybot.commands import *
|
|
|
|
try:
|
|
|
|
from supybot.i18n import PluginInternationalization
|
|
|
|
_ = PluginInternationalization('SnoParser')
|
|
|
|
except ImportError:
|
|
|
|
# Placeholder that allows to run the plugin on a bot
|
|
|
|
# without the i18n module
|
|
|
|
_ = lambda x: x
|
|
|
|
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
2021-08-18 11:39:38 +02:00
|
|
|
import sqlite3
|
2021-08-26 11:41:41 +02:00
|
|
|
import redis
|
|
|
|
import json
|
2021-08-26 12:50:25 +02:00
|
|
|
from datetime import timedelta
|
2021-08-26 11:41:41 +02:00
|
|
|
from ipwhois import IPWhois
|
|
|
|
import ipwhois
|
2021-06-19 12:38:43 +02:00
|
|
|
|
|
|
|
class SnoParser(callbacks.Plugin):
|
|
|
|
"""Parses the Server Notices from ErgoIRCd"""
|
|
|
|
threaded = True
|
|
|
|
|
2021-08-26 11:41:41 +02:00
|
|
|
def redis_connect() -> redis.client.Redis:
|
|
|
|
try:
|
|
|
|
redis_client = redis.Redis(
|
|
|
|
host="localhost",
|
|
|
|
port=6378,
|
|
|
|
#password="test",
|
|
|
|
db=0,
|
|
|
|
socket_timeout=5,
|
|
|
|
)
|
|
|
|
ping = redis_client.ping()
|
|
|
|
if ping is True:
|
|
|
|
return redis_client
|
|
|
|
except redis.AuthenticationError:
|
|
|
|
print("Could not authenticate to Redis backend.")
|
|
|
|
|
|
|
|
redis_client = redis_connect()
|
|
|
|
|
|
|
|
def whois_fresh(coordinates: str) -> dict:
|
|
|
|
"""Data from cache."""
|
|
|
|
asn = 0
|
|
|
|
subnet = ''
|
|
|
|
try:
|
|
|
|
whois = IPWhois(coordinates)
|
|
|
|
whoisres = whois.lookup_rdap(depth=1,retry_count=0)
|
|
|
|
results = whoisres
|
|
|
|
print(results)
|
|
|
|
asn = whoisres['asn_registry']
|
|
|
|
country = whoisres['asn_country_code']
|
|
|
|
description = whoisres['asn_description']
|
2021-08-26 12:50:25 +02:00
|
|
|
whoisout = 'WHOIS ' + asn + ' ' + country + ' ' + description
|
2021-08-26 11:41:41 +02:00
|
|
|
except ipwhois.exceptions.IPDefinedError:
|
|
|
|
whoisout = 'RFC 4291 (Local)'
|
|
|
|
|
2021-08-26 12:50:25 +02:00
|
|
|
# rr = f"{results}"
|
|
|
|
# response = rr
|
|
|
|
# return response.json()
|
|
|
|
# response = results
|
|
|
|
response = whoisout
|
|
|
|
return response
|
2021-08-26 11:41:41 +02:00
|
|
|
|
|
|
|
def whois_get_cache(key: str) -> str:
|
|
|
|
"""Data from Redis."""
|
|
|
|
|
|
|
|
val = SnoParser.redis_client.get(key)
|
|
|
|
return val
|
|
|
|
|
|
|
|
def whois_set_cache(key: str, value: str) -> bool:
|
|
|
|
"""Data to Redis."""
|
|
|
|
|
2021-08-26 12:50:25 +02:00
|
|
|
state = SnoParser.redis_client.setex(key, timedelta(seconds=3600), value=value,)
|
2021-08-26 11:41:41 +02:00
|
|
|
return state
|
|
|
|
|
|
|
|
def whois_run(coordinates: str) -> dict:
|
|
|
|
data = SnoParser.whois_get_cache(key=coordinates)
|
|
|
|
if data is not None:
|
|
|
|
data = json.loads(data)
|
2021-08-26 12:50:25 +02:00
|
|
|
#data["cache"] = True
|
|
|
|
print("DEBUG - CACHE: TRUE")
|
|
|
|
print(data)
|
|
|
|
print(coordinates)
|
2021-08-26 11:41:41 +02:00
|
|
|
return data
|
|
|
|
else:
|
|
|
|
data = SnoParser.whois_fresh(coordinates)
|
2021-08-26 12:50:25 +02:00
|
|
|
print("ELSE WHOIS_FRESH CALLED")
|
|
|
|
print(data)
|
|
|
|
print(coordinates)
|
|
|
|
if data.startswith("WHOIS"):
|
|
|
|
#data["cache"] = False
|
|
|
|
print("DEBUG - CACHE: FALSE")
|
2021-08-26 11:41:41 +02:00
|
|
|
data = json.dumps(data)
|
2021-08-26 12:50:25 +02:00
|
|
|
state = SnoParser.whois_set_cache(key=coordinates, value=data)
|
|
|
|
|
|
|
|
print(data)
|
|
|
|
print(coordinates)
|
2021-08-26 11:41:41 +02:00
|
|
|
|
|
|
|
if state is True:
|
|
|
|
return json.loads(data)
|
2021-08-26 12:50:25 +02:00
|
|
|
else:
|
|
|
|
print("Data does not start with correct string")
|
|
|
|
print(data)
|
2021-08-26 11:41:41 +02:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
2021-06-19 12:38:43 +02:00
|
|
|
def doNotice(self, irc, msg):
|
|
|
|
(target, text) = msg.args
|
2021-08-26 11:41:41 +02:00
|
|
|
|
2021-06-19 12:38:43 +02:00
|
|
|
if target == irc.nick:
|
2021-08-18 11:39:38 +02:00
|
|
|
# server notices CONNECT, KILL, XLINE, NICK, ACCOUNT
|
2021-06-19 12:38:43 +02:00
|
|
|
text = ircutils.stripFormatting(text)
|
|
|
|
if 'CONNECT' in text:
|
|
|
|
connregex = "^-CONNECT- Client connected \[(.+)\] \[u\:~(.+)\] \[h\:(.+)\] \[ip\:(.+)\] \[r\:(.+)\]$"
|
|
|
|
couple = re.match(connregex, text)
|
|
|
|
nickname = couple.group(1)
|
|
|
|
username = couple.group(2)
|
|
|
|
host = couple.group(3)
|
2021-08-26 11:41:41 +02:00
|
|
|
#ip = couple.group(4)
|
2021-08-26 12:50:25 +02:00
|
|
|
ip = '::1'
|
2021-06-19 12:38:43 +02:00
|
|
|
realname = couple.group(5)
|
|
|
|
ip_seen = 0
|
|
|
|
nick_seen = 0
|
2021-08-26 11:41:41 +02:00
|
|
|
asn = 0
|
|
|
|
subnet = ''
|
|
|
|
|
2021-08-26 12:50:25 +02:00
|
|
|
# SnoParser.whois_run(coordinates=ip)
|
|
|
|
whoisout = SnoParser.whois_run(ip)
|
|
|
|
# whoisout = SnoParser.whois_run(whoisout)
|
|
|
|
# SnoParser.whois_run(ip)
|
|
|
|
# whoisout = SnoParser.whois_run.coordinates
|
2021-08-26 11:41:41 +02:00
|
|
|
|
|
|
|
# try:
|
|
|
|
# #whois = IPWhois(ip)
|
|
|
|
# #whoisres = whois.lookup_rdap(depth=1,retry_count=0)
|
|
|
|
# #results = whoisres
|
|
|
|
# whoisres = data
|
|
|
|
# print(results)
|
|
|
|
# asn = whoisres['asn_registry']
|
|
|
|
# country = whoisres['asn_country_code']
|
|
|
|
# description = whoisres['asn_description']
|
|
|
|
# whoisout = asn + ' ' + country + ' ' + description
|
|
|
|
# except ipwhois.exceptions.IPDefinedError:
|
|
|
|
# whoisout = 'RFC 4291 (Local)'
|
2021-06-19 12:38:43 +02:00
|
|
|
DictFromSnotice = {'notice': 'connect', 'nickname': nickname, 'username': username, 'host': host, 'ip': ip, 'realname': realname, 'ipCount': ip_seen, 'nickCount': nick_seen}
|
2021-08-26 11:41:41 +02:00
|
|
|
#repl = f"\x02\x1FNOTICE: {DictFromSnotice['notice']} \x0F\x11\x0303==>>\x0F \x02Nick:\x0F {DictFromSnotice['nickname']} \x02Username:\x0F {DictFromSnotice['username']} \x02Hostname:\x0F {DictFromSnotice['host']} \x02IP:\x0F {DictFromSnotice['ip']} \x02Realname:\x0F {DictFromSnotice['realname']} \x02IPcount:\x0F {DictFromSnotice['ipCount']} \x02NickCount:\x0F {DictFromSnotice['nickCount']}"
|
|
|
|
repl = f"\x02\x1F{DictFromSnotice['notice']} \x0F\x11\x0303==>>\x0F \x02Nick:\x0F {DictFromSnotice['nickname']} \x02Username:\x0F {DictFromSnotice['username']} \x02Hostname:\x0F {DictFromSnotice['host']} \x02IP:\x0F {DictFromSnotice['ip']} {whoisout} \x02Realname:\x0F {DictFromSnotice['realname']} \x02IPcount:\x0F {DictFromSnotice['ipCount']} \x02NickCount:\x0F {DictFromSnotice['nickCount']}"
|
|
|
|
|
2021-06-19 12:38:43 +02:00
|
|
|
self._sendSnotice(irc, msg, repl)
|
|
|
|
if 'XLINE' in text and 'temporary' in text:
|
|
|
|
xlineregex = "^-XLINE- (.+) \[(.+)\] added temporary \((.+)\) (K-Line|D-Line) for (.+)$"
|
|
|
|
couple = re.match(xlineregex, text)
|
|
|
|
who = couple.group(1)
|
|
|
|
who_operator = couple.group(2)
|
|
|
|
duration = couple.group(3)
|
|
|
|
which_line = couple.group(4)
|
|
|
|
host_or_ip = couple.group(5)
|
|
|
|
DictFromSnotice = {'notice': 'tempban', 'who': who, 'operator': who_operator, 'duration': duration, 'type': which_line, 'target': host_or_ip}
|
|
|
|
repl = f"\x02\x1FNOTICE: {DictFromSnotice['notice']}\x0F \x11\x0303 X_X \x0F \x02BannedBy:\x0F {DictFromSnotice['who']} \x02BannedByOper:\x0F {DictFromSnotice['operator']} \x02Duration:\x0F {DictFromSnotice['duration']} \x02XLINE Type:\x0F {DictFromSnotice['type']} \x02Nick:\x0F {DictFromSnotice['target']}"
|
|
|
|
self._sendSnotice(irc, msg, repl)
|
|
|
|
# WHY THE FUCK IS IT elif ??
|
|
|
|
elif 'XLINE' in text and 'temporary' not in text and 'removed' not in text:
|
|
|
|
perm_xline_regex = "^-XLINE- (.+) \[(.+)\] added (D-Line|K-Line) for (.+)$"
|
|
|
|
couple = re.match(perm_xline_regex, text)
|
|
|
|
who = couple.group(1)
|
|
|
|
who_operator = couple.group(2)
|
|
|
|
which_line = couple.group(3)
|
|
|
|
host_or_ip = couple.group(4)
|
|
|
|
DictFromSnotice = {'notice': 'Permaban', 'who': who, 'operator': who_operator, 'type': which_line, 'target': host_or_ip}
|
|
|
|
repl = f"\x02\x1FNOTICE: {DictFromSnotice['notice']} \x0F \x11\x0303 X_X \x0F \x02BannedBy:\x0F {DictFromSnotice['who']} \x02BannedByOper:\x0F {DictFromSnotice['operator']} \x02XLINE Type:\x0F {DictFromSnotice['type']} \x02Host/IP:\x0F {DictFromSnotice['target']}"
|
|
|
|
self._sendSnotice(irc, msg, repl)
|
|
|
|
elif 'XLINE' in text and 'removed' in text:
|
|
|
|
unxlineregex = "^-XLINE- (.+) removed (D-Line|K-Line) for (.+)$"
|
|
|
|
couple = re.match(unxlineregex, text)
|
|
|
|
who = couple.group(1)
|
|
|
|
which_line = couple.group(2)
|
|
|
|
host_or_ip = couple.group(3)
|
|
|
|
DictFromSnotice = {'notice': 'unxline', 'who': who, 'type': which_line, 'target': host_or_ip}
|
|
|
|
repl = f"\x02\x1FNOTICE: {DictFromSnotice['notice']} \x0F\x11\x0303 :=D\x0F \x02UnbannedBy:\x0F {DictFromSnotice['who']} \x02XLINE type:\x0F {DictFromSnotice['type']} \x02Host/IP:\x0F {DictFromSnotice['target']}"
|
|
|
|
self._sendSnotice(irc, msg, repl)
|
|
|
|
if 'KILL' in text:
|
|
|
|
killregex = "^-KILL- (.+) \[(.+)\] killed (\d) clients with a (KLINE|DLINE) \[(.+)\]$"
|
|
|
|
couple = re.match(killregex, text)
|
|
|
|
who = couple.group(1)
|
|
|
|
who_operator = couple.group(2)
|
|
|
|
clients = couple.group(3)
|
|
|
|
which_line = couple.group(4)
|
|
|
|
nick = couple.group(5)
|
|
|
|
DictFromSnotice = {'notice': 'kill', 'who': who, 'operator': who_operator, "client": clients, 'type': which_line, 'nick': nick}
|
|
|
|
repl = f"\x02\x1FNOTICE: {DictFromSnotice['notice']} \x0F\x11\x0303☠\x0F \x02KilledBy:\x0F {DictFromSnotice['who']} \x02KilledByOper:\x0F {DictFromSnotice['operator']} \x02NumofClientsAffected:\x0F {DictFromSnotice['client']} \x02XLINE Type:\x0F {DictFromSnotice['type']} \x02Nick:\x0F {DictFromSnotice['nick']}"
|
|
|
|
self._sendSnotice(irc, msg, repl)
|
|
|
|
if 'NICK' in text and 'changed nickname to' in text:
|
|
|
|
nickregex = "^-NICK- (.+) changed nickname to (.+)$"
|
|
|
|
couple = re.match(nickregex, text)
|
|
|
|
old_nick = couple.group(1)
|
|
|
|
new_nick = couple.group(2)
|
|
|
|
DictFromSnotice = {'notice': 'nick change', 'old_nick': old_nick, 'new_nick': new_nick}
|
|
|
|
repl = f"\x02\x1FNOTICE: {DictFromSnotice['notice']} ==> {DictFromSnotice['old_nick']} changed their nick to {DictFromSnotice['new_nick']}"
|
|
|
|
self._sendSnotice(irc, msg, repl)
|
|
|
|
if 'QUIT' in text and 'exited' in text:
|
|
|
|
quitregex = "^-QUIT- (.+) exited the network$"
|
|
|
|
couple = re.match(quitregex, text)
|
|
|
|
nick = couple.group(1)
|
2021-08-20 10:31:27 +02:00
|
|
|
DictFromSnotice = {'notice': 'quit', 'nick': nick}
|
2021-06-19 12:38:43 +02:00
|
|
|
repl = f"\x02\x1FNOTICE: quit nick: {nick} has exited the network"
|
|
|
|
self._sendSnotice(irc, msg, repl)
|
2021-08-20 10:31:27 +02:00
|
|
|
if 'ACCOUNT' in text and 'registered account' in text:
|
|
|
|
accregex = "^-ACCOUNT- Client \[(.*)\] registered account \[(.*)\] from IP (.*)$"
|
|
|
|
couple = re.match(accregex, text)
|
|
|
|
hostmask = couple.group(1)
|
|
|
|
account = couple.group(2)
|
|
|
|
ip = couple.group(3)
|
|
|
|
DictFromSnotice = {'notice': 'accreg', 'hostmask': hostmask, 'account': account, 'ip': ip}
|
|
|
|
repl = f"\x02\x1FNOTICE: accreg -> [{account}] was registered by hostmask [{hostmask}] from IP {ip}"
|
2021-08-20 13:44:44 +02:00
|
|
|
|
|
|
|
# Trigger HS SET
|
|
|
|
self._setvhost(irc, msg, account)
|
|
|
|
|
2021-08-20 10:31:27 +02:00
|
|
|
self._sendSnotice(irc, msg, repl)
|
|
|
|
if 'ACCOUNT' in text and 'registered account' in text and 'SAREGISTER' in text:
|
|
|
|
accregex = "^-ACCOUNT- Operator \[(.*)\] registered account \[(.*)\] with SAREGISTER$"
|
|
|
|
couple = re.match(accregex, text)
|
|
|
|
oper = couple.group(1)
|
|
|
|
account = couple.group(2)
|
|
|
|
DictFromSnotice = {'notice': 'sareg', 'oper': oper, 'account': account}
|
|
|
|
repl = f"\x02\x1FNOTICE: sareg -> [{account}] was registered by operator [{oper}]"
|
2021-08-20 13:44:44 +02:00
|
|
|
|
|
|
|
self._setvhost(irc, msg, account)
|
2021-08-20 10:31:27 +02:00
|
|
|
self._sendSnotice(irc, msg, repl)
|
2021-06-19 12:38:43 +02:00
|
|
|
|
2021-08-20 13:44:44 +02:00
|
|
|
|
|
|
|
# Post Registration
|
2021-06-19 12:38:43 +02:00
|
|
|
|
2021-08-20 13:44:44 +02:00
|
|
|
def _setvhost(self, irc, msg, account):
|
|
|
|
arg = ['SET']
|
|
|
|
arg.append(account)
|
|
|
|
vhost = self.registryValue('AutoVhost')
|
|
|
|
arg.append(f'{vhost}{account}')
|
|
|
|
irc.sendMsg(msg=ircmsgs.IrcMsg(command='HS',
|
|
|
|
args=arg))
|
|
|
|
|
|
|
|
|
|
|
|
# Send formatted SNO to channel
|
2021-06-19 12:38:43 +02:00
|
|
|
|
|
|
|
def _sendSnotice(self, irc, msg, repl):
|
2021-08-20 13:44:44 +02:00
|
|
|
try:
|
|
|
|
channel = self.registryValue('targetChannel')
|
|
|
|
if channel[0] == '#':
|
|
|
|
irc.queueMsg(msg=ircmsgs.IrcMsg(command='NOTICE',
|
|
|
|
args=(channel, repl)))
|
|
|
|
# what sort of exception does one raise
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2021-06-19 12:38:43 +02:00
|
|
|
|
|
|
|
Class = SnoParser
|
|
|
|
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|