2012-08-14 03:51:55 +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/>.
|
|
|
|
|
2012-08-16 02:32:47 +00:00
|
|
|
from datetime import datetime
|
2012-10-15 00:46:23 +00:00
|
|
|
from datetime import timedelta
|
2014-08-14 10:49:38 +00:00
|
|
|
import logging
|
2012-08-23 16:20:45 +00:00
|
|
|
from queue import Queue
|
2014-08-29 09:53:32 +00:00
|
|
|
import re
|
2014-08-30 17:15:14 +00:00
|
|
|
from select import select
|
2012-08-16 02:32:47 +00:00
|
|
|
import threading
|
2012-10-15 00:46:23 +00:00
|
|
|
import time
|
2014-08-29 09:53:32 +00:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
__version__ = '3.4.dev0'
|
|
|
|
__author__ = 'nemunaire'
|
2012-08-16 02:32:47 +00:00
|
|
|
|
2014-08-29 09:53:32 +00:00
|
|
|
from consumer import Consumer, EventConsumer, MessageConsumer
|
|
|
|
from event import ModuleEvent
|
2012-08-31 03:21:19 +00:00
|
|
|
import hooks
|
2014-09-01 17:21:54 +00:00
|
|
|
from hooksmanager import HooksManager
|
2012-08-27 22:27:02 +00:00
|
|
|
from networkbot import NetworkBot
|
2014-08-13 15:11:33 +00:00
|
|
|
from server.IRC import IRCServer
|
|
|
|
from server.DCC import DCC
|
2012-11-02 11:10:37 +00:00
|
|
|
import response
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2014-08-27 05:57:00 +00:00
|
|
|
logger = logging.getLogger("nemubot.bot")
|
2014-08-14 10:49:38 +00:00
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
class Bot(threading.Thread):
|
2012-11-02 11:10:37 +00:00
|
|
|
|
2014-08-29 09:53:32 +00:00
|
|
|
"""Class containing the bot context and ensuring key goals"""
|
|
|
|
|
|
|
|
def __init__(self, ip="127.0.0.1", modules_paths=list(), data_path="./datas/"):
|
|
|
|
"""Initialize the bot context
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
ip -- The external IP of the bot (default: 127.0.0.1)
|
|
|
|
modules_paths -- Paths to all directories where looking for module
|
|
|
|
data_path -- Path to directory where store bot context data
|
|
|
|
"""
|
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
2014-08-29 09:53:32 +00:00
|
|
|
logger.info("Initiate nemubot v%s", __version__)
|
|
|
|
|
|
|
|
# External IP for accessing this bot
|
2012-11-02 11:10:37 +00:00
|
|
|
self.ip = ip
|
2014-08-29 09:53:32 +00:00
|
|
|
|
|
|
|
# Context paths
|
|
|
|
self.modules_paths = modules_paths
|
|
|
|
self.data_path = data_path
|
|
|
|
|
|
|
|
# Save various informations
|
2012-11-02 11:10:37 +00:00
|
|
|
self.ctcp_capabilities = dict()
|
|
|
|
self.init_ctcp_capabilities()
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-08-27 22:27:02 +00:00
|
|
|
# Keep global context: servers and modules
|
2012-11-02 11:10:37 +00:00
|
|
|
self.servers = dict()
|
|
|
|
self.modules = dict()
|
2014-08-28 10:26:02 +00:00
|
|
|
self.modules_configuration = dict()
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-08-27 22:27:02 +00:00
|
|
|
# Events
|
2012-08-23 16:20:45 +00:00
|
|
|
self.events = list()
|
2012-08-16 02:32:47 +00:00
|
|
|
self.event_timer = None
|
|
|
|
|
2012-08-31 03:21:19 +00:00
|
|
|
# Own hooks
|
2014-09-01 17:21:54 +00:00
|
|
|
self.hooks = HooksManager()
|
|
|
|
def in_ping(msg):
|
|
|
|
if re.match("^ *(m[' ]?entends?[ -]+tu|h?ear me|do you copy|ping)", msg.text, re.I) is not None:
|
|
|
|
return response.Response(msg.sender, message="pong", channel=msg.receivers, nick=msg.nick)
|
|
|
|
self.hooks.add_hook(hooks.Hook(in_ping), "in", "PRIVMSG")
|
2012-08-31 03:21:19 +00:00
|
|
|
|
2012-08-27 22:27:02 +00:00
|
|
|
# Other known bots, making a bots network
|
2012-09-01 13:49:30 +00:00
|
|
|
self.network = dict()
|
2012-08-31 03:21:19 +00:00
|
|
|
self.hooks_cache = dict()
|
2012-08-27 22:27:02 +00:00
|
|
|
|
|
|
|
# Messages to be treated
|
2012-10-04 11:36:25 +00:00
|
|
|
self.cnsr_queue = Queue()
|
|
|
|
self.cnsr_thrd = list()
|
|
|
|
self.cnsr_thrd_size = -1
|
2012-08-23 16:20:45 +00:00
|
|
|
|
2012-08-16 02:32:47 +00:00
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
def run(self):
|
|
|
|
from server import _rlist, _wlist, _xlist
|
|
|
|
|
|
|
|
self.stop = False
|
|
|
|
while not self.stop:
|
|
|
|
rl, wl, xl = select(_rlist, _wlist, _xlist, 0.1)
|
|
|
|
|
|
|
|
for x in xl:
|
|
|
|
x.exception()
|
|
|
|
for w in wl:
|
|
|
|
w.write_select()
|
|
|
|
for r in rl:
|
|
|
|
for i in r.read():
|
|
|
|
self.receive_message(r, i)
|
|
|
|
|
|
|
|
|
2012-11-02 11:10:37 +00:00
|
|
|
def init_ctcp_capabilities(self):
|
|
|
|
"""Reset existing CTCP capabilities to default one"""
|
2014-08-29 10:25:25 +00:00
|
|
|
|
|
|
|
def _ctcp_clientinfo(srv, msg):
|
|
|
|
"""Response to CLIENTINFO CTCP message"""
|
|
|
|
return _ctcp_response(msg.sender,
|
|
|
|
" ".join(self.ctcp_capabilities.keys()))
|
|
|
|
|
|
|
|
def _ctcp_dcc(srv, msg):
|
|
|
|
"""Response to DCC CTCP message"""
|
|
|
|
try:
|
|
|
|
ip = srv.toIP(int(msg.cmds[3]))
|
|
|
|
port = int(msg.cmds[4])
|
|
|
|
conn = DCC(srv, msg.sender)
|
|
|
|
except:
|
|
|
|
return _ctcp_response(msg.sender, "ERRMSG invalid parameters provided as DCC CTCP request")
|
|
|
|
|
|
|
|
logger.info("Receive DCC connection request from %s to %s:%d", conn.sender, ip, port)
|
|
|
|
|
|
|
|
if conn.accept_user(ip, port):
|
|
|
|
srv.dcc_clients[conn.sender] = conn
|
|
|
|
conn.send_dcc("Hello %s!" % conn.nick)
|
|
|
|
else:
|
|
|
|
logger.error("DCC: unable to connect to %s:%d", ip, port)
|
|
|
|
return _ctcp_response(msg.sender, "ERRMSG unable to connect to %s:%d" % (ip, port))
|
|
|
|
|
2014-08-29 14:33:45 +00:00
|
|
|
self.ctcp_capabilities["ACTION"] = lambda srv, msg: print ("ACTION receive: %s" % msg.text)
|
2014-08-29 10:25:25 +00:00
|
|
|
self.ctcp_capabilities["CLIENTINFO"] = _ctcp_clientinfo
|
|
|
|
self.ctcp_capabilities["DCC"] = _ctcp_dcc
|
|
|
|
self.ctcp_capabilities["FINGER"] = lambda srv, msg: _ctcp_response(
|
|
|
|
msg.sender, "VERSION nemubot v%s" % __version__)
|
2012-11-02 11:10:37 +00:00
|
|
|
self.ctcp_capabilities["NEMUBOT"] = lambda srv, msg: _ctcp_response(
|
2014-08-29 10:25:25 +00:00
|
|
|
msg.sender, "NEMUBOT %s" % __version__)
|
|
|
|
self.ctcp_capabilities["PING"] = lambda srv, msg: _ctcp_response(
|
|
|
|
msg.sender, "PING %s" % " ".join(msg.cmds[1:]))
|
|
|
|
self.ctcp_capabilities["SOURCE"] = lambda srv, msg: _ctcp_response(
|
|
|
|
msg.sender, "SOURCE https://github.com/nemunaire/nemubot")
|
2012-11-02 11:10:37 +00:00
|
|
|
self.ctcp_capabilities["TIME"] = lambda srv, msg: _ctcp_response(
|
2014-08-29 10:25:25 +00:00
|
|
|
msg.sender, "TIME %s" % (datetime.now()))
|
2012-11-02 11:10:37 +00:00
|
|
|
self.ctcp_capabilities["USERINFO"] = lambda srv, msg: _ctcp_response(
|
2014-08-29 10:25:25 +00:00
|
|
|
msg.sender, "USERINFO %s" % srv.realname)
|
2012-11-02 11:10:37 +00:00
|
|
|
self.ctcp_capabilities["VERSION"] = lambda srv, msg: _ctcp_response(
|
2014-08-29 10:25:25 +00:00
|
|
|
msg.sender, "VERSION nemubot v%s" % __version__)
|
2012-11-02 11:10:37 +00:00
|
|
|
|
2014-08-29 10:25:25 +00:00
|
|
|
logger.debug("CTCP capabilities setup: %s", ", ".join(self.ctcp_capabilities))
|
2012-11-02 11:10:37 +00:00
|
|
|
|
|
|
|
|
2014-08-29 09:53:32 +00:00
|
|
|
# Events methods
|
|
|
|
|
2012-11-04 03:28:24 +00:00
|
|
|
def add_event(self, evt, eid=None, module_src=None):
|
2014-08-29 09:53:32 +00:00
|
|
|
"""Register an event and return its identifiant for futur update
|
|
|
|
|
|
|
|
Return:
|
|
|
|
None if the event is not in the queue (eg. if it has been executed during the call) or
|
|
|
|
returns the event ID.
|
|
|
|
|
|
|
|
Argument:
|
|
|
|
evt -- The event object to add
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
eid -- The desired event ID (object or string UUID)
|
|
|
|
module_src -- The module to which the event is attached to
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Generate the event id if no given
|
2012-09-02 16:40:44 +00:00
|
|
|
if eid is None:
|
2014-08-29 09:53:32 +00:00
|
|
|
eid = uuid.uuid1()
|
|
|
|
|
|
|
|
# Fill the id field of the event
|
|
|
|
if type(eid) is uuid.UUID:
|
|
|
|
evt.id = str(eid)
|
2012-09-02 16:40:44 +00:00
|
|
|
else:
|
2014-08-29 09:53:32 +00:00
|
|
|
# Ok, this is quite useless...
|
|
|
|
try:
|
|
|
|
evt.id = str(uuid.UUID(eid))
|
|
|
|
except ValueError:
|
|
|
|
evt.id = eid
|
2012-08-30 23:36:23 +00:00
|
|
|
|
2014-08-29 09:53:32 +00:00
|
|
|
# Add the event in its place
|
2012-08-16 02:32:47 +00:00
|
|
|
t = evt.current
|
2014-08-29 09:53:32 +00:00
|
|
|
i = 0 # sentinel
|
2012-08-16 02:32:47 +00:00
|
|
|
for i in range(0, len(self.events)):
|
|
|
|
if self.events[i].current > t:
|
|
|
|
break
|
2014-08-29 09:53:32 +00:00
|
|
|
self.events.insert(i, evt)
|
|
|
|
|
|
|
|
if i == 0:
|
|
|
|
# First event changed, reset timer
|
|
|
|
self._update_event_timer()
|
|
|
|
if len(self.events) <= 0 or self.events[i] != evt:
|
|
|
|
# Our event has been executed and removed from queue
|
2012-09-02 16:40:44 +00:00
|
|
|
return None
|
2012-08-16 02:32:47 +00:00
|
|
|
|
2014-08-29 09:53:32 +00:00
|
|
|
# Register the event in the source module
|
2012-11-04 03:28:24 +00:00
|
|
|
if module_src is not None:
|
|
|
|
module_src.REGISTERED_EVENTS.append(evt.id)
|
2014-08-29 09:53:32 +00:00
|
|
|
evt.module_src = module_src
|
2012-11-04 03:28:24 +00:00
|
|
|
|
2014-08-27 05:57:00 +00:00
|
|
|
logger.info("New event registered: %s -> %s", evt.id, evt)
|
2012-08-30 23:36:23 +00:00
|
|
|
return evt.id
|
|
|
|
|
2014-08-29 09:53:32 +00:00
|
|
|
|
|
|
|
def del_event(self, evt, module_src=None):
|
|
|
|
"""Find and remove an event from list
|
|
|
|
|
|
|
|
Return:
|
|
|
|
True if the event has been found and removed, False else
|
|
|
|
|
|
|
|
Argument:
|
|
|
|
evt -- The ModuleEvent object to remove or just the event identifier
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
module_src -- The module to which the event is attached to (ignored if evt is a ModuleEvent)
|
|
|
|
"""
|
|
|
|
|
|
|
|
logger.info("Removing event: %s from %s", evt, module_src)
|
|
|
|
|
|
|
|
if type(evt) is ModuleEvent:
|
|
|
|
id = evt.id
|
|
|
|
module_src = evt.module_src
|
|
|
|
else:
|
|
|
|
id = evt
|
|
|
|
|
2012-09-01 13:49:30 +00:00
|
|
|
if len(self.events) > 0 and id == self.events[0].id:
|
|
|
|
self.events.remove(self.events[0])
|
2014-08-29 09:53:32 +00:00
|
|
|
self._update_event_timer()
|
2012-11-04 03:28:24 +00:00
|
|
|
if module_src is not None:
|
|
|
|
module_src.REGISTERED_EVENTS.remove(evt.id)
|
2012-09-01 13:49:30 +00:00
|
|
|
return True
|
|
|
|
|
2012-08-30 23:36:23 +00:00
|
|
|
for evt in self.events:
|
|
|
|
if evt.id == id:
|
|
|
|
self.events.remove(evt)
|
2012-11-04 03:28:24 +00:00
|
|
|
|
|
|
|
if module_src is not None:
|
|
|
|
module_src.REGISTERED_EVENTS.remove(evt.id)
|
2012-08-30 23:36:23 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2014-08-29 09:53:32 +00:00
|
|
|
|
|
|
|
def _update_event_timer(self):
|
|
|
|
"""(Re)launch the timer to end with the closest event"""
|
|
|
|
|
2012-08-16 02:32:47 +00:00
|
|
|
# Reset the timer if this is the first item
|
|
|
|
if self.event_timer is not None:
|
|
|
|
self.event_timer.cancel()
|
2014-08-29 09:53:32 +00:00
|
|
|
|
2012-08-16 02:32:47 +00:00
|
|
|
if len(self.events) > 0:
|
2014-08-27 05:57:00 +00:00
|
|
|
logger.debug("Update timer: next event in %d seconds",
|
2014-08-14 10:49:38 +00:00
|
|
|
self.events[0].time_left.seconds)
|
2012-10-15 00:46:23 +00:00
|
|
|
if datetime.now() + timedelta(seconds=5) >= self.events[0].current:
|
|
|
|
while datetime.now() < self.events[0].current:
|
|
|
|
time.sleep(0.6)
|
2014-08-29 09:53:32 +00:00
|
|
|
self._end_event_timer()
|
2012-08-16 02:32:47 +00:00
|
|
|
else:
|
|
|
|
self.event_timer = threading.Timer(
|
2014-08-29 09:53:32 +00:00
|
|
|
self.events[0].time_left.seconds + 1, self._end_event_timer)
|
2012-08-16 02:32:47 +00:00
|
|
|
self.event_timer.start()
|
2014-08-14 10:49:38 +00:00
|
|
|
else:
|
|
|
|
logger.debug("Update timer: no timer left")
|
2012-08-16 02:32:47 +00:00
|
|
|
|
2014-08-29 09:53:32 +00:00
|
|
|
|
|
|
|
def _end_event_timer(self):
|
|
|
|
"""Function called at the end of the event timer"""
|
|
|
|
|
2014-08-14 10:49:38 +00:00
|
|
|
while len(self.events) > 0 and datetime.now() >= self.events[0].current:
|
2012-08-16 02:32:47 +00:00
|
|
|
evt = self.events.pop(0)
|
2014-08-29 09:53:32 +00:00
|
|
|
self.cnsr_queue.put_nowait(EventConsumer(evt))
|
|
|
|
self._launch_consumers()
|
|
|
|
|
|
|
|
self._update_event_timer()
|
2012-10-04 11:36:25 +00:00
|
|
|
|
2014-08-29 09:53:32 +00:00
|
|
|
|
|
|
|
# Consumers methods
|
|
|
|
|
|
|
|
def _launch_consumers(self):
|
|
|
|
"""Launch new consumer threads if necessary"""
|
|
|
|
|
|
|
|
while self.cnsr_queue.qsize() > self.cnsr_thrd_size:
|
|
|
|
# Next launch if two more items in queue
|
|
|
|
self.cnsr_thrd_size += 2
|
|
|
|
|
|
|
|
c = Consumer(self)
|
|
|
|
self.cnsr_thrd.append(c)
|
|
|
|
c.start()
|
2012-08-14 04:56:28 +00:00
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
def add_server(self, node, nick, owner, realname):
|
2012-08-14 03:51:55 +00:00
|
|
|
"""Add a new server to the context"""
|
2014-08-30 17:15:14 +00:00
|
|
|
srv = IRCServer(node, nick, owner, realname)
|
|
|
|
#srv.register_hooks()
|
2012-08-14 03:51:55 +00:00
|
|
|
if srv.id not in self.servers:
|
|
|
|
self.servers[srv.id] = srv
|
2014-08-30 17:15:14 +00:00
|
|
|
srv.open()
|
2012-08-14 03:51:55 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2014-08-29 09:53:32 +00:00
|
|
|
# Modules methods
|
|
|
|
|
|
|
|
def add_modules_path(self, path):
|
|
|
|
"""Add a path to the modules_path array, used by module loader"""
|
|
|
|
# The path must end by / char
|
2014-08-29 14:33:45 +00:00
|
|
|
if path[-1] != "/":
|
|
|
|
path += "/"
|
2014-08-29 09:53:32 +00:00
|
|
|
|
|
|
|
if path not in self.modules_paths:
|
|
|
|
self.modules_paths.append(path)
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
def add_module(self, module):
|
|
|
|
"""Add a module to the context, if already exists, unload the
|
|
|
|
old one before"""
|
|
|
|
# Check if the module already exists
|
|
|
|
for mod in self.modules.keys():
|
|
|
|
if self.modules[mod].name == module.name:
|
|
|
|
self.unload_module(self.modules[mod].name)
|
|
|
|
break
|
|
|
|
|
|
|
|
self.modules[module.name] = module
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2014-08-14 10:49:38 +00:00
|
|
|
def unload_module(self, name):
|
2012-08-14 03:51:55 +00:00
|
|
|
"""Unload a module"""
|
|
|
|
if name in self.modules:
|
2014-08-12 15:51:37 +00:00
|
|
|
self.modules[name].print_debug("Unloading module %s" % name)
|
2012-08-14 03:51:55 +00:00
|
|
|
self.modules[name].save()
|
|
|
|
if hasattr(self.modules[name], "unload"):
|
2012-09-01 13:49:30 +00:00
|
|
|
self.modules[name].unload(self)
|
2012-09-01 09:23:41 +00:00
|
|
|
# Remove registered hooks
|
|
|
|
for (s, h) in self.modules[name].REGISTERED_HOOKS:
|
2014-09-01 17:21:54 +00:00
|
|
|
self.hooks.del_hook(h, s)
|
2012-12-31 19:17:58 +00:00
|
|
|
# Remove registered events
|
|
|
|
for e in self.modules[name].REGISTERED_EVENTS:
|
|
|
|
self.del_event(e)
|
2012-08-14 03:51:55 +00:00
|
|
|
# Remove from the dict
|
|
|
|
del self.modules[name]
|
2014-08-27 05:57:00 +00:00
|
|
|
logger.info("Module `%s' successfully unloaded.", name)
|
2012-08-14 03:51:55 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2012-08-31 01:52:49 +00:00
|
|
|
def receive_message(self, srv, raw_msg, private=False, data=None):
|
2012-08-23 16:20:45 +00:00
|
|
|
"""Queued the message for treatment"""
|
2014-09-01 17:21:54 +00:00
|
|
|
#print("READ", raw_msg)
|
2014-08-29 09:53:32 +00:00
|
|
|
self.cnsr_queue.put_nowait(MessageConsumer(srv, raw_msg, datetime.now(), private, data))
|
2012-08-23 16:20:45 +00:00
|
|
|
|
|
|
|
# Launch a new thread if necessary
|
2014-08-29 09:53:32 +00:00
|
|
|
self._launch_consumers()
|
2012-08-23 16:20:45 +00:00
|
|
|
|
|
|
|
|
2012-08-27 22:27:02 +00:00
|
|
|
def add_networkbot(self, srv, dest, dcc=None):
|
|
|
|
"""Append a new bot into the network"""
|
|
|
|
id = srv.id + "/" + dest
|
|
|
|
if id not in self.network:
|
|
|
|
self.network[id] = NetworkBot(self, srv, dest, dcc)
|
|
|
|
return self.network[id]
|
|
|
|
|
2012-11-08 12:23:58 +00:00
|
|
|
def send_networkbot(self, srv, cmd, data=None):
|
|
|
|
for bot in self.network:
|
|
|
|
if self.network[bot].srv == srv:
|
|
|
|
self.network[bot].send_cmd(cmd, data)
|
2012-08-27 22:27:02 +00:00
|
|
|
|
2014-08-14 10:49:38 +00:00
|
|
|
def quit(self):
|
2012-08-14 03:51:55 +00:00
|
|
|
"""Save and unload modules and disconnect servers"""
|
2012-08-16 02:53:46 +00:00
|
|
|
if self.event_timer is not None:
|
2014-08-14 10:49:38 +00:00
|
|
|
logger.info("Stop the event timer...")
|
2012-08-16 02:53:46 +00:00
|
|
|
self.event_timer.cancel()
|
|
|
|
|
2014-08-14 10:49:38 +00:00
|
|
|
logger.info("Save and unload all modules...")
|
2012-08-14 03:51:55 +00:00
|
|
|
k = list(self.modules.keys())
|
|
|
|
for mod in k:
|
2014-08-14 10:49:38 +00:00
|
|
|
self.unload_module(mod)
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2014-08-14 10:49:38 +00:00
|
|
|
logger.info("Close all servers connection...")
|
2012-08-14 03:51:55 +00:00
|
|
|
k = list(self.servers.keys())
|
|
|
|
for srv in k:
|
2014-08-31 08:51:44 +00:00
|
|
|
self.servers[srv].close()
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
self.stop = True
|
|
|
|
|
|
|
|
|
2014-08-29 09:53:32 +00:00
|
|
|
# Treatment
|
2012-08-31 03:21:19 +00:00
|
|
|
|
|
|
|
def check_rest_times(self, store, hook):
|
|
|
|
"""Remove from store the hook if it has been executed given time"""
|
|
|
|
if hook.times == 0:
|
|
|
|
if isinstance(store, dict):
|
|
|
|
store[hook.name].remove(hook)
|
|
|
|
if len(store) == 0:
|
|
|
|
del store[hook.name]
|
|
|
|
elif isinstance(store, list):
|
|
|
|
store.remove(hook)
|
|
|
|
|
2012-11-04 03:28:24 +00:00
|
|
|
|
|
|
|
def treat_prvmsg_ask(self, msg, srv):
|
|
|
|
# Treat ping
|
|
|
|
if re.match("^ *(m[' ]?entends?[ -]+tu|h?ear me|do you copy|ping)",
|
2014-08-29 14:33:45 +00:00
|
|
|
msg.text, re.I) is not None:
|
2012-11-04 03:28:24 +00:00
|
|
|
return response.Response(msg.sender, message="pong",
|
2014-08-29 14:33:45 +00:00
|
|
|
channel=msg.receivers, nick=msg.nick)
|
2012-11-04 03:28:24 +00:00
|
|
|
|
|
|
|
# Ask hooks
|
|
|
|
else:
|
|
|
|
return self.treat_ask(msg, srv)
|
|
|
|
|
|
|
|
def treat_prvmsg(self, msg, srv):
|
|
|
|
# First, treat CTCP
|
2014-08-29 14:33:45 +00:00
|
|
|
if msg.is_ctcp:
|
2012-11-04 03:28:24 +00:00
|
|
|
if msg.cmds[0] in self.ctcp_capabilities:
|
|
|
|
return self.ctcp_capabilities[msg.cmds[0]](srv, msg)
|
|
|
|
else:
|
|
|
|
return _ctcp_response(msg.sender, "ERRMSG Unknown or unimplemented CTCP request")
|
|
|
|
|
|
|
|
# Owner commands
|
2014-08-29 14:33:45 +00:00
|
|
|
if len(msg.text) > 1 and msg.text[0] == '`' and msg.nick == srv.owner:
|
2012-11-04 03:28:24 +00:00
|
|
|
#TODO: owner commands
|
|
|
|
pass
|
|
|
|
|
2014-08-29 14:33:45 +00:00
|
|
|
elif len(msg.text) > 1 and msg.text[0] == '!':
|
2012-11-04 03:28:24 +00:00
|
|
|
# Remove the !
|
|
|
|
msg.cmds[0] = msg.cmds[0][1:]
|
|
|
|
|
|
|
|
if msg.cmds[0] == "help":
|
2012-11-04 13:04:49 +00:00
|
|
|
return _help_msg(msg.sender, self.modules, msg.cmds)
|
2012-11-04 03:28:24 +00:00
|
|
|
|
|
|
|
elif msg.cmds[0] == "dcc":
|
2014-08-27 05:57:00 +00:00
|
|
|
logger.debug("dcctest for %s", msg.sender)
|
2012-11-04 03:28:24 +00:00
|
|
|
srv.send_dcc("Hello %s!" % msg.nick, msg.sender)
|
|
|
|
elif msg.cmds[0] == "pvdcctest":
|
2014-08-14 10:49:38 +00:00
|
|
|
logger.debug("dcctest")
|
2012-11-04 03:28:24 +00:00
|
|
|
return Response(msg.sender, message="Test DCC")
|
|
|
|
elif msg.cmds[0] == "dccsendtest":
|
2014-08-14 10:49:38 +00:00
|
|
|
logger.debug("dccsendtest")
|
2012-11-04 03:28:24 +00:00
|
|
|
conn = DCC(srv, msg.sender)
|
|
|
|
conn.send_file("bot_sample.xml")
|
|
|
|
|
|
|
|
else:
|
|
|
|
return self.treat_cmd(msg, srv)
|
|
|
|
|
|
|
|
else:
|
|
|
|
res = self.treat_answer(msg, srv)
|
2014-08-07 17:13:52 +00:00
|
|
|
|
2012-11-04 03:28:24 +00:00
|
|
|
# Assume the message starts with nemubot:
|
2014-08-07 17:13:52 +00:00
|
|
|
if not res and msg.private:
|
2012-11-04 03:28:24 +00:00
|
|
|
return self.treat_prvmsg_ask(msg, srv)
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2012-11-02 11:10:37 +00:00
|
|
|
def _ctcp_response(sndr, msg):
|
|
|
|
return response.Response(sndr, msg, ctcp=True)
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-11-04 13:04:49 +00:00
|
|
|
|
|
|
|
def _help_msg(sndr, modules, cmd):
|
|
|
|
"""Parse and response to help messages"""
|
|
|
|
res = response.Response(sndr)
|
|
|
|
if len(cmd) > 1:
|
|
|
|
if cmd[1] in modules:
|
|
|
|
if len(cmd) > 2:
|
|
|
|
if hasattr(modules[cmd[1]], "HELP_cmd"):
|
|
|
|
res.append_message(modules[cmd[1]].HELP_cmd(cmd[2]))
|
|
|
|
else:
|
|
|
|
res.append_message("No help for command %s in module %s" % (cmd[2], cmd[1]))
|
|
|
|
elif hasattr(modules[cmd[1]], "help_full"):
|
|
|
|
res.append_message(modules[cmd[1]].help_full())
|
|
|
|
else:
|
|
|
|
res.append_message("No help for module %s" % cmd[1])
|
|
|
|
else:
|
|
|
|
res.append_message("No module named %s" % cmd[1])
|
|
|
|
else:
|
|
|
|
res.append_message("Pour me demander quelque chose, commencez "
|
|
|
|
"votre message par mon nom ; je réagis "
|
|
|
|
"également à certaine commandes commençant par"
|
|
|
|
" !. Pour plus d'informations, envoyez le "
|
|
|
|
"message \"!more\".")
|
|
|
|
res.append_message("Mon code source est libre, publié sous "
|
|
|
|
"licence AGPL (http://www.gnu.org/licenses/). "
|
|
|
|
"Vous pouvez le consulter, le dupliquer, "
|
|
|
|
"envoyer des rapports de bogues ou bien "
|
|
|
|
"contribuer au projet sur GitHub : "
|
|
|
|
"http://github.com/nemunaire/nemubot/")
|
|
|
|
res.append_message(title="Pour plus de détails sur un module, "
|
|
|
|
"envoyez \"!help nomdumodule\". Voici la liste"
|
|
|
|
" de tous les modules disponibles localement",
|
2014-08-27 23:39:31 +00:00
|
|
|
message=["\x03\x02%s\x03\x02 (%s)" % (im, modules[im].__doc__) for im in modules if modules[im].__doc__])
|
2012-11-04 13:04:49 +00:00
|
|
|
return res
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
def hotswap(bak):
|
2014-08-29 09:53:32 +00:00
|
|
|
new = Bot(str(bak.ip), bak.modules_paths, bak.data_path)
|
|
|
|
new.ctcp_capabilities = bak.ctcp_capabilities
|
|
|
|
new.servers = bak.servers
|
|
|
|
new.modules = bak.modules
|
|
|
|
new.modules_configuration = bak.modules_configuration
|
|
|
|
new.events = bak.events
|
|
|
|
new.hooks = bak.hooks
|
|
|
|
new.network = bak.network
|
|
|
|
return new
|
2012-08-14 03:51:55 +00:00
|
|
|
|
|
|
|
def reload():
|
|
|
|
import imp
|
|
|
|
|
2012-09-01 13:49:30 +00:00
|
|
|
import channel
|
|
|
|
imp.reload(channel)
|
|
|
|
|
|
|
|
import consumer
|
|
|
|
imp.reload(consumer)
|
|
|
|
|
|
|
|
import DCC
|
|
|
|
imp.reload(DCC)
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-08-14 04:56:28 +00:00
|
|
|
import event
|
|
|
|
imp.reload(event)
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
import hooks
|
|
|
|
imp.reload(hooks)
|
|
|
|
|
|
|
|
import importer
|
|
|
|
imp.reload(importer)
|
|
|
|
|
2012-09-01 13:49:30 +00:00
|
|
|
import message
|
|
|
|
imp.reload(message)
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-09-01 13:49:30 +00:00
|
|
|
import prompt.builtins
|
|
|
|
imp.reload(prompt.builtins)
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-09-01 13:49:30 +00:00
|
|
|
import server
|
|
|
|
imp.reload(server)
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2012-09-01 13:49:30 +00:00
|
|
|
import xmlparser
|
|
|
|
imp.reload(xmlparser)
|
|
|
|
import xmlparser.node
|
|
|
|
imp.reload(xmlparser.node)
|