2015-09-03 08:08:04 +02:00
|
|
|
"""Networks plugin - allows you to manipulate connections to various configured networks."""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
|
|
|
import threading
|
|
|
|
|
|
|
|
import utils
|
|
|
|
import world
|
|
|
|
from log import log
|
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def disconnect(irc, source, args):
|
|
|
|
"""<network>
|
|
|
|
|
|
|
|
Disconnects the network <network>. When all networks are disconnected, PyLink will automatically exit.
|
|
|
|
Note: This does not affect the autoreconnect settings of any network, so the network will likely just reconnect unless autoconnect is disabled (see the 'autoconnect' command)."""
|
2016-05-01 01:54:11 +02:00
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
2015-09-03 08:08:04 +02:00
|
|
|
try:
|
|
|
|
netname = args[0]
|
|
|
|
network = world.networkobjects[netname]
|
|
|
|
except IndexError: # No argument given.
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply('Error: Not enough arguments (needs 1: network name (case sensitive)).')
|
2015-09-03 08:08:04 +02:00
|
|
|
return
|
|
|
|
except KeyError: # Unknown network.
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply('Error: No such network "%s" (case sensitive).' % netname)
|
2015-09-03 08:08:04 +02:00
|
|
|
return
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Done.")
|
2016-01-10 05:03:42 +01:00
|
|
|
|
2015-09-03 08:08:04 +02:00
|
|
|
# Abort the connection! Simple as that.
|
2016-01-10 05:03:42 +01:00
|
|
|
network.disconnect()
|
2015-09-03 08:08:04 +02:00
|
|
|
|
2016-03-05 18:42:51 +01:00
|
|
|
if network.serverdata["autoconnect"] < 1: # Remove networks if autoconnect is disabled.
|
|
|
|
del world.networkobjects[netname]
|
|
|
|
|
2015-09-03 08:08:04 +02:00
|
|
|
@utils.add_cmd
|
|
|
|
def connect(irc, source, args):
|
|
|
|
"""<network>
|
|
|
|
|
|
|
|
Initiates a connection to the network <network>."""
|
2016-05-01 01:54:11 +02:00
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
2015-09-03 08:08:04 +02:00
|
|
|
try:
|
|
|
|
netname = args[0]
|
|
|
|
network = world.networkobjects[netname]
|
|
|
|
except IndexError: # No argument given.
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply('Error: Not enough arguments (needs 1: network name (case sensitive)).')
|
2015-09-03 08:08:04 +02:00
|
|
|
return
|
|
|
|
except KeyError: # Unknown network.
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply('Error: No such network "%s" (case sensitive).' % netname)
|
2015-09-03 08:08:04 +02:00
|
|
|
return
|
|
|
|
if network.connection_thread.is_alive():
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply('Error: Network "%s" seems to be already connected.' % netname)
|
2015-09-03 08:08:04 +02:00
|
|
|
else: # Reconnect the network!
|
|
|
|
network.connection_thread = threading.Thread(target=network.connect)
|
|
|
|
network.connection_thread.start()
|
2015-12-25 03:14:05 +01:00
|
|
|
|
|
|
|
# And the plugins we have too.
|
|
|
|
for plugin in world.plugins.values():
|
|
|
|
if hasattr(plugin, 'main'):
|
|
|
|
log.debug('(%s) Calling main() function of plugin %r', irc.name, plugin)
|
|
|
|
plugin.main(irc)
|
|
|
|
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Done.")
|
2015-09-03 08:08:04 +02:00
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def autoconnect(irc, source, args):
|
|
|
|
"""<network> <seconds>
|
|
|
|
|
|
|
|
Sets the autoconnect time for <network> to <seconds>.
|
|
|
|
You can disable autoconnect for a network by setting <seconds> to a negative value."""
|
2016-05-01 01:54:11 +02:00
|
|
|
irc.checkAuthenticated(source)
|
2015-09-03 08:08:04 +02:00
|
|
|
try:
|
|
|
|
netname = args[0]
|
|
|
|
seconds = float(args[1])
|
|
|
|
network = world.networkobjects[netname]
|
|
|
|
except IndexError: # Arguments not given.
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply('Error: Not enough arguments (needs 2: network name (case sensitive), autoconnect time (in seconds)).')
|
2015-09-03 08:08:04 +02:00
|
|
|
return
|
|
|
|
except KeyError: # Unknown network.
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply('Error: No such network "%s" (case sensitive).' % netname)
|
2015-09-03 08:08:04 +02:00
|
|
|
return
|
|
|
|
except ValueError:
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply('Error: Invalid argument "%s" for <seconds>.' % seconds)
|
2015-09-03 08:08:04 +02:00
|
|
|
return
|
|
|
|
network.serverdata['autoconnect'] = seconds
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Done.")
|
2015-12-22 19:58:58 +01:00
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def remote(irc, source, args):
|
|
|
|
"""<network> <command>
|
|
|
|
|
|
|
|
Runs <command> on the remote network <network>. No replies are sent back due to protocol limitations."""
|
2016-05-01 01:54:11 +02:00
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
2015-12-22 19:58:58 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
netname = args[0]
|
|
|
|
cmd_args = ' '.join(args[1:]).strip()
|
|
|
|
remoteirc = world.networkobjects[netname]
|
|
|
|
except IndexError: # Arguments not given.
|
|
|
|
irc.reply('Error: Not enough arguments (needs 2 or more: network name (case sensitive), command name & arguments).')
|
|
|
|
return
|
|
|
|
except KeyError: # Unknown network.
|
|
|
|
irc.reply('Error: No such network "%s" (case sensitive).' % netname)
|
|
|
|
return
|
|
|
|
|
|
|
|
if not cmd_args:
|
|
|
|
irc.reply('No text entered!')
|
|
|
|
return
|
|
|
|
|
|
|
|
# Force remoteirc.called_by to something private in order to prevent
|
|
|
|
# accidental information leakage from replies.
|
|
|
|
remoteirc.called_by = remoteirc.pseudoclient.uid
|
|
|
|
|
|
|
|
# Set PyLink's identification to admin.
|
|
|
|
remoteirc.pseudoclient.identified = "<PyLink networks.remote override>"
|
|
|
|
|
|
|
|
try: # Remotely call the command (use the PyLink client as a dummy user).
|
|
|
|
remoteirc.callCommand(remoteirc.pseudoclient.uid, cmd_args)
|
|
|
|
finally: # Remove the identification override after we finish.
|
2016-02-08 03:09:02 +01:00
|
|
|
remoteirc.pseudoclient.identified = ''
|
2015-12-22 19:58:58 +01:00
|
|
|
|
|
|
|
irc.reply("Done.")
|