Remove legacy prompt
This commit is contained in:
parent
a7d7013639
commit
38fd9e5091
@ -1,202 +0,0 @@
|
||||
# Nemubot is a smart and modulable IM bot.
|
||||
# Copyright (C) 2012-2015 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 traceback
|
||||
import sys
|
||||
|
||||
from nemubot.hooks import hook
|
||||
|
||||
nemubotversion = 3.4
|
||||
NODATA = True
|
||||
|
||||
|
||||
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
|
||||
else:
|
||||
from nemubot.prompt.error import PromptError
|
||||
raise PromptError("Please SELECT a server or give its name in argument.")
|
||||
|
||||
|
||||
@hook("prompt_cmd", "close")
|
||||
def close(toks, context, **kwargs):
|
||||
"""Disconnect and forget (remove from the servers list) the server"""
|
||||
srv = getserver(toks, context=context, mandatory=True, **kwargs)
|
||||
|
||||
if srv.close():
|
||||
del context.servers[srv.id]
|
||||
return 0
|
||||
return 1
|
||||
|
||||
|
||||
@hook("prompt_cmd", "connect")
|
||||
def connect(toks, **kwargs):
|
||||
"""Make the connexion to a server"""
|
||||
srv = getserver(toks, mandatory=True, **kwargs)
|
||||
|
||||
return not srv.open()
|
||||
|
||||
|
||||
@hook("prompt_cmd", "disconnect")
|
||||
def disconnect(toks, **kwargs):
|
||||
"""Close the connection to a server"""
|
||||
srv = getserver(toks, mandatory=True, **kwargs)
|
||||
|
||||
return not srv.close()
|
||||
|
||||
|
||||
@hook("prompt_cmd", "discover")
|
||||
def discover(toks, context, **kwargs):
|
||||
"""Discover a new bot on a server"""
|
||||
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()
|
||||
else:
|
||||
print(" %s is not a valid fullname, for example: "
|
||||
"nemubot!nemubotV3@bot.nemunai.re" % ''.join(toks[1:1]))
|
||||
return 1
|
||||
|
||||
|
||||
@hook("prompt_cmd", "join")
|
||||
@hook("prompt_cmd", "leave")
|
||||
@hook("prompt_cmd", "part")
|
||||
def join(toks, **kwargs):
|
||||
"""Join or leave a channel"""
|
||||
srv = getserver(toks, mandatory=True, **kwargs)
|
||||
|
||||
if len(toks) <= 2:
|
||||
print("%s: not enough arguments." % toks[0])
|
||||
return 1
|
||||
|
||||
if toks[0] == "join":
|
||||
if len(toks) > 2:
|
||||
srv.write("JOIN %s %s" % (toks[1], toks[2]))
|
||||
else:
|
||||
srv.write("JOIN %s" % toks[1])
|
||||
|
||||
elif toks[0] == "leave" or toks[0] == "part":
|
||||
if len(toks) > 2:
|
||||
srv.write("PART %s :%s" % (toks[1], " ".join(toks[2:])))
|
||||
else:
|
||||
srv.write("PART %s" % toks[1])
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
@hook("prompt_cmd", "save")
|
||||
def save_mod(toks, context, **kwargs):
|
||||
"""Force save module data"""
|
||||
if len(toks) < 2:
|
||||
print("save: not enough arguments.")
|
||||
return 1
|
||||
|
||||
wrn = 0
|
||||
for mod in toks[1:]:
|
||||
if mod in context.modules:
|
||||
context.modules[mod].save()
|
||||
print("save: module `%s´ saved successfully" % mod)
|
||||
else:
|
||||
wrn += 1
|
||||
print("save: no module named `%s´" % mod)
|
||||
return wrn
|
||||
|
||||
|
||||
@hook("prompt_cmd", "send")
|
||||
def send(toks, **kwargs):
|
||||
"""Send a message on a channel"""
|
||||
srv = getserver(toks, mandatory=True, **kwargs)
|
||||
|
||||
# Check the server is connected
|
||||
if not srv.connected:
|
||||
print ("send: server `%s' not connected." % srv.id)
|
||||
return 2
|
||||
|
||||
if len(toks) <= 3:
|
||||
print ("send: not enough arguments.")
|
||||
return 1
|
||||
|
||||
if toks[1] not in srv.channels:
|
||||
print ("send: channel `%s' not authorized in server `%s'."
|
||||
% (toks[1], srv.id))
|
||||
return 3
|
||||
|
||||
from nemubot.message import Text
|
||||
srv.send_response(Text(" ".join(toks[2:]), server=None,
|
||||
to=[toks[1]]))
|
||||
return 0
|
||||
|
||||
|
||||
@hook("prompt_cmd", "zap")
|
||||
def zap(toks, **kwargs):
|
||||
"""Hard change connexion state"""
|
||||
srv = getserver(toks, mandatory=True, **kwargs)
|
||||
|
||||
srv.connected = not srv.connected
|
||||
|
||||
|
||||
@hook("prompt_cmd", "top")
|
||||
def top(toks, context, **kwargs):
|
||||
"""Display consumers load information"""
|
||||
print("Queue size: %d, %d thread(s) running (counter: %d)" %
|
||||
(context.cnsr_queue.qsize(),
|
||||
len(context.cnsr_thrd),
|
||||
context.cnsr_thrd_size))
|
||||
if len(context.events) > 0:
|
||||
print("Events registered: %d, next in %d seconds" %
|
||||
(len(context.events),
|
||||
context.events[0].time_left.seconds))
|
||||
else:
|
||||
print("No events registered")
|
||||
|
||||
for th in context.cnsr_thrd:
|
||||
if th.is_alive():
|
||||
print(("#" * 15 + " Stack trace for thread %u " + "#" * 15) %
|
||||
th.ident)
|
||||
traceback.print_stack(sys._current_frames()[th.ident])
|
||||
|
||||
|
||||
@hook("prompt_cmd", "netstat")
|
||||
def netstat(toks, context, **kwargs):
|
||||
"""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
|
||||
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):
|
||||
print(" %s- %s" % (' ' * lvl * 2, hook))
|
||||
for kind in ["irc_hook", "cmd_hook", "ask_hook", "msg_hook"]:
|
||||
print(" %s- <%s> %s" % (' ' * lvl * 2, kind,
|
||||
", ".join(hlvl.__dict__[kind].keys())))
|
||||
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")
|
@ -127,11 +127,6 @@ def reload():
|
||||
|
||||
nemubot.message.reload()
|
||||
|
||||
import nemubot.prompt
|
||||
imp.reload(nemubot.prompt)
|
||||
|
||||
nemubot.prompt.reload()
|
||||
|
||||
import nemubot.server
|
||||
rl, wl, xl = nemubot.server._rlist, nemubot.server._wlist, nemubot.server._xlist
|
||||
imp.reload(nemubot.server)
|
||||
|
@ -133,10 +133,6 @@ def main():
|
||||
if args.no_connect:
|
||||
context.noautoconnect = True
|
||||
|
||||
# Load the prompt
|
||||
import nemubot.prompt
|
||||
prmpt = nemubot.prompt.Prompt()
|
||||
|
||||
# Register the hook for futur import
|
||||
from nemubot.importer import ModuleFinder
|
||||
module_finder = ModuleFinder(context.modules_paths, context.add_module)
|
||||
|
@ -1,142 +0,0 @@
|
||||
# Nemubot is a smart and modulable IM bot.
|
||||
# Copyright (C) 2012-2015 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 shlex
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
from nemubot.prompt import builtins
|
||||
|
||||
|
||||
class Prompt:
|
||||
|
||||
def __init__(self):
|
||||
self.selectedServer = None
|
||||
self.lastretcode = 0
|
||||
|
||||
self.HOOKS_CAPS = dict()
|
||||
self.HOOKS_LIST = dict()
|
||||
|
||||
def add_cap_hook(self, name, call, data=None):
|
||||
self.HOOKS_CAPS[name] = lambda t, c: call(t, data=data,
|
||||
context=c, prompt=self)
|
||||
|
||||
def add_list_hook(self, name, call):
|
||||
self.HOOKS_LIST[name] = call
|
||||
|
||||
def lex_cmd(self, line):
|
||||
"""Return an array of tokens
|
||||
|
||||
Argument:
|
||||
line -- the line to lex
|
||||
"""
|
||||
|
||||
try:
|
||||
cmds = shlex.split(line)
|
||||
except:
|
||||
exc_type, exc_value, _ = sys.exc_info()
|
||||
sys.stderr.write(traceback.format_exception_only(exc_type,
|
||||
exc_value)[0])
|
||||
return
|
||||
|
||||
bgn = 0
|
||||
|
||||
# Separate commands (command separator: ;)
|
||||
for i in range(0, len(cmds)):
|
||||
if cmds[i][-1] == ';':
|
||||
if i != bgn:
|
||||
yield cmds[bgn:i]
|
||||
bgn = i + 1
|
||||
|
||||
# Return rest of the command (that not end with a ;)
|
||||
if bgn != len(cmds):
|
||||
yield cmds[bgn:]
|
||||
|
||||
def exec_cmd(self, toks, context):
|
||||
"""Execute the command
|
||||
|
||||
Arguments:
|
||||
toks -- lexed tokens to executes
|
||||
context -- current bot context
|
||||
"""
|
||||
|
||||
if toks[0] in builtins.CAPS:
|
||||
self.lastretcode = builtins.CAPS[toks[0]](toks, context, self)
|
||||
elif toks[0] in self.HOOKS_CAPS:
|
||||
self.lastretcode = self.HOOKS_CAPS[toks[0]](toks, context)
|
||||
else:
|
||||
print("Unknown command: `%s'" % toks[0])
|
||||
self.lastretcode = 127
|
||||
|
||||
def getPS1(self):
|
||||
"""Get the PS1 associated to the selected server"""
|
||||
if self.selectedServer is None:
|
||||
return "nemubot"
|
||||
else:
|
||||
return self.selectedServer.id
|
||||
|
||||
def run(self, context):
|
||||
"""Launch the prompt
|
||||
|
||||
Argument:
|
||||
context -- current bot context
|
||||
"""
|
||||
|
||||
from nemubot.prompt.error import PromptError
|
||||
from nemubot.prompt.reset import PromptReset
|
||||
|
||||
while True: # Stopped by exception
|
||||
try:
|
||||
line = input("\033[0;33m%s\033[0;%dm§\033[0m " %
|
||||
(self.getPS1(), 31 if self.lastretcode else 32))
|
||||
cmds = self.lex_cmd(line.strip())
|
||||
for toks in cmds:
|
||||
try:
|
||||
self.exec_cmd(toks, context)
|
||||
except PromptReset:
|
||||
raise
|
||||
except PromptError as e:
|
||||
print(e.message)
|
||||
self.lastretcode = 128
|
||||
except:
|
||||
exc_type, exc_value, exc_traceback = sys.exc_info()
|
||||
traceback.print_exception(exc_type, exc_value,
|
||||
exc_traceback)
|
||||
except KeyboardInterrupt:
|
||||
print("")
|
||||
except EOFError:
|
||||
print("quit")
|
||||
return True
|
||||
|
||||
|
||||
def hotswap(bak):
|
||||
p = Prompt()
|
||||
p.HOOKS_CAPS = bak.HOOKS_CAPS
|
||||
p.HOOKS_LIST = bak.HOOKS_LIST
|
||||
return p
|
||||
|
||||
|
||||
def reload():
|
||||
import imp
|
||||
|
||||
import nemubot.prompt.builtins
|
||||
imp.reload(nemubot.prompt.builtins)
|
||||
|
||||
import nemubot.prompt.error
|
||||
imp.reload(nemubot.prompt.error)
|
||||
|
||||
import nemubot.prompt.reset
|
||||
imp.reload(nemubot.prompt.reset)
|
@ -1,128 +0,0 @@
|
||||
# Nemubot is a smart and modulable IM bot.
|
||||
# Copyright (C) 2012-2015 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/>.
|
||||
|
||||
def end(toks, context, prompt):
|
||||
"""Quit the prompt for reload or exit"""
|
||||
from nemubot.prompt.reset import PromptReset
|
||||
|
||||
if toks[0] == "refresh":
|
||||
raise PromptReset("refresh")
|
||||
elif toks[0] == "reset":
|
||||
raise PromptReset("reset")
|
||||
raise PromptReset("quit")
|
||||
|
||||
|
||||
def liste(toks, context, prompt):
|
||||
"""Show some lists"""
|
||||
if len(toks) > 1:
|
||||
for l in toks[1:]:
|
||||
l = l.lower()
|
||||
if l == "server" or l == "servers":
|
||||
for srv in context.servers.keys():
|
||||
print (" - %s (state: %s) ;" % (srv,
|
||||
"connected" if context.servers[srv].connected else "disconnected"))
|
||||
if len(context.servers) == 0:
|
||||
print (" > No server loaded")
|
||||
|
||||
elif l == "mod" or l == "mods" or l == "module" or l == "modules":
|
||||
for mod in context.modules.keys():
|
||||
print (" - %s ;" % mod)
|
||||
if len(context.modules) == 0:
|
||||
print (" > No module loaded")
|
||||
|
||||
elif l in prompt.HOOKS_LIST:
|
||||
f, d = prompt.HOOKS_LIST[l]
|
||||
f(d, context, prompt)
|
||||
|
||||
else:
|
||||
print (" Unknown list `%s'" % l)
|
||||
return 2
|
||||
return 0
|
||||
else:
|
||||
print (" Please give a list to show: servers, ...")
|
||||
return 1
|
||||
|
||||
|
||||
def load(toks, context, prompt):
|
||||
"""Load an XML configuration file"""
|
||||
if len(toks) > 1:
|
||||
for filename in toks[1:]:
|
||||
context.load_file(filename)
|
||||
else:
|
||||
print ("Not enough arguments. `load' takes a filename.")
|
||||
return 1
|
||||
|
||||
|
||||
def select(toks, context, prompt):
|
||||
"""Select the current server"""
|
||||
if (len(toks) == 2 and toks[1] != "None" and
|
||||
toks[1] != "nemubot" and toks[1] != "none"):
|
||||
if toks[1] in context.servers:
|
||||
prompt.selectedServer = context.servers[toks[1]]
|
||||
else:
|
||||
print ("select: server `%s' not found." % toks[1])
|
||||
return 1
|
||||
else:
|
||||
prompt.selectedServer = None
|
||||
|
||||
|
||||
def unload(toks, context, prompt):
|
||||
"""Unload a module"""
|
||||
if len(toks) == 2 and toks[1] == "all":
|
||||
for name in context.modules.keys():
|
||||
context.unload_module(name)
|
||||
elif len(toks) > 1:
|
||||
for name in toks[1:]:
|
||||
if context.unload_module(name):
|
||||
print (" Module `%s' successfully unloaded." % name)
|
||||
else:
|
||||
print (" No module `%s' loaded, can't unload!" % name)
|
||||
return 2
|
||||
else:
|
||||
print ("Not enough arguments. `unload' takes a module name.")
|
||||
return 1
|
||||
|
||||
|
||||
def debug(toks, context, prompt):
|
||||
"""Enable/Disable debug mode on a module"""
|
||||
if len(toks) > 1:
|
||||
for name in toks[1:]:
|
||||
if name in context.modules:
|
||||
context.modules[name].DEBUG = not context.modules[name].DEBUG
|
||||
if context.modules[name].DEBUG:
|
||||
print (" Module `%s' now in DEBUG mode." % name)
|
||||
else:
|
||||
print (" Debug for module module `%s' disabled." % name)
|
||||
else:
|
||||
print (" No module `%s' loaded, can't debug!" % name)
|
||||
return 2
|
||||
else:
|
||||
print ("Not enough arguments. `debug' takes a module name.")
|
||||
return 1
|
||||
|
||||
|
||||
# Register build-ins
|
||||
CAPS = {
|
||||
'quit': end, # Disconnect all server and quit
|
||||
'exit': end, # Alias for quit
|
||||
'reset': end, # Reload the prompt
|
||||
'refresh': end, # Reload the prompt but save modules
|
||||
'load': load, # Load a servers or module configuration file
|
||||
'unload': unload, # Unload a module and remove it from the list
|
||||
'select': select, # Select a server
|
||||
'list': liste, # Show lists
|
||||
'debug': debug, # Pass a module in debug mode
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
# Nemubot is a smart and modulable IM bot.
|
||||
# Copyright (C) 2012-2015 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/>.
|
||||
|
||||
class PromptError(Exception):
|
||||
|
||||
def __init__(self, message):
|
||||
super(PromptError, self).__init__(message)
|
||||
self.message = message
|
@ -1,23 +0,0 @@
|
||||
# coding=utf-8
|
||||
|
||||
# Nemubot is a smart and modulable IM bot.
|
||||
# Copyright (C) 2012-2015 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/>.
|
||||
|
||||
class PromptReset(Exception):
|
||||
|
||||
def __init__(self, type):
|
||||
super(PromptReset, self).__init__("Prompt reset asked")
|
||||
self.type = type
|
Loading…
Reference in New Issue
Block a user