2013-07-23 21:02:06 +02:00
|
|
|
###
|
|
|
|
# Copyright (c) 2013, Valentin Lorentz
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import sys
|
2013-07-23 22:47:50 +02:00
|
|
|
import datetime
|
2013-07-23 21:02:06 +02:00
|
|
|
|
2013-11-27 17:49:03 +01:00
|
|
|
import supybot.conf as conf
|
2013-07-23 21:02:06 +02:00
|
|
|
import supybot.utils as utils
|
2013-07-23 23:16:08 +02:00
|
|
|
import supybot.ircdb as ircdb
|
2013-07-23 21:02:06 +02:00
|
|
|
from supybot.commands import *
|
|
|
|
import supybot.plugins as plugins
|
2015-08-11 16:50:23 +02:00
|
|
|
import supybot.utils.minisix as minisix
|
2013-07-23 21:02:06 +02:00
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.callbacks as callbacks
|
2019-10-10 17:27:34 +02:00
|
|
|
import supybot.httpserver as httpserver
|
2013-07-23 21:02:06 +02:00
|
|
|
from supybot.i18n import PluginInternationalization
|
|
|
|
_ = PluginInternationalization('Aka')
|
|
|
|
|
2014-01-22 15:16:12 +01:00
|
|
|
try:
|
|
|
|
import sqlite3
|
|
|
|
except ImportError:
|
|
|
|
sqlite3 = None
|
2013-07-23 21:02:06 +02:00
|
|
|
try:
|
|
|
|
import sqlalchemy
|
|
|
|
import sqlalchemy.ext
|
|
|
|
import sqlalchemy.ext.declarative
|
|
|
|
except ImportError:
|
2014-01-22 15:16:12 +01:00
|
|
|
sqlalchemy = None
|
|
|
|
|
|
|
|
if not (sqlite3 or sqlalchemy):
|
|
|
|
raise callbacks.Error('You have to install python-sqlite3 or '
|
|
|
|
'python-sqlalchemy in order to load this plugin.')
|
|
|
|
|
|
|
|
available_db = {}
|
|
|
|
|
|
|
|
class Alias(object):
|
|
|
|
__slots__ = ('name', 'alias', 'locked', 'locked_by', 'locked_at')
|
|
|
|
def __init__(self, name, alias):
|
|
|
|
self.name = name
|
|
|
|
self.alias = alias
|
|
|
|
self.locked = False
|
|
|
|
self.locked_by = None
|
|
|
|
self.locked_at = None
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Alias('%r', '%r')>" % (self.name, self.alias)
|
|
|
|
if sqlite3:
|
|
|
|
class SQLiteAkaDB(object):
|
|
|
|
__slots__ = ('engines', 'filename', 'dbs',)
|
|
|
|
def __init__(self, filename):
|
|
|
|
self.engines = ircutils.IrcDict()
|
|
|
|
self.filename = filename.replace('sqlite3', 'sqlalchemy')
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.dbs.clear()
|
|
|
|
|
|
|
|
def get_db(self, channel):
|
|
|
|
if channel in self.engines:
|
|
|
|
engine = self.engines[channel]
|
|
|
|
else:
|
|
|
|
filename = plugins.makeChannelFilename(self.filename, channel)
|
|
|
|
exists = os.path.exists(filename)
|
|
|
|
engine = sqlite3.connect(filename, check_same_thread=False)
|
|
|
|
if not exists:
|
|
|
|
cursor = engine.cursor()
|
|
|
|
cursor.execute("""CREATE TABLE aliases (
|
|
|
|
id INTEGER NOT NULL,
|
|
|
|
name VARCHAR NOT NULL,
|
|
|
|
alias VARCHAR NOT NULL,
|
|
|
|
locked BOOLEAN NOT NULL,
|
|
|
|
locked_by VARCHAR,
|
|
|
|
locked_at DATETIME,
|
|
|
|
PRIMARY KEY (id),
|
|
|
|
UNIQUE (name))""")
|
|
|
|
engine.commit()
|
|
|
|
self.engines[channel] = engine
|
|
|
|
assert engine.execute("select 1").fetchone() == (1,)
|
|
|
|
return engine
|
|
|
|
|
|
|
|
|
|
|
|
def has_aka(self, channel, name):
|
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2 and isinstance(name, str):
|
2014-01-22 15:16:12 +01:00
|
|
|
name = name.decode('utf8')
|
|
|
|
db = self.get_db(channel)
|
|
|
|
return self.get_db(channel).cursor() \
|
|
|
|
.execute("""SELECT COUNT() as count
|
|
|
|
FROM aliases WHERE name = ?;""", (name,)) \
|
|
|
|
.fetchone()[0]
|
|
|
|
|
|
|
|
def get_aka_list(self, channel):
|
|
|
|
cursor = self.get_db(channel).cursor()
|
|
|
|
cursor.execute("""SELECT name FROM aliases;""")
|
|
|
|
list_ = cursor.fetchall()
|
|
|
|
return list_
|
|
|
|
|
|
|
|
def get_alias(self, channel, name):
|
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2 and isinstance(name, str):
|
2014-01-22 15:16:12 +01:00
|
|
|
name = name.decode('utf8')
|
|
|
|
cursor = self.get_db(channel).cursor()
|
|
|
|
cursor.execute("""SELECT alias FROM aliases
|
|
|
|
WHERE name = ?;""", (name,))
|
|
|
|
r = cursor.fetchone()
|
|
|
|
if r:
|
|
|
|
return r[0]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def add_aka(self, channel, name, alias):
|
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
|
|
|
if self.has_aka(channel, name):
|
|
|
|
raise AkaError(_('This Aka already exists.'))
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2:
|
2014-01-22 15:16:12 +01:00
|
|
|
if isinstance(name, str):
|
|
|
|
name = name.decode('utf8')
|
|
|
|
if isinstance(alias, str):
|
|
|
|
alias = alias.decode('utf8')
|
|
|
|
db = self.get_db(channel)
|
2014-01-22 16:44:17 +01:00
|
|
|
cursor = db.cursor()
|
2014-01-22 15:16:12 +01:00
|
|
|
cursor.execute("""INSERT INTO aliases VALUES (
|
2014-01-22 16:44:17 +01:00
|
|
|
NULL, ?, ?, 0, NULL, NULL);""", (name, alias))
|
2014-01-22 15:16:12 +01:00
|
|
|
db.commit()
|
2013-07-23 21:02:06 +02:00
|
|
|
|
2014-01-22 15:16:12 +01:00
|
|
|
def remove_aka(self, channel, name):
|
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2 and isinstance(name, str):
|
2014-01-22 15:16:12 +01:00
|
|
|
name = name.decode('utf8')
|
|
|
|
db = self.get_db(channel)
|
|
|
|
db.cursor().execute('DELETE FROM aliases WHERE name = ?', (name,))
|
|
|
|
db.commit()
|
2013-07-23 21:02:06 +02:00
|
|
|
|
2014-01-22 15:16:12 +01:00
|
|
|
def lock_aka(self, channel, name, by):
|
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2 and isinstance(name, str):
|
2014-01-22 15:16:12 +01:00
|
|
|
name = name.decode('utf8')
|
|
|
|
db = self.get_db(channel)
|
|
|
|
cursor = db.cursor().execute("""UPDATE aliases
|
|
|
|
SET locked=1, locked_at=?, locked_by=? WHERE name = ?""",
|
|
|
|
(datetime.datetime.now(), by, name))
|
|
|
|
if cursor.rowcount == 0:
|
2014-12-26 22:21:20 +01:00
|
|
|
raise AkaError(_('This Aka does not exist.'))
|
2014-01-22 15:16:12 +01:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def unlock_aka(self, channel, name, by):
|
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2 and isinstance(name, str):
|
2014-01-22 15:16:12 +01:00
|
|
|
name = name.decode('utf8')
|
|
|
|
db = self.get_db(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""UPDATE aliases SET locked=0, locked_at=?
|
|
|
|
WHERE name = ?""", (datetime.datetime.now(), name))
|
|
|
|
if cursor.rowcount == 0:
|
2014-12-26 22:21:20 +01:00
|
|
|
raise AkaError(_('This Aka does not exist.'))
|
2014-01-22 15:16:12 +01:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def get_aka_lock(self, channel, name):
|
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2 and isinstance(name, str):
|
2014-01-22 15:16:12 +01:00
|
|
|
name = name.decode('utf8')
|
|
|
|
cursor = self.get_db(channel).cursor()
|
|
|
|
cursor.execute("""SELECT locked, locked_by, locked_at
|
|
|
|
FROM aliases WHERE name = ?;""", (name,))
|
|
|
|
r = cursor.fetchone()
|
|
|
|
if r:
|
|
|
|
return (bool(r[0]), r[1], r[2])
|
|
|
|
else:
|
2014-12-26 22:21:20 +01:00
|
|
|
raise AkaError(_('This Aka does not exist.'))
|
2019-10-10 17:27:34 +02:00
|
|
|
|
|
|
|
def get_all(self, channel):
|
|
|
|
cursor = self.get_db(channel).cursor()
|
|
|
|
cursor.execute("""
|
|
|
|
SELECT id, name, alias, locked, locked_by, locked_at
|
|
|
|
FROM aliases;
|
|
|
|
""")
|
|
|
|
return cursor.fetchall()
|
|
|
|
|
2014-01-22 15:16:12 +01:00
|
|
|
available_db.update({'sqlite3': SQLiteAkaDB})
|
|
|
|
elif sqlalchemy:
|
2013-07-23 21:02:06 +02:00
|
|
|
Base = sqlalchemy.ext.declarative.declarative_base()
|
2014-01-22 15:16:12 +01:00
|
|
|
class SQLAlchemyAlias(Alias, Base):
|
|
|
|
__slots__ = ()
|
2013-07-23 21:02:06 +02:00
|
|
|
__tablename__ = 'aliases'
|
|
|
|
|
|
|
|
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
|
2013-07-23 22:47:50 +02:00
|
|
|
name = sqlalchemy.Column(sqlalchemy.String, unique=True, nullable=False)
|
|
|
|
alias = sqlalchemy.Column(sqlalchemy.String, nullable=False)
|
2013-07-23 21:02:06 +02:00
|
|
|
|
2013-07-23 22:47:50 +02:00
|
|
|
locked = sqlalchemy.Column(sqlalchemy.Boolean, nullable=False)
|
|
|
|
locked_by = sqlalchemy.Column(sqlalchemy.String, nullable=True)
|
|
|
|
locked_at = sqlalchemy.Column(sqlalchemy.DateTime, nullable=True)
|
2013-07-23 21:21:21 +02:00
|
|
|
|
2013-07-23 21:02:06 +02:00
|
|
|
# TODO: Add table for usage statistics
|
|
|
|
|
|
|
|
class SqlAlchemyAkaDB(object):
|
2014-01-22 15:16:12 +01:00
|
|
|
__slots__ = ('engines', 'filename', 'sqlalchemy', 'dbs')
|
2013-07-23 21:02:06 +02:00
|
|
|
def __init__(self, filename):
|
|
|
|
self.engines = ircutils.IrcDict()
|
|
|
|
self.filename = filename
|
|
|
|
self.sqlalchemy = sqlalchemy
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.dbs.clear()
|
|
|
|
|
|
|
|
def get_db(self, channel):
|
|
|
|
if channel in self.engines:
|
|
|
|
engine = self.engines[channel]
|
|
|
|
else:
|
|
|
|
filename = plugins.makeChannelFilename(self.filename, channel)
|
|
|
|
exists = os.path.exists(filename)
|
|
|
|
engine = sqlalchemy.create_engine('sqlite:///' + filename)
|
|
|
|
if not exists:
|
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
self.engines[channel] = engine
|
|
|
|
assert engine.execute("select 1").scalar() == 1
|
|
|
|
Session = sqlalchemy.orm.sessionmaker()
|
|
|
|
Session.configure(bind=engine)
|
|
|
|
return Session()
|
|
|
|
|
|
|
|
|
|
|
|
def has_aka(self, channel, name):
|
2013-12-11 17:01:01 +01:00
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2 and isinstance(name, str):
|
2013-07-24 11:28:55 +02:00
|
|
|
name = name.decode('utf8')
|
2014-01-22 15:16:12 +01:00
|
|
|
count = self.get_db(channel).query(SQLAlchemyAlias) \
|
|
|
|
.filter(SQLAlchemyAlias.name == name) \
|
2013-07-23 21:02:06 +02:00
|
|
|
.count()
|
|
|
|
return bool(count)
|
|
|
|
def get_aka_list(self, channel):
|
2014-01-22 15:16:12 +01:00
|
|
|
list_ = list(self.get_db(channel).query(SQLAlchemyAlias.name))
|
2013-07-23 21:02:06 +02:00
|
|
|
return list_
|
|
|
|
|
|
|
|
def get_alias(self, channel, name):
|
2013-12-11 17:01:01 +01:00
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2 and isinstance(name, str):
|
2013-07-24 11:28:55 +02:00
|
|
|
name = name.decode('utf8')
|
2013-07-23 21:10:26 +02:00
|
|
|
try:
|
2014-01-22 15:16:12 +01:00
|
|
|
return self.get_db(channel).query(SQLAlchemyAlias.alias) \
|
|
|
|
.filter(SQLAlchemyAlias.name == name).one()[0]
|
2013-07-23 21:10:26 +02:00
|
|
|
except sqlalchemy.orm.exc.NoResultFound:
|
|
|
|
return None
|
2013-07-23 21:02:06 +02:00
|
|
|
|
|
|
|
def add_aka(self, channel, name, alias):
|
2013-12-11 17:01:01 +01:00
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
2013-07-23 21:10:26 +02:00
|
|
|
if self.has_aka(channel, name):
|
2013-07-23 22:47:50 +02:00
|
|
|
raise AkaError(_('This Aka already exists.'))
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2:
|
2013-07-23 21:02:06 +02:00
|
|
|
if isinstance(name, str):
|
|
|
|
name = name.decode('utf8')
|
|
|
|
if isinstance(alias, str):
|
|
|
|
alias = alias.decode('utf8')
|
|
|
|
db = self.get_db(channel)
|
2014-01-22 15:16:12 +01:00
|
|
|
db.add(SQLAlchemyAlias(name, alias))
|
2013-07-23 21:02:06 +02:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def remove_aka(self, channel, name):
|
2013-12-11 17:01:01 +01:00
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2 and isinstance(name, str):
|
2013-07-24 11:28:55 +02:00
|
|
|
name = name.decode('utf8')
|
2013-07-23 21:02:06 +02:00
|
|
|
db = self.get_db(channel)
|
2014-01-22 15:16:12 +01:00
|
|
|
db.query(SQLAlchemyAlias).filter(SQLAlchemyAlias.name == name).delete()
|
2013-07-23 21:02:06 +02:00
|
|
|
db.commit()
|
|
|
|
|
2013-07-23 22:47:50 +02:00
|
|
|
def lock_aka(self, channel, name, by):
|
2013-12-11 17:01:01 +01:00
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2 and isinstance(name, str):
|
2013-07-24 11:28:55 +02:00
|
|
|
name = name.decode('utf8')
|
2013-07-23 22:47:50 +02:00
|
|
|
db = self.get_db(channel)
|
|
|
|
try:
|
2014-01-22 15:16:12 +01:00
|
|
|
aka = db.query(SQLAlchemyAlias) \
|
|
|
|
.filter(SQLAlchemyAlias.name == name).one()
|
2013-07-23 22:47:50 +02:00
|
|
|
except sqlalchemy.orm.exc.NoResultFound:
|
2014-12-26 22:21:20 +01:00
|
|
|
raise AkaError(_('This Aka does not exist.'))
|
2013-07-23 22:47:50 +02:00
|
|
|
if aka.locked:
|
|
|
|
raise AkaError(_('This Aka is already locked.'))
|
|
|
|
aka.locked = True
|
|
|
|
aka.locked_by = by
|
|
|
|
aka.locked_at = datetime.datetime.now()
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def unlock_aka(self, channel, name, by):
|
2013-12-11 17:01:01 +01:00
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2 and isinstance(name, str):
|
2013-07-24 11:28:55 +02:00
|
|
|
name = name.decode('utf8')
|
2013-07-23 22:47:50 +02:00
|
|
|
db = self.get_db(channel)
|
|
|
|
try:
|
2014-01-22 15:16:12 +01:00
|
|
|
aka = db.query(SQLAlchemyAlias) \
|
|
|
|
.filter(SQLAlchemyAlias.name == name).one()
|
2013-07-23 22:47:50 +02:00
|
|
|
except sqlalchemy.orm.exc.NoResultFound:
|
2014-12-26 22:21:20 +01:00
|
|
|
raise AkaError(_('This Aka does not exist.'))
|
2013-07-23 23:05:45 +02:00
|
|
|
if not aka.locked:
|
2013-07-23 22:47:50 +02:00
|
|
|
raise AkaError(_('This Aka is already unlocked.'))
|
|
|
|
aka.locked = False
|
|
|
|
aka.locked_by = by
|
|
|
|
aka.locked_at = datetime.datetime.now()
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def get_aka_lock(self, channel, name):
|
2013-12-11 17:01:01 +01:00
|
|
|
name = callbacks.canonicalName(name, preserve_spaces=True)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2 and isinstance(name, str):
|
2013-07-24 11:28:55 +02:00
|
|
|
name = name.decode('utf8')
|
2013-07-23 22:47:50 +02:00
|
|
|
try:
|
|
|
|
return self.get_db(channel) \
|
2014-01-22 15:16:12 +01:00
|
|
|
.query(SQLAlchemyAlias.locked, SQLAlchemyAlias.locked_by, SQLAlchemyAlias.locked_at)\
|
|
|
|
.filter(SQLAlchemyAlias.name == name).one()
|
2013-07-23 22:47:50 +02:00
|
|
|
except sqlalchemy.orm.exc.NoResultFound:
|
2014-12-26 22:21:20 +01:00
|
|
|
raise AkaError(_('This Aka does not exist.'))
|
2013-07-23 22:47:50 +02:00
|
|
|
|
2019-10-10 17:27:34 +02:00
|
|
|
def get_all(self, channel):
|
|
|
|
akas = self.get_db(channel).query(
|
|
|
|
SQLAlchemyAlias.id, SQLAlchemyAlias.name, SQLAlchemyAlias.alias,
|
|
|
|
SQLAlchemyAlias.locked, SQLAlchemyAlias.locked_by, SQLAlchemyAlias.locked_at
|
|
|
|
).all()
|
|
|
|
return map(
|
|
|
|
lambda aka: (aka.name, aka.alias, aka.locked, aka.locked_by, aka.locked_at),
|
|
|
|
akas
|
|
|
|
)
|
|
|
|
|
2014-01-22 15:16:12 +01:00
|
|
|
available_db.update({'sqlalchemy': SqlAlchemyAkaDB})
|
|
|
|
|
2013-07-23 22:47:50 +02:00
|
|
|
|
2013-07-23 21:02:06 +02:00
|
|
|
def getArgs(args, required=1, optional=0, wildcard=0):
|
|
|
|
if len(args) < required:
|
|
|
|
raise callbacks.ArgumentError
|
|
|
|
if len(args) < required + optional:
|
|
|
|
ret = list(args) + ([''] * (required + optional - len(args)))
|
|
|
|
elif len(args) >= required + optional:
|
|
|
|
if not wildcard:
|
|
|
|
ret = list(args[:required + optional - 1])
|
|
|
|
ret.append(' '.join(args[required + optional - 1:]))
|
|
|
|
else:
|
|
|
|
ret = list(args)
|
|
|
|
return ret
|
|
|
|
|
2013-07-23 22:47:50 +02:00
|
|
|
class AkaError(Exception):
|
2013-07-23 21:02:06 +02:00
|
|
|
pass
|
|
|
|
|
2013-07-23 22:47:50 +02:00
|
|
|
class RecursiveAlias(AkaError):
|
2013-07-23 21:02:06 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
dollarRe = re.compile(r'\$(\d+)')
|
|
|
|
def findBiggestDollar(alias):
|
|
|
|
dollars = dollarRe.findall(alias)
|
2014-01-21 10:57:38 +01:00
|
|
|
dollars = list(map(int, dollars))
|
2013-07-23 21:02:06 +02:00
|
|
|
dollars.sort()
|
|
|
|
if dollars:
|
|
|
|
return dollars[-1]
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
atRe = re.compile(r'@(\d+)')
|
|
|
|
def findBiggestAt(alias):
|
|
|
|
ats = atRe.findall(alias)
|
2014-01-21 10:57:38 +01:00
|
|
|
ats = list(map(int, ats))
|
2013-07-23 21:02:06 +02:00
|
|
|
ats.sort()
|
|
|
|
if ats:
|
|
|
|
return ats[-1]
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
2015-11-07 18:29:43 +01:00
|
|
|
if 'sqlite3' in conf.supybot.databases() and 'sqlite3' in available_db:
|
|
|
|
AkaDB = SQLiteAkaDB
|
|
|
|
elif 'sqlalchemy' in conf.supybot.databases() and 'sqlalchemy' in available_db:
|
|
|
|
log.warning('Aka\'s only enabled database engine is SQLAlchemy, which '
|
|
|
|
'is deprecated. Please consider adding \'sqlite3\' to '
|
|
|
|
'supybot.databases (and/or install sqlite3).')
|
|
|
|
AkaDB = SqlAlchemyAkaDB
|
|
|
|
else:
|
|
|
|
raise plugins.NoSuitableDatabase(['sqlite3', 'sqlalchemy'])
|
2013-07-23 21:02:06 +02:00
|
|
|
|
2019-10-10 17:27:34 +02:00
|
|
|
|
|
|
|
class AkaHTTPCallback(httpserver.SupyHTTPServerCallback):
|
|
|
|
name = 'Aka web interface'
|
|
|
|
base_template = '''\
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Aka</title>
|
|
|
|
<link rel="stylesheet" href="/default.css">
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
<h1>Aka</h1>
|
|
|
|
|
|
|
|
%s
|
|
|
|
</body>
|
|
|
|
</html>'''
|
|
|
|
index_template = base_template % '''\
|
|
|
|
<p>To view the global Akas either click <a href="list/global">here</a> or
|
|
|
|
enter 'global' in the form below.</p>
|
|
|
|
|
|
|
|
<form action="" method="post">
|
|
|
|
<label for="channel">Channel name:</label>
|
|
|
|
<input type="text" placeholder="#channel" name="channel" id="channel">
|
|
|
|
<input type="submit" name="submit" value="view">
|
|
|
|
</form>'''
|
|
|
|
list_template = base_template % '''\
|
|
|
|
<table>
|
|
|
|
<thead>
|
|
|
|
<th>Name</th>
|
|
|
|
<th>Alias</th>
|
|
|
|
<th>Locked</th>
|
|
|
|
</thead>
|
|
|
|
%s
|
|
|
|
</table>'''
|
|
|
|
|
|
|
|
def doGet(self, handler, path, *args, **kwargs):
|
|
|
|
if path == '/':
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header('Content-type', 'text/html; charset=utf-8')
|
|
|
|
self.end_headers()
|
|
|
|
self.write(self.index_template)
|
|
|
|
|
|
|
|
elif path.startswith('/list/'):
|
|
|
|
parts = path.split('/')
|
|
|
|
channel = parts[2] if len(parts) == 3 else 'global'
|
|
|
|
channel = utils.web.urlunquote(channel)
|
|
|
|
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header('Content-type', 'text/html; charset=utf-8')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
akas = {}
|
|
|
|
for aka in self._plugin._db.get_all('global'):
|
|
|
|
akas[aka[1]] = aka
|
|
|
|
|
|
|
|
if channel != 'global':
|
|
|
|
for aka in self._plugin._db.get_all(channel):
|
|
|
|
akas[aka[1]] = aka
|
|
|
|
|
|
|
|
aka_rows = []
|
|
|
|
for (name, aka) in sorted(akas.items()):
|
|
|
|
(id, name, alias, locked, locked_by, locked_at) = aka
|
|
|
|
locked_column = 'False'
|
|
|
|
if locked:
|
|
|
|
locked_column = format(
|
|
|
|
_('By %s at %s'),
|
|
|
|
locked_by,
|
|
|
|
locked_at.split('.')[0],
|
|
|
|
)
|
|
|
|
aka_rows.append(
|
|
|
|
format(
|
|
|
|
"""
|
|
|
|
<tr>
|
|
|
|
<td style="white-space: nowrap;"><code>%s</code></td>
|
|
|
|
<td><code>%s<code></td>
|
|
|
|
<td style="white-space: nowrap;">%s</td>
|
|
|
|
</tr>
|
|
|
|
""",
|
|
|
|
utils.web.html_escape(name),
|
|
|
|
utils.web.html_escape(alias),
|
|
|
|
utils.web.html_escape(locked_column),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.write(format(self.list_template, ''.join(aka_rows)))
|
|
|
|
|
|
|
|
def doPost(self, handler, path, form, *args, **kwargs):
|
|
|
|
if path == '/' and 'channel' in form:
|
|
|
|
self.send_response(303)
|
|
|
|
self.send_header(
|
|
|
|
'Location',
|
|
|
|
format('list/%s', utils.web.urlquote(form['channel'].value))
|
|
|
|
)
|
|
|
|
self.end_headers()
|
|
|
|
else:
|
|
|
|
self.send_response(400)
|
|
|
|
self.send_header('Content-type', 'text/plain; charset=utf-8')
|
|
|
|
self.end_headers()
|
|
|
|
self.write('Missing field \'channel\'.')
|
|
|
|
|
|
|
|
|
2013-07-23 21:02:06 +02:00
|
|
|
class Aka(callbacks.Plugin):
|
2014-11-30 08:18:44 +01:00
|
|
|
"""Aka is the improved version of the Alias plugin. It stores akas outside
|
|
|
|
of the bot.conf, which doesn't have risk of corrupting the bot.conf file
|
|
|
|
(this often happens when there are Unicode issues). Aka also
|
2014-11-16 18:26:05 +01:00
|
|
|
introduces multi-worded akas."""
|
2013-07-23 21:02:06 +02:00
|
|
|
|
|
|
|
def __init__(self, irc):
|
|
|
|
self.__parent = super(Aka, self)
|
|
|
|
self.__parent.__init__(irc)
|
2015-12-02 09:06:34 +01:00
|
|
|
# "sqlalchemy" is only for backward compatibility
|
2015-12-02 08:55:00 +01:00
|
|
|
filename = conf.supybot.directories.data.dirize('Aka.sqlalchemy.db')
|
2015-11-07 18:29:43 +01:00
|
|
|
self._db = AkaDB(filename)
|
2019-10-10 17:27:34 +02:00
|
|
|
self._http_running = False
|
|
|
|
conf.supybot.plugins.Aka.web.enable.addCallback(self._httpConfCallback)
|
|
|
|
if self.registryValue('web.enable'):
|
|
|
|
self._startHttp()
|
|
|
|
|
|
|
|
def die(self):
|
|
|
|
if self._http_running:
|
|
|
|
self._stopHttp()
|
|
|
|
|
|
|
|
def _httpConfCallback(self):
|
|
|
|
if self.registryValue('web.enable'):
|
|
|
|
if not self._http_running:
|
|
|
|
self._startHttp()
|
|
|
|
else:
|
|
|
|
if self._http_running:
|
|
|
|
self._stopHttp()
|
|
|
|
|
|
|
|
def _startHttp(self):
|
|
|
|
callback = AkaHTTPCallback()
|
|
|
|
callback._plugin = self
|
|
|
|
httpserver.hook('aka', callback)
|
|
|
|
self._http_running = True
|
|
|
|
|
|
|
|
def _stopHttp(self):
|
|
|
|
httpserver.unhook('aka')
|
|
|
|
self._http_running = False
|
2013-07-23 21:02:06 +02:00
|
|
|
|
|
|
|
def isCommandMethod(self, name):
|
2013-08-25 01:06:31 +02:00
|
|
|
args = name.split(' ')
|
2013-11-09 22:19:19 +01:00
|
|
|
if '|' in args:
|
|
|
|
return False
|
2013-08-25 01:06:31 +02:00
|
|
|
if len(args) > 1 and \
|
|
|
|
callbacks.canonicalName(args[0]) != self.canonicalName():
|
2013-08-25 01:23:53 +02:00
|
|
|
for cb in dynamic.irc.callbacks: # including this plugin
|
2013-12-11 17:01:01 +01:00
|
|
|
if cb.isCommandMethod(' '.join(args[0:-1])):
|
2013-08-25 01:06:31 +02:00
|
|
|
return False
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2 and isinstance(name, str):
|
2013-08-25 15:40:56 +02:00
|
|
|
name = name.decode('utf8')
|
2013-07-23 23:05:45 +02:00
|
|
|
channel = dynamic.channel or 'global'
|
2013-07-23 21:02:06 +02:00
|
|
|
return self._db.has_aka(channel, name) or \
|
|
|
|
self._db.has_aka('global', name) or \
|
|
|
|
self.__parent.isCommandMethod(name)
|
2013-07-24 12:16:02 +02:00
|
|
|
isCommand = isCommandMethod
|
2013-07-23 21:02:06 +02:00
|
|
|
|
|
|
|
def listCommands(self):
|
2014-12-17 01:27:22 +01:00
|
|
|
commands = ['add', 'remove', 'lock', 'unlock', 'importaliasdatabase',
|
2015-04-27 07:08:01 +02:00
|
|
|
'show', 'list', 'set', 'search']
|
2014-12-17 01:27:22 +01:00
|
|
|
return commands
|
2013-07-24 12:16:02 +02:00
|
|
|
|
2013-12-11 17:01:01 +01:00
|
|
|
def getCommand(self, args, check_other_plugins=True):
|
2013-07-24 12:16:02 +02:00
|
|
|
canonicalName = callbacks.canonicalName
|
|
|
|
# All the code from here to the 'for' loop is copied from callbacks.py
|
2014-01-21 10:57:38 +01:00
|
|
|
assert args == list(map(canonicalName, args))
|
2013-07-24 12:16:02 +02:00
|
|
|
first = args[0]
|
|
|
|
for cb in self.cbs:
|
|
|
|
if first == cb.canonicalName():
|
2013-12-05 13:35:02 +01:00
|
|
|
return cb.getCommand(args[1:])
|
2013-07-24 12:16:02 +02:00
|
|
|
if first == self.canonicalName() and len(args) > 1:
|
2013-12-11 17:01:01 +01:00
|
|
|
ret = self.getCommand(args[1:], False)
|
2013-07-24 12:16:02 +02:00
|
|
|
if ret:
|
|
|
|
return [first] + ret
|
2013-12-24 15:37:44 +01:00
|
|
|
max_length = self.registryValue('maximumWordsInName')
|
2015-08-10 19:36:07 +02:00
|
|
|
for i in range(1, min(len(args)+1, max_length)):
|
2013-07-24 12:16:02 +02:00
|
|
|
if self.isCommandMethod(callbacks.formatCommand(args[0:i])):
|
|
|
|
return args[0:i]
|
|
|
|
return []
|
|
|
|
|
|
|
|
def getCommandMethod(self, command):
|
|
|
|
if len(command) == 1 or command[0] == self.canonicalName():
|
2013-07-23 21:02:06 +02:00
|
|
|
try:
|
|
|
|
return self.__parent.getCommandMethod(command)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2013-07-24 12:16:02 +02:00
|
|
|
name = callbacks.formatCommand(command)
|
2013-07-23 23:05:45 +02:00
|
|
|
channel = dynamic.channel or 'global'
|
2013-07-23 21:02:06 +02:00
|
|
|
original = self._db.get_alias(channel, name)
|
|
|
|
if not original:
|
|
|
|
original = self._db.get_alias('global', name)
|
|
|
|
biggestDollar = findBiggestDollar(original)
|
|
|
|
biggestAt = findBiggestAt(original)
|
|
|
|
wildcard = '$*' in original
|
|
|
|
def f(irc, msg, args):
|
|
|
|
tokens = callbacks.tokenize(original)
|
|
|
|
if biggestDollar or biggestAt:
|
|
|
|
args = getArgs(args, required=biggestDollar, optional=biggestAt,
|
|
|
|
wildcard=wildcard)
|
2017-04-17 11:05:04 +02:00
|
|
|
remaining_len = conf.supybot.reply.maximumLength()
|
|
|
|
for (i, arg) in enumerate(args):
|
|
|
|
if remaining_len < len(arg):
|
|
|
|
arg = arg[0:remaining_len]
|
|
|
|
args[i+1:] = []
|
|
|
|
break
|
|
|
|
remaining_len -= len(arg)
|
2013-07-23 21:02:06 +02:00
|
|
|
def regexpReplace(m):
|
|
|
|
idx = int(m.group(1))
|
|
|
|
return args[idx-1]
|
|
|
|
def replace(tokens, replacer):
|
|
|
|
for (i, token) in enumerate(tokens):
|
|
|
|
if isinstance(token, list):
|
|
|
|
replace(token, replacer)
|
|
|
|
else:
|
|
|
|
tokens[i] = replacer(token)
|
|
|
|
replace(tokens, lambda s: dollarRe.sub(regexpReplace, s))
|
|
|
|
if biggestAt:
|
2013-12-23 17:41:33 +01:00
|
|
|
assert not wildcard
|
2017-08-18 18:30:22 +02:00
|
|
|
args = args[biggestDollar:]
|
2013-07-23 21:02:06 +02:00
|
|
|
replace(tokens, lambda s: atRe.sub(regexpReplace, s))
|
|
|
|
if wildcard:
|
2013-12-23 17:41:33 +01:00
|
|
|
assert not biggestAt
|
|
|
|
# Gotta remove the things that have already been subbed in.
|
|
|
|
i = biggestDollar
|
2017-04-17 10:31:43 +02:00
|
|
|
args[:] = args[i:]
|
2013-07-23 21:02:06 +02:00
|
|
|
def everythingReplace(tokens):
|
2017-04-17 10:53:26 +02:00
|
|
|
skip = 0
|
2013-07-23 21:02:06 +02:00
|
|
|
for (i, token) in enumerate(tokens):
|
2017-04-17 10:53:26 +02:00
|
|
|
if skip:
|
|
|
|
skip -= 1
|
|
|
|
continue
|
2013-07-23 21:02:06 +02:00
|
|
|
if isinstance(token, list):
|
2017-04-17 10:53:26 +02:00
|
|
|
everythingReplace(token)
|
2013-07-23 21:02:06 +02:00
|
|
|
if token == '$*':
|
2013-12-23 17:41:33 +01:00
|
|
|
tokens[i:i+1] = args
|
2017-04-17 10:53:26 +02:00
|
|
|
skip = len(args)-1 # do not make replacements in
|
|
|
|
# tokens we just added
|
2013-12-23 17:41:33 +01:00
|
|
|
elif '$*' in token:
|
|
|
|
tokens[i] = token.replace('$*', ' '.join(args))
|
|
|
|
everythingReplace(tokens)
|
2013-11-27 17:57:30 +01:00
|
|
|
maxNesting = conf.supybot.commands.nested.maximum()
|
2013-11-27 17:38:02 +01:00
|
|
|
if maxNesting and irc.nested+1 > maxNesting:
|
|
|
|
irc.error(_('You\'ve attempted more nesting than is '
|
|
|
|
'currently allowed on this bot.'), Raise=True)
|
2017-04-17 11:05:04 +02:00
|
|
|
self.Proxy(irc, msg, tokens, nested=irc.nested+1)
|
2013-07-23 21:02:06 +02:00
|
|
|
if biggestDollar and (wildcard or biggestAt):
|
|
|
|
flexargs = _(' at least')
|
|
|
|
else:
|
|
|
|
flexargs = ''
|
2013-07-23 23:05:45 +02:00
|
|
|
try:
|
|
|
|
lock = self._db.get_aka_lock(channel, name)
|
|
|
|
except AkaError:
|
|
|
|
lock = self._db.get_aka_lock('global', name)
|
|
|
|
(locked, locked_by, locked_at) = lock
|
|
|
|
if locked:
|
|
|
|
lock = ' ' + _('Locked by %s at %s') % (locked_by, locked_at)
|
|
|
|
else:
|
|
|
|
lock = ''
|
2017-10-01 10:40:09 +02:00
|
|
|
escaped_command = original.replace('\\', '\\\\').replace('"', '\\"')
|
2017-08-20 19:55:28 +02:00
|
|
|
if channel == 'global':
|
|
|
|
doc = format(_('<a global alias,%s %n>\n\nAlias for %q.%s'),
|
2017-10-01 10:40:09 +02:00
|
|
|
flexargs, (biggestDollar, _('argument')),
|
|
|
|
escaped_command, lock)
|
2017-08-20 19:55:28 +02:00
|
|
|
else:
|
|
|
|
doc = format(_('<an alias on %s,%s %n>\n\nAlias for %q.%s'),
|
2017-10-01 10:40:09 +02:00
|
|
|
channel, flexargs, (biggestDollar, _('argument')),
|
|
|
|
escaped_command, lock)
|
2013-07-23 21:02:06 +02:00
|
|
|
f = utils.python.changeFunctionName(f, name, doc)
|
|
|
|
return f
|
|
|
|
|
|
|
|
def _add_aka(self, channel, name, alias):
|
2013-07-23 21:10:26 +02:00
|
|
|
if self.__parent.isCommandMethod(name):
|
2013-07-23 22:47:50 +02:00
|
|
|
raise AkaError(_('You can\'t overwrite commands in '
|
2013-07-23 21:02:06 +02:00
|
|
|
'this plugin.'))
|
2013-07-23 21:21:21 +02:00
|
|
|
if self._db.has_aka(channel, name):
|
2013-07-23 22:47:50 +02:00
|
|
|
raise AkaError(_('This Aka already exists.'))
|
2013-12-24 15:37:44 +01:00
|
|
|
if len(name.split(' ')) > self.registryValue('maximumWordsInName'):
|
|
|
|
raise AkaError(_('This Aka has too many spaces in its name.'))
|
2013-07-23 21:02:06 +02:00
|
|
|
biggestDollar = findBiggestDollar(alias)
|
|
|
|
biggestAt = findBiggestAt(alias)
|
|
|
|
wildcard = '$*' in alias
|
2013-12-23 17:41:33 +01:00
|
|
|
if biggestAt and wildcard:
|
|
|
|
raise AkaError(_('Can\'t mix $* and optional args (@1, etc.)'))
|
2013-07-23 21:02:06 +02:00
|
|
|
self._db.add_aka(channel, name, alias)
|
|
|
|
|
2013-07-23 22:47:50 +02:00
|
|
|
def _remove_aka(self, channel, name, evenIfLocked=False):
|
|
|
|
if not evenIfLocked:
|
|
|
|
(locked, by, at) = self._db.get_aka_lock(channel, name)
|
|
|
|
if locked:
|
|
|
|
raise AkaError(_('This Aka is locked.'))
|
2013-07-23 21:02:06 +02:00
|
|
|
self._db.remove_aka(channel, name)
|
|
|
|
|
2013-07-23 21:10:26 +02:00
|
|
|
def add(self, irc, msg, args, optlist, name, alias):
|
|
|
|
"""[--channel <#channel>] <name> <command>
|
2013-07-23 21:02:06 +02:00
|
|
|
|
|
|
|
Defines an alias <name> that executes <command>. The <command>
|
|
|
|
should be in the standard "command argument [nestedcommand argument]"
|
|
|
|
arguments to the alias; they'll be filled with the first, second, etc.
|
|
|
|
arguments. $1, $2, etc. can be used for required arguments. @1, @2,
|
|
|
|
etc. can be used for optional arguments. $* simply means "all
|
2013-07-24 18:23:33 +02:00
|
|
|
arguments that have not replaced $1, $2, etc.", ie. it will also
|
|
|
|
include optional arguments.
|
2013-07-23 21:02:06 +02:00
|
|
|
"""
|
2013-07-23 21:10:26 +02:00
|
|
|
channel = 'global'
|
|
|
|
for (option, arg) in optlist:
|
|
|
|
if option == 'channel':
|
2019-08-04 18:11:28 +02:00
|
|
|
if not irc.isChannel(arg):
|
2013-07-23 21:10:26 +02:00
|
|
|
irc.error(_('%r is not a valid channel.') % arg,
|
|
|
|
Raise=True)
|
|
|
|
channel = arg
|
2013-07-23 21:02:06 +02:00
|
|
|
if ' ' not in alias:
|
|
|
|
# If it's a single word, they probably want $*.
|
|
|
|
alias += ' $*'
|
|
|
|
try:
|
|
|
|
self._add_aka(channel, name, alias)
|
2013-08-11 13:28:53 +02:00
|
|
|
self.log.info('Adding Aka %r for %r (from %s)',
|
2013-08-11 13:20:36 +02:00
|
|
|
name, alias, msg.prefix)
|
2013-07-23 21:02:06 +02:00
|
|
|
irc.replySuccess()
|
2013-07-23 22:47:50 +02:00
|
|
|
except AkaError as e:
|
2013-07-23 21:02:06 +02:00
|
|
|
irc.error(str(e))
|
2013-07-23 21:10:26 +02:00
|
|
|
add = wrap(add, [getopts({
|
2014-12-26 22:18:39 +01:00
|
|
|
'channel': 'somethingWithoutSpaces',
|
2013-07-24 12:16:02 +02:00
|
|
|
}), 'something', 'text'])
|
2013-07-23 21:02:06 +02:00
|
|
|
|
2014-09-18 11:56:05 +02:00
|
|
|
def set(self, irc, msg, args, optlist, name, alias):
|
|
|
|
"""[--channel <#channel>] <name> <command>
|
|
|
|
|
|
|
|
Overwrites an existing alias <name> to execute <command> instead. The
|
|
|
|
<command> should be in the standard "command argument [nestedcommand
|
|
|
|
argument]" arguments to the alias; they'll be filled with the first,
|
|
|
|
second, etc. arguments. $1, $2, etc. can be used for required
|
|
|
|
arguments. @1, @2, etc. can be used for optional arguments. $* simply
|
|
|
|
means "all arguments that have not replaced $1, $2, etc.", ie. it will
|
|
|
|
also include optional arguments.
|
|
|
|
"""
|
|
|
|
channel = 'global'
|
|
|
|
for (option, arg) in optlist:
|
|
|
|
if option == 'channel':
|
2019-08-04 18:11:28 +02:00
|
|
|
if not irc.isChannel(arg):
|
2014-09-18 11:56:05 +02:00
|
|
|
irc.error(_('%r is not a valid channel.') % arg,
|
|
|
|
Raise=True)
|
|
|
|
channel = arg
|
|
|
|
try:
|
|
|
|
self._remove_aka(channel, name)
|
|
|
|
except AkaError as e:
|
|
|
|
irc.error(str(e), Raise=True)
|
|
|
|
|
|
|
|
if ' ' not in alias:
|
|
|
|
# If it's a single word, they probably want $*.
|
|
|
|
alias += ' $*'
|
|
|
|
try:
|
|
|
|
self._add_aka(channel, name, alias)
|
|
|
|
self.log.info('Setting Aka %r to %r (from %s)',
|
|
|
|
name, alias, msg.prefix)
|
|
|
|
irc.replySuccess()
|
|
|
|
except AkaError as e:
|
|
|
|
irc.error(str(e))
|
|
|
|
set = wrap(set, [getopts({
|
2014-12-26 22:18:39 +01:00
|
|
|
'channel': 'somethingWithoutSpaces',
|
2014-09-18 11:56:05 +02:00
|
|
|
}), 'something', 'text'])
|
|
|
|
|
2013-07-23 22:47:50 +02:00
|
|
|
def remove(self, irc, msg, args, optlist, name):
|
|
|
|
"""[--channel <#channel>] <name>
|
2013-07-23 21:02:06 +02:00
|
|
|
|
|
|
|
Removes the given alias, if unlocked.
|
|
|
|
"""
|
2013-07-23 22:47:50 +02:00
|
|
|
channel = 'global'
|
|
|
|
for (option, arg) in optlist:
|
|
|
|
if option == 'channel':
|
2019-08-04 18:11:28 +02:00
|
|
|
if not irc.isChannel(arg):
|
2013-07-23 22:47:50 +02:00
|
|
|
irc.error(_('%r is not a valid channel.') % arg,
|
|
|
|
Raise=True)
|
|
|
|
channel = arg
|
2013-07-23 21:02:06 +02:00
|
|
|
try:
|
|
|
|
self._remove_aka(channel, name)
|
2013-08-11 13:28:53 +02:00
|
|
|
self.log.info('Removing Aka %r (from %s)', name, msg.prefix)
|
2013-07-23 21:02:06 +02:00
|
|
|
irc.replySuccess()
|
2013-07-23 22:47:50 +02:00
|
|
|
except AkaError as e:
|
|
|
|
irc.error(str(e))
|
|
|
|
remove = wrap(remove, [getopts({
|
2014-12-26 22:18:39 +01:00
|
|
|
'channel': 'somethingWithoutSpaces',
|
2013-07-24 12:16:02 +02:00
|
|
|
}), 'something'])
|
2013-07-23 22:47:50 +02:00
|
|
|
|
2013-07-23 23:16:08 +02:00
|
|
|
def _checkManageCapabilities(self, irc, msg, channel):
|
|
|
|
"""Check if the user has any of the required capabilities to manage
|
|
|
|
the regexp database."""
|
|
|
|
if channel != 'global':
|
|
|
|
capability = ircdb.makeChannelCapability(channel, 'op')
|
|
|
|
else:
|
|
|
|
capability = 'admin'
|
|
|
|
if not ircdb.checkCapability(msg.prefix, capability):
|
|
|
|
irc.errorNoCapability(capability, Raise=True)
|
2013-07-23 22:47:50 +02:00
|
|
|
|
|
|
|
def lock(self, irc, msg, args, optlist, user, name):
|
|
|
|
"""[--channel <#channel>] <alias>
|
|
|
|
|
|
|
|
Locks an alias so that no one else can change it.
|
|
|
|
"""
|
|
|
|
channel = 'global'
|
|
|
|
for (option, arg) in optlist:
|
|
|
|
if option == 'channel':
|
2019-08-04 18:11:28 +02:00
|
|
|
if not irc.isChannel(arg):
|
2013-07-23 22:47:50 +02:00
|
|
|
irc.error(_('%r is not a valid channel.') % arg,
|
|
|
|
Raise=True)
|
|
|
|
channel = arg
|
2013-07-23 23:16:08 +02:00
|
|
|
self._checkManageCapabilities(irc, msg, channel)
|
2013-07-23 22:47:50 +02:00
|
|
|
try:
|
2013-07-23 23:05:45 +02:00
|
|
|
self._db.lock_aka(channel, name, user.name)
|
2013-07-23 22:47:50 +02:00
|
|
|
except AkaError as e:
|
2013-07-23 21:02:06 +02:00
|
|
|
irc.error(str(e))
|
2013-07-23 23:05:45 +02:00
|
|
|
else:
|
|
|
|
irc.replySuccess()
|
2013-07-23 22:47:50 +02:00
|
|
|
lock = wrap(lock, [getopts({
|
2014-12-26 22:18:39 +01:00
|
|
|
'channel': 'somethingWithoutSpaces',
|
2013-07-24 12:16:02 +02:00
|
|
|
}), 'user', 'something'])
|
2013-07-23 21:02:06 +02:00
|
|
|
|
2013-07-23 22:47:50 +02:00
|
|
|
def unlock(self, irc, msg, args, optlist, user, name):
|
|
|
|
"""[--channel <#channel>] <alias>
|
|
|
|
|
|
|
|
Unlocks an alias so that people can define new aliases over it.
|
|
|
|
"""
|
|
|
|
channel = 'global'
|
|
|
|
for (option, arg) in optlist:
|
|
|
|
if option == 'channel':
|
2019-08-04 18:11:28 +02:00
|
|
|
if not irc.isChannel(arg):
|
2013-07-23 22:47:50 +02:00
|
|
|
irc.error(_('%r is not a valid channel.') % arg,
|
|
|
|
Raise=True)
|
|
|
|
channel = arg
|
2013-07-23 23:16:08 +02:00
|
|
|
self._checkManageCapabilities(irc, msg, channel)
|
2013-07-23 22:47:50 +02:00
|
|
|
try:
|
2013-07-23 23:05:45 +02:00
|
|
|
self._db.unlock_aka(channel, name, user.name)
|
2013-07-23 22:47:50 +02:00
|
|
|
except AkaError as e:
|
|
|
|
irc.error(str(e))
|
2013-07-23 23:05:45 +02:00
|
|
|
else:
|
|
|
|
irc.replySuccess()
|
2013-07-23 22:47:50 +02:00
|
|
|
unlock = wrap(unlock, [getopts({
|
2014-12-26 22:18:39 +01:00
|
|
|
'channel': 'somethingWithoutSpaces',
|
2013-07-24 12:16:02 +02:00
|
|
|
}), 'user', 'something'])
|
2013-07-23 21:02:06 +02:00
|
|
|
|
2014-07-13 21:19:48 +02:00
|
|
|
def show(self, irc, msg, args, optlist, name):
|
2014-12-16 23:25:43 +01:00
|
|
|
"""[--channel <#channel>] <alias>
|
2014-07-13 21:19:48 +02:00
|
|
|
|
|
|
|
This command shows the content of an Aka.
|
|
|
|
"""
|
|
|
|
channel = 'global'
|
|
|
|
for (option, arg) in optlist:
|
|
|
|
if option == 'channel':
|
2019-08-04 18:11:28 +02:00
|
|
|
if not irc.isChannel(arg):
|
2014-07-13 21:19:48 +02:00
|
|
|
irc.error(_('%r is not a valid channel.') % arg,
|
|
|
|
Raise=True)
|
|
|
|
channel = arg
|
|
|
|
command = self._db.get_alias(channel, name)
|
2014-10-03 17:03:38 +02:00
|
|
|
if command:
|
|
|
|
irc.reply(command)
|
|
|
|
else:
|
2014-12-26 22:21:20 +01:00
|
|
|
irc.error(_('This Aka does not exist.'))
|
2014-12-26 22:18:39 +01:00
|
|
|
show = wrap(show, [getopts({'channel': 'somethingWithoutSpaces'}),
|
2014-07-13 21:19:48 +02:00
|
|
|
'text'])
|
|
|
|
|
2013-07-31 19:08:49 +02:00
|
|
|
def importaliasdatabase(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Imports the Alias database into Aka's, and clean the former."""
|
|
|
|
alias_plugin = irc.getCallback('Alias')
|
|
|
|
if alias_plugin is None:
|
|
|
|
irc.error(_('Alias plugin is not loaded.'), Raise=True)
|
|
|
|
errors = {}
|
2015-08-08 22:20:14 +02:00
|
|
|
for (name, (command, locked, func)) in \
|
|
|
|
list(alias_plugin.aliases.items()):
|
2013-07-31 19:08:49 +02:00
|
|
|
try:
|
|
|
|
self._add_aka('global', name, command)
|
|
|
|
except AkaError as e:
|
|
|
|
errors[name] = e.args[0]
|
|
|
|
else:
|
2013-08-15 10:55:57 +02:00
|
|
|
alias_plugin.removeAlias(name, evenIfLocked=True)
|
2013-07-31 19:08:49 +02:00
|
|
|
if errors:
|
|
|
|
irc.error(format(_('Error occured when importing the %n: %L'),
|
|
|
|
(len(errors), 'following', 'command'),
|
2014-01-21 10:57:38 +01:00
|
|
|
['%s (%s)' % x for x in errors.items()]))
|
2013-07-31 19:08:49 +02:00
|
|
|
else:
|
|
|
|
irc.replySuccess()
|
|
|
|
importaliasdatabase = wrap(importaliasdatabase, ['owner'])
|
|
|
|
|
2014-12-17 01:18:25 +01:00
|
|
|
def list(self, irc, msg, args, optlist):
|
2015-08-30 02:18:57 +02:00
|
|
|
"""[--channel <#channel>] [--keys] [--unlocked|--locked]
|
2014-12-17 01:18:25 +01:00
|
|
|
|
|
|
|
Lists all Akas defined for <channel>. If <channel> is not specified,
|
2015-01-15 03:39:31 +01:00
|
|
|
lists all global Akas. If --keys is given, lists only the Aka names
|
|
|
|
and not their commands."""
|
2014-12-17 01:18:25 +01:00
|
|
|
channel = 'global'
|
2015-08-30 02:18:57 +02:00
|
|
|
filterlocked = filterunlocked = False
|
2014-12-17 01:18:25 +01:00
|
|
|
for (option, arg) in optlist:
|
|
|
|
if option == 'channel':
|
2019-08-04 18:11:28 +02:00
|
|
|
if not irc.isChannel(arg):
|
2014-12-17 01:18:25 +01:00
|
|
|
irc.error(_('%r is not a valid channel.') % arg,
|
|
|
|
Raise=True)
|
|
|
|
channel = arg
|
2015-08-30 02:18:57 +02:00
|
|
|
if option == 'locked':
|
|
|
|
filterlocked = True
|
|
|
|
if option == 'unlocked':
|
|
|
|
filterunlocked = True
|
2014-12-17 01:18:25 +01:00
|
|
|
aka_list = self._db.get_aka_list(channel)
|
2015-08-30 02:18:57 +02:00
|
|
|
if filterlocked and filterunlocked:
|
|
|
|
irc.error(_('--locked and --unlocked are incompatible options.'), Raise=True)
|
|
|
|
elif filterlocked:
|
|
|
|
aka_list = [aka for aka in aka_list if
|
|
|
|
self._db.get_aka_lock(channel, aka)[0]]
|
|
|
|
elif filterunlocked:
|
|
|
|
aka_list = [aka for aka in aka_list if not
|
|
|
|
self._db.get_aka_lock(channel, aka)[0]]
|
2014-12-17 01:51:52 +01:00
|
|
|
if aka_list:
|
2015-01-15 03:39:31 +01:00
|
|
|
if 'keys' in dict(optlist):
|
|
|
|
# Strange, aka_list is a list of one length tuples
|
|
|
|
s = [k[0] for k in aka_list]
|
2016-03-11 21:00:01 +01:00
|
|
|
oneToOne = True
|
2015-01-15 03:39:31 +01:00
|
|
|
else:
|
|
|
|
aka_values = [self._db.get_alias(channel, aka) for aka in
|
|
|
|
aka_list]
|
|
|
|
s = ('{0}: "{1}"'.format(ircutils.bold(k), v) for (k, v) in
|
|
|
|
zip(aka_list, aka_values))
|
2016-03-11 21:00:01 +01:00
|
|
|
oneToOne = None
|
|
|
|
irc.replies(s, oneToOne=oneToOne)
|
2014-12-17 01:51:52 +01:00
|
|
|
else:
|
|
|
|
irc.error(_("No Akas found."))
|
2015-08-30 02:18:57 +02:00
|
|
|
list = wrap(list, [getopts({'channel': 'channel', 'keys': '', 'locked': '',
|
|
|
|
'unlocked': ''})])
|
2014-12-17 01:18:25 +01:00
|
|
|
|
2015-04-27 07:08:01 +02:00
|
|
|
def search(self, irc, msg, args, optlist, query):
|
|
|
|
"""[--channel <#channel>] <query>
|
|
|
|
|
|
|
|
Searches Akas defined for <channel>. If <channel> is not specified,
|
|
|
|
searches all global Akas."""
|
2015-05-18 23:20:23 +02:00
|
|
|
query = callbacks.canonicalName(query, preserve_spaces=True)
|
2015-04-27 07:08:01 +02:00
|
|
|
channel = 'global'
|
|
|
|
for (option, arg) in optlist:
|
|
|
|
if option == 'channel':
|
2019-08-04 18:11:28 +02:00
|
|
|
if not irc.isChannel(arg):
|
2015-04-27 07:08:01 +02:00
|
|
|
irc.error(_('%r is not a valid channel.') % arg,
|
|
|
|
Raise=True)
|
|
|
|
channel = arg
|
|
|
|
aka_list = self._db.get_aka_list(channel)
|
2015-05-18 23:20:23 +02:00
|
|
|
aka_list = [callbacks.canonicalName(k[0], preserve_spaces=True)
|
|
|
|
for k in aka_list]
|
|
|
|
matching = [aka for aka in aka_list if query in aka]
|
|
|
|
if matching:
|
2015-04-27 07:08:01 +02:00
|
|
|
irc.replies(matching)
|
|
|
|
else:
|
2015-05-18 23:20:23 +02:00
|
|
|
irc.error(_("No matching Akas were found."))
|
2015-04-27 07:08:01 +02:00
|
|
|
search = wrap(search, [getopts({'channel': 'channel'}), 'text'])
|
2013-07-31 19:08:49 +02:00
|
|
|
|
2013-07-23 21:02:06 +02:00
|
|
|
Class = Aka
|
|
|
|
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|