2012-08-14 03:55:48 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2015-01-03 19:34:44 +00:00
|
|
|
|
# Nemubot is a smart and modulable IM bot.
|
|
|
|
|
# Copyright (C) 2012-2015 Mercier Pierre-Olivier
|
2012-08-14 03:55:48 +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-07-17 09:58:24 +00:00
|
|
|
|
import traceback
|
|
|
|
|
import sys
|
|
|
|
|
|
2015-01-03 19:34:44 +00:00
|
|
|
|
from nemubot.hooks import hook
|
2012-08-27 22:27:02 +00:00
|
|
|
|
|
2014-08-13 13:53:55 +00:00
|
|
|
|
nemubotversion = 3.4
|
2012-08-14 03:55:48 +00:00
|
|
|
|
NODATA = True
|
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
|
2015-01-02 19:44:56 +00:00
|
|
|
|
def getserver(toks, context, prompt, mandatory=False, **kwargs):
|
|
|
|
|
"""Choose the server in toks or prompt.
|
|
|
|
|
This function modify the tokens list passed as argument"""
|
|
|
|
|
|
|
|
|
|
if len(toks) > 1 and toks[1] in context.servers:
|
|
|
|
|
return context.servers[toks.pop(1)]
|
|
|
|
|
elif not mandatory or prompt.selectedServer:
|
|
|
|
|
return prompt.selectedServer
|
2012-08-27 22:27:02 +00:00
|
|
|
|
else:
|
2015-02-21 12:51:40 +00:00
|
|
|
|
from nemubot.prompt.error import PromptError
|
2015-01-02 19:44:56 +00:00
|
|
|
|
raise PromptError("Please SELECT a server or give its name in argument.")
|
2012-08-27 22:27:02 +00:00
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
|
2014-08-28 10:26:02 +00:00
|
|
|
|
@hook("prompt_cmd", "close")
|
2015-01-02 19:44:56 +00:00
|
|
|
|
def close(toks, context, **kwargs):
|
2012-08-14 03:55:48 +00:00
|
|
|
|
"""Disconnect and forget (remove from the servers list) the server"""
|
2015-01-02 19:44:56 +00:00
|
|
|
|
srv = getserver(toks, context=context, mandatory=True, **kwargs)
|
|
|
|
|
|
|
|
|
|
if srv.close():
|
|
|
|
|
del context.servers[srv.id]
|
|
|
|
|
return 0
|
|
|
|
|
return 1
|
2012-08-14 03:55:48 +00:00
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
|
2014-08-28 10:26:02 +00:00
|
|
|
|
@hook("prompt_cmd", "connect")
|
2015-01-02 19:44:56 +00:00
|
|
|
|
def connect(toks, **kwargs):
|
2012-08-14 03:55:48 +00:00
|
|
|
|
"""Make the connexion to a server"""
|
2015-01-02 19:44:56 +00:00
|
|
|
|
srv = getserver(toks, mandatory=True, **kwargs)
|
|
|
|
|
|
|
|
|
|
return not srv.open()
|
2012-08-14 03:55:48 +00:00
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
|
2014-08-28 10:26:02 +00:00
|
|
|
|
@hook("prompt_cmd", "disconnect")
|
2015-01-02 19:44:56 +00:00
|
|
|
|
def disconnect(toks, **kwargs):
|
2012-08-14 03:55:48 +00:00
|
|
|
|
"""Close the connection to a server"""
|
2015-01-02 19:44:56 +00:00
|
|
|
|
srv = getserver(toks, mandatory=True, **kwargs)
|
|
|
|
|
|
|
|
|
|
return not srv.close()
|
2012-08-14 03:55:48 +00:00
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
|
2014-08-28 10:26:02 +00:00
|
|
|
|
@hook("prompt_cmd", "discover")
|
2015-01-02 19:44:56 +00:00
|
|
|
|
def discover(toks, context, **kwargs):
|
2012-08-27 22:27:02 +00:00
|
|
|
|
"""Discover a new bot on a server"""
|
2015-01-02 19:44:56 +00:00
|
|
|
|
srv = getserver(toks, context=context, mandatory=True, **kwargs)
|
|
|
|
|
|
|
|
|
|
if len(toks) > 1 and "!" in toks[1]:
|
|
|
|
|
bot = context.add_networkbot(srv, name)
|
|
|
|
|
return not bot.connect()
|
2012-08-14 03:55:48 +00:00
|
|
|
|
else:
|
2015-01-02 19:44:56 +00:00
|
|
|
|
print(" %s is not a valid fullname, for example: "
|
|
|
|
|
"nemubot!nemubotV3@bot.nemunai.re" % ''.join(toks[1:1]))
|
|
|
|
|
return 1
|
2012-08-14 03:55:48 +00:00
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
|
2014-08-28 10:26:02 +00:00
|
|
|
|
@hook("prompt_cmd", "join")
|
2014-09-28 18:34:49 +00:00
|
|
|
|
@hook("prompt_cmd", "leave")
|
|
|
|
|
@hook("prompt_cmd", "part")
|
2015-01-02 19:44:56 +00:00
|
|
|
|
def join(toks, **kwargs):
|
2012-08-14 03:55:48 +00:00
|
|
|
|
"""Join or leave a channel"""
|
2015-01-02 19:44:56 +00:00
|
|
|
|
srv = getserver(toks, mandatory=True, **kwargs)
|
2012-08-14 03:55:48 +00:00
|
|
|
|
|
2015-01-02 19:44:56 +00:00
|
|
|
|
if len(toks) <= 2:
|
2014-11-13 01:51:49 +00:00
|
|
|
|
print("%s: not enough arguments." % toks[0])
|
2015-01-02 19:44:56 +00:00
|
|
|
|
return 1
|
2012-08-14 03:55:48 +00:00
|
|
|
|
|
|
|
|
|
if toks[0] == "join":
|
2015-01-02 19:44:56 +00:00
|
|
|
|
if len(toks) > 2:
|
|
|
|
|
srv.write("JOIN %s %s" % (toks[1], toks[2]))
|
2012-08-14 03:55:48 +00:00
|
|
|
|
else:
|
2015-01-02 19:44:56 +00:00
|
|
|
|
srv.write("JOIN %s" % toks[1])
|
|
|
|
|
|
2012-08-14 03:55:48 +00:00
|
|
|
|
elif toks[0] == "leave" or toks[0] == "part":
|
2015-01-02 19:44:56 +00:00
|
|
|
|
if len(toks) > 2:
|
|
|
|
|
srv.write("PART %s :%s" % (toks[1], " ".join(toks[2:])))
|
|
|
|
|
else:
|
|
|
|
|
srv.write("PART %s" % toks[1])
|
|
|
|
|
|
|
|
|
|
return 0
|
2012-08-14 03:55:48 +00:00
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
|
2014-08-28 10:26:02 +00:00
|
|
|
|
@hook("prompt_cmd", "save")
|
2015-01-02 19:44:56 +00:00
|
|
|
|
def save_mod(toks, context, **kwargs):
|
2013-03-20 08:56:12 +00:00
|
|
|
|
"""Force save module data"""
|
|
|
|
|
if len(toks) < 2:
|
2015-01-02 19:44:56 +00:00
|
|
|
|
print("save: not enough arguments.")
|
|
|
|
|
return 1
|
2013-03-20 08:56:12 +00:00
|
|
|
|
|
2015-01-02 19:44:56 +00:00
|
|
|
|
wrn = 0
|
2013-03-20 08:56:12 +00:00
|
|
|
|
for mod in toks[1:]:
|
|
|
|
|
if mod in context.modules:
|
|
|
|
|
context.modules[mod].save()
|
2015-01-02 19:44:56 +00:00
|
|
|
|
print("save: module `%s´ saved successfully" % mod)
|
2013-03-20 08:56:12 +00:00
|
|
|
|
else:
|
2015-01-02 19:44:56 +00:00
|
|
|
|
wrn += 1
|
|
|
|
|
print("save: no module named `%s´" % mod)
|
|
|
|
|
return wrn
|
2013-03-20 08:56:12 +00:00
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
|
2014-08-28 10:26:02 +00:00
|
|
|
|
@hook("prompt_cmd", "send")
|
2015-01-02 19:44:56 +00:00
|
|
|
|
def send(toks, **kwargs):
|
2012-08-14 03:55:48 +00:00
|
|
|
|
"""Send a message on a channel"""
|
2015-01-02 19:44:56 +00:00
|
|
|
|
srv = getserver(toks, mandatory=True, **kwargs)
|
2012-08-14 03:55:48 +00:00
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
# Check the server is connected
|
2012-08-14 03:55:48 +00:00
|
|
|
|
if not srv.connected:
|
|
|
|
|
print ("send: server `%s' not connected." % srv.id)
|
2015-01-02 19:44:56 +00:00
|
|
|
|
return 2
|
2012-08-14 03:55:48 +00:00
|
|
|
|
|
2015-01-02 19:44:56 +00:00
|
|
|
|
if len(toks) <= 3:
|
2012-08-14 03:55:48 +00:00
|
|
|
|
print ("send: not enough arguments.")
|
2015-01-02 19:44:56 +00:00
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
if toks[1] not in srv.channels:
|
|
|
|
|
print ("send: channel `%s' not authorized in server `%s'."
|
|
|
|
|
% (toks[1], srv.id))
|
|
|
|
|
return 3
|
2012-08-14 03:55:48 +00:00
|
|
|
|
|
2015-04-06 07:50:13 +00:00
|
|
|
|
from nemubot.message import Text
|
|
|
|
|
srv.send_response(Text(" ".join(toks[2:]), server=None,
|
|
|
|
|
to=[toks[1]]))
|
2015-01-02 19:44:56 +00:00
|
|
|
|
return 0
|
2012-08-14 03:55:48 +00:00
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
|
2014-08-28 10:26:02 +00:00
|
|
|
|
@hook("prompt_cmd", "zap")
|
2015-01-02 19:44:56 +00:00
|
|
|
|
def zap(toks, **kwargs):
|
2012-08-14 03:55:48 +00:00
|
|
|
|
"""Hard change connexion state"""
|
2015-01-02 19:44:56 +00:00
|
|
|
|
srv = getserver(toks, mandatory=True, **kwargs)
|
|
|
|
|
|
|
|
|
|
srv.connected = not srv.connected
|
2014-07-17 09:58:24 +00:00
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
|
2014-08-28 10:26:02 +00:00
|
|
|
|
@hook("prompt_cmd", "top")
|
2015-01-02 19:44:56 +00:00
|
|
|
|
def top(toks, context, **kwargs):
|
2014-07-17 09:58:24 +00:00
|
|
|
|
"""Display consumers load information"""
|
2014-11-13 01:51:49 +00:00
|
|
|
|
print("Queue size: %d, %d thread(s) running (counter: %d)" %
|
|
|
|
|
(context.cnsr_queue.qsize(),
|
|
|
|
|
len(context.cnsr_thrd),
|
|
|
|
|
context.cnsr_thrd_size))
|
2014-07-17 09:58:24 +00:00
|
|
|
|
if len(context.events) > 0:
|
2014-11-13 01:51:49 +00:00
|
|
|
|
print("Events registered: %d, next in %d seconds" %
|
|
|
|
|
(len(context.events),
|
|
|
|
|
context.events[0].time_left.seconds))
|
2014-07-17 09:58:24 +00:00
|
|
|
|
else:
|
|
|
|
|
print("No events registered")
|
|
|
|
|
|
|
|
|
|
for th in context.cnsr_thrd:
|
|
|
|
|
if th.is_alive():
|
2014-11-13 01:51:49 +00:00
|
|
|
|
print(("#" * 15 + " Stack trace for thread %u " + "#" * 15) %
|
|
|
|
|
th.ident)
|
2014-07-17 09:58:24 +00:00
|
|
|
|
traceback.print_stack(sys._current_frames()[th.ident])
|
2014-07-23 14:32:42 +00:00
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
|
2014-08-28 10:26:02 +00:00
|
|
|
|
@hook("prompt_cmd", "netstat")
|
2015-01-02 19:44:56 +00:00
|
|
|
|
def netstat(toks, context, **kwargs):
|
2014-07-23 14:32:42 +00:00
|
|
|
|
"""Display sockets in use and many other things"""
|
|
|
|
|
if len(context.network) > 0:
|
|
|
|
|
print("Distant bots connected: %d:" % len(context.network))
|
|
|
|
|
for name, bot in context.network.items():
|
|
|
|
|
print("# %s:" % name)
|
|
|
|
|
print(" * Declared hooks:")
|
|
|
|
|
lvl = 0
|
|
|
|
|
for hlvl in bot.hooks:
|
|
|
|
|
lvl += 1
|
2014-11-13 01:51:49 +00:00
|
|
|
|
for hook in (hlvl.all_pre + hlvl.all_post + hlvl.cmd_rgxp +
|
|
|
|
|
hlvl.cmd_default + hlvl.ask_rgxp +
|
|
|
|
|
hlvl.ask_default + hlvl.msg_rgxp +
|
|
|
|
|
hlvl.msg_default):
|
2014-07-23 14:32:42 +00:00
|
|
|
|
print(" %s- %s" % (' ' * lvl * 2, hook))
|
2014-11-13 01:51:49 +00:00
|
|
|
|
for kind in ["irc_hook", "cmd_hook", "ask_hook", "msg_hook"]:
|
|
|
|
|
print(" %s- <%s> %s" % (' ' * lvl * 2, kind,
|
|
|
|
|
", ".join(hlvl.__dict__[kind].keys())))
|
2014-07-23 14:32:42 +00:00
|
|
|
|
print(" * My tag: %d" % bot.my_tag)
|
|
|
|
|
print(" * Tags in use (%d):" % bot.inc_tag)
|
|
|
|
|
for tag, (cmd, data) in bot.tags.items():
|
|
|
|
|
print(" - %11s: %s « %s »" % (tag, cmd, data))
|
|
|
|
|
else:
|
|
|
|
|
print("No distant bot connected")
|