[cmd_server] rework due to previous prompt rework

This commit is contained in:
nemunaire 2015-01-02 20:44:56 +01:00
parent fd6d9288f7
commit 8aebeb6346

View file

@ -19,6 +19,7 @@
import traceback import traceback
import sys import sys
from prompt.error import PromptError
from hooks import hook from hooks import hook
from message import TextMessage from message import TextMessage
from networkbot import NetworkBot from networkbot import NetworkBot
@ -27,209 +28,137 @@ nemubotversion = 3.4
NODATA = True NODATA = True
def getserver(toks, context, prompt): def getserver(toks, context, prompt, mandatory=False, **kwargs):
"""Choose the server in toks or prompt""" """Choose the server in toks or prompt.
if len(toks) > 1 and toks[0] in context.servers: This function modify the tokens list passed as argument"""
return (context.servers[toks[0]], toks[1:])
elif prompt.selectedServer is not None: if len(toks) > 1 and toks[1] in context.servers:
return (prompt.selectedServer, toks) return context.servers[toks.pop(1)]
elif not mandatory or prompt.selectedServer:
return prompt.selectedServer
else: else:
return (None, toks) raise PromptError("Please SELECT a server or give its name in argument.")
@hook("prompt_cmd", "close") @hook("prompt_cmd", "close")
def close(data, toks, context, prompt): def close(toks, context, **kwargs):
"""Disconnect and forget (remove from the servers list) the server""" """Disconnect and forget (remove from the servers list) the server"""
if len(toks) > 1: srv = getserver(toks, context=context, mandatory=True, **kwargs)
for s in toks[1:]:
if s in servers: if srv.close():
context.servers[s].close() del context.servers[srv.id]
del context.servers[s] return 0
else: return 1
print ("close: server `%s' not found." % s)
elif prompt.selectedServer is not None:
prompt.selectedServer.close()
del prompt.servers[selectedServer.id]
prompt.selectedServer = None
return
@hook("prompt_cmd", "connect") @hook("prompt_cmd", "connect")
def connect(data, toks, context, prompt): def connect(toks, **kwargs):
"""Make the connexion to a server""" """Make the connexion to a server"""
if len(toks) > 1: srv = getserver(toks, mandatory=True, **kwargs)
for s in toks[1:]:
if s in context.servers:
context.servers[s].open()
else:
print ("connect: server `%s' not found." % s)
elif prompt.selectedServer is not None: return not srv.open()
prompt.selectedServer.open()
else:
print (" Please SELECT a server or give its name in argument.")
@hook("prompt_cmd", "disconnect") @hook("prompt_cmd", "disconnect")
def disconnect(data, toks, context, prompt): def disconnect(toks, **kwargs):
"""Close the connection to a server""" """Close the connection to a server"""
if len(toks) > 1: srv = getserver(toks, mandatory=True, **kwargs)
for s in toks[1:]:
if s in context.servers: return not srv.close()
if not context.servers[s].close():
print ("disconnect: server `%s' already disconnected." % s)
else:
print ("disconnect: server `%s' not found." % s)
elif prompt.selectedServer is not None:
if not prompt.selectedServer.close():
print ("disconnect: server `%s' already disconnected."
% prompt.selectedServer.id)
else:
print (" Please SELECT a server or give its name in argument.")
@hook("prompt_cmd", "discover") @hook("prompt_cmd", "discover")
def discover(data, toks, context, prompt): def discover(toks, context, **kwargs):
"""Discover a new bot on a server""" """Discover a new bot on a server"""
(srv, toks) = getserver(toks, context, prompt) srv = getserver(toks, context=context, mandatory=True, **kwargs)
if srv is not None:
for name in toks[1:]:
if "!" in name:
bot = context.add_networkbot(srv, name)
bot.connect()
else:
print (" %s is not a valid fullname, for example: "
"nemubot!nemubotV3@bot.nemunai.re")
else:
print (" Please SELECT a server or give its name in first argument.")
if len(toks) > 1 and "!" in toks[1]:
@hook("prompt_cmd", "hotswap") bot = context.add_networkbot(srv, name)
def hotswap(data, toks, context, prompt): return not bot.connect()
"""Reload a server class"""
if len(toks) > 1:
print ("hotswap: apply only on selected server")
elif prompt.selectedServer is not None:
del context.servers[prompt.selectedServer.id]
srv = server.Server(selectedServer.node, selectedServer.nick,
selectedServer.owner, selectedServer.realname,
selectedServer.s)
context.servers[srv.id] = srv
prompt.selectedServer.kill()
prompt.selectedServer = srv
prompt.selectedServer.start()
else: else:
print (" Please SELECT a server or give its name in argument.") 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", "join")
@hook("prompt_cmd", "leave") @hook("prompt_cmd", "leave")
@hook("prompt_cmd", "part") @hook("prompt_cmd", "part")
def join(data, toks, context, prompt): def join(toks, **kwargs):
"""Join or leave a channel""" """Join or leave a channel"""
rd = 1 srv = getserver(toks, mandatory=True, **kwargs)
if len(toks) <= rd:
print ("%s: not enough arguments." % toks[0])
return
if toks[rd] in context.servers: if len(toks) <= 2:
srv = context.servers[toks[rd]]
rd += 1
elif prompt.selectedServer is not None:
srv = prompt.selectedServer
else:
print (" Please SELECT a server or give its name in argument.")
return
if len(toks) <= rd:
print("%s: not enough arguments." % toks[0]) print("%s: not enough arguments." % toks[0])
return return 1
if toks[0] == "join": if toks[0] == "join":
if len(toks) > rd + 1: if len(toks) > 2:
srv.write("JOIN %s %s" % (toks[rd], toks[rd + 1])) srv.write("JOIN %s %s" % (toks[1], toks[2]))
else: else:
srv.write("JOIN %s" % toks[rd]) srv.write("JOIN %s" % toks[1])
elif toks[0] == "leave" or toks[0] == "part": elif toks[0] == "leave" or toks[0] == "part":
srv.write("PART %s" % toks[rd]) if len(toks) > 2:
return srv.write("PART %s :%s" % (toks[1], " ".join(toks[2:])))
else:
srv.write("PART %s" % toks[1])
return 0
@hook("prompt_cmd", "save") @hook("prompt_cmd", "save")
def save_mod(data, toks, context, prompt): def save_mod(toks, context, **kwargs):
"""Force save module data""" """Force save module data"""
if len(toks) < 2: if len(toks) < 2:
print ("save: not enough arguments.") print("save: not enough arguments.")
return return 1
wrn = 0
for mod in toks[1:]: for mod in toks[1:]:
if mod in context.modules: if mod in context.modules:
context.modules[mod].save() context.modules[mod].save()
print ("save: module `%s´ saved successfully" % mod) print("save: module `%s´ saved successfully" % mod)
else: else:
print ("save: no module named `%s´" % mod) wrn += 1
return print("save: no module named `%s´" % mod)
return wrn
@hook("prompt_cmd", "send") @hook("prompt_cmd", "send")
def send(data, toks, context, prompt): def send(toks, **kwargs):
"""Send a message on a channel""" """Send a message on a channel"""
rd = 1 srv = getserver(toks, mandatory=True, **kwargs)
if len(toks) <= rd:
print ("send: not enough arguments.")
return
if toks[rd] in context.servers:
srv = context.servers[toks[rd]]
rd += 1
elif prompt.selectedServer is not None:
srv = prompt.selectedServer
else:
print (" Please SELECT a server or give its name in argument.")
return
if len(toks) <= rd:
print ("send: not enough arguments.")
return
# Check the server is connected # Check the server is connected
if not srv.connected: if not srv.connected:
print ("send: server `%s' not connected." % srv.id) print ("send: server `%s' not connected." % srv.id)
return return 2
if toks[rd] in srv.channels: if len(toks) <= 3:
chan = toks[rd]
rd += 1
else:
print ("send: channel `%s' not authorized in server `%s'."
% (toks[rd], srv.id))
return
if len(toks) <= rd:
print ("send: not enough arguments.") print ("send: not enough arguments.")
return return 1
srv.send_response(TextMessage(" ".join(toks[rd:]), server=None, to=[chan])) if toks[1] not in srv.channels:
return "done" print ("send: channel `%s' not authorized in server `%s'."
% (toks[1], srv.id))
return 3
srv.send_response(TextMessage(" ".join(toks[2:]), server=None,
to=[toks[1]]))
return 0
@hook("prompt_cmd", "zap") @hook("prompt_cmd", "zap")
def zap(data, toks, context, prompt): def zap(toks, **kwargs):
"""Hard change connexion state""" """Hard change connexion state"""
if len(toks) > 1: srv = getserver(toks, mandatory=True, **kwargs)
for s in toks[1:]:
if s in context.servers: srv.connected = not srv.connected
context.servers[s].connected = not context.servers[s].connected
else:
print ("zap: server `%s' not found." % s)
elif prompt.selectedServer is not None:
prompt.selectedServer.connected = not prompt.selectedServer.connected
else:
print (" Please SELECT a server or give its name in argument.")
@hook("prompt_cmd", "top") @hook("prompt_cmd", "top")
def top(data, toks, context, prompt): def top(toks, context, **kwargs):
"""Display consumers load information""" """Display consumers load information"""
print("Queue size: %d, %d thread(s) running (counter: %d)" % print("Queue size: %d, %d thread(s) running (counter: %d)" %
(context.cnsr_queue.qsize(), (context.cnsr_queue.qsize(),
@ -250,7 +179,7 @@ def top(data, toks, context, prompt):
@hook("prompt_cmd", "netstat") @hook("prompt_cmd", "netstat")
def netstat(data, toks, context, prompt): def netstat(toks, context, **kwargs):
"""Display sockets in use and many other things""" """Display sockets in use and many other things"""
if len(context.network) > 0: if len(context.network) > 0:
print("Distant bots connected: %d:" % len(context.network)) print("Distant bots connected: %d:" % len(context.network))