From d3877b019429f62a81d1834603c0dcd2d41f3288 Mon Sep 17 00:00:00 2001 From: James Lu Date: Wed, 6 Jul 2016 23:11:36 -0700 Subject: [PATCH] Irc: introduce matchHost() wrapper around ircmatch This makes the latter a core dependency. Refactor changehost and opercmds plugins to take advantage of this new core function. --- README.md | 2 +- classes.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c6dc30f..fac2c8a 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,8 @@ You can also find support via our IRC channels: `#PyLink @ irc.overdrivenetworks * Python 3.4+ * PyYAML (`pip install pyyaml`) +* [ircmatch](https://github.com/mammon-ircd/ircmatch) (`pip install ircmatch`) * *For the servprotect plugin*: [expiringdict](https://github.com/mailgun/expiringdict) (note: unfortunately, installation is broken in pip due to [mailgun/expiringdict#13](https://github.com/mailgun/expiringdict/issues/13)) -* *For the changehost and opercmds plugins*: [ircmatch](https://github.com/mammon-ircd/ircmatch) (`pip install ircmatch`) ## Supported IRCds diff --git a/classes.py b/classes.py index ee35cbb..e916fc3 100644 --- a/classes.py +++ b/classes.py @@ -11,12 +11,13 @@ import threading from random import randint import time import socket -import threading import ssl import hashlib from copy import deepcopy import inspect +import ircmatch + from . import world, utils, structures, __version__ from .log import * @@ -908,6 +909,36 @@ class Irc(): raise utils.NotAuthenticatedError("You are not authenticated!") return True + def matchHost(self, glob, target, ip=True, realhost=True): + """ + Checks whether the given host, or given UID's hostmask matches the given nick!user@host + glob. + + If the target given is a UID, and the ip or realhost options are True, this will also match + against the target's IP address and real host, respectively. + """ + # Get the corresponding casemapping value used by ircmatch. + casemapping = getattr(ircmatch, self.proto.casemapping) + + # Prepare a list of hosts to check against. + if target in self.users: + hosts = {self.getHostmask(target)} + + if ip: + hosts.add(self.getHostmask(target, ip=True)) + + if realhost: + hosts.add(self.getHostmask(target, ip=True)) + else: # We were given a host, use that. + hosts = [target] + + # Iterate over the hosts to match using ircmatch. + for host in hosts: + if ircmatch.match(casemapping, glob, host): + return True + + return False + class IrcUser(): """PyLink IRC user class.""" def __init__(self, nick, ts, uid, ident='null', host='null',