[cmd_server] rework due to previous prompt rework
This commit is contained in:
parent
fd6d9288f7
commit
8aebeb6346
|
@ -19,6 +19,7 @@
|
|||
import traceback
|
||||
import sys
|
||||
|
||||
from prompt.error import PromptError
|
||||
from hooks import hook
|
||||
from message import TextMessage
|
||||
from networkbot import NetworkBot
|
||||
|
@ -27,209 +28,137 @@ nemubotversion = 3.4
|
|||
NODATA = True
|
||||
|
||||
|
||||
def getserver(toks, context, prompt):
|
||||
"""Choose the server in toks or prompt"""
|
||||
if len(toks) > 1 and toks[0] in context.servers:
|
||||
return (context.servers[toks[0]], toks[1:])
|
||||
elif prompt.selectedServer is not None:
|
||||
return (prompt.selectedServer, toks)
|
||||
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:
|
||||
return (None, toks)
|
||||
raise PromptError("Please SELECT a server or give its name in argument.")
|
||||
|
||||
|
||||
@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"""
|
||||
if len(toks) > 1:
|
||||
for s in toks[1:]:
|
||||
if s in servers:
|
||||
context.servers[s].close()
|
||||
del context.servers[s]
|
||||
else:
|
||||
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
|
||||
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(data, toks, context, prompt):
|
||||
def connect(toks, **kwargs):
|
||||
"""Make the connexion to a server"""
|
||||
if len(toks) > 1:
|
||||
for s in toks[1:]:
|
||||
if s in context.servers:
|
||||
context.servers[s].open()
|
||||
else:
|
||||
print ("connect: server `%s' not found." % s)
|
||||
srv = getserver(toks, mandatory=True, **kwargs)
|
||||
|
||||
elif prompt.selectedServer is not None:
|
||||
prompt.selectedServer.open()
|
||||
else:
|
||||
print (" Please SELECT a server or give its name in argument.")
|
||||
return not srv.open()
|
||||
|
||||
|
||||
@hook("prompt_cmd", "disconnect")
|
||||
def disconnect(data, toks, context, prompt):
|
||||
def disconnect(toks, **kwargs):
|
||||
"""Close the connection to a server"""
|
||||
if len(toks) > 1:
|
||||
for s in toks[1:]:
|
||||
if s in context.servers:
|
||||
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.")
|
||||
srv = getserver(toks, mandatory=True, **kwargs)
|
||||
|
||||
return not srv.close()
|
||||
|
||||
|
||||
@hook("prompt_cmd", "discover")
|
||||
def discover(data, toks, context, prompt):
|
||||
def discover(toks, context, **kwargs):
|
||||
"""Discover a new bot on a server"""
|
||||
(srv, toks) = getserver(toks, context, prompt)
|
||||
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.")
|
||||
srv = getserver(toks, context=context, mandatory=True, **kwargs)
|
||||
|
||||
|
||||
@hook("prompt_cmd", "hotswap")
|
||||
def hotswap(data, toks, context, prompt):
|
||||
"""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()
|
||||
if len(toks) > 1 and "!" in toks[1]:
|
||||
bot = context.add_networkbot(srv, name)
|
||||
return not bot.connect()
|
||||
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", "leave")
|
||||
@hook("prompt_cmd", "part")
|
||||
def join(data, toks, context, prompt):
|
||||
def join(toks, **kwargs):
|
||||
"""Join or leave a channel"""
|
||||
rd = 1
|
||||
if len(toks) <= rd:
|
||||
print ("%s: not enough arguments." % toks[0])
|
||||
return
|
||||
srv = getserver(toks, mandatory=True, **kwargs)
|
||||
|
||||
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:
|
||||
if len(toks) <= 2:
|
||||
print("%s: not enough arguments." % toks[0])
|
||||
return
|
||||
return 1
|
||||
|
||||
if toks[0] == "join":
|
||||
if len(toks) > rd + 1:
|
||||
srv.write("JOIN %s %s" % (toks[rd], toks[rd + 1]))
|
||||
if len(toks) > 2:
|
||||
srv.write("JOIN %s %s" % (toks[1], toks[2]))
|
||||
else:
|
||||
srv.write("JOIN %s" % toks[rd])
|
||||
srv.write("JOIN %s" % toks[1])
|
||||
|
||||
elif toks[0] == "leave" or toks[0] == "part":
|
||||
srv.write("PART %s" % toks[rd])
|
||||
return
|
||||
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(data, toks, context, prompt):
|
||||
def save_mod(toks, context, **kwargs):
|
||||
"""Force save module data"""
|
||||
if len(toks) < 2:
|
||||
print ("save: not enough arguments.")
|
||||
return
|
||||
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)
|
||||
print("save: module `%s´ saved successfully" % mod)
|
||||
else:
|
||||
print ("save: no module named `%s´" % mod)
|
||||
return
|
||||
wrn += 1
|
||||
print("save: no module named `%s´" % mod)
|
||||
return wrn
|
||||
|
||||
|
||||
@hook("prompt_cmd", "send")
|
||||
def send(data, toks, context, prompt):
|
||||
def send(toks, **kwargs):
|
||||
"""Send a message on a channel"""
|
||||
rd = 1
|
||||
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
|
||||
srv = getserver(toks, mandatory=True, **kwargs)
|
||||
|
||||
# Check the server is connected
|
||||
if not srv.connected:
|
||||
print ("send: server `%s' not connected." % srv.id)
|
||||
return
|
||||
return 2
|
||||
|
||||
if toks[rd] in srv.channels:
|
||||
chan = toks[rd]
|
||||
rd += 1
|
||||
else:
|
||||
print ("send: channel `%s' not authorized in server `%s'."
|
||||
% (toks[rd], srv.id))
|
||||
return
|
||||
|
||||
if len(toks) <= rd:
|
||||
if len(toks) <= 3:
|
||||
print ("send: not enough arguments.")
|
||||
return
|
||||
return 1
|
||||
|
||||
srv.send_response(TextMessage(" ".join(toks[rd:]), server=None, to=[chan]))
|
||||
return "done"
|
||||
if toks[1] not in srv.channels:
|
||||
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")
|
||||
def zap(data, toks, context, prompt):
|
||||
def zap(toks, **kwargs):
|
||||
"""Hard change connexion state"""
|
||||
if len(toks) > 1:
|
||||
for s in toks[1:]:
|
||||
if s in context.servers:
|
||||
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.")
|
||||
srv = getserver(toks, mandatory=True, **kwargs)
|
||||
|
||||
srv.connected = not srv.connected
|
||||
|
||||
|
||||
@hook("prompt_cmd", "top")
|
||||
def top(data, toks, context, prompt):
|
||||
def top(toks, context, **kwargs):
|
||||
"""Display consumers load information"""
|
||||
print("Queue size: %d, %d thread(s) running (counter: %d)" %
|
||||
(context.cnsr_queue.qsize(),
|
||||
|
@ -250,7 +179,7 @@ def top(data, toks, context, prompt):
|
|||
|
||||
|
||||
@hook("prompt_cmd", "netstat")
|
||||
def netstat(data, toks, context, prompt):
|
||||
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))
|
||||
|
|
Loading…
Reference in New Issue
Block a user