2016-07-08 06:06:28 +02:00
|
|
|
"""
|
|
|
|
automode.py - Provide simple channel ACL management by giving prefix modes to users matching
|
|
|
|
hostmasks or exttargets.
|
|
|
|
"""
|
|
|
|
import collections
|
|
|
|
import threading
|
|
|
|
import json
|
|
|
|
|
2016-07-08 21:58:01 +02:00
|
|
|
from pylinkirc import utils, conf, world
|
2016-07-08 06:06:28 +02:00
|
|
|
from pylinkirc.log import log
|
|
|
|
|
|
|
|
mydesc = ("The \x02Automode\x02 plugin provides simple channel ACL management by giving prefix modes "
|
|
|
|
"to users matching hostmasks or exttargets.")
|
|
|
|
|
|
|
|
# Register ourselves as a service.
|
2016-07-24 06:46:29 +02:00
|
|
|
modebot = world.services.get("automode", utils.registerService("automode", desc=mydesc))
|
2016-07-08 06:06:28 +02:00
|
|
|
reply = modebot.reply
|
|
|
|
|
|
|
|
# Databasing variables.
|
|
|
|
dbname = utils.getDatabaseName('automode')
|
|
|
|
db = collections.defaultdict(dict)
|
|
|
|
exportdb_timer = None
|
|
|
|
|
2016-07-08 07:16:21 +02:00
|
|
|
save_delay = conf.conf['bot'].get('save_delay', 300)
|
|
|
|
|
2016-07-08 06:06:28 +02:00
|
|
|
def loadDB():
|
|
|
|
"""Loads the Automode database, silently creating a new one if this fails."""
|
|
|
|
global db
|
|
|
|
try:
|
|
|
|
with open(dbname, "r") as f:
|
|
|
|
db.update(json.load(f))
|
|
|
|
except (ValueError, IOError, OSError):
|
2016-07-18 00:19:25 +02:00
|
|
|
log.info("Automode: failed to load links database %s; creating a new one in "
|
2016-07-08 06:06:28 +02:00
|
|
|
"memory.", dbname)
|
|
|
|
|
|
|
|
def exportDB():
|
|
|
|
"""Exports the automode database."""
|
|
|
|
|
|
|
|
log.debug("Automode: exporting database to %s.", dbname)
|
|
|
|
with open(dbname, 'w') as f:
|
|
|
|
# Pretty print the JSON output for better readability.
|
|
|
|
json.dump(db, f, indent=4)
|
|
|
|
|
|
|
|
def scheduleExport(starting=False):
|
|
|
|
"""
|
|
|
|
Schedules exporting of the Automode database in a repeated loop.
|
|
|
|
"""
|
|
|
|
global exportdb_timer
|
|
|
|
|
|
|
|
if not starting:
|
|
|
|
# Export the database, unless this is being called the first
|
|
|
|
# thing after start (i.e. DB has just been loaded).
|
|
|
|
exportDB()
|
|
|
|
|
2016-07-08 07:16:21 +02:00
|
|
|
exportdb_timer = threading.Timer(save_delay, scheduleExport)
|
2016-07-08 06:06:28 +02:00
|
|
|
exportdb_timer.name = 'Automode exportDB Loop'
|
|
|
|
exportdb_timer.start()
|
|
|
|
|
|
|
|
def main(irc=None):
|
|
|
|
"""Main function, called during plugin loading at start."""
|
|
|
|
|
2016-07-08 21:58:01 +02:00
|
|
|
# Load the automode database.
|
2016-07-08 06:06:28 +02:00
|
|
|
loadDB()
|
|
|
|
|
|
|
|
# Schedule periodic exports of the automode database.
|
|
|
|
scheduleExport(starting=True)
|
|
|
|
|
2016-07-08 21:58:01 +02:00
|
|
|
# Queue joins to all channels where Automode has entries.
|
|
|
|
for entry in db:
|
|
|
|
netname, channel = entry.split('#', 1)
|
|
|
|
channel = '#' + channel
|
|
|
|
log.debug('automode: auto-joining %s on %s', channel, netname)
|
|
|
|
modebot.extra_channels[netname].add(channel)
|
|
|
|
|
|
|
|
# This explicitly forces a join to connected networks (on plugin load, etc.).
|
|
|
|
mb_uid = modebot.uids.get(netname)
|
|
|
|
if netname in world.networkobjects and mb_uid in world.networkobjects[netname].users:
|
|
|
|
remoteirc = world.networkobjects[netname]
|
|
|
|
remoteirc.proto.join(mb_uid, channel)
|
|
|
|
|
|
|
|
# Call a join hook manually so other plugins like relay can understand it.
|
|
|
|
remoteirc.callHooks([mb_uid, 'PYLINK_AUTOMODE_JOIN', {'channel': channel, 'users': [mb_uid],
|
|
|
|
'modes': remoteirc.channels[channel].modes,
|
|
|
|
'parse_as': 'JOIN'}])
|
|
|
|
|
2016-07-08 06:06:28 +02:00
|
|
|
def die(sourceirc):
|
|
|
|
"""Saves the Automode database and quit."""
|
|
|
|
exportDB()
|
|
|
|
|
2016-07-08 07:41:51 +02:00
|
|
|
# Kill the scheduling for exports.
|
2016-07-08 06:06:28 +02:00
|
|
|
global exportdb_timer
|
|
|
|
if exportdb_timer:
|
|
|
|
log.debug("Automode: cancelling exportDB timer thread %s due to die()", threading.get_ident())
|
|
|
|
exportdb_timer.cancel()
|
|
|
|
|
2016-07-08 07:41:51 +02:00
|
|
|
utils.unregisterService('automode')
|
|
|
|
|
2016-07-08 06:06:28 +02:00
|
|
|
def setacc(irc, source, args):
|
2016-07-17 19:44:01 +02:00
|
|
|
"""<channel> <mask> <mode list>
|
2016-07-08 06:06:28 +02:00
|
|
|
|
|
|
|
Assigns the given prefix mode characters to the given mask for the channel given. Extended targets are supported for masks - use this to your advantage!
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
SET #channel *!*@localhost ohv
|
|
|
|
SET #channel $account v
|
|
|
|
SET #channel $oper:Network?Administrator qo
|
|
|
|
SET #staffchan $channel:#mainchan:op o
|
|
|
|
"""
|
2016-07-08 06:57:36 +02:00
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
2016-07-08 06:06:28 +02:00
|
|
|
try:
|
|
|
|
channel, mask, modes = args
|
|
|
|
except ValueError:
|
|
|
|
reply(irc, "Error: Invalid arguments given. Needs 3: channel, mask, mode list.")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
if not utils.isChannel(channel):
|
|
|
|
reply(irc, "Error: Invalid channel name %s." % channel)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Store channels case insensitively
|
|
|
|
channel = irc.toLower(channel)
|
|
|
|
|
|
|
|
# Database entries for any network+channel pair are automatically created using
|
|
|
|
# defaultdict. Note: string keys are used here instead of tuples so they can be
|
|
|
|
# exported easily as JSON.
|
|
|
|
dbentry = db[irc.name+channel]
|
|
|
|
|
|
|
|
# Otherwise, update the modes as is.
|
|
|
|
dbentry[mask] = modes
|
|
|
|
reply(irc, "Done. \x02%s\x02 now has modes \x02%s\x02 in \x02%s\x02." % (mask, modes, channel))
|
|
|
|
|
|
|
|
modebot.add_cmd(setacc, 'setaccess')
|
|
|
|
modebot.add_cmd(setacc, 'set')
|
|
|
|
modebot.add_cmd(setacc, featured=True)
|
|
|
|
|
|
|
|
def delacc(irc, source, args):
|
|
|
|
"""<channel> <mask>
|
|
|
|
|
|
|
|
Removes the Automode entry for the given mask on the given channel, if one exists.
|
|
|
|
"""
|
2016-07-08 06:57:36 +02:00
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
2016-07-08 06:06:28 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
channel, mask = args
|
2016-07-12 01:29:02 +02:00
|
|
|
channel = irc.toLower(channel)
|
2016-07-08 06:06:28 +02:00
|
|
|
except ValueError:
|
|
|
|
reply(irc, "Error: Invalid arguments given. Needs 2: channel, mask")
|
|
|
|
return
|
|
|
|
|
|
|
|
dbentry = db.get(irc.name+channel)
|
|
|
|
|
|
|
|
if dbentry is None:
|
|
|
|
reply(irc, "Error: no Automode access entries exist for \x02%s\x02." % channel)
|
|
|
|
return
|
|
|
|
|
|
|
|
if mask in dbentry:
|
|
|
|
del dbentry[mask]
|
|
|
|
reply(irc, "Done. Removed the Automode access entry for \x02%s\x02 in \x02%s\x02." % (mask, channel))
|
|
|
|
else:
|
|
|
|
reply(irc, "Error: No Automode access entry for \x02%s\x02 exists in \x02%s\x02." % (mask, channel))
|
|
|
|
|
|
|
|
# Remove channels if no more entries are left.
|
|
|
|
if not dbentry:
|
|
|
|
log.debug("Automode: purging empty channel pair %s/%s", irc.name, channel)
|
|
|
|
del db[irc.name+channel]
|
|
|
|
|
|
|
|
return
|
|
|
|
modebot.add_cmd(delacc, 'delaccess')
|
|
|
|
modebot.add_cmd(delacc, 'del')
|
|
|
|
modebot.add_cmd(delacc, featured=True)
|
|
|
|
|
|
|
|
def listacc(irc, source, args):
|
|
|
|
"""<channel>
|
|
|
|
|
|
|
|
Lists all Automode entries for the given channel."""
|
|
|
|
irc.checkAuthenticated(source)
|
|
|
|
try:
|
|
|
|
channel = irc.toLower(args[0])
|
|
|
|
except IndexError:
|
|
|
|
reply(irc, "Error: Invalid arguments given. Needs 1: channel.")
|
|
|
|
return
|
|
|
|
dbentry = db.get(irc.name+channel)
|
|
|
|
if not dbentry:
|
|
|
|
reply(irc, "Error: No Automode access entries exist for \x02%s\x02." % channel)
|
|
|
|
return
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Iterate over all entries and print them. Do this in private to prevent channel
|
|
|
|
# floods.
|
|
|
|
reply(irc, "Showing Automode entries for \x02%s\x02:" % channel, private=True)
|
|
|
|
for entrynum, entry in enumerate(dbentry.items(), start=1):
|
|
|
|
mask, modes = entry
|
|
|
|
reply(irc, "[%s] \x02%s\x02 has modes +\x02%s\x02" % (entrynum, mask, modes), private=True)
|
2016-07-08 06:23:06 +02:00
|
|
|
reply(irc, "End of Automode entries list.", private=True)
|
2016-07-08 06:06:28 +02:00
|
|
|
modebot.add_cmd(listacc, featured=True)
|
|
|
|
modebot.add_cmd(listacc, 'listaccess')
|
|
|
|
|
|
|
|
def save(irc, source, args):
|
|
|
|
"""takes no arguments.
|
|
|
|
|
|
|
|
Saves the Automode database to disk."""
|
|
|
|
irc.checkAuthenticated(source)
|
|
|
|
exportDB()
|
|
|
|
reply(irc, 'Done.')
|
|
|
|
modebot.add_cmd(save)
|
2016-07-08 06:23:06 +02:00
|
|
|
|
2016-07-13 06:57:25 +02:00
|
|
|
def match(irc, channel, uids=None):
|
2016-07-08 06:31:36 +02:00
|
|
|
"""
|
|
|
|
Automode matcher engine.
|
|
|
|
"""
|
|
|
|
dbentry = db.get(irc.name+channel)
|
|
|
|
if dbentry is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
modebot_uid = modebot.uids.get(irc.name)
|
|
|
|
|
|
|
|
# Check every mask defined in the channel ACL.
|
2016-07-12 09:05:12 +02:00
|
|
|
outgoing_modes = []
|
2016-07-08 06:31:36 +02:00
|
|
|
|
2016-07-13 06:57:25 +02:00
|
|
|
# If UIDs are given, match those. Otherwise, match all users in the given channel.
|
|
|
|
uids = uids or irc.channels[channel].users
|
2016-07-13 06:53:15 +02:00
|
|
|
|
|
|
|
for mask, modes in dbentry.items():
|
|
|
|
for uid in uids:
|
|
|
|
if irc.matchHost(mask, uid):
|
|
|
|
# User matched a mask. Filter the mode list given to only those that are valid
|
|
|
|
# prefix mode characters.
|
|
|
|
outgoing_modes += [('+'+mode, uid) for mode in modes if mode in irc.prefixmodes]
|
|
|
|
log.debug("(%s) automode: Filtered mode list of %s to %s (protocol:%s)",
|
|
|
|
irc.name, modes, outgoing_modes, irc.protoname)
|
|
|
|
|
2016-07-13 07:16:12 +02:00
|
|
|
# If the Automode bot is missing, send the mode through the PyLink server.
|
|
|
|
if modebot_uid not in irc.users:
|
|
|
|
modebot_uid = irc.sid
|
|
|
|
|
|
|
|
log.debug("(%s) automode: sending modes from modebot_uid %s",
|
|
|
|
irc.name, modebot_uid)
|
2016-07-08 06:31:36 +02:00
|
|
|
|
2016-07-12 09:05:12 +02:00
|
|
|
irc.proto.mode(modebot_uid, channel, outgoing_modes)
|
2016-07-08 06:31:36 +02:00
|
|
|
|
2016-07-12 09:05:12 +02:00
|
|
|
# Create a hook payload to support plugins like relay.
|
|
|
|
irc.callHooks([modebot_uid, 'AUTOMODE_MODE',
|
|
|
|
{'target': channel, 'modes': outgoing_modes, 'parse_as': 'MODE'}])
|
2016-07-08 06:31:36 +02:00
|
|
|
|
2016-07-12 01:29:17 +02:00
|
|
|
def syncacc(irc, source, args):
|
2016-07-17 19:52:56 +02:00
|
|
|
"""<channel>
|
|
|
|
|
2016-07-12 01:29:17 +02:00
|
|
|
Syncs Automode access lists to the channel.
|
|
|
|
"""
|
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
|
|
|
try:
|
|
|
|
channel = irc.toLower(args[0])
|
|
|
|
except IndexError:
|
|
|
|
reply(irc, "Error: Invalid arguments given. Needs 1: channel.")
|
|
|
|
return
|
|
|
|
|
2016-07-13 06:53:15 +02:00
|
|
|
match(irc, channel)
|
2016-07-12 01:29:17 +02:00
|
|
|
|
|
|
|
reply(irc, 'Done.')
|
|
|
|
|
|
|
|
modebot.add_cmd(syncacc, featured=True)
|
|
|
|
modebot.add_cmd(syncacc, 'sync')
|
|
|
|
modebot.add_cmd(syncacc, 'syncaccess')
|
|
|
|
|
|
|
|
def clearacc(irc, source, args):
|
|
|
|
"""<channel>
|
|
|
|
|
|
|
|
Removes all Automode entries for the given channel.
|
|
|
|
"""
|
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
|
|
|
|
|
|
|
try:
|
|
|
|
channel = irc.toLower(args[0])
|
|
|
|
except IndexError:
|
|
|
|
reply(irc, "Error: Invalid arguments given. Needs 1: channel.")
|
|
|
|
return
|
|
|
|
|
|
|
|
if db.get(irc.name+channel):
|
|
|
|
log.debug("Automode: purging channel pair %s/%s", irc.name, channel)
|
|
|
|
del db[irc.name+channel]
|
|
|
|
reply(irc, "Done. Removed all Automode access entries for \x02%s\x02." % channel)
|
|
|
|
else:
|
|
|
|
reply(irc, "Error: No Automode access entries exist for \x02%s\x02." % channel)
|
|
|
|
|
|
|
|
modebot.add_cmd(clearacc, 'clearaccess')
|
|
|
|
modebot.add_cmd(clearacc, 'clear')
|
|
|
|
modebot.add_cmd(clearacc, featured=True)
|
|
|
|
|
2016-07-08 06:23:06 +02:00
|
|
|
def handle_join(irc, source, command, args):
|
|
|
|
"""
|
|
|
|
Automode JOIN listener. This sets modes accordingly if the person joining matches a mask in the
|
|
|
|
ACL.
|
|
|
|
"""
|
|
|
|
channel = irc.toLower(args['channel'])
|
2016-07-13 06:57:25 +02:00
|
|
|
match(irc, channel, args['users'])
|
2016-07-08 06:23:06 +02:00
|
|
|
|
|
|
|
utils.add_hook(handle_join, 'JOIN')
|
2016-07-08 06:54:59 +02:00
|
|
|
utils.add_hook(handle_join, 'PYLINK_RELAY_JOIN') # Handle the relay version of join
|
|
|
|
utils.add_hook(handle_join, 'PYLINK_SERVICE_JOIN') # And the version for service bots
|
2016-07-08 06:31:36 +02:00
|
|
|
|
|
|
|
def handle_services_login(irc, source, command, args):
|
|
|
|
"""
|
2016-07-17 19:52:56 +02:00
|
|
|
Handles services login change, to trigger Automode matching.
|
|
|
|
"""
|
2016-07-08 06:31:36 +02:00
|
|
|
for channel in irc.users[source].channels:
|
|
|
|
# Look at all the users' channels for any possible changes.
|
2016-07-13 06:57:25 +02:00
|
|
|
match(irc, channel, [source])
|
2016-07-08 06:31:36 +02:00
|
|
|
|
|
|
|
utils.add_hook(handle_services_login, 'CLIENT_SERVICES_LOGIN')
|
2016-07-08 06:43:25 +02:00
|
|
|
utils.add_hook(handle_services_login, 'PYLINK_RELAY_SERVICES_LOGIN')
|