2015-01-03 19:17:46 +00:00
|
|
|
# Nemubot is a smart and modulable IM bot.
|
2016-05-16 15:35:24 +00:00
|
|
|
# Copyright (C) 2012-2016 Mercier Pierre-Olivier
|
2012-08-23 16:20:45 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2014-08-14 10:49:38 +00:00
|
|
|
import logging
|
2012-08-23 16:20:45 +00:00
|
|
|
import queue
|
|
|
|
import threading
|
|
|
|
|
2014-08-27 05:57:00 +00:00
|
|
|
logger = logging.getLogger("nemubot.consumer")
|
2014-08-14 10:49:38 +00:00
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
|
2012-10-04 11:36:25 +00:00
|
|
|
class MessageConsumer:
|
2014-08-29 14:33:45 +00:00
|
|
|
|
2012-10-04 11:36:25 +00:00
|
|
|
"""Store a message before treating"""
|
2014-08-29 14:33:45 +00:00
|
|
|
|
2014-09-11 15:23:07 +00:00
|
|
|
def __init__(self, srv, msg):
|
2012-10-04 11:36:25 +00:00
|
|
|
self.srv = srv
|
2015-07-13 22:48:56 +00:00
|
|
|
self.orig = msg
|
2014-09-01 17:21:54 +00:00
|
|
|
|
|
|
|
|
2015-09-02 23:26:13 +00:00
|
|
|
def run(self, context):
|
|
|
|
"""Create, parse and treat the message"""
|
2014-10-23 13:47:00 +00:00
|
|
|
|
2015-09-02 23:26:13 +00:00
|
|
|
from nemubot.bot import Bot
|
|
|
|
assert isinstance(context, Bot)
|
2012-11-04 03:28:24 +00:00
|
|
|
|
2015-09-02 23:26:13 +00:00
|
|
|
msgs = []
|
2012-11-02 11:51:16 +00:00
|
|
|
|
2016-05-16 15:35:24 +00:00
|
|
|
# Parse message
|
2015-07-13 22:48:56 +00:00
|
|
|
try:
|
|
|
|
for msg in self.srv.parse(self.orig):
|
2015-09-02 23:26:13 +00:00
|
|
|
msgs.append(msg)
|
2015-07-13 22:48:56 +00:00
|
|
|
except:
|
|
|
|
logger.exception("Error occurred during the processing of the %s: "
|
2016-03-06 20:43:08 +00:00
|
|
|
"%s", type(self.orig).__name__, self.orig)
|
2015-07-13 22:48:56 +00:00
|
|
|
|
2016-05-16 15:35:24 +00:00
|
|
|
# Treat message
|
2015-09-02 23:26:13 +00:00
|
|
|
for msg in msgs:
|
2015-09-04 20:47:02 +00:00
|
|
|
for res in context.treater.treat_msg(msg):
|
2016-05-16 15:35:24 +00:00
|
|
|
# Identify destination
|
2015-09-02 23:26:13 +00:00
|
|
|
to_server = None
|
|
|
|
if isinstance(res, str):
|
|
|
|
to_server = self.srv
|
2016-03-06 20:43:08 +00:00
|
|
|
elif not hasattr(res, "server"):
|
|
|
|
logger.error("No server defined for response of type %s: %s", type(res).__name__, res)
|
|
|
|
continue
|
2015-09-02 23:26:13 +00:00
|
|
|
elif res.server is None:
|
|
|
|
to_server = self.srv
|
2016-05-16 15:35:24 +00:00
|
|
|
res.server = self.srv.fileno()
|
|
|
|
elif res.server in context.servers:
|
2015-09-02 23:26:13 +00:00
|
|
|
to_server = context.servers[res.server]
|
2016-03-06 20:43:08 +00:00
|
|
|
else:
|
|
|
|
to_server = res.server
|
2015-09-02 23:26:13 +00:00
|
|
|
|
2016-03-06 20:43:08 +00:00
|
|
|
if to_server is None or not hasattr(to_server, "send_response") or not callable(to_server.send_response):
|
|
|
|
logger.error("The server defined in this response doesn't exist: %s", res.server)
|
2015-09-02 23:26:13 +00:00
|
|
|
continue
|
|
|
|
|
2016-05-16 15:35:24 +00:00
|
|
|
# Sent message
|
2015-09-02 23:26:13 +00:00
|
|
|
to_server.send_response(res)
|
2012-11-02 11:51:16 +00:00
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
|
2012-10-04 11:36:25 +00:00
|
|
|
class EventConsumer:
|
2015-09-02 23:26:13 +00:00
|
|
|
|
2012-10-04 11:36:25 +00:00
|
|
|
"""Store a event before treating"""
|
2015-09-02 23:26:13 +00:00
|
|
|
|
2012-11-02 11:10:37 +00:00
|
|
|
def __init__(self, evt, timeout=20):
|
2012-10-04 11:36:25 +00:00
|
|
|
self.evt = evt
|
|
|
|
self.timeout = timeout
|
|
|
|
|
2012-11-02 11:51:16 +00:00
|
|
|
|
2012-11-02 11:10:37 +00:00
|
|
|
def run(self, context):
|
2012-10-04 11:36:25 +00:00
|
|
|
try:
|
2014-09-23 20:27:53 +00:00
|
|
|
self.evt.check()
|
2012-10-04 11:36:25 +00:00
|
|
|
except:
|
2014-08-27 05:57:00 +00:00
|
|
|
logger.exception("Error during event end")
|
2014-09-23 20:27:53 +00:00
|
|
|
|
|
|
|
# Reappend the event in the queue if it has next iteration
|
2012-10-04 11:36:25 +00:00
|
|
|
if self.evt.next is not None:
|
2014-09-23 20:27:53 +00:00
|
|
|
context.add_event(self.evt, eid=self.evt.id)
|
|
|
|
|
|
|
|
# Or remove reference of this event
|
2014-11-09 13:11:54 +00:00
|
|
|
elif (hasattr(self.evt, "module_src") and
|
|
|
|
self.evt.module_src is not None):
|
2017-09-26 22:13:26 +00:00
|
|
|
self.evt.module_src.__nemubot_context__.events.remove((self.evt, self.evt.id))
|
2012-11-02 11:10:37 +00:00
|
|
|
|
|
|
|
|
2012-10-04 11:36:25 +00:00
|
|
|
|
2012-08-23 16:20:45 +00:00
|
|
|
class Consumer(threading.Thread):
|
2015-09-02 23:26:13 +00:00
|
|
|
|
2012-10-04 11:36:25 +00:00
|
|
|
"""Dequeue and exec requested action"""
|
2015-09-02 23:26:13 +00:00
|
|
|
|
2012-08-23 16:20:45 +00:00
|
|
|
def __init__(self, context):
|
|
|
|
self.context = context
|
|
|
|
self.stop = False
|
2016-05-16 15:35:24 +00:00
|
|
|
super().__init__(name="Nemubot consumer")
|
2012-08-23 16:20:45 +00:00
|
|
|
|
2015-09-02 23:26:13 +00:00
|
|
|
|
2012-08-23 16:20:45 +00:00
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
while not self.stop:
|
2015-09-02 05:12:51 +00:00
|
|
|
stm = self.context.cnsr_queue.get(True, 1)
|
2012-11-02 11:10:37 +00:00
|
|
|
stm.run(self.context)
|
2014-07-17 10:01:04 +00:00
|
|
|
self.context.cnsr_queue.task_done()
|
2012-08-31 03:21:19 +00:00
|
|
|
|
2012-08-23 16:20:45 +00:00
|
|
|
except queue.Empty:
|
|
|
|
pass
|
|
|
|
finally:
|
2012-10-04 11:36:25 +00:00
|
|
|
self.context.cnsr_thrd_size -= 2
|
2014-07-17 10:01:04 +00:00
|
|
|
self.context.cnsr_thrd.remove(self)
|