Added better logging system
This commit is contained in:
parent
49339f8c7a
commit
ee3fc0a75f
52
irc.py
52
irc.py
|
@ -1,6 +1,6 @@
|
|||
# Manage the IRC layer of GLaDOS
|
||||
|
||||
import re, socket, ssl
|
||||
import logging, re, socket, ssl
|
||||
from functools import wraps
|
||||
from queue import Queue
|
||||
from threading import Thread
|
||||
|
@ -37,9 +37,12 @@ class IRC(object):
|
|||
""" Handle new messages """
|
||||
while True:
|
||||
message = self.receive()
|
||||
for event, callback in self._callbacks:
|
||||
if event(message):
|
||||
callback(message)
|
||||
logging.info(f"received {message}")
|
||||
if message is not None:
|
||||
for event, callback in self._callbacks:
|
||||
if event(message):
|
||||
logging.info(f"matched {event.__name__}")
|
||||
callback(message)
|
||||
|
||||
def send(self, target, message):
|
||||
""" Send a message to the specified target (channel or user) """
|
||||
|
@ -49,13 +52,15 @@ class IRC(object):
|
|||
""" Receive a private message """
|
||||
while True:
|
||||
message = self._recv()
|
||||
print(f"receive: {message}")
|
||||
if " PRIVMSG " in message:
|
||||
return Message(message)
|
||||
msg = Message(message)
|
||||
if msg:
|
||||
return msg
|
||||
|
||||
def join(self, channel):
|
||||
""" Join a channel """
|
||||
self._send(f"JOIN {channel}")
|
||||
logging.info(f"joined {channel}")
|
||||
|
||||
def on(self, event):
|
||||
""" Adds a callback to the IRC handler
|
||||
|
@ -67,6 +72,7 @@ class IRC(object):
|
|||
def wrapper(message):
|
||||
func(message)
|
||||
self._callbacks.append((event, wrapper))
|
||||
logging.info(f"added callback {func.__name__}")
|
||||
return wrapper
|
||||
|
||||
return callback
|
||||
|
@ -88,7 +94,7 @@ class IRC(object):
|
|||
# Or add a new message to inbox
|
||||
elif len(m):
|
||||
self._inbox.put(m)
|
||||
print(f"_handle: <{m}>")
|
||||
logging.debug(f"received {m}")
|
||||
|
||||
def _send(self, raw):
|
||||
""" Wrap and encode raw message to send """
|
||||
|
@ -107,28 +113,20 @@ class IRC(object):
|
|||
|
||||
|
||||
class Message(object):
|
||||
r = re.compile("^:(?P<author>[\w.~]+)(?:!(?P<host>\S+))? PRIVMSG (?P<to>\S+) :(?P<text>.+)")
|
||||
r = re.compile("^:(?P<author>[\w.~|]+)(?:!(?P<host>\S+))? PRIVMSG (?P<to>\S+) :(?P<text>.+)")
|
||||
|
||||
def __init__(self, raw):
|
||||
match = re.search(Message.r, raw)
|
||||
self.author = match.group("author")
|
||||
self.to = match.group("to")
|
||||
self.text = match.group("text")
|
||||
print(self)
|
||||
if match:
|
||||
self.author = match.group("author")
|
||||
self.to = match.group("to")
|
||||
self.text = match.group("text")
|
||||
logging.debug(f"sucessfully parsed {raw} into {self}")
|
||||
else:
|
||||
self.author = ""
|
||||
self.to = ""
|
||||
self.text = ""
|
||||
logging.warning(f"failed to parse {raw} into valid message")
|
||||
|
||||
def __str__(self):
|
||||
return f"<{self.author}> à <{self.to}> : {self.text}"
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
bot = IRC("irc.planet-casio.com", 6697)
|
||||
|
||||
@bot.on(lambda m: "Hello" in m.text)
|
||||
def hello(msg):
|
||||
bot.send(msg.to, f"Nice to see you {msg.author}!")
|
||||
|
||||
bot.start("glados", "abcdef123456")
|
||||
bot.join("#glados")
|
||||
|
||||
bot.run()
|
||||
return f"{self.author} to {self.to}: {self.text}"
|
||||
|
|
13
main.py
13
main.py
|
@ -1,20 +1,21 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import time
|
||||
from threading import Thread
|
||||
|
||||
import logging, re
|
||||
from bot import Bot
|
||||
|
||||
|
||||
LOG_FORMAT = "%(asctime)s [%(levelname)s] <%(filename)s> %(funcName)s: %(message)s"
|
||||
logging.basicConfig(format=LOG_FORMAT, level=logging.DEBUG)
|
||||
|
||||
glados = Bot(
|
||||
('irc.planet-casio.com', 6697),
|
||||
('127.0.0.1', 5555),
|
||||
["#general", "#glados"]
|
||||
)
|
||||
|
||||
@glados.irc.on(lambda m: "Hello" in m.text)
|
||||
@glados.irc.on(lambda m: re.match("bonjour glados", m.text, re.IGNORECASE))
|
||||
def say_hello(msg):
|
||||
glados.irc.send(msg.to, f"Nice to see you, {msg.author}")
|
||||
glados.irc.send(msg.to, f"Heureuse de vous revoir, {msg.author}")
|
||||
|
||||
@glados.v5.on(lambda c, m: True)
|
||||
def announce(channels, message):
|
||||
|
|
5
v5.py
5
v5.py
|
@ -1,4 +1,4 @@
|
|||
import socket
|
||||
import logging, socket
|
||||
from threading import Thread
|
||||
from functools import wraps
|
||||
|
||||
|
@ -15,6 +15,7 @@ class V5(object):
|
|||
def start(self):
|
||||
# Start v5 handler
|
||||
self._handler.start()
|
||||
logging.info("started")
|
||||
|
||||
def on(self, event):
|
||||
""" Adds a callback to the v5 handler
|
||||
|
@ -35,9 +36,11 @@ class V5(object):
|
|||
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)
|
||||
|
|
Loading…
Reference in New Issue