summaryrefslogtreecommitdiffstats
path: root/plugin.py
diff options
context:
space:
mode:
authorGeorg2021-08-26 16:36:34 +0200
committerGeorg2021-08-26 16:36:34 +0200
commit7d9db9dd9d75b5a3aaff4617b95563125d9c1bdf (patch)
tree67bc708273d1c49f55ef1cdcd351c1ea272fd966 /plugin.py
parenta8665607bbef6f96955ef53e1c14a4aa85a1f8d8 (diff)
downloadsnoparser-7d9db9dd9d75b5a3aaff4617b95563125d9c1bdf.tar.gz
snoparser-7d9db9dd9d75b5a3aaff4617b95563125d9c1bdf.tar.bz2
snoparser-7d9db9dd9d75b5a3aaff4617b95563125d9c1bdf.zip
Moving Redis options to config values
Signed-off-by: Georg <georg@lysergic.dev>
Diffstat (limited to 'plugin.py')
-rw-r--r--plugin.py50
1 files changed, 35 insertions, 15 deletions
diff --git a/plugin.py b/plugin.py
index 997b2bd..39e2457 100644
--- a/plugin.py
+++ b/plugin.py
@@ -53,25 +53,27 @@ class SnoParser(callbacks.Plugin):
"""Parses the Server Notices from ErgoIRCd"""
threaded = True
- def redis_connect() -> redis.client.Redis:
+ def redis_connect(self) -> redis.client.Redis:
try:
redis_client = redis.Redis(
- host = .registryValue('whois.redis.host'),
+ host = self.registryValue('whois.redis.host'),
port = self.registryValue('whois.redis.port'),
password = self.registryValue('whois.redis.password'),
username = self.registryValue('whois.redis.username'),
db = self.registryValue('whois.redis.db'),
- socket_timeout = self.registryValue('whois.redis.timeout')
+ socket_timeout = int(self.registryValue('whois.redis.timeout'))
)
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(sourceip: str) -> dict:
+ def __init__(self, irc):
+ super().__init__(irc)
+ self.redis_client = self.redis_connect()
+
+ def whois_fresh(self, sourceip: str) -> dict:
"""Data from cache."""
asn = 0
subnet = ''
@@ -90,22 +92,26 @@ class SnoParser(callbacks.Plugin):
response = whoisout
return response
- def whois_get_cache(key: str) -> str:
+ def whois_get_cache(self, key: str) -> str:
"""Data from Redis."""
- val = SnoParser.redis_client.get(key)
+ k = self.redis_client.get(key)
+
+# self = SnoParser()
+# val = self.redis_client.get(key)
+ val = k
return val
- def whois_set_cache(key: str, value: str) -> bool:
+ def whois_set_cache(self, key: str, value: str) -> bool:
"""Data to Redis."""
- state = SnoParser.redis_client.setex(key, timedelta(seconds=3600), value=value,)
+ state = self.redis_client.setex(key, timedelta(seconds=3600), value=value,)
return state
- def whois_run(sourceip: str) -> dict:
+ def whois_run(self, sourceip: str) -> dict:
"""Whois query router."""
- data = SnoParser.whois_get_cache(key=sourceip)
+ data = self.whois_get_cache(key=sourceip)
if data is not None:
data = json.loads(data)
#data["cache"] = True
@@ -114,7 +120,7 @@ class SnoParser(callbacks.Plugin):
print(sourceip)
return data
else:
- data = SnoParser.whois_fresh(sourceip)
+ data = self.whois_fresh(sourceip)
print("DEBUG - ELSE WHOIS_FRESH CALLED")
print(data)
print(sourceip)
@@ -122,7 +128,7 @@ class SnoParser(callbacks.Plugin):
#data["cache"] = False
print("DEBUG - CACHE: FALSE")
data = json.dumps(data)
- state = SnoParser.whois_set_cache(key=sourceip, value=data)
+ state = self.whois_set_cache(key=sourceip, value=data)
print(data)
print(sourceip)
@@ -134,6 +140,20 @@ class SnoParser(callbacks.Plugin):
print(data)
return data
+ def query(self, irc, msg, args, ipaddress):
+ """<IP address>
+ Queries the cache for an address.
+ """
+
+ data = self.whois_get_cache(key=ipaddress)
+ ttl = self.redis_client.get(ipaddress)
+
+ print(data, ' ', ttl)
+ #irc.reply(str(data), ' Remaining: ', int(ttl), 's')
+ irc.reply(data, ttl)
+
+ query = wrap(query, ['anything'])
+
def doNotice(self, irc, msg):
(target, text) = msg.args
@@ -155,7 +175,7 @@ class SnoParser(callbacks.Plugin):
asn = 0
subnet = ''
- whoisout = SnoParser.whois_run(ip)
+ whoisout = self.whois_run(sourceip=ip)
DictFromSnotice = {'notice': 'connect', 'nickname': nickname, 'username': username, 'host': host, 'ip': ip, 'realname': realname, 'ipCount': ip_seen, 'nickCount': nick_seen}
#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']}"