""" opercmds.py: Provides a subset of network management commands. """ import sys import os # Add the base PyLink folder to path, so we can import utils and log. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # ircmatch library from https://github.com/mammon-ircd/ircmatch # (pip install ircmatch) try: import ircmatch except ImportError: ircmatch = None import utils from log import log @utils.add_cmd def checkban(irc, source, args): """ [] 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.""" utils.checkAuthenticated(irc, source, allowOper=False) if ircmatch is None: irc.reply("Error: missing ircmatch module (install it via 'pip install ircmatch').") return try: banmask = args[0] except IndexError: irc.reply("Error: Not enough arguments. Needs 1-2: banmask, nick or hostmask to check (optional).") return # Casemapping value (0 is rfc1459, 1 is ascii) used by ircmatch. if irc.proto.casemapping == 'rfc1459': casemapping = 0 else: casemapping = 1 try: targetmask = args[1] except IndexError: # No hostmask was given, return a list of affected users. irc.msg(source, "Checking matches for \x02%s\x02:" % banmask, notice=True) results = 0 for uid, userobj in irc.users.copy().items(): targetmask = utils.getHostmask(irc, uid) if ircmatch.match(casemapping, banmask, targetmask): if results < 50: # XXX rather arbitrary limit serverobj = irc.servers[irc.getServer(uid)] s = "\x02%s\x02 (%s@%s) [%s] {\x02%s\x02}" % (userobj.nick, userobj.ident, userobj.host, userobj.realname, serverobj.name) # Always reply in private to prevent information leaks. irc.msg(source, s, notice=True) 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: # Target can be both a nick (of an online user) or a hostmask. uid = irc.nickToUid(targetmask) if uid: targetmask = utils.getHostmask(irc, uid) elif not utils.isHostmask(targetmask): irc.reply("Error: Invalid nick or hostmask '%s'." % targetmask) return if ircmatch.match(casemapping, banmask, targetmask): 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)) @utils.add_cmd def jupe(irc, source, args): """ [] Oper-only, jupes the given server.""" # Check that the caller is either opered or logged in as admin. utils.checkAuthenticated(irc, source) try: servername = args[0] reason = ' '.join(args[1:]) or "No reason given" desc = "Juped by %s: [%s]" % (utils.getHostmask(irc, source), reason) 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): """ [] Admin only. Kicks from via , where is either the nick of a PyLink client or the SID of a PyLink server.""" utils.checkAuthenticated(irc, source, allowOper=False) 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. u = irc.nickToUid(sourcenick) or sourcenick 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 if irc.isInternalServer(u): # Send kick from server if the given kicker is a SID irc.proto.kick(u, channel, targetu, reason) elif u not in irc.users: # Whatever we were told to send the kick from wasn't valid; try to be # somewhat user friendly in the error. message 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 elif targetu not in irc.users: # Whatever we were told to kick doesn't exist! irc.reply("Error: No such nick '%s'." % target) return else: irc.proto.kick(u, channel, targetu, reason) irc.callHooks([u, 'CHANCMDS_KICK', {'channel': channel, 'target': targetu, 'text': reason, 'parse_as': 'KICK'}]) @utils.add_cmd def kill(irc, source, args): """ [] Admin only. Kills via , where is either the nick of a PyLink client or the SID of a PyLink server.""" utils.checkAuthenticated(irc, source, allowOper=False) 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. u = irc.nickToUid(sourcenick) or sourcenick targetu = irc.nickToUid(target) userdata = irc.users.get(targetu) if irc.isInternalServer(u): # Send kill from server if the given kicker is a SID irc.proto.kill(u, targetu, reason) elif u not in irc.users: # Whatever we were told to send the kick from wasn't valid; try to be # somewhat user friendly in the error. message 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 " "'help kick' for details." % (sourcenick, 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 else: irc.proto.kill(u, targetu, reason) irc.callHooks([u, 'CHANCMDS_KILL', {'target': targetu, 'text': reason, 'userdata': userdata, 'parse_as': 'KILL'}]) @utils.add_cmd def mode(irc, source, args): """ Oper-only, sets modes on the target channel.""" # Check that the caller is either opered or logged in as admin. utils.checkAuthenticated(irc, source) 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 parsedmodes = utils.parseModes(irc, target, modes) 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 irc.proto.mode(irc.pseudoclient.uid, target, parsedmodes) # Call the appropriate hooks for plugins like relay. irc.callHooks([irc.pseudoclient.uid, 'OPERCMDS_MODEOVERRIDE', {'target': target, 'modes': parsedmodes, 'parse_as': 'MODE'}]) irc.reply("Done.") @utils.add_cmd def topic(irc, source, args): """ Admin only. Updates the topic in a channel.""" utils.checkAuthenticated(irc, source, allowOper=False) try: channel = args[0] topic = ' '.join(args[1:]) except IndexError: irc.reply("Error: Not enough arguments. Needs 2: channel, topic.") return if channel not in irc.channels: irc.reply("Error: Unknown channel %r." % channel) return irc.proto.topicClient(irc.pseudoclient.uid, channel, topic) irc.callHooks([irc.pseudoclient.uid, 'CHANCMDS_TOPIC', {'channel': channel, 'text': topic, 'setter': source, 'parse_as': 'TOPIC'}])