2015-12-19 06:42:46 +01:00
|
|
|
"""
|
|
|
|
opercmds.py: Provides a subset of network management commands.
|
|
|
|
"""
|
2017-08-02 15:43:58 +02:00
|
|
|
import argparse
|
|
|
|
|
2016-06-21 03:18:54 +02:00
|
|
|
from pylinkirc import utils
|
|
|
|
from pylinkirc.log import log
|
2016-12-10 06:48:39 +01:00
|
|
|
from pylinkirc.coremods import permissions
|
2015-12-19 06:42:46 +01:00
|
|
|
|
2017-08-02 15:43:58 +02:00
|
|
|
# Having a hard limit here is sensible because otherwise it can flood the client or server off.
|
|
|
|
CHECKBAN_MAX_RESULTS = 200
|
|
|
|
|
|
|
|
def _checkban_positiveint(value):
|
|
|
|
value = int(value)
|
|
|
|
if value <= 0 or value > CHECKBAN_MAX_RESULTS:
|
|
|
|
raise argparse.ArgumentTypeError("%s is not a positive integer between 1 and %s." % (value, CHECKBAN_MAX_RESULTS))
|
|
|
|
return value
|
|
|
|
|
2017-07-29 19:10:51 +02:00
|
|
|
checkban_parser = utils.IRCParser()
|
|
|
|
checkban_parser.add_argument('banmask')
|
|
|
|
checkban_parser.add_argument('target', nargs='?', default='')
|
|
|
|
checkban_parser.add_argument('--channel', default='')
|
2017-08-02 15:43:58 +02:00
|
|
|
checkban_parser.add_argument('--maxresults', type=_checkban_positiveint, default=50)
|
2017-07-29 19:10:51 +02:00
|
|
|
|
2016-01-04 06:05:30 +01:00
|
|
|
@utils.add_cmd
|
|
|
|
def checkban(irc, source, args):
|
2017-08-02 15:43:58 +02:00
|
|
|
"""<banmask> [<target nick or hostmask>] [--channel #channel] [--maxresults <num>]
|
2016-01-04 06:05:30 +01:00
|
|
|
|
2017-07-29 19:10:51 +02:00
|
|
|
CHECKBAN provides a ban checker command based on nick!user@host masks, user@host masks, and
|
|
|
|
PyLink extended targets.
|
2016-01-04 06:05:30 +01:00
|
|
|
|
2017-07-29 19:10:51 +02:00
|
|
|
If a target nick or hostmask is given, return whether the given banmask will match it.
|
|
|
|
Otherwise, returns a list of connected users that would be affected by such a ban, up to 50
|
|
|
|
results.
|
2016-01-04 06:05:30 +01:00
|
|
|
|
2017-07-29 19:10:51 +02:00
|
|
|
If the --channel argument is given without a target mask, the returned results will only
|
2017-08-02 15:43:58 +02:00
|
|
|
include users in the given channel.
|
|
|
|
|
|
|
|
The --maxresults option configures how many responses will be shown."""
|
2017-07-29 19:10:51 +02:00
|
|
|
permissions.checkPermissions(irc, source, ['opercmds.checkban'])
|
2016-01-04 06:05:30 +01:00
|
|
|
|
2017-07-29 19:10:51 +02:00
|
|
|
args = checkban_parser.parse_args(args)
|
|
|
|
if not args.target:
|
|
|
|
# No hostmask was given, return a list of matched users.
|
2016-01-04 06:05:30 +01:00
|
|
|
|
|
|
|
results = 0
|
2017-07-29 19:10:51 +02:00
|
|
|
|
|
|
|
# Process the --channel argument if it exists. This is just a lazy wrapper around the
|
|
|
|
# $and and $channel exttargets, but it's mostly to convenience users.
|
|
|
|
if args.channel:
|
|
|
|
args.banmask = "$and:(%s+$channel:%s)" % (args.banmask, args.channel)
|
|
|
|
|
|
|
|
irc.msg(source, "Checking for hosts that match \x02%s\x02:" % args.banmask, notice=True)
|
2016-01-04 06:05:30 +01:00
|
|
|
for uid, userobj in irc.users.copy().items():
|
2017-07-29 19:10:51 +02:00
|
|
|
if irc.match_host(args.banmask, uid):
|
2017-08-02 15:43:58 +02:00
|
|
|
if results < args.maxresults:
|
2016-01-04 06:05:30 +01:00
|
|
|
s = "\x02%s\x02 (%s@%s) [%s] {\x02%s\x02}" % (userobj.nick, userobj.ident,
|
2017-06-30 08:01:39 +02:00
|
|
|
userobj.host, userobj.realname, irc.get_friendly_name(irc.get_server(uid)))
|
2016-01-04 06:05:30 +01:00
|
|
|
|
|
|
|
# Always reply in private to prevent information leaks.
|
2016-07-07 08:47:31 +02:00
|
|
|
irc.reply(s, private=True)
|
2016-01-04 06:05:30 +01:00
|
|
|
results += 1
|
|
|
|
else:
|
|
|
|
if results:
|
|
|
|
irc.msg(source, "\x02%s\x02 out of \x02%s\x02 results shown." %
|
2017-08-02 15:43:58 +02:00
|
|
|
(min([results, args.maxresults]), results), notice=True)
|
2016-01-04 06:05:30 +01:00
|
|
|
else:
|
|
|
|
irc.msg(source, "No results found.", notice=True)
|
|
|
|
else:
|
2017-06-30 08:01:39 +02:00
|
|
|
# Target can be both a nick (of an online user) or a hostmask. irc.match_host() handles this
|
2016-07-07 08:47:31 +02:00
|
|
|
# automatically.
|
2017-07-29 19:10:51 +02:00
|
|
|
if irc.match_host(args.banmask, args.target):
|
|
|
|
irc.reply('Yes, \x02%s\x02 matches \x02%s\x02.' % (args.target, args.banmask))
|
2016-01-04 06:05:30 +01:00
|
|
|
else:
|
2017-07-29 19:10:51 +02:00
|
|
|
irc.reply('No, \x02%s\x02 does not match \x02%s\x02.' % (args.target, args.banmask))
|
2016-01-04 06:05:30 +01:00
|
|
|
|
2016-01-04 04:59:48 +01:00
|
|
|
@utils.add_cmd
|
|
|
|
def jupe(irc, source, args):
|
|
|
|
"""<server> [<reason>]
|
|
|
|
|
2016-07-25 01:40:23 +02:00
|
|
|
Admin only, jupes the given server."""
|
2016-01-04 04:59:48 +01:00
|
|
|
|
|
|
|
# Check that the caller is either opered or logged in as admin.
|
2016-12-10 06:48:39 +01:00
|
|
|
permissions.checkPermissions(irc, source, ['opercmds.jupe'])
|
2016-01-04 04:59:48 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
servername = args[0]
|
|
|
|
reason = ' '.join(args[1:]) or "No reason given"
|
2017-06-30 08:01:39 +02:00
|
|
|
desc = "Juped by %s: [%s]" % (irc.get_hostmask(source), reason)
|
2016-01-04 04:59:48 +01:00
|
|
|
except IndexError:
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('Not enough arguments. Needs 1-2: servername, reason (optional).')
|
2016-01-04 04:59:48 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
if not utils.isServerName(servername):
|
2017-07-29 18:37:27 +02:00
|
|
|
irc.error("Invalid server name %r." % servername)
|
2016-01-04 04:59:48 +01:00
|
|
|
return
|
|
|
|
|
2017-07-01 06:30:20 +02:00
|
|
|
sid = irc.spawn_server(servername, desc=desc)
|
2016-01-04 04:59:48 +01:00
|
|
|
|
2017-06-30 08:01:39 +02:00
|
|
|
irc.call_hooks([irc.pseudoclient.uid, 'OPERCMDS_SPAWNSERVER',
|
2016-01-04 04:59:48 +01:00
|
|
|
{'name': servername, 'sid': sid, 'text': desc}])
|
|
|
|
|
|
|
|
irc.reply("Done.")
|
|
|
|
|
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def kick(irc, source, args):
|
2016-12-24 19:57:26 +01:00
|
|
|
"""<channel> <user> [<reason>]
|
2016-01-04 04:59:48 +01:00
|
|
|
|
2016-12-24 19:57:26 +01:00
|
|
|
Admin only. Kicks <user> from the specified channel."""
|
2016-12-10 06:48:39 +01:00
|
|
|
permissions.checkPermissions(irc, source, ['opercmds.kick'])
|
2016-01-04 04:59:48 +01:00
|
|
|
try:
|
2017-06-30 08:01:39 +02:00
|
|
|
channel = irc.to_lower(args[0])
|
2016-12-24 19:57:26 +01:00
|
|
|
target = args[1]
|
|
|
|
reason = ' '.join(args[2:])
|
2016-01-04 04:59:48 +01:00
|
|
|
except IndexError:
|
2016-12-24 19:57:26 +01:00
|
|
|
irc.error("Not enough arguments. Needs 2-3: channel, target, reason (optional).")
|
2016-01-04 04:59:48 +01:00
|
|
|
return
|
|
|
|
|
2017-06-30 08:01:39 +02:00
|
|
|
targetu = irc.nick_to_uid(target)
|
2016-01-04 04:59:48 +01:00
|
|
|
|
|
|
|
if channel not in irc.channels: # KICK only works on channels that exist.
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error("Unknown channel %r." % channel)
|
2016-01-04 04:59:48 +01:00
|
|
|
return
|
|
|
|
|
2016-12-24 19:57:26 +01:00
|
|
|
if not targetu:
|
2016-01-04 04:59:48 +01:00
|
|
|
# Whatever we were told to kick doesn't exist!
|
2017-07-29 18:37:27 +02:00
|
|
|
irc.error("No such target nick %r." % target)
|
2016-01-04 04:59:48 +01:00
|
|
|
return
|
|
|
|
|
2016-12-24 19:57:26 +01:00
|
|
|
sender = irc.pseudoclient.uid
|
2017-06-25 11:07:24 +02:00
|
|
|
irc.kick(sender, channel, targetu, reason)
|
2017-03-10 05:49:38 +01:00
|
|
|
irc.reply("Done.")
|
2017-06-30 08:01:39 +02:00
|
|
|
irc.call_hooks([sender, 'CHANCMDS_KICK', {'channel': channel, 'target': targetu,
|
2016-01-04 04:59:48 +01:00
|
|
|
'text': reason, 'parse_as': 'KICK'}])
|
|
|
|
|
2016-01-10 03:34:57 +01:00
|
|
|
@utils.add_cmd
|
|
|
|
def kill(irc, source, args):
|
2016-12-24 19:57:26 +01:00
|
|
|
"""<target> [<reason>]
|
2016-01-10 03:34:57 +01:00
|
|
|
|
2016-12-24 19:57:26 +01:00
|
|
|
Admin only. Kills the given target."""
|
2016-12-10 06:48:39 +01:00
|
|
|
permissions.checkPermissions(irc, source, ['opercmds.kill'])
|
2016-01-10 03:34:57 +01:00
|
|
|
try:
|
2016-12-24 19:57:26 +01:00
|
|
|
target = args[0]
|
|
|
|
reason = ' '.join(args[1:])
|
2016-01-10 03:34:57 +01:00
|
|
|
except IndexError:
|
2016-12-24 19:57:26 +01:00
|
|
|
irc.error("Not enough arguments. Needs 1-2: target, reason (optional).")
|
2016-01-10 03:34:57 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
# Convert the source and target nicks to UIDs.
|
2016-12-24 19:57:26 +01:00
|
|
|
sender = irc.pseudoclient.uid
|
2017-06-30 08:01:39 +02:00
|
|
|
targetu = irc.nick_to_uid(target)
|
2016-01-10 03:34:57 +01:00
|
|
|
userdata = irc.users.get(targetu)
|
|
|
|
|
2016-12-24 19:57:26 +01:00
|
|
|
if targetu not in irc.users:
|
2016-01-10 03:34:57 +01:00
|
|
|
# Whatever we were told to kick doesn't exist!
|
2017-07-29 18:37:27 +02:00
|
|
|
irc.error("No such nick %r." % target)
|
2016-01-10 03:34:57 +01:00
|
|
|
return
|
|
|
|
|
2017-06-25 11:07:24 +02:00
|
|
|
irc.kill(sender, targetu, reason)
|
2016-07-07 07:36:06 +02:00
|
|
|
|
|
|
|
# Format the kill reason properly in hooks.
|
2017-06-30 08:01:39 +02:00
|
|
|
reason = "Killed (%s (%s))" % (irc.get_friendly_name(sender), reason)
|
2016-07-07 07:36:06 +02:00
|
|
|
|
2017-03-10 05:49:38 +01:00
|
|
|
irc.reply("Done.")
|
2017-06-30 08:01:39 +02:00
|
|
|
irc.call_hooks([sender, 'CHANCMDS_KILL', {'target': targetu, 'text': reason,
|
2016-01-10 03:34:57 +01:00
|
|
|
'userdata': userdata, 'parse_as': 'KILL'}])
|
|
|
|
|
2015-12-19 06:42:46 +01:00
|
|
|
@utils.add_cmd
|
|
|
|
def mode(irc, source, args):
|
|
|
|
"""<channel> <modes>
|
|
|
|
|
|
|
|
Oper-only, sets modes <modes> on the target channel."""
|
|
|
|
|
|
|
|
# Check that the caller is either opered or logged in as admin.
|
2016-12-10 06:48:39 +01:00
|
|
|
permissions.checkPermissions(irc, source, ['opercmds.mode'])
|
2015-12-19 06:42:46 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
target, modes = args[0], args[1:]
|
|
|
|
except IndexError:
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('Not enough arguments. Needs 2: target, modes to set.')
|
2015-12-19 06:42:46 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
if target not in irc.channels:
|
2017-07-29 18:37:27 +02:00
|
|
|
irc.error("Unknown channel %r." % target)
|
2015-12-19 06:42:46 +01:00
|
|
|
return
|
|
|
|
elif not modes:
|
|
|
|
# No modes were given before parsing (i.e. mode list was blank).
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error("No valid modes were given.")
|
2015-12-19 06:42:46 +01:00
|
|
|
return
|
|
|
|
|
2017-06-30 08:01:39 +02:00
|
|
|
parsedmodes = irc.parse_modes(target, modes)
|
2015-12-19 06:42:46 +01:00
|
|
|
|
|
|
|
if not parsedmodes:
|
|
|
|
# Modes were given but they failed to parse into anything meaningful.
|
|
|
|
# For example, "mode #somechan +o" would be erroneous because +o
|
|
|
|
# requires an argument!
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error("No valid modes were given.")
|
2015-12-19 06:42:46 +01:00
|
|
|
return
|
|
|
|
|
2017-06-25 11:07:24 +02:00
|
|
|
irc.mode(irc.pseudoclient.uid, target, parsedmodes)
|
2015-12-19 06:42:46 +01:00
|
|
|
|
2016-01-03 20:45:01 +01:00
|
|
|
# Call the appropriate hooks for plugins like relay.
|
2017-06-30 08:01:39 +02:00
|
|
|
irc.call_hooks([irc.pseudoclient.uid, 'OPERCMDS_MODEOVERRIDE',
|
2015-12-19 06:42:46 +01:00
|
|
|
{'target': target, 'modes': parsedmodes, 'parse_as': 'MODE'}])
|
|
|
|
|
2016-01-03 20:45:01 +01:00
|
|
|
irc.reply("Done.")
|
|
|
|
|
|
|
|
@utils.add_cmd
|
2016-01-04 04:59:48 +01:00
|
|
|
def topic(irc, source, args):
|
|
|
|
"""<channel> <topic>
|
2016-01-03 20:45:01 +01:00
|
|
|
|
2016-01-04 04:59:48 +01:00
|
|
|
Admin only. Updates the topic in a channel."""
|
2016-12-10 06:48:39 +01:00
|
|
|
permissions.checkPermissions(irc, source, ['opercmds.topic'])
|
2016-01-03 20:45:01 +01:00
|
|
|
try:
|
2016-01-04 04:59:48 +01:00
|
|
|
channel = args[0]
|
|
|
|
topic = ' '.join(args[1:])
|
2016-01-03 20:45:01 +01:00
|
|
|
except IndexError:
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error("Not enough arguments. Needs 2: channel, topic.")
|
2016-01-03 20:45:01 +01:00
|
|
|
return
|
|
|
|
|
2016-01-04 04:59:48 +01:00
|
|
|
if channel not in irc.channels:
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error("Unknown channel %r." % channel)
|
2016-01-03 20:45:01 +01:00
|
|
|
return
|
|
|
|
|
2017-06-25 11:07:24 +02:00
|
|
|
irc.topic(irc.pseudoclient.uid, channel, topic)
|
2016-01-03 20:45:01 +01:00
|
|
|
|
2017-03-10 05:49:38 +01:00
|
|
|
irc.reply("Done.")
|
2017-06-30 08:01:39 +02:00
|
|
|
irc.call_hooks([irc.pseudoclient.uid, 'CHANCMDS_TOPIC',
|
2016-01-04 04:59:48 +01:00
|
|
|
{'channel': channel, 'text': topic, 'setter': source,
|
|
|
|
'parse_as': 'TOPIC'}])
|
2017-07-15 01:41:16 +02:00
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def chghost(irc, source, args):
|
|
|
|
"""<user> <new host>
|
|
|
|
|
|
|
|
Admin only. Changes the visible host of the target user."""
|
|
|
|
chgfield(irc, source, args, 'host')
|
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def chgident(irc, source, args):
|
|
|
|
"""<user> <new ident>
|
|
|
|
|
|
|
|
Admin only. Changes the ident of the target user."""
|
|
|
|
chgfield(irc, source, args, 'ident')
|
2017-07-29 18:37:27 +02:00
|
|
|
|
2017-07-15 01:41:16 +02:00
|
|
|
@utils.add_cmd
|
|
|
|
def chgname(irc, source, args):
|
|
|
|
"""<user> <new name>
|
|
|
|
|
|
|
|
Admin only. Changes the GECOS (realname) of the target user."""
|
|
|
|
chgfield(irc, source, args, 'name', 'GECOS')
|
|
|
|
|
|
|
|
def chgfield(irc, source, args, human_field, internal_field=None):
|
|
|
|
permissions.checkPermissions(irc, source, ['opercmds.chg' + human_field])
|
|
|
|
try:
|
|
|
|
target = args[0]
|
|
|
|
new = args[1]
|
|
|
|
except IndexError:
|
|
|
|
irc.error("Not enough arguments. Needs 2: target, new %s." % human_field)
|
|
|
|
return
|
2017-07-29 18:37:27 +02:00
|
|
|
|
2017-07-15 01:41:16 +02:00
|
|
|
# Find the user
|
|
|
|
targetu = irc.nick_to_uid(target)
|
|
|
|
if targetu not in irc.users:
|
|
|
|
irc.error("No such nick %r." % target)
|
|
|
|
return
|
2017-07-29 18:37:27 +02:00
|
|
|
|
2017-07-15 01:41:16 +02:00
|
|
|
internal_field = internal_field or human_field.upper()
|
|
|
|
irc.update_client(targetu, internal_field, new)
|
|
|
|
irc.reply("Done.")
|