2015-12-07 02:40:13 +01:00
|
|
|
"""
|
|
|
|
coreplugin.py - Implements core PyLink functions as a plugin.
|
|
|
|
"""
|
2015-10-24 03:22:29 +02:00
|
|
|
|
|
|
|
import gc
|
|
|
|
import sys
|
2015-07-17 06:30:25 +02:00
|
|
|
|
|
|
|
import utils
|
|
|
|
from log import log
|
2015-08-29 18:39:33 +02:00
|
|
|
import world
|
2015-07-17 06:30:25 +02:00
|
|
|
|
|
|
|
def handle_kill(irc, source, command, args):
|
2015-09-20 22:21:04 +02:00
|
|
|
"""Handle KILLs to the main PyLink client, respawning it as needed."""
|
2015-07-17 06:30:25 +02:00
|
|
|
if args['target'] == irc.pseudoclient.uid:
|
|
|
|
irc.spawnMain()
|
2015-08-18 14:44:36 +02:00
|
|
|
utils.add_hook(handle_kill, 'KILL')
|
2015-07-17 06:30:25 +02:00
|
|
|
|
|
|
|
def handle_kick(irc, source, command, args):
|
2015-09-20 22:21:04 +02:00
|
|
|
"""Handle KICKs to the main PyLink client, rejoining channels as needed."""
|
2015-07-17 06:30:25 +02:00
|
|
|
kicked = args['target']
|
|
|
|
channel = args['channel']
|
|
|
|
if kicked == irc.pseudoclient.uid:
|
2016-01-17 01:36:45 +01:00
|
|
|
irc.proto.join(irc.pseudoclient.uid, channel)
|
2015-08-18 14:44:36 +02:00
|
|
|
utils.add_hook(handle_kick, 'KICK')
|
2015-07-17 06:30:25 +02:00
|
|
|
|
|
|
|
def handle_commands(irc, source, command, args):
|
2015-09-20 22:21:04 +02:00
|
|
|
"""Handle commands sent to the PyLink client (PRIVMSG)."""
|
2016-01-01 02:28:47 +01:00
|
|
|
if args['target'] == irc.pseudoclient.uid and not irc.isInternalClient(source):
|
2015-09-26 18:05:44 +02:00
|
|
|
irc.called_by = source
|
|
|
|
irc.callCommand(source, args['text'])
|
|
|
|
|
2015-07-17 06:30:25 +02:00
|
|
|
utils.add_hook(handle_commands, 'PRIVMSG')
|
2015-07-23 06:15:34 +02:00
|
|
|
|
|
|
|
def handle_whois(irc, source, command, args):
|
2015-09-20 22:21:04 +02:00
|
|
|
"""Handle WHOIS queries, for IRCds that send them across servers (charybdis, UnrealIRCd; NOT InspIRCd)."""
|
2015-07-23 06:15:34 +02:00
|
|
|
target = args['target']
|
|
|
|
user = irc.users.get(target)
|
|
|
|
if user is None:
|
2015-09-19 19:25:50 +02:00
|
|
|
log.warning('(%s) Got a WHOIS request for %r from %r, but the target '
|
|
|
|
'doesn\'t exist in irc.users!', irc.name, target, source)
|
|
|
|
return
|
2015-07-23 06:15:34 +02:00
|
|
|
f = irc.proto.numericServer
|
2016-01-01 02:28:47 +01:00
|
|
|
server = irc.getServer(target) or irc.sid
|
2015-07-23 06:15:34 +02:00
|
|
|
nick = user.nick
|
2015-07-23 09:05:05 +02:00
|
|
|
sourceisOper = ('o', None) in irc.users[source].modes
|
2015-07-23 06:15:34 +02:00
|
|
|
# https://www.alien.net.au/irc/irc2numerics.html
|
2015-12-18 06:51:15 +01:00
|
|
|
|
2015-07-23 06:15:34 +02:00
|
|
|
# 311: sends nick!user@host information
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, 311, source, "%s %s %s * :%s" % (nick, user.ident, user.host, user.realname))
|
2015-12-18 06:51:15 +01:00
|
|
|
|
|
|
|
# 319: RPL_WHOISCHANNELS, shows public channel list of target
|
2015-07-23 09:01:51 +02:00
|
|
|
public_chans = []
|
|
|
|
for chan in user.channels:
|
2015-12-18 06:51:15 +01:00
|
|
|
c = irc.channels[chan]
|
2015-07-23 09:01:51 +02:00
|
|
|
# Here, we'll want to hide secret/private channels from non-opers
|
|
|
|
# who are not in them.
|
2015-12-18 06:51:15 +01:00
|
|
|
|
2015-07-23 09:01:51 +02:00
|
|
|
if ((irc.cmodes.get('secret'), None) in c.modes or \
|
|
|
|
(irc.cmodes.get('private'), None) in c.modes) \
|
2015-07-23 09:05:05 +02:00
|
|
|
and not (sourceisOper or source in c.users):
|
2015-07-23 09:01:51 +02:00
|
|
|
continue
|
2015-08-25 03:37:46 +02:00
|
|
|
# Show prefix modes like a regular IRCd does.
|
|
|
|
for prefixmode, prefixchar in irc.prefixmodes.items():
|
|
|
|
modename = [mname for mname, char in irc.cmodes.items() if char == prefixmode]
|
|
|
|
if modename and target in c.prefixmodes[modename[0]+'s']:
|
|
|
|
chan = prefixchar + chan
|
2015-07-23 09:01:51 +02:00
|
|
|
public_chans.append(chan)
|
2015-12-18 06:51:15 +01:00
|
|
|
if public_chans: # Only send the line if the person is in any visible channels...
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, 319, source, '%s :%s' % (nick, ' '.join(public_chans)))
|
2015-12-18 06:51:15 +01:00
|
|
|
|
2015-08-25 03:26:02 +02:00
|
|
|
# 312: sends the server the target is on, and its server description.
|
2015-09-07 07:40:22 +02:00
|
|
|
f(server, 312, source, "%s %s :%s" % (nick, irc.servers[server].name,
|
2015-09-20 21:11:28 +02:00
|
|
|
irc.servers[server].desc))
|
2015-12-18 06:51:15 +01:00
|
|
|
|
2015-08-25 03:26:02 +02:00
|
|
|
# 313: sends a string denoting the target's operator privilege,
|
|
|
|
# only if they have umode +o.
|
|
|
|
if ('o', None) in user.modes:
|
2015-08-31 23:52:56 +02:00
|
|
|
if hasattr(user, 'opertype'):
|
2015-09-20 20:25:45 +02:00
|
|
|
opertype = user.opertype
|
2015-12-18 06:51:15 +01:00
|
|
|
else: # If the IRCd OPERTYPE doesn't exist, just write "IRC Operator"
|
2015-08-31 23:52:56 +02:00
|
|
|
opertype = "IRC Operator"
|
2015-12-18 06:51:15 +01:00
|
|
|
|
|
|
|
# Let's be gramatically correct. (If the opertype starts with a vowel,
|
|
|
|
# write "an Operator" instead of "a Operator")
|
2015-08-31 23:52:56 +02:00
|
|
|
n = 'n' if opertype[0].lower() in 'aeiou' else ''
|
2015-12-18 06:51:15 +01:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, 313, source, "%s :is a%s %s" % (nick, n, opertype))
|
2015-12-18 06:51:15 +01:00
|
|
|
|
|
|
|
# 379: RPL_WHOISMODES, used by UnrealIRCd and InspIRCd to show user modes.
|
2015-08-25 03:26:02 +02:00
|
|
|
# Only show this to opers!
|
|
|
|
if sourceisOper:
|
2015-09-19 19:25:50 +02:00
|
|
|
f(server, 378, source, "%s :is connecting from %s@%s %s" % (nick, user.ident, user.realhost, user.ip))
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, 379, source, '%s :is using modes %s' % (nick, utils.joinModes(user.modes)))
|
2015-12-18 06:51:15 +01:00
|
|
|
|
|
|
|
# 301: used to show away information if present
|
|
|
|
away_text = user.away
|
|
|
|
log.debug('(%s) coreplugin/handle_whois: away_text for %s is %r', irc.name, target, away_text)
|
|
|
|
if away_text:
|
|
|
|
f(server, 301, source, '%s :%s' % (nick, away_text))
|
|
|
|
|
2015-08-25 03:26:02 +02:00
|
|
|
# 317: shows idle and signon time. However, we don't track the user's real
|
|
|
|
# idle time, so we simply return 0.
|
|
|
|
# <- 317 GL GL 15 1437632859 :seconds idle, signon time
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, 317, source, "%s 0 %s :seconds idle, signon time" % (nick, user.ts))
|
2015-12-18 06:51:15 +01:00
|
|
|
|
2015-08-29 18:39:33 +02:00
|
|
|
for func in world.whois_handlers:
|
2015-08-25 03:38:58 +02:00
|
|
|
# Iterate over custom plugin WHOIS handlers. They return a tuple
|
|
|
|
# or list with two arguments: the numeric, and the text to send.
|
|
|
|
try:
|
2015-07-23 09:10:54 +02:00
|
|
|
res = func(irc, target)
|
|
|
|
if res:
|
|
|
|
num, text = res
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, num, source, text)
|
2015-08-25 03:38:58 +02:00
|
|
|
except Exception as e:
|
|
|
|
# Again, we wouldn't want this to crash our service, in case
|
|
|
|
# something goes wrong!
|
|
|
|
log.exception('(%s) Error caught in WHOIS handler: %s', irc.name, e)
|
|
|
|
# 318: End of WHOIS.
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, 318, source, "%s :End of /WHOIS list" % nick)
|
2015-07-23 06:15:34 +02:00
|
|
|
utils.add_hook(handle_whois, 'WHOIS')
|
2015-09-20 22:21:04 +02:00
|
|
|
|
|
|
|
def handle_mode(irc, source, command, args):
|
|
|
|
"""Protect against forced deoper attempts."""
|
|
|
|
target = args['target']
|
|
|
|
modes = args['modes']
|
|
|
|
# If the sender is not a PyLink client, and the target IS a protected
|
|
|
|
# client, revert any forced deoper attempts.
|
2016-01-01 02:28:47 +01:00
|
|
|
if irc.isInternalClient(target) and not irc.isInternalClient(source):
|
2015-09-21 01:55:04 +02:00
|
|
|
if ('-o', None) in modes and (target == irc.pseudoclient.uid or not utils.isManipulatableClient(irc, target)):
|
2015-09-20 22:21:04 +02:00
|
|
|
irc.proto.modeServer(irc.sid, target, {('+o', None)})
|
|
|
|
utils.add_hook(handle_mode, 'MODE')
|
2015-10-24 03:22:29 +02:00
|
|
|
|
2015-10-24 04:08:03 +02:00
|
|
|
def handle_operup(irc, source, command, args):
|
|
|
|
"""Logs successful oper-ups on networks."""
|
|
|
|
log.info("(%s) Successful oper-up (opertype %r) from %s", irc.name, args.get('text'), utils.getHostmask(irc, source))
|
2015-12-27 00:41:22 +01:00
|
|
|
utils.add_hook(handle_operup, 'CLIENT_OPERED')
|
2015-10-24 04:08:03 +02:00
|
|
|
|
2015-10-24 03:22:29 +02:00
|
|
|
# Essential, core commands go here so that the "commands" plugin with less-important,
|
|
|
|
# but still generic functions can be reloaded.
|
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def identify(irc, source, args):
|
|
|
|
"""<username> <password>
|
|
|
|
|
|
|
|
Logs in to PyLink using the configured administrator account."""
|
|
|
|
if utils.isChannel(irc.called_by):
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply('Error: This command must be sent in private. '
|
2015-10-24 03:22:29 +02:00
|
|
|
'(Would you really type a password inside a channel?)')
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
username, password = args[0], args[1]
|
|
|
|
except IndexError:
|
|
|
|
irc.msg(source, 'Error: Not enough arguments.')
|
|
|
|
return
|
|
|
|
# Usernames are case-insensitive, passwords are NOT.
|
|
|
|
if username.lower() == irc.conf['login']['user'].lower() and password == irc.conf['login']['password']:
|
|
|
|
realuser = irc.conf['login']['user']
|
|
|
|
irc.users[source].identified = realuser
|
|
|
|
irc.msg(source, 'Successfully logged in as %s.' % realuser)
|
2015-10-24 04:07:39 +02:00
|
|
|
log.info("(%s) Successful login to %r by %s",
|
2015-10-24 03:22:29 +02:00
|
|
|
irc.name, username, utils.getHostmask(irc, source))
|
|
|
|
else:
|
|
|
|
irc.msg(source, 'Error: Incorrect credentials.')
|
|
|
|
u = irc.users[source]
|
2015-10-24 04:07:39 +02:00
|
|
|
log.warning("(%s) Failed login to %r from %s",
|
2015-10-24 03:22:29 +02:00
|
|
|
irc.name, username, utils.getHostmask(irc, source))
|
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def shutdown(irc, source, args):
|
|
|
|
"""takes no arguments.
|
|
|
|
|
|
|
|
Exits PyLink by disconnecting all networks."""
|
|
|
|
utils.checkAuthenticated(irc, source, allowOper=False)
|
|
|
|
u = irc.users[source]
|
|
|
|
log.info('(%s) SHUTDOWN requested by "%s!%s@%s", exiting...', irc.name, u.nick,
|
|
|
|
u.ident, u.host)
|
|
|
|
for ircobj in world.networkobjects.values():
|
|
|
|
# Disable auto-connect first by setting the time to negative.
|
|
|
|
ircobj.serverdata['autoconnect'] = -1
|
2016-01-10 05:03:42 +01:00
|
|
|
ircobj.disconnect()
|
2015-10-24 03:22:29 +02:00
|
|
|
|
|
|
|
def load(irc, source, args):
|
|
|
|
"""<plugin name>.
|
|
|
|
|
|
|
|
Loads a plugin from the plugin folder."""
|
|
|
|
utils.checkAuthenticated(irc, source, allowOper=False)
|
|
|
|
try:
|
|
|
|
name = args[0]
|
|
|
|
except IndexError:
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Error: Not enough arguments. Needs 1: plugin name.")
|
2015-10-24 03:22:29 +02:00
|
|
|
return
|
|
|
|
if name in world.plugins:
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Error: %r is already loaded." % name)
|
2015-10-24 03:22:29 +02:00
|
|
|
return
|
2015-10-25 18:39:47 +01:00
|
|
|
log.info('(%s) Loading plugin %r for %s', irc.name, name, utils.getHostmask(irc, source))
|
2015-10-24 03:22:29 +02:00
|
|
|
try:
|
|
|
|
world.plugins[name] = pl = utils.loadModuleFromFolder(name, world.plugins_folder)
|
|
|
|
except ImportError as e:
|
|
|
|
if str(e) == ('No module named %r' % name):
|
|
|
|
log.exception('Failed to load plugin %r: The plugin could not be found.', name)
|
|
|
|
else:
|
|
|
|
log.exception('Failed to load plugin %r: ImportError.', name)
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
if hasattr(pl, 'main'):
|
|
|
|
log.debug('Calling main() function of plugin %r', pl)
|
|
|
|
pl.main(irc)
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Loaded plugin %r." % name)
|
2015-10-24 03:22:29 +02:00
|
|
|
utils.add_cmd(load)
|
|
|
|
|
|
|
|
def unload(irc, source, args):
|
|
|
|
"""<plugin name>.
|
|
|
|
|
|
|
|
Unloads a currently loaded plugin."""
|
|
|
|
utils.checkAuthenticated(irc, source, allowOper=False)
|
|
|
|
try:
|
|
|
|
name = args[0]
|
|
|
|
except IndexError:
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Error: Not enough arguments. Needs 1: plugin name.")
|
2015-10-24 03:22:29 +02:00
|
|
|
return
|
|
|
|
if name in world.plugins:
|
2015-10-25 18:39:47 +01:00
|
|
|
log.info('(%s) Unloading plugin %r for %s', irc.name, name, utils.getHostmask(irc, source))
|
2015-10-24 03:22:29 +02:00
|
|
|
pl = world.plugins[name]
|
|
|
|
log.debug('sys.getrefcount of plugin %s is %s', pl, sys.getrefcount(pl))
|
|
|
|
# Remove any command functions set by the plugin.
|
|
|
|
for cmdname, cmdfuncs in world.commands.copy().items():
|
|
|
|
log.debug('cmdname=%s, cmdfuncs=%s', cmdname, cmdfuncs)
|
|
|
|
for cmdfunc in cmdfuncs:
|
|
|
|
log.debug('__module__ of cmdfunc %s is %s', cmdfunc, cmdfunc.__module__)
|
|
|
|
if cmdfunc.__module__ == name:
|
|
|
|
log.debug('Removing %s from world.commands[%s]', cmdfunc, cmdname)
|
|
|
|
world.commands[cmdname].remove(cmdfunc)
|
|
|
|
# If the cmdfunc list is empty, remove it.
|
|
|
|
if not cmdfuncs:
|
|
|
|
log.debug("Removing world.commands[%s] (it's empty now)", cmdname)
|
|
|
|
del world.commands[cmdname]
|
|
|
|
|
|
|
|
# Remove any command hooks set by the plugin.
|
|
|
|
for hookname, hookfuncs in world.hooks.copy().items():
|
|
|
|
for hookfunc in hookfuncs:
|
|
|
|
if hookfunc.__module__ == name:
|
|
|
|
world.hooks[hookname].remove(hookfunc)
|
|
|
|
# If the hookfuncs list is empty, remove it.
|
|
|
|
if not hookfuncs:
|
|
|
|
del world.hooks[hookname]
|
|
|
|
|
|
|
|
# Remove whois handlers too.
|
|
|
|
for f in world.whois_handlers:
|
|
|
|
if f.__module__ == name:
|
|
|
|
world.whois_handlers.remove(f)
|
|
|
|
|
|
|
|
# Call the die() function in the plugin, if present.
|
|
|
|
if hasattr(pl, 'die'):
|
|
|
|
try:
|
|
|
|
pl.die(irc)
|
|
|
|
except: # But don't allow it to crash the server.
|
|
|
|
log.exception('(%s) Error occurred in die() of plugin %s, skipping...', irc.name, pl)
|
|
|
|
|
|
|
|
# Delete it from memory (hopefully).
|
|
|
|
del world.plugins[name]
|
|
|
|
if name in sys.modules:
|
|
|
|
del sys.modules[name]
|
|
|
|
if name in globals():
|
|
|
|
del globals()[name]
|
|
|
|
|
|
|
|
# Garbage collect.
|
|
|
|
gc.collect()
|
|
|
|
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Unloaded plugin %r." % name)
|
2015-10-24 03:22:29 +02:00
|
|
|
return True # We succeeded, make it clear (this status is used by reload() below)
|
|
|
|
else:
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Unknown plugin %r." % name)
|
2015-10-24 03:22:29 +02:00
|
|
|
utils.add_cmd(unload)
|
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def reload(irc, source, args):
|
|
|
|
"""<plugin name>.
|
|
|
|
|
|
|
|
Loads a plugin from the plugin folder."""
|
|
|
|
try:
|
|
|
|
name = args[0]
|
|
|
|
except IndexError:
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Error: Not enough arguments. Needs 1: plugin name.")
|
2015-10-24 03:22:29 +02:00
|
|
|
return
|
|
|
|
if unload(irc, source, args):
|
|
|
|
load(irc, source, args)
|
2015-11-16 06:50:23 +01:00
|
|
|
|
|
|
|
def main(irc=None):
|
|
|
|
# This is a global sanity check, to make sure the protocol module is doing
|
|
|
|
# its job.
|
|
|
|
if irc and not irc.connected.wait(2):
|
|
|
|
log.warning('(%s) IRC network %s (protocol %s) has not set '
|
2015-11-27 07:43:37 +01:00
|
|
|
'irc.connected state after 2 seconds - this may be a bug '
|
|
|
|
'in the protocol module, and will cause plugins like '
|
2015-11-16 06:50:23 +01:00
|
|
|
'relay to not work correctly!', irc.name, irc.name,
|
|
|
|
irc.protoname)
|