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.
 

43 lines
1.3 KiB

import 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._sock.bind(v5)
self._handler = Thread(target=self._handle)
self._callbacks = []
def start(self):
# Start v5 handler
self._handler.start()
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):
@wraps(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()
channels, message = data.split(":", 1)
channels = channels.split(" ")
for event, callback in self._callbacks:
if event(channels, message):
callback(channels, message)