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

3 months ago
  1. import socket
  2. from threading import Thread
  3. from functools import wraps
  4. class V5(object):
  5. def __init__(self, v5, irc):
  6. """ Initialize v5 handle
  7. :irc <IRC>: an initialized IRC object """
  8. self.irc = irc
  9. self._sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  10. self._sock.bind(v5)
  11. self._handler = Thread(target=self._handle)
  12. self._callbacks = []
  13. def start(self):
  14. # Start v5 handler
  15. self._handler.start()
  16. def on(self, event):
  17. """ Adds a callback to the v5 handler
  18. Event is a function taking in parameter a list of channels and a
  19. string, and return True if the callback should be executed """
  20. def callback(func):
  21. @wraps(func)
  22. def wrapper(channels, message):
  23. func(channels, message)
  24. self._callbacks.append((event, wrapper))
  25. return wrapper
  26. return callback
  27. def _handle(self):
  28. while True:
  29. data, addr = self._sock.recvfrom(4096)
  30. data = data.decode()
  31. channels, message = data.split(":", 1)
  32. channels = channels.split(" ")
  33. for event, callback in self._callbacks:
  34. if event(channels, message):
  35. callback(channels, message)