From c0f5bedbd3dc89e2113560c5e79307ed914fc3a7 Mon Sep 17 00:00:00 2001 From: James Lu Date: Wed, 2 Sep 2015 23:08:04 -0700 Subject: [PATCH] Add new "networks" plugin, for controlling connections to networks Daemonized threads are lovely; when all connections are closed, PyLink just exits :) Closes #82. --- config.yml.example | 1 + plugins/networks.py | 78 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 plugins/networks.py diff --git a/config.yml.example b/config.yml.example index 27c8e0f..9736985 100644 --- a/config.yml.example +++ b/config.yml.example @@ -110,5 +110,6 @@ servers: # Plugins to load (omit the .py extension) plugins: - commands + - networks # - admin # - relay diff --git a/plugins/networks.py b/plugins/networks.py new file mode 100644 index 0000000..d0c266f --- /dev/null +++ b/plugins/networks.py @@ -0,0 +1,78 @@ +"""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 conf +import utils +import world +from log import log + +@utils.add_cmd +def disconnect(irc, source, args): + """ + + Disconnects the 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).""" + utils.checkAuthenticated(irc, source, allowOper=False) + try: + netname = args[0] + network = world.networkobjects[netname] + except IndexError: # No argument given. + utils.msg(irc, source, 'Error: Not enough arguments (needs 1: network name (case sensitive)).') + return + except KeyError: # Unknown network. + utils.msg(irc, source, 'Error: No such network "%s" (case sensitive).' % netname) + return + utils.msg(irc, source, "Done.") + # Abort the connection! Simple as that. + network.aborted.set() + +@utils.add_cmd +def connect(irc, source, args): + """ + + Initiates a connection to the network .""" + utils.checkAuthenticated(irc, source, allowOper=False) + try: + netname = args[0] + network = world.networkobjects[netname] + except IndexError: # No argument given. + utils.msg(irc, source, 'Error: Not enough arguments (needs 1: network name (case sensitive)).') + return + except KeyError: # Unknown network. + utils.msg(irc, source, 'Error: No such network "%s" (case sensitive).' % netname) + return + if network.connection_thread.is_alive(): + utils.msg(irc, source, 'Error: Network "%s" seems to be already connected.' % netname) + else: # Reconnect the network! + network.initVars() + network.connection_thread = threading.Thread(target=network.connect) + network.connection_thread.start() + utils.msg(irc, source, "Done.") + +@utils.add_cmd +def autoconnect(irc, source, args): + """ + + Sets the autoconnect time for to . + You can disable autoconnect for a network by setting to a negative value.""" + utils.checkAuthenticated(irc, source, allowOper=False) + try: + netname = args[0] + seconds = float(args[1]) + network = world.networkobjects[netname] + except IndexError: # Arguments not given. + utils.msg(irc, source, 'Error: Not enough arguments (needs 2: network name (case sensitive), autoconnect time (in seconds)).') + return + except KeyError: # Unknown network. + utils.msg(irc, source, 'Error: No such network "%s" (case sensitive).' % netname) + return + except ValueError: + utils.msg(irc, source, 'Error: Invalid argument "%s" for .' % seconds) + return + network.serverdata['autoconnect'] = seconds + utils.msg(irc, source, "Done.")