2015-09-03 08:08:04 +02:00
|
|
|
"""Networks plugin - allows you to manipulate connections to various configured networks."""
|
2016-07-25 20:00:56 +02:00
|
|
|
import importlib
|
2017-03-24 08:59:48 +01:00
|
|
|
import types
|
2017-04-01 01:40:26 +02:00
|
|
|
import threading
|
2015-09-03 08:08:04 +02:00
|
|
|
|
2016-06-23 04:10:59 +02:00
|
|
|
from pylinkirc import utils, world, conf, classes
|
2016-06-21 03:18:54 +02:00
|
|
|
from pylinkirc.log import log
|
2016-12-10 06:55:34 +01:00
|
|
|
from pylinkirc.coremods import control, permissions
|
2015-09-03 08:08:04 +02:00
|
|
|
|
2017-04-01 01:40:26 +02:00
|
|
|
REMOTE_IN_USE = threading.Event()
|
|
|
|
|
2015-09-03 08:08:04 +02:00
|
|
|
@utils.add_cmd
|
|
|
|
def disconnect(irc, source, args):
|
|
|
|
"""<network>
|
|
|
|
|
|
|
|
Disconnects the network <network>. When all networks are disconnected, PyLink will automatically exit.
|
2016-12-10 06:51:57 +01:00
|
|
|
|
|
|
|
To reconnect a network disconnected using this command, use REHASH to reload the networks list."""
|
2017-08-02 16:24:23 +02:00
|
|
|
permissions.check_permissions(irc, source, ['networks.disconnect'])
|
2015-09-03 08:08:04 +02:00
|
|
|
try:
|
|
|
|
netname = args[0]
|
|
|
|
network = world.networkobjects[netname]
|
|
|
|
except IndexError: # No argument given.
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('Not enough arguments (needs 1: network name (case sensitive)).')
|
2015-09-03 08:08:04 +02:00
|
|
|
return
|
|
|
|
except KeyError: # Unknown network.
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('No such network "%s" (case sensitive).' % netname)
|
2015-09-03 08:08:04 +02:00
|
|
|
return
|
2016-07-02 04:54:19 +02:00
|
|
|
irc.reply("Done. If you want to reconnect this network, use the 'rehash' command.")
|
2017-07-13 07:12:55 +02:00
|
|
|
log.info('Disconnecting network %r per %s', netname, irc.get_hostmask(source))
|
2016-01-10 05:03:42 +01:00
|
|
|
|
2016-07-02 05:08:38 +02:00
|
|
|
control.remove_network(network)
|
2016-03-05 18:42:51 +01:00
|
|
|
|
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."""
|
2017-08-02 16:24:23 +02:00
|
|
|
permissions.check_permissions(irc, source, ['networks.autoconnect'])
|
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.
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.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.
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('No such network "%s" (case sensitive).' % netname)
|
2015-09-03 08:08:04 +02:00
|
|
|
return
|
|
|
|
except ValueError:
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.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
|
|
|
|
2017-03-24 08:25:19 +01:00
|
|
|
remote_parser = utils.IRCParser()
|
|
|
|
remote_parser.add_argument('network')
|
|
|
|
remote_parser.add_argument('--service', type=str, default='pylink')
|
|
|
|
remote_parser.add_argument('command', nargs=utils.IRCParser.REMAINDER)
|
2015-12-22 19:58:58 +01:00
|
|
|
@utils.add_cmd
|
|
|
|
def remote(irc, source, args):
|
2017-03-24 08:25:19 +01:00
|
|
|
"""<network> [--service <service name>] <command>
|
2015-12-22 19:58:58 +01:00
|
|
|
|
2017-03-24 08:59:48 +01:00
|
|
|
Runs <command> on the remote network <network>. Plugin responses sent using irc.reply() are
|
|
|
|
supported and returned here, but others are dropped due to protocol limitations."""
|
2017-08-02 16:24:23 +02:00
|
|
|
permissions.check_permissions(irc, source, ['networks.remote'])
|
2015-12-22 19:58:58 +01:00
|
|
|
|
2017-03-24 08:25:19 +01:00
|
|
|
args = remote_parser.parse_args(args)
|
|
|
|
netname = args.network
|
2017-03-24 09:08:01 +01:00
|
|
|
|
2017-04-01 02:13:04 +02:00
|
|
|
if not args.command:
|
|
|
|
irc.error("No command specified!")
|
|
|
|
return
|
|
|
|
|
2017-04-01 01:40:26 +02:00
|
|
|
# XXX: things like 'remote network1 remote network2 echo hi' will crash PyLink if the source network is network1...
|
|
|
|
global REMOTE_IN_USE
|
|
|
|
if REMOTE_IN_USE.is_set():
|
|
|
|
irc.error("The 'remote' command can not be nested.")
|
|
|
|
return
|
|
|
|
|
|
|
|
REMOTE_IN_USE.set()
|
2017-03-24 09:08:01 +01:00
|
|
|
if netname == irc.name:
|
|
|
|
# This would actually throw _remote_reply() into a loop, so check for it here...
|
|
|
|
# XXX: properly fix this.
|
|
|
|
irc.error("Cannot remote-send a command to the local network; use a normal command!")
|
2017-04-01 01:40:26 +02:00
|
|
|
REMOTE_IN_USE.clear()
|
2017-03-24 09:08:01 +01:00
|
|
|
return
|
|
|
|
|
2015-12-22 19:58:58 +01:00
|
|
|
try:
|
|
|
|
remoteirc = world.networkobjects[netname]
|
|
|
|
except KeyError: # Unknown network.
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('No such network "%s" (case sensitive).' % netname)
|
2017-04-01 01:40:26 +02:00
|
|
|
REMOTE_IN_USE.clear()
|
2015-12-22 19:58:58 +01:00
|
|
|
return
|
|
|
|
|
2017-03-24 08:25:19 +01:00
|
|
|
if args.service not in world.services:
|
2017-03-24 09:10:56 +01:00
|
|
|
irc.error('Unknown service %r.' % args.service)
|
2017-04-01 01:40:26 +02:00
|
|
|
REMOTE_IN_USE.clear()
|
2015-12-22 19:58:58 +01:00
|
|
|
return
|
|
|
|
|
2016-07-01 03:22:45 +02:00
|
|
|
# Force remoteirc.called_in to something private in order to prevent
|
2015-12-22 19:58:58 +01:00
|
|
|
# accidental information leakage from replies.
|
2017-05-13 04:58:03 +02:00
|
|
|
try:
|
|
|
|
remoteirc.called_in = remoteirc.called_by = remoteirc.pseudoclient.uid
|
2015-12-22 19:58:58 +01:00
|
|
|
|
2017-05-13 04:58:03 +02:00
|
|
|
# Set the identification override to the caller's account.
|
|
|
|
remoteirc.pseudoclient.account = irc.users[source].account
|
|
|
|
except:
|
|
|
|
REMOTE_IN_USE.clear()
|
|
|
|
raise
|
2015-12-22 19:58:58 +01:00
|
|
|
|
2017-03-24 08:59:48 +01:00
|
|
|
def _remote_reply(placeholder_self, text, **kwargs):
|
|
|
|
"""
|
|
|
|
reply() rerouter for the 'remote' command.
|
|
|
|
"""
|
2017-03-24 09:08:01 +01:00
|
|
|
assert irc.name != placeholder_self.name, \
|
|
|
|
"Refusing to route reply back to the same " \
|
|
|
|
"network, as this would cause a recursive loop"
|
2017-03-24 08:59:48 +01:00
|
|
|
log.debug('(%s) networks.remote: re-routing reply %r from network %s', irc.name,
|
|
|
|
text, placeholder_self.name)
|
|
|
|
|
|
|
|
# Override the source option to make sure the source is valid on the local network.
|
|
|
|
if 'source' in kwargs:
|
|
|
|
del kwargs['source']
|
2017-03-27 23:59:33 +02:00
|
|
|
irc.reply(text, source=irc.pseudoclient.uid, **kwargs)
|
2017-03-24 08:59:48 +01:00
|
|
|
|
2017-04-01 01:25:28 +02:00
|
|
|
old_reply = remoteirc._reply
|
2017-03-24 08:59:48 +01:00
|
|
|
|
2017-07-14 14:22:05 +02:00
|
|
|
with remoteirc._reply_lock:
|
2017-03-24 08:59:48 +01:00
|
|
|
try: # Remotely call the command (use the PyLink client as a dummy user).
|
|
|
|
# Override the remote irc.reply() to send replies HERE.
|
|
|
|
log.debug('(%s) networks.remote: overriding reply() of IRC object %s', irc.name, netname)
|
2017-04-01 01:25:28 +02:00
|
|
|
remoteirc._reply = types.MethodType(_remote_reply, remoteirc)
|
2017-03-24 08:59:48 +01:00
|
|
|
world.services[args.service].call_cmd(remoteirc, remoteirc.pseudoclient.uid,
|
|
|
|
' '.join(args.command))
|
|
|
|
finally:
|
|
|
|
# Restore the original remoteirc.reply()
|
|
|
|
log.debug('(%s) networks.remote: restoring reply() of IRC object %s', irc.name, netname)
|
2017-04-01 01:25:28 +02:00
|
|
|
remoteirc._reply = old_reply
|
2017-03-24 08:59:48 +01:00
|
|
|
# Remove the identification override after we finish.
|
2017-04-10 00:21:19 +02:00
|
|
|
try:
|
|
|
|
remoteirc.pseudoclient.account = ''
|
|
|
|
except:
|
|
|
|
log.warning('(%s) networks.remote: failed to restore pseudoclient account for %s; '
|
|
|
|
'did the remote network disconnect while running this command?', irc.name, netname)
|
2017-04-01 01:40:26 +02:00
|
|
|
REMOTE_IN_USE.clear()
|
2016-07-25 20:00:56 +02:00
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def reloadproto(irc, source, args):
|
|
|
|
"""<protocol module name>
|
|
|
|
|
|
|
|
Reloads the given protocol module without restart. You will have to manually disconnect and reconnect any network using the module for changes to apply."""
|
2017-08-02 16:24:23 +02:00
|
|
|
permissions.check_permissions(irc, source, ['networks.reloadproto'])
|
2016-07-25 20:00:56 +02:00
|
|
|
try:
|
|
|
|
name = args[0]
|
|
|
|
except IndexError:
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('Not enough arguments (needs 1: protocol module name)')
|
2016-07-25 20:00:56 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
proto = utils.getProtocolModule(name)
|
|
|
|
importlib.reload(proto)
|
|
|
|
|
|
|
|
irc.reply("Done. You will have to manually disconnect and reconnect any network using the %r module for changes to apply." % name)
|