Well, you found me. Congratulations. Was it worth it?
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.4 KiB

import logging, socket
from threading import Thread
from functools import wraps
class V5(object):
def __init__(self, v5, irc):
""" Initialize v5 handle
:irc <IRC>: an initialized IRC object """
self.irc = irc
self._sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
self._handler = Thread(target=self._handle)
self._callbacks = []
def start(self):
# Start v5 handler
def on(self, event):
""" Adds a callback to the v5 handler
Event is a function taking in parameter a list of channels and a
string, and return True if the callback should be executed """
def callback(func):
def wrapper(channels, message):
func(channels, message)
self._callbacks.append((event, wrapper))
return wrapper
return callback
def _handle(self):
while True:
data, addr = self._sock.recvfrom(4096)
data = data.decode()
logging.debug(f"received {data}")
channels, message = data.split(":", 1)
channels = channels.split(" ")
for event, callback in self._callbacks:
if event(channels, message):
logging.info(f"passed {event.__name__}")
callback(channels, message)