Let consumer parse the message instead of server

This commit is contained in:
nemunaire 2015-07-14 00:48:56 +02:00
parent a1ac7d480d
commit d269468287
4 changed files with 34 additions and 7 deletions

View File

@ -423,9 +423,14 @@ class Bot(threading.Thread):
return False
def receive_message(self, srv, msg, private=False, data=None):
"""Queued the message for treatment"""
#print("READ", raw_msg)
def receive_message(self, srv, msg):
"""Queued the message for treatment
Arguments:
srv -- The server where the message comes from
msg -- The message not parsed, as simple as possible
"""
self.cnsr_queue.put_nowait(MessageConsumer(srv, msg))
# Launch a new thread if necessary

View File

@ -30,7 +30,8 @@ class MessageConsumer:
def __init__(self, srv, msg):
self.srv = srv
self.msgs = [ msg ]
self.orig = msg
self.msgs = [ ]
self.responses = None
@ -145,6 +146,16 @@ class MessageConsumer:
def run(self, context):
"""Create, parse and treat the message"""
try:
for msg in self.srv.parse(self.orig):
self.msgs.append(msg)
except:
logger.exception("Error occurred during the processing of the %s: "
"%s", type(self.msgs[0]).__name__, self.msgs[0])
if len(self.msgs) <= 0:
return
try:
for msg in self.msgs:
self.first_treat(msg)

View File

@ -257,11 +257,16 @@ class IRC(SocketServer):
def read(self):
for line in SocketServer.read(self):
# PING should be handled here, so start parsing here :/
msg = IRCMessage(line, self.encoding)
if msg.cmd in self.hookscmd:
self.hookscmd[msg.cmd](msg)
mes = msg.to_bot_message(self)
if mes is not None:
yield mes
yield msg
def parse(self, msg):
mes = msg.to_bot_message(self)
if mes is not None:
yield mes

View File

@ -139,6 +139,12 @@ class AbstractServer(io.IOBase):
self.write(vprnt.pp)
# Read
def parse(self, msg):
raise NotImplemented
# Exceptions
def exception(self):