2015-12-19 06:42:46 +01:00
|
|
|
"""
|
|
|
|
opercmds.py: Provides a subset of network management commands.
|
|
|
|
"""
|
2016-06-21 03:18:54 +02:00
|
|
|
from pylinkirc import utils
|
|
|
|
from pylinkirc.log import log
|
2015-12-19 06:42:46 +01:00
|
|
|
|
2016-01-04 06:05:30 +01:00
|
|
|
@utils.add_cmd
|
|
|
|
def checkban(irc, source, args):
|
|
|
|
"""<banmask (nick!user@host or user@host)> [<nick or hostmask to check>]
|
|
|
|
|
|
|
|
Oper only. If a 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-07-07 20:17:24 +02:00
|
|
|
irc.checkAuthenticated(source)
|
2016-01-04 06:05:30 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
banmask = args[0]
|
|
|
|
except IndexError:
|
|
|
|
irc.reply("Error: Not enough arguments. Needs 1-2: banmask, nick or hostmask to check (optional).")
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
targetmask = args[1]
|
|
|
|
except IndexError:
|
|
|
|
# No hostmask was given, return a list of affected users.
|
|
|
|
|
2016-07-07 08:47:31 +02:00
|
|
|
irc.msg(source, "Checking for hosts that match \x02%s\x02:" % banmask, notice=True)
|
2016-01-04 06:05:30 +01:00
|
|
|
|
|
|
|
results = 0
|
|
|
|
for uid, userobj in irc.users.copy().items():
|
2016-07-07 08:47:31 +02:00
|
|
|
if irc.matchHost(banmask, uid):
|
2016-01-04 06:05:30 +01:00
|
|
|
if results < 50: # XXX rather arbitrary limit
|
|
|
|
s = "\x02%s\x02 (%s@%s) [%s] {\x02%s\x02}" % (userobj.nick, userobj.ident,
|
2016-07-07 08:47:31 +02:00
|
|
|
userobj.host, userobj.realname, irc.getFriendlyName(irc.getServer(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." %
|
|
|
|
(min([results, 50]), results), notice=True)
|
|
|
|
else:
|
|
|
|
irc.msg(source, "No results found.", notice=True)
|
|
|
|
else:
|
2016-07-07 08:47:31 +02:00
|
|
|
# Target can be both a nick (of an online user) or a hostmask. irc.matchHost() handles this
|
|
|
|
# automatically.
|
|
|
|
if irc.matchHost(banmask, targetmask):
|
2016-01-04 06:05:30 +01:00
|
|
|
irc.reply('Yes, \x02%s\x02 matches \x02%s\x02.' % (targetmask, banmask))
|
|
|
|
else:
|
|
|
|
irc.reply('No, \x02%s\x02 does not match \x02%s\x02.' % (targetmask, banmask))
|
|
|
|
|
2016-01-04 04:59:48 +01:00
|
|
|
@utils.add_cmd
|
|
|
|
def jupe(irc, source, args):
|
|
|
|
"""<server> [<reason>]
|
|
|
|
|
|
|
|
Oper-only, jupes the given server."""
|
|
|
|
|
|
|
|
# Check that the caller is either opered or logged in as admin.
|
2016-05-01 01:54:11 +02:00
|
|
|
irc.checkAuthenticated(source)
|
2016-01-04 04:59:48 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
servername = args[0]
|
|
|
|
reason = ' '.join(args[1:]) or "No reason given"
|
2016-05-01 01:44:37 +02:00
|
|
|
desc = "Juped by %s: [%s]" % (irc.getHostmask(source), reason)
|
2016-01-04 04:59:48 +01:00
|
|
|
except IndexError:
|
|
|
|
irc.reply('Error: Not enough arguments. Needs 1-2: servername, reason (optional).')
|
|
|
|
return
|
|
|
|
|
|
|
|
if not utils.isServerName(servername):
|
|
|
|
irc.reply("Error: Invalid server name '%s'." % servername)
|
|
|
|
return
|
|
|
|
|
|
|
|
sid = irc.proto.spawnServer(servername, desc=desc)
|
|
|
|
|
|
|
|
irc.callHooks([irc.pseudoclient.uid, 'OPERCMDS_SPAWNSERVER',
|
|
|
|
{'name': servername, 'sid': sid, 'text': desc}])
|
|
|
|
|
|
|
|
irc.reply("Done.")
|
|
|
|
|
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def kick(irc, source, args):
|
|
|
|
"""<source> <channel> <user> [<reason>]
|
|
|
|
|
|
|
|
Admin only. Kicks <user> from <channel> via <source>, where <source> is either the nick of a PyLink client or the SID of a PyLink server."""
|
2016-05-01 01:54:11 +02:00
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
2016-01-04 04:59:48 +01:00
|
|
|
try:
|
|
|
|
sourcenick = args[0]
|
|
|
|
channel = args[1]
|
|
|
|
target = args[2]
|
|
|
|
reason = ' '.join(args[3:])
|
|
|
|
except IndexError:
|
|
|
|
irc.reply("Error: Not enough arguments. Needs 3-4: source nick, channel, target, reason (optional).")
|
|
|
|
return
|
|
|
|
|
|
|
|
# Convert the source and target nicks to UIDs.
|
2016-01-17 02:29:18 +01:00
|
|
|
sender = irc.nickToUid(sourcenick) or sourcenick
|
2016-01-04 04:59:48 +01:00
|
|
|
targetu = irc.nickToUid(target)
|
|
|
|
|
|
|
|
if channel not in irc.channels: # KICK only works on channels that exist.
|
|
|
|
irc.reply("Error: Unknown channel %r." % channel)
|
|
|
|
return
|
|
|
|
|
2016-01-17 02:29:18 +01:00
|
|
|
if (not irc.isInternalClient(sender)) and \
|
|
|
|
(not irc.isInternalServer(sender)):
|
2016-01-04 04:59:48 +01:00
|
|
|
# Whatever we were told to send the kick from wasn't valid; try to be
|
2016-01-17 02:29:18 +01:00
|
|
|
# somewhat user friendly in the error message
|
2016-01-04 04:59:48 +01:00
|
|
|
irc.reply("Error: No such PyLink client '%s'. The first argument to "
|
|
|
|
"KICK should be the name of a PyLink client (e.g. '%s'; see "
|
|
|
|
"'help kick' for details." % (sourcenick,
|
|
|
|
irc.pseudoclient.nick))
|
|
|
|
return
|
2016-01-17 02:29:18 +01:00
|
|
|
elif not targetu:
|
2016-01-04 04:59:48 +01:00
|
|
|
# Whatever we were told to kick doesn't exist!
|
2016-01-17 02:29:18 +01:00
|
|
|
irc.reply("Error: No such target nick '%s'." % target)
|
2016-01-04 04:59:48 +01:00
|
|
|
return
|
|
|
|
|
2016-01-17 02:29:18 +01:00
|
|
|
irc.proto.kick(sender, channel, targetu, reason)
|
|
|
|
irc.callHooks([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-02-26 06:10:41 +01:00
|
|
|
"""<source> <target> [<reason>]
|
2016-01-10 03:34:57 +01:00
|
|
|
|
2016-02-26 06:10:41 +01:00
|
|
|
Admin only. Kills <target> via <source>, where <source> is either the nick of a PyLink client or the SID of a PyLink server."""
|
2016-05-01 01:54:11 +02:00
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
2016-01-10 03:34:57 +01:00
|
|
|
try:
|
|
|
|
sourcenick = args[0]
|
|
|
|
target = args[1]
|
|
|
|
reason = ' '.join(args[2:])
|
|
|
|
except IndexError:
|
|
|
|
irc.reply("Error: Not enough arguments. Needs 3-4: source nick, target, reason (optional).")
|
|
|
|
return
|
|
|
|
|
|
|
|
# Convert the source and target nicks to UIDs.
|
2016-01-17 02:29:18 +01:00
|
|
|
sender = irc.nickToUid(sourcenick) or sourcenick
|
2016-01-10 03:34:57 +01:00
|
|
|
targetu = irc.nickToUid(target)
|
|
|
|
userdata = irc.users.get(targetu)
|
|
|
|
|
2016-01-17 02:29:18 +01:00
|
|
|
if (not irc.isInternalClient(sender)) and \
|
|
|
|
(not irc.isInternalServer(sender)):
|
2016-01-10 03:34:57 +01:00
|
|
|
# Whatever we were told to send the kick from wasn't valid; try to be
|
2016-01-17 02:29:18 +01:00
|
|
|
# somewhat user friendly in the error message
|
2016-01-10 03:34:57 +01:00
|
|
|
irc.reply("Error: No such PyLink client '%s'. The first argument to "
|
|
|
|
"KILL should be the name of a PyLink client (e.g. '%s'; see "
|
2016-07-07 07:36:24 +02:00
|
|
|
"'help kill' for details." % (sourcenick,
|
2016-01-10 03:34:57 +01:00
|
|
|
irc.pseudoclient.nick))
|
|
|
|
return
|
|
|
|
elif targetu not in irc.users:
|
|
|
|
# Whatever we were told to kick doesn't exist!
|
|
|
|
irc.reply("Error: No such nick '%s'." % target)
|
|
|
|
return
|
|
|
|
|
2016-01-17 02:29:18 +01:00
|
|
|
irc.proto.kill(sender, targetu, reason)
|
2016-07-07 07:36:06 +02:00
|
|
|
|
|
|
|
# Format the kill reason properly in hooks.
|
|
|
|
reason = "Killed (%s (%s))" % (irc.getFriendlyName(sender), reason)
|
|
|
|
|
2016-01-17 02:29:18 +01:00
|
|
|
irc.callHooks([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-05-01 01:54:11 +02:00
|
|
|
irc.checkAuthenticated(source)
|
2015-12-19 06:42:46 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
target, modes = args[0], args[1:]
|
|
|
|
except IndexError:
|
|
|
|
irc.reply('Error: Not enough arguments. Needs 2: target, modes to set.')
|
|
|
|
return
|
|
|
|
|
|
|
|
if target not in irc.channels:
|
|
|
|
irc.reply("Error: Unknown channel '%s'." % target)
|
|
|
|
return
|
|
|
|
elif not modes:
|
|
|
|
# No modes were given before parsing (i.e. mode list was blank).
|
|
|
|
irc.reply("Error: No valid modes were given.")
|
|
|
|
return
|
|
|
|
|
2016-06-09 01:39:46 +02:00
|
|
|
parsedmodes = irc.parseModes(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!
|
|
|
|
irc.reply("Error: No valid modes were given.")
|
|
|
|
return
|
|
|
|
|
2016-01-17 02:08:17 +01:00
|
|
|
irc.proto.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.
|
2015-12-19 06:42:46 +01:00
|
|
|
irc.callHooks([irc.pseudoclient.uid, 'OPERCMDS_MODEOVERRIDE',
|
|
|
|
{'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-05-01 01:54:11 +02:00
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
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-01-04 04:59:48 +01:00
|
|
|
irc.reply("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:
|
|
|
|
irc.reply("Error: Unknown channel %r." % channel)
|
2016-01-03 20:45:01 +01:00
|
|
|
return
|
|
|
|
|
2016-01-17 02:09:52 +01:00
|
|
|
irc.proto.topic(irc.pseudoclient.uid, channel, topic)
|
2016-01-03 20:45:01 +01:00
|
|
|
|
2016-01-04 04:59:48 +01:00
|
|
|
irc.callHooks([irc.pseudoclient.uid, 'CHANCMDS_TOPIC',
|
|
|
|
{'channel': channel, 'text': topic, 'setter': source,
|
|
|
|
'parse_as': 'TOPIC'}])
|