2012-08-23 16:20:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Nemubot is a modulable IRC bot, built around XML configuration files.
|
|
|
|
# Copyright (C) 2012 Mercier Pierre-Olivier
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import queue
|
|
|
|
import threading
|
2012-08-29 16:08:09 +00:00
|
|
|
import traceback
|
|
|
|
import sys
|
2012-08-23 16:20:45 +00:00
|
|
|
|
|
|
|
from message import Message
|
2012-08-30 23:34:58 +00:00
|
|
|
from response import Response
|
2012-08-23 16:20:45 +00:00
|
|
|
|
|
|
|
class Consumer(threading.Thread):
|
|
|
|
def __init__(self, context):
|
|
|
|
self.context = context
|
|
|
|
self.stop = False
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
while not self.stop:
|
2012-08-31 01:52:49 +00:00
|
|
|
(srv, raw, time, prvt, data) = self.context.msg_queue.get(True, 20)
|
2012-08-23 16:20:45 +00:00
|
|
|
|
2012-08-26 16:00:56 +00:00
|
|
|
# Create, parse and treat the message
|
2012-08-23 16:20:45 +00:00
|
|
|
try:
|
2012-08-26 16:00:56 +00:00
|
|
|
msg = Message(srv, raw, time, prvt)
|
2012-08-31 03:21:19 +00:00
|
|
|
res = msg.treat()
|
2012-08-23 16:20:45 +00:00
|
|
|
except:
|
|
|
|
print ("\033[1;31mERROR:\033[0m occurred during the "
|
2012-08-29 16:08:09 +00:00
|
|
|
"processing of the message: %s" % raw)
|
2012-08-23 16:20:45 +00:00
|
|
|
exc_type, exc_value, exc_traceback = sys.exc_info()
|
|
|
|
traceback.print_exception(exc_type, exc_value,
|
|
|
|
exc_traceback)
|
2012-08-30 13:29:11 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Send message
|
|
|
|
if res is not None:
|
|
|
|
if isinstance(res, list):
|
|
|
|
for r in res:
|
2012-08-30 23:34:58 +00:00
|
|
|
if isinstance(r, Response):
|
2012-08-31 01:52:49 +00:00
|
|
|
srv.send_response(r, data)
|
2012-08-30 23:34:58 +00:00
|
|
|
elif isinstance(res, Response):
|
2012-08-31 01:52:49 +00:00
|
|
|
srv.send_response(res, data)
|
2012-08-23 16:20:45 +00:00
|
|
|
|
2012-08-31 03:21:19 +00:00
|
|
|
# Inform that the message has been treated
|
|
|
|
srv.msg_treated(data)
|
|
|
|
|
2012-08-23 16:20:45 +00:00
|
|
|
except queue.Empty:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
self.context.msg_thrd_size -= 2
|