2016-06-21 20:25:47 +02:00
|
|
|
"""
|
|
|
|
service_support.py - Implements handlers for the PyLink ServiceBot API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from pylinkirc import utils, world, conf
|
|
|
|
from pylinkirc.log import log
|
|
|
|
|
|
|
|
def spawn_service(irc, source, command, args):
|
|
|
|
"""Handles new service bot introductions."""
|
|
|
|
|
|
|
|
if not irc.connected.is_set():
|
|
|
|
return
|
|
|
|
|
|
|
|
# Service name
|
|
|
|
name = args['name']
|
|
|
|
|
2017-06-30 08:01:39 +02:00
|
|
|
if name != 'pylink' and not irc.has_cap('can-spawn-clients'):
|
2017-03-24 07:53:43 +01:00
|
|
|
log.debug("(%s) Not spawning service %s because the server doesn't support spawning clients",
|
|
|
|
irc.name, name)
|
|
|
|
return
|
|
|
|
|
2016-06-21 20:25:47 +02:00
|
|
|
# Get the ServiceBot object.
|
|
|
|
sbot = world.services[name]
|
|
|
|
|
|
|
|
# Look up the nick or ident in the following order:
|
|
|
|
# 1) Network specific nick/ident settings for this service (servers::irc.name::servicename_nick)
|
|
|
|
# 2) Global settings for this service (servicename::nick)
|
|
|
|
# 3) The preferred nick/ident combination defined by the plugin (sbot.nick / sbot.ident)
|
|
|
|
# 4) The literal service name.
|
|
|
|
# settings, and then falling back to the literal service name.
|
2017-07-13 00:53:02 +02:00
|
|
|
sbconf = conf.conf.get(name, {})
|
|
|
|
nick = irc.serverdata.get("%s_nick" % name) or sbconf.get('nick') or sbot.nick or name
|
|
|
|
ident = irc.serverdata.get("%s_ident" % name) or sbconf.get('ident') or sbot.ident or name
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2017-07-13 00:53:02 +02:00
|
|
|
# Determine host the same way as above, except fall back to server hostname.
|
|
|
|
host = irc.serverdata.get("%s_host" % name) or sbconf.get('host') or irc.hostname()
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2017-07-14 14:51:40 +02:00
|
|
|
# Determine realname the same way as above, except fall back to pylink:realname (and if that fails, the service name).
|
|
|
|
realname = irc.serverdata.get("%s_realname" % name) or sbconf.get('realname') or conf.conf['pylink'].get('realname') or name
|
|
|
|
|
2016-07-02 06:25:58 +02:00
|
|
|
# Spawning service clients with these umodes where supported. servprotect usage is a
|
|
|
|
# configuration option.
|
|
|
|
preferred_modes = ['oper', 'hideoper', 'hidechans', 'invisible', 'bot']
|
2016-06-21 20:25:47 +02:00
|
|
|
modes = []
|
2016-07-02 06:25:58 +02:00
|
|
|
|
2017-07-14 14:51:40 +02:00
|
|
|
if conf.conf['pylink'].get('protect_services'):
|
2016-07-02 06:25:58 +02:00
|
|
|
preferred_modes.append('servprotect')
|
|
|
|
|
|
|
|
for mode in preferred_modes:
|
2016-06-21 20:25:47 +02:00
|
|
|
mode = irc.umodes.get(mode)
|
|
|
|
if mode:
|
|
|
|
modes.append((mode, None))
|
|
|
|
|
|
|
|
# Track the service's UIDs on each network.
|
2017-05-13 03:27:54 +02:00
|
|
|
log.debug('(%s) spawn_service: Using nick %s for service %s', irc.name, nick, name)
|
2017-06-30 08:01:39 +02:00
|
|
|
u = irc.nick_to_uid(nick)
|
|
|
|
if u and irc.is_internal_client(u): # If an internal client exists, reuse it.
|
2017-05-13 03:27:54 +02:00
|
|
|
log.debug('(%s) spawn_service: Using existing client %s/%s', irc.name, u, nick)
|
|
|
|
userobj = irc.users[u]
|
|
|
|
else:
|
|
|
|
log.debug('(%s) spawn_service: Spawning new client %s', irc.name, nick)
|
2017-07-01 06:30:20 +02:00
|
|
|
userobj = irc.spawn_client(nick, ident, host, modes=modes, opertype="PyLink Service",
|
2017-07-13 00:53:02 +02:00
|
|
|
realname=realname, manipulatable=sbot.manipulatable)
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2017-07-01 06:44:31 +02:00
|
|
|
# Store the service name in the User object for easier access.
|
2016-11-10 04:07:01 +01:00
|
|
|
userobj.service = name
|
|
|
|
|
2016-06-21 20:25:47 +02:00
|
|
|
sbot.uids[irc.name] = u = userobj.uid
|
|
|
|
|
|
|
|
# Special case: if this is the main PyLink client being spawned,
|
|
|
|
# assign this as irc.pseudoclient.
|
|
|
|
if name == 'pylink':
|
2017-05-13 03:27:54 +02:00
|
|
|
log.debug('(%s) spawn_service: irc.pseudoclient set to UID %s', irc.name, u)
|
2016-06-21 20:25:47 +02:00
|
|
|
irc.pseudoclient = userobj
|
|
|
|
|
2017-02-27 03:11:20 +01:00
|
|
|
channels = set(irc.serverdata.get(name+'_channels', [])) | set(irc.serverdata.get('channels', [])) | \
|
|
|
|
sbot.extra_channels.get(irc.name, set())
|
2016-09-24 08:39:12 +02:00
|
|
|
sbot.join(irc, channels)
|
2016-06-21 20:25:47 +02:00
|
|
|
|
|
|
|
utils.add_hook(spawn_service, 'PYLINK_NEW_SERVICE')
|
|
|
|
|
|
|
|
def handle_disconnect(irc, source, command, args):
|
|
|
|
"""Handles network disconnections."""
|
|
|
|
for name, sbot in world.services.items():
|
|
|
|
try:
|
|
|
|
del sbot.uids[irc.name]
|
2016-07-02 06:08:50 +02:00
|
|
|
log.debug("coremods.service_support: removing uids[%s] from service bot %s", irc.name, sbot.name)
|
2016-06-21 20:25:47 +02:00
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
utils.add_hook(handle_disconnect, 'PYLINK_DISCONNECT')
|
|
|
|
|
|
|
|
def handle_endburst(irc, source, command, args):
|
|
|
|
"""Handles network bursts."""
|
|
|
|
if source == irc.uplink:
|
|
|
|
log.debug('(%s): spawning service bots now.', irc.name)
|
|
|
|
|
|
|
|
# We just connected. Burst all our registered services.
|
|
|
|
for name, sbot in world.services.items():
|
|
|
|
spawn_service(irc, source, command, {'name': name})
|
|
|
|
|
|
|
|
utils.add_hook(handle_endburst, 'ENDBURST')
|
|
|
|
|
2016-06-25 22:58:59 +02:00
|
|
|
def handle_kill(irc, source, command, args):
|
|
|
|
"""Handle KILLs to PyLink service bots, respawning them as needed."""
|
|
|
|
target = args['target']
|
2017-05-13 03:45:27 +02:00
|
|
|
userdata = args.get('userdata')
|
2017-06-30 08:01:39 +02:00
|
|
|
sbot = irc.get_service_bot(target)
|
2017-05-13 03:45:27 +02:00
|
|
|
servicename = None
|
|
|
|
|
|
|
|
if userdata and hasattr(userdata, 'service'): # Look for the target's service name attribute
|
|
|
|
servicename = userdata.service
|
|
|
|
elif sbot: # Or their service bot instance
|
|
|
|
servicename = sbot.name
|
|
|
|
if servicename:
|
2017-08-02 16:14:57 +02:00
|
|
|
log.info('(%s) Received kill to service %r (nick: %r) from %s (reason: %r).', irc.name, servicename,
|
|
|
|
userdata.nick if userdata else irc.users[target].nick, irc.get_hostmask(source), args.get('text'))
|
2017-05-13 03:45:27 +02:00
|
|
|
spawn_service(irc, source, command, {'name': servicename})
|
|
|
|
|
2016-06-25 22:58:59 +02:00
|
|
|
utils.add_hook(handle_kill, 'KILL')
|
|
|
|
|
|
|
|
def handle_kick(irc, source, command, args):
|
|
|
|
"""Handle KICKs to the PyLink service bots, rejoining channels as needed."""
|
|
|
|
kicked = args['target']
|
|
|
|
channel = args['channel']
|
2017-06-30 08:01:39 +02:00
|
|
|
sbot = irc.get_service_bot(kicked)
|
2016-09-24 21:22:12 +02:00
|
|
|
if sbot:
|
|
|
|
sbot.join(irc, channel)
|
2016-06-25 22:58:59 +02:00
|
|
|
utils.add_hook(handle_kick, 'KICK')
|
|
|
|
|
|
|
|
def handle_commands(irc, source, command, args):
|
|
|
|
"""Handle commands sent to the PyLink service bots (PRIVMSG)."""
|
|
|
|
target = args['target']
|
|
|
|
text = args['text']
|
|
|
|
|
2017-06-30 08:01:39 +02:00
|
|
|
sbot = irc.get_service_bot(target)
|
2016-06-25 22:58:59 +02:00
|
|
|
if sbot:
|
|
|
|
sbot.call_cmd(irc, source, text)
|
|
|
|
|
|
|
|
utils.add_hook(handle_commands, 'PRIVMSG')
|
|
|
|
|
2016-06-21 20:25:47 +02:00
|
|
|
# Register the main PyLink service. All command definitions MUST go after this!
|
2016-07-01 02:44:35 +02:00
|
|
|
# TODO: be more specific, and possibly allow plugins to modify this to mention
|
|
|
|
# their features?
|
2017-03-11 08:27:10 +01:00
|
|
|
mydesc = "\x02PyLink\x02 provides extended network services for IRC."
|
|
|
|
utils.registerService('pylink', desc=mydesc, manipulatable=True)
|