2015-12-06 17:40:13 -08:00
|
|
|
"""
|
|
|
|
utils.py - PyLink utilities module.
|
|
|
|
|
|
|
|
This module contains various utility functions related to IRC and/or the PyLink
|
|
|
|
framework.
|
|
|
|
"""
|
|
|
|
|
2015-04-24 22:37:07 -07:00
|
|
|
import string
|
2015-06-16 20:46:01 -07:00
|
|
|
import re
|
2015-11-22 20:14:47 -08:00
|
|
|
import importlib
|
|
|
|
import os
|
2015-04-24 22:37:07 -07:00
|
|
|
|
2015-07-05 13:44:48 -07:00
|
|
|
from log import log
|
2015-08-29 09:39:33 -07:00
|
|
|
import world
|
2015-12-06 17:13:47 -08:00
|
|
|
import conf
|
2015-05-31 12:20:09 -07:00
|
|
|
|
2016-04-05 18:05:52 -07:00
|
|
|
class IncrementalUIDGenerator():
|
2015-12-26 14:45:28 -08:00
|
|
|
"""
|
2016-04-05 18:05:52 -07:00
|
|
|
Incremental UID Generator module, adapted from InspIRCd source:
|
2015-06-19 10:43:42 -07:00
|
|
|
https://github.com/inspircd/inspircd/blob/f449c6b296ab/src/server.cpp#L85-L156
|
|
|
|
"""
|
|
|
|
|
2015-06-22 16:51:42 -07:00
|
|
|
def __init__(self, sid):
|
2016-04-05 18:05:52 -07:00
|
|
|
if not (hasattr(self, 'allowedchars') and hasattr(self, 'length')):
|
|
|
|
raise RuntimeError("Allowed characters list not defined. Subclass "
|
|
|
|
"%s by defining self.allowedchars and self.length "
|
|
|
|
"and then calling super().__init__()." % self.__class__.__name__)
|
|
|
|
self.uidchars = [self.allowedchars[0]]*self.length
|
2015-06-22 16:51:42 -07:00
|
|
|
self.sid = sid
|
2015-06-19 10:43:42 -07:00
|
|
|
|
2016-04-05 18:05:52 -07:00
|
|
|
def increment(self, pos=None):
|
2015-12-26 14:45:28 -08:00
|
|
|
"""
|
2016-04-05 18:05:52 -07:00
|
|
|
Increments the UID generator to the next available UID.
|
2015-12-26 14:45:28 -08:00
|
|
|
"""
|
2016-04-05 18:05:52 -07:00
|
|
|
# Position starts at 1 less than the UID length.
|
2016-04-05 18:44:00 -07:00
|
|
|
if pos is None:
|
|
|
|
pos = self.length - 1
|
2016-04-05 18:05:52 -07:00
|
|
|
|
2015-06-19 10:43:42 -07:00
|
|
|
# If we're at the last character in the list of allowed ones, reset
|
|
|
|
# and increment the next level above.
|
|
|
|
if self.uidchars[pos] == self.allowedchars[-1]:
|
|
|
|
self.uidchars[pos] = self.allowedchars[0]
|
|
|
|
self.increment(pos-1)
|
|
|
|
else:
|
|
|
|
# Find what position in the allowed characters list we're currently
|
|
|
|
# on, and add one.
|
|
|
|
idx = self.allowedchars.find(self.uidchars[pos])
|
|
|
|
self.uidchars[pos] = self.allowedchars[idx+1]
|
|
|
|
|
2015-06-22 16:51:42 -07:00
|
|
|
def next_uid(self):
|
2015-12-26 14:45:28 -08:00
|
|
|
"""
|
2016-04-05 18:05:52 -07:00
|
|
|
Returns the next unused UID for the server.
|
2015-12-26 14:45:28 -08:00
|
|
|
"""
|
2015-06-22 16:51:42 -07:00
|
|
|
uid = self.sid + ''.join(self.uidchars)
|
2015-06-19 10:43:42 -07:00
|
|
|
self.increment()
|
2015-06-21 18:11:17 -07:00
|
|
|
return uid
|
2015-05-30 23:00:39 -07:00
|
|
|
|
2015-05-31 12:20:09 -07:00
|
|
|
def add_cmd(func, name=None):
|
2015-11-22 20:14:47 -08:00
|
|
|
"""Binds an IRC command function to the given command name."""
|
2015-05-31 12:20:09 -07:00
|
|
|
if name is None:
|
|
|
|
name = func.__name__
|
|
|
|
name = name.lower()
|
2015-09-27 10:53:25 -07:00
|
|
|
world.commands[name].append(func)
|
2015-10-23 18:47:11 -07:00
|
|
|
return func
|
2015-06-07 19:31:56 -07:00
|
|
|
|
2015-06-23 19:08:43 -07:00
|
|
|
def add_hook(func, command):
|
2015-11-22 20:14:47 -08:00
|
|
|
"""Binds a hook function to the given command name."""
|
2015-07-04 19:00:29 -07:00
|
|
|
command = command.upper()
|
2015-09-27 10:53:25 -07:00
|
|
|
world.hooks[command].append(func)
|
2015-10-23 18:47:11 -07:00
|
|
|
return func
|
2015-06-23 19:08:43 -07:00
|
|
|
|
2015-06-16 20:46:01 -07:00
|
|
|
_nickregex = r'^[A-Za-z\|\\_\[\]\{\}\^\`][A-Z0-9a-z\-\|\\_\[\]\{\}\^\`]*$'
|
|
|
|
def isNick(s, nicklen=None):
|
2015-11-22 20:14:47 -08:00
|
|
|
"""Returns whether the string given is a valid nick."""
|
2015-06-16 20:46:01 -07:00
|
|
|
if nicklen and len(s) > nicklen:
|
|
|
|
return False
|
|
|
|
return bool(re.match(_nickregex, s))
|
|
|
|
|
|
|
|
def isChannel(s):
|
2015-11-22 20:14:47 -08:00
|
|
|
"""Returns whether the string given is a valid channel name."""
|
2015-09-12 22:28:34 -07:00
|
|
|
return str(s).startswith('#')
|
2015-06-21 15:00:33 -07:00
|
|
|
|
2015-07-03 17:05:44 -07:00
|
|
|
def _isASCII(s):
|
2015-11-22 20:14:47 -08:00
|
|
|
"""Returns whether the string given is valid ASCII."""
|
2015-07-03 17:05:44 -07:00
|
|
|
chars = string.ascii_letters + string.digits + string.punctuation
|
|
|
|
return all(char in chars for char in s)
|
2015-06-21 15:00:33 -07:00
|
|
|
|
|
|
|
def isServerName(s):
|
2015-11-22 20:14:47 -08:00
|
|
|
"""Returns whether the string given is a valid IRC server name."""
|
2015-07-07 14:31:47 -07:00
|
|
|
return _isASCII(s) and '.' in s and not s.startswith('.')
|
2015-06-20 20:36:35 -07:00
|
|
|
|
2015-09-12 22:28:34 -07:00
|
|
|
hostmaskRe = re.compile(r'^\S+!\S+@\S+$')
|
|
|
|
def isHostmask(text):
|
|
|
|
"""Returns whether the given text is a valid hostmask."""
|
2015-10-08 17:25:17 -07:00
|
|
|
# Band-aid patch here to prevent bad bans set by Janus forwarding people into invalid channels.
|
|
|
|
return hostmaskRe.match(text) and '#' not in text
|
2015-09-12 22:28:34 -07:00
|
|
|
|
2015-07-05 12:48:39 -07:00
|
|
|
def parseModes(irc, target, args):
|
2015-08-28 19:27:38 -07:00
|
|
|
"""Parses a modestring list into a list of (mode, argument) tuples.
|
2015-07-04 23:49:28 -07:00
|
|
|
['+mitl-o', '3', 'person'] => [('+m', None), ('+i', None), ('+t', None), ('+l', '3'), ('-o', 'person')]
|
2016-03-23 18:04:12 -07:00
|
|
|
|
2016-04-24 21:37:23 -07:00
|
|
|
This method is deprecated. Use irc.parseModes() instead.
|
|
|
|
"""
|
2016-04-24 21:44:02 -07:00
|
|
|
log.warning("(%s) utils.parseModes is deprecated. Use irc.parseModes() instead!", irc.name)
|
2016-04-24 21:37:23 -07:00
|
|
|
return irc.parseModes(target, args)
|
2015-06-20 20:36:35 -07:00
|
|
|
|
2015-07-05 12:48:39 -07:00
|
|
|
def applyModes(irc, target, changedmodes):
|
2015-09-12 22:28:34 -07:00
|
|
|
"""Takes a list of parsed IRC modes, and applies them on the given target.
|
2015-07-08 16:58:59 -07:00
|
|
|
|
2016-04-24 21:37:23 -07:00
|
|
|
The target can be either a channel or a user; this is handled automatically.
|
2016-03-26 18:14:53 -07:00
|
|
|
|
2016-04-24 21:37:23 -07:00
|
|
|
This method is deprecated. Use irc.applyModes() instead.
|
|
|
|
"""
|
2016-04-24 21:44:02 -07:00
|
|
|
log.warning("(%s) utils.applyModes is deprecated. Use irc.applyModes() instead!", irc.name)
|
2016-04-24 21:37:23 -07:00
|
|
|
return irc.applyModes(target, changedmodes)
|
2015-06-20 20:54:01 -07:00
|
|
|
|
2015-09-28 19:12:45 -07:00
|
|
|
def loadModuleFromFolder(name, folder):
|
2015-12-26 14:45:28 -08:00
|
|
|
"""
|
|
|
|
Imports and returns a module, if existing, from a specific folder.
|
|
|
|
"""
|
2015-11-22 20:14:47 -08:00
|
|
|
fullpath = os.path.join(folder, '%s.py' % name)
|
|
|
|
m = importlib.machinery.SourceFileLoader(name, fullpath).load_module()
|
2015-09-28 19:12:45 -07:00
|
|
|
return m
|
|
|
|
|
2015-12-24 17:33:49 -08:00
|
|
|
def getProtocolModule(protoname):
|
2015-12-26 14:45:28 -08:00
|
|
|
"""
|
|
|
|
Imports and returns the protocol module requested.
|
|
|
|
"""
|
2015-09-28 19:12:45 -07:00
|
|
|
return loadModuleFromFolder(protoname, world.protocols_folder)
|
2015-12-06 17:13:47 -08:00
|
|
|
|
|
|
|
def getDatabaseName(dbname):
|
|
|
|
"""
|
|
|
|
Returns a database filename with the given base DB name appropriate for the
|
|
|
|
current PyLink instance.
|
|
|
|
|
|
|
|
This returns '<dbname>.db' if the running config name is PyLink's default
|
|
|
|
(config.yml), and '<dbname>-<config name>.db' for anything else. For example,
|
|
|
|
if this is called from an instance running as './pylink testing.yml', it
|
|
|
|
would return '<dbname>-testing.db'."""
|
|
|
|
if conf.confname != 'pylink':
|
|
|
|
dbname += '-%s' % conf.confname
|
|
|
|
dbname += '.db'
|
|
|
|
return dbname
|