3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-24 03:29:28 +01:00

opercmds/changehost: actually commit changes mentioned in last commit

This commit is contained in:
James Lu 2016-07-06 23:47:31 -07:00
parent d3877b0194
commit 2b88c8d630
2 changed files with 8 additions and 39 deletions

View File

@ -30,12 +30,8 @@ def _changehost(irc, target, args):
"Changehost will not function correctly!", irc.name) "Changehost will not function correctly!", irc.name)
return return
# Match against both the user's IP and real host.
target_host = irc.getHostmask(target, realhost=True)
target_ip = irc.getHostmask(target, ip=True)
for host_glob, host_template in changehost_hosts.items(): for host_glob, host_template in changehost_hosts.items():
if ircmatch.match(0, host_glob, target_host) or ircmatch.match(0, host_glob, target_ip): if irc.matchHost(host_glob, target):
# This uses template strings for simple substitution: # This uses template strings for simple substitution:
# https://docs.python.org/3/library/string.html#template-strings # https://docs.python.org/3/library/string.html#template-strings
template = string.Template(host_template) template = string.Template(host_template)

View File

@ -1,14 +1,6 @@
""" """
opercmds.py: Provides a subset of network management commands. opercmds.py: Provides a subset of network management commands.
""" """
# ircmatch library from https://github.com/mammon-ircd/ircmatch
# (pip install ircmatch)
try:
import ircmatch
except ImportError:
ircmatch = None
from pylinkirc import utils from pylinkirc import utils
from pylinkirc.log import log from pylinkirc.log import log
@ -19,41 +11,28 @@ 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.""" 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."""
irc.checkAuthenticated(source, allowOper=False) irc.checkAuthenticated(source, allowOper=False)
if ircmatch is None:
irc.reply("Error: missing ircmatch module (install it via 'pip install ircmatch').")
return
try: try:
banmask = args[0] banmask = args[0]
except IndexError: except IndexError:
irc.reply("Error: Not enough arguments. Needs 1-2: banmask, nick or hostmask to check (optional).") irc.reply("Error: Not enough arguments. Needs 1-2: banmask, nick or hostmask to check (optional).")
return return
# Casemapping value (0 is rfc1459, 1 is ascii) used by ircmatch.
if irc.proto.casemapping == 'rfc1459':
casemapping = 0
else:
casemapping = 1
try: try:
targetmask = args[1] targetmask = args[1]
except IndexError: except IndexError:
# No hostmask was given, return a list of affected users. # No hostmask was given, return a list of affected users.
irc.msg(source, "Checking matches for \x02%s\x02:" % banmask, notice=True) irc.msg(source, "Checking for hosts that match \x02%s\x02:" % banmask, notice=True)
results = 0 results = 0
for uid, userobj in irc.users.copy().items(): for uid, userobj in irc.users.copy().items():
targetmask = irc.getHostmask(uid) if irc.matchHost(banmask, uid):
if ircmatch.match(casemapping, banmask, targetmask):
if results < 50: # XXX rather arbitrary limit 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, s = "\x02%s\x02 (%s@%s) [%s] {\x02%s\x02}" % (userobj.nick, userobj.ident,
userobj.host, userobj.realname, serverobj.name) userobj.host, userobj.realname, irc.getFriendlyName(irc.getServer(uid)))
# Always reply in private to prevent information leaks. # Always reply in private to prevent information leaks.
irc.msg(source, s, notice=True) irc.reply(s, private=True)
results += 1 results += 1
else: else:
if results: if results:
@ -62,15 +41,9 @@ def checkban(irc, source, args):
else: else:
irc.msg(source, "No results found.", notice=True) irc.msg(source, "No results found.", notice=True)
else: else:
# Target can be both a nick (of an online user) or a hostmask. # Target can be both a nick (of an online user) or a hostmask. irc.matchHost() handles this
uid = irc.nickToUid(targetmask) # automatically.
if uid: if irc.matchHost(banmask, targetmask):
targetmask = irc.getHostmask(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)) irc.reply('Yes, \x02%s\x02 matches \x02%s\x02.' % (targetmask, banmask))
else: else:
irc.reply('No, \x02%s\x02 does not match \x02%s\x02.' % (targetmask, banmask)) irc.reply('No, \x02%s\x02 does not match \x02%s\x02.' % (targetmask, banmask))