2016-07-23 09:55:06 +02:00
|
|
|
# relay_clientbot.py: Clientbot extensions for Relay
|
2016-07-23 21:05:22 +02:00
|
|
|
import string
|
2016-08-09 07:20:31 +02:00
|
|
|
import time
|
2016-07-23 09:55:06 +02:00
|
|
|
|
2016-07-23 21:05:22 +02:00
|
|
|
from pylinkirc import utils, conf, world
|
2016-07-23 09:55:06 +02:00
|
|
|
from pylinkirc.log import log
|
|
|
|
|
2017-06-30 03:07:25 +02:00
|
|
|
# Clientbot default styles:
|
2016-08-04 21:50:38 +02:00
|
|
|
# These use template strings as documented @ https://docs.python.org/3/library/string.html#template-strings
|
2017-10-15 10:42:07 +02:00
|
|
|
default_styles = {'MESSAGE': '\x02[$netname]\x02 <$mode_prefix$colored_sender> $text',
|
2017-06-30 03:07:25 +02:00
|
|
|
'KICK': '\x02[$netname]\x02 - $colored_sender$sender_identhost has kicked $target_nick from $channel ($text)',
|
|
|
|
'PART': '\x02[$netname]\x02 - $colored_sender$sender_identhost has left $channel ($text)',
|
|
|
|
'JOIN': '\x02[$netname]\x02 - $colored_sender$sender_identhost has joined $channel',
|
|
|
|
'NICK': '\x02[$netname]\x02 - $colored_sender$sender_identhost is now known as $newnick',
|
|
|
|
'QUIT': '\x02[$netname]\x02 - $colored_sender$sender_identhost has quit ($text)',
|
2017-10-15 10:42:07 +02:00
|
|
|
'ACTION': '\x02[$netname]\x02 * $mode_prefix$colored_sender $text',
|
|
|
|
'NOTICE': '\x02[$netname]\x02 - Notice from $mode_prefix$colored_sender: $text',
|
2017-06-30 03:07:25 +02:00
|
|
|
'SQUIT': '\x02[$netname]\x02 - Netsplit lost users: $colored_nicks',
|
|
|
|
'SJOIN': '\x02[$netname]\x02 - Netjoin gained users: $colored_nicks',
|
2017-08-12 04:22:14 +02:00
|
|
|
'MODE': '\x02[$netname]\x02 - $colored_sender$sender_identhost sets mode $modes on $channel',
|
2016-08-31 23:07:32 +02:00
|
|
|
'PM': 'PM from $sender on $netname: $text',
|
2016-09-01 03:30:51 +02:00
|
|
|
'PNOTICE': '<$sender> $text',
|
2016-07-23 21:25:52 +02:00
|
|
|
}
|
2016-07-23 09:55:06 +02:00
|
|
|
|
2016-07-23 21:05:22 +02:00
|
|
|
def color_text(s):
|
|
|
|
"""
|
2016-12-22 08:48:05 +01:00
|
|
|
Returns a colorized version of the given text based on a simple hash algorithm.
|
2016-07-23 21:05:22 +02:00
|
|
|
"""
|
2019-02-07 22:49:38 +01:00
|
|
|
if not s:
|
|
|
|
return s
|
2017-07-02 03:06:50 +02:00
|
|
|
colors = ('03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '15')
|
2016-12-22 08:48:05 +01:00
|
|
|
hash_output = hash(s.encode())
|
|
|
|
num = hash_output % len(colors)
|
2016-07-23 21:05:22 +02:00
|
|
|
return "\x03%s%s\x03" % (colors[num], s)
|
|
|
|
|
|
|
|
def cb_relay_core(irc, source, command, args):
|
|
|
|
"""
|
2016-08-31 23:07:32 +02:00
|
|
|
This function takes Clientbot events and formats them as text to the target channel / user.
|
2016-07-23 21:05:22 +02:00
|
|
|
"""
|
|
|
|
real_command = command.split('_')[-1]
|
|
|
|
|
|
|
|
relay = world.plugins.get('relay')
|
2016-08-31 23:07:32 +02:00
|
|
|
|
2016-09-01 03:30:51 +02:00
|
|
|
private = False
|
2016-08-31 23:07:32 +02:00
|
|
|
|
2016-07-23 21:05:22 +02:00
|
|
|
if irc.pseudoclient and relay:
|
2016-07-24 03:48:27 +02:00
|
|
|
try:
|
2017-06-30 08:01:39 +02:00
|
|
|
sourcename = irc.get_friendly_name(source)
|
2016-07-24 03:48:27 +02:00
|
|
|
except KeyError: # User has left due to /quit
|
|
|
|
sourcename = args['userdata'].nick
|
|
|
|
|
2017-10-21 22:19:31 +02:00
|
|
|
relay_conf = conf.conf.get('relay') or {}
|
2016-08-09 07:20:31 +02:00
|
|
|
|
|
|
|
# Be less floody on startup: don't relay non-PRIVMSGs for the first X seconds after connect.
|
2017-08-22 08:16:38 +02:00
|
|
|
startup_delay = relay_conf.get('clientbot_startup_delay', 20)
|
2016-08-09 07:20:31 +02:00
|
|
|
|
2019-02-10 23:35:34 +01:00
|
|
|
target = args.get('target')
|
2019-02-10 23:22:18 +01:00
|
|
|
if isinstance(target, str):
|
|
|
|
# Remove STATUSMSG prefixes (e.g. @#channel) before checking whether target is a channel
|
|
|
|
target = target.lstrip(''.join(irc.prefixmodes.values()))
|
|
|
|
|
2016-07-24 03:48:27 +02:00
|
|
|
# Special case for CTCPs.
|
|
|
|
if real_command == 'MESSAGE':
|
|
|
|
# CTCP action, format accordingly
|
|
|
|
if (not args.get('is_notice')) and args['text'].startswith('\x01ACTION ') and args['text'].endswith('\x01'):
|
|
|
|
args['text'] = args['text'][8:-1]
|
|
|
|
real_command = 'ACTION'
|
|
|
|
|
2019-02-10 23:22:18 +01:00
|
|
|
elif not irc.is_channel(target):
|
2016-09-01 03:30:51 +02:00
|
|
|
# Target is a user; handle this accordingly.
|
2016-08-31 23:07:32 +02:00
|
|
|
if relay_conf.get('allow_clientbot_pms'):
|
2016-09-01 03:30:51 +02:00
|
|
|
real_command = 'PNOTICE' if args.get('is_notice') else 'PM'
|
|
|
|
private = True
|
2016-08-31 23:07:32 +02:00
|
|
|
|
2016-07-24 03:48:27 +02:00
|
|
|
# Other CTCPs are ignored
|
|
|
|
elif args['text'].startswith('\x01'):
|
|
|
|
return
|
2016-08-31 23:07:32 +02:00
|
|
|
|
2016-07-24 03:48:27 +02:00
|
|
|
elif args.get('is_notice'): # Different syntax for notices
|
|
|
|
real_command = 'NOTICE'
|
2016-08-09 07:20:31 +02:00
|
|
|
elif (time.time() - irc.start_ts) < startup_delay:
|
|
|
|
log.debug('(%s) relay_cb_core: Not relaying %s because of startup delay of %s.', irc.name,
|
|
|
|
real_command, startup_delay)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Try to fetch the format for the given command from the relay:clientbot_styles:$command
|
|
|
|
# key, falling back to one defined in default_styles above, and then nothing if not found
|
|
|
|
# there.
|
2018-06-09 04:04:45 +02:00
|
|
|
text_template = irc.get_service_option('relay', 'clientbot_styles', {}).get(
|
|
|
|
real_command, default_styles.get(real_command, ''))
|
2016-07-23 21:05:22 +02:00
|
|
|
text_template = string.Template(text_template)
|
|
|
|
|
|
|
|
if text_template:
|
2017-06-30 08:01:39 +02:00
|
|
|
if irc.get_service_bot(source):
|
2016-09-07 04:53:31 +02:00
|
|
|
# HACK: service bots are global and lack the relay state we look for.
|
|
|
|
# just pretend the message comes from the current network.
|
|
|
|
log.debug('(%s) relay_cb_core: Overriding network origin to local (source=%s)', irc.name, source)
|
2016-09-24 21:19:56 +02:00
|
|
|
sourcenet = irc.name
|
2017-10-15 10:42:07 +02:00
|
|
|
realsource = source
|
2016-09-07 04:53:31 +02:00
|
|
|
else:
|
|
|
|
# Get the original client that the relay client source was meant for.
|
|
|
|
log.debug('(%s) relay_cb_core: Trying to find original sender (user) for %s', irc.name, source)
|
2016-07-24 20:54:14 +02:00
|
|
|
try:
|
2016-12-10 02:57:02 +01:00
|
|
|
origuser = relay.get_orig_user(irc, source) or args['userdata'].remote
|
2016-07-24 20:54:14 +02:00
|
|
|
except (AttributeError, KeyError):
|
2016-09-07 04:53:31 +02:00
|
|
|
log.debug('(%s) relay_cb_core: Trying to find original sender (server) for %s. serverdata=%s', irc.name, source, args.get('serverdata'))
|
|
|
|
try:
|
2017-10-15 10:42:07 +02:00
|
|
|
localsid = args.get('serverdata') or irc.servers[source]
|
|
|
|
origuser = (localsid.remote, world.networkobjects[localsid.remote].uplink)
|
2016-09-07 04:53:31 +02:00
|
|
|
except (AttributeError, KeyError):
|
|
|
|
return
|
|
|
|
|
2017-10-15 10:42:07 +02:00
|
|
|
sourcenet, realsource = origuser
|
2019-05-19 04:38:47 +02:00
|
|
|
log.debug('(%s) relay_cb_core: Original sender found as %s', irc.name, origuser)
|
|
|
|
if 'newnick' not in args: # XXX: we can't really normalize sender names for nick changes, because we'll always see the new nick
|
|
|
|
try:
|
|
|
|
sourcename = world.networkobjects[sourcenet].get_friendly_name(realsource)
|
|
|
|
except LookupError:
|
|
|
|
# Fallbacks for users that have just quit / servers that have just split
|
|
|
|
if args.get('serverdata'):
|
|
|
|
sourcename = args['serverdata'].name
|
|
|
|
elif args.get('userdata'):
|
|
|
|
sourcename = args['userdata'].nick
|
|
|
|
else:
|
|
|
|
log.debug('(%s) relay_cb_core: could not get original name of sender %s/%s', irc.name, realsource, sourcenet, exc_info=True)
|
2016-08-04 21:50:38 +02:00
|
|
|
|
2016-07-24 07:30:25 +02:00
|
|
|
try: # Try to get the full network name
|
2016-09-24 21:19:56 +02:00
|
|
|
netname = conf.conf['servers'][sourcenet]['netname']
|
2016-07-24 07:30:25 +02:00
|
|
|
except KeyError:
|
2016-09-24 21:19:56 +02:00
|
|
|
netname = sourcenet
|
2016-07-23 21:05:22 +02:00
|
|
|
|
|
|
|
# Figure out where the message is destined to.
|
2018-03-11 04:54:33 +01:00
|
|
|
stripped_target = target = args.get('channel') or args.get('target')
|
2019-02-10 23:22:18 +01:00
|
|
|
if isinstance(target, str):
|
2018-03-11 04:54:33 +01:00
|
|
|
# HACK: cheap fix to prevent @#channel messages from interpreted as non-channel specific
|
|
|
|
stripped_target = target.lstrip(''.join(irc.prefixmodes.values()))
|
|
|
|
|
|
|
|
if target is None or not (irc.is_channel(stripped_target) or private):
|
2016-08-31 23:07:32 +02:00
|
|
|
# Non-channel specific message (e.g. QUIT or NICK). If this isn't a PM, figure out
|
|
|
|
# all channels that the sender shares over the relay, and relay them to those
|
|
|
|
# channels.
|
2016-07-24 03:48:27 +02:00
|
|
|
userdata = args.get('userdata') or irc.users.get(source)
|
2016-08-04 21:50:38 +02:00
|
|
|
if not userdata:
|
|
|
|
# No user data given. This was probably some other global event such as SQUIT.
|
|
|
|
userdata = irc.pseudoclient
|
|
|
|
|
2017-08-18 21:39:47 +02:00
|
|
|
targets = [channel for channel in userdata.channels if relay.get_relay(irc, channel)]
|
2016-07-24 03:48:27 +02:00
|
|
|
else:
|
|
|
|
# Pluralize the channel so that we can iterate over it.
|
2016-09-01 03:30:51 +02:00
|
|
|
targets = [target]
|
2018-03-11 04:54:33 +01:00
|
|
|
args['channel'] = stripped_target
|
2016-09-01 03:30:51 +02:00
|
|
|
log.debug('(%s) relay_cb_core: Relaying event %s to channels: %s', irc.name, real_command, targets)
|
2016-07-23 21:05:22 +02:00
|
|
|
|
2017-10-15 10:42:07 +02:00
|
|
|
identhost = ''
|
2016-07-23 21:05:22 +02:00
|
|
|
if source in irc.users:
|
2016-07-23 21:25:52 +02:00
|
|
|
try:
|
2019-05-19 04:38:47 +02:00
|
|
|
identhost = irc.get_hostmask(source).split('!', 1)[-1]
|
2016-07-23 21:25:52 +02:00
|
|
|
except KeyError: # User got removed due to quit
|
2019-05-19 04:38:47 +02:00
|
|
|
identhost = '%s@%s' % (args['userdata'].ident, args['userdata'].host)
|
2016-07-23 21:05:22 +02:00
|
|
|
# This is specifically spaced so that ident@host is only shown for users that have
|
|
|
|
# one, and not servers.
|
|
|
|
identhost = ' (%s)' % identhost
|
|
|
|
|
2016-08-04 21:50:38 +02:00
|
|
|
# $target_nick: Convert the target for kicks, etc. from a UID to a nick
|
2016-07-23 21:25:52 +02:00
|
|
|
if args.get("target") in irc.users:
|
2017-06-30 08:01:39 +02:00
|
|
|
args["target_nick"] = irc.get_friendly_name(args['target'])
|
2016-07-24 03:48:27 +02:00
|
|
|
|
2017-08-12 04:22:14 +02:00
|
|
|
# Join up modes from their list form
|
|
|
|
if args.get('modes'):
|
|
|
|
args['modes'] = irc.join_modes(args['modes'])
|
|
|
|
|
2017-10-15 10:42:07 +02:00
|
|
|
mode_prefix = ''
|
2016-09-20 02:40:50 +02:00
|
|
|
if 'channel' in args:
|
2017-10-15 10:42:07 +02:00
|
|
|
# Display the real (remote) channel name instead of the local one, if applicable.
|
2016-09-20 02:40:50 +02:00
|
|
|
args['local_channel'] = args['channel']
|
|
|
|
log.debug('(%s) relay_clientbot: coersing $channel from %s to %s', irc.name, args['local_channel'], args['channel'])
|
2016-08-04 21:50:38 +02:00
|
|
|
|
2017-10-15 10:42:07 +02:00
|
|
|
sourceirc = world.networkobjects.get(sourcenet)
|
|
|
|
log.debug('(%s) relay_clientbot: Checking prefix modes for %s on %s (relaying to %s)',
|
|
|
|
irc.name, realsource, sourcenet, args['channel'])
|
|
|
|
if sourceirc:
|
|
|
|
args['channel'] = remotechan = relay.get_remote_channel(irc, sourceirc, args['channel'])
|
|
|
|
if source in irc.users and remotechan in sourceirc.channels and \
|
|
|
|
realsource in sourceirc.channels[remotechan].users:
|
|
|
|
# Fetch the prefixmode prefixes (e.g. ~@%) for the sender, if available.
|
|
|
|
prefixmodes = sourceirc.channels[remotechan].get_prefix_modes(realsource)
|
|
|
|
log.debug('(%s) relay_clientbot: got prefix modes %s for %s on %s@%s',
|
|
|
|
irc.name, prefixmodes, realsource, remotechan, sourcenet)
|
|
|
|
if prefixmodes:
|
|
|
|
# Only pick the highest prefix.
|
|
|
|
mode_prefix = sourceirc.prefixmodes.get(
|
|
|
|
sourceirc.cmodes.get(prefixmodes[0]))
|
|
|
|
|
|
|
|
args.update({
|
|
|
|
'netname': netname, 'sender': sourcename, 'sender_identhost': identhost,
|
|
|
|
'colored_sender': color_text(sourcename), 'colored_netname': color_text(netname),
|
|
|
|
'mode_prefix': mode_prefix
|
|
|
|
})
|
|
|
|
|
2016-09-01 03:30:51 +02:00
|
|
|
for target in targets:
|
|
|
|
cargs = args.copy() # Copy args list to manipulate them in a channel specific way
|
2016-08-09 05:59:15 +02:00
|
|
|
|
|
|
|
# $nicks / $colored_nicks: used when the event affects multiple users, such as SJOIN or SQUIT.
|
|
|
|
# For SJOIN, this is simply a list of nicks. For SQUIT, this is sent as a dict
|
|
|
|
# mapping channels to lists of nicks, as netsplits aren't channel specific but
|
|
|
|
# still have to be relayed as such.
|
|
|
|
nicklist = args.get('nicks')
|
|
|
|
if nicklist:
|
2016-08-09 07:20:31 +02:00
|
|
|
# Get channel-specific nick list if relevent.
|
2017-06-30 03:08:41 +02:00
|
|
|
if isinstance(nicklist, dict):
|
2016-09-01 03:30:51 +02:00
|
|
|
nicklist = nicklist.get(target, [])
|
2016-08-09 05:59:15 +02:00
|
|
|
|
2016-08-09 06:05:55 +02:00
|
|
|
# Ignore if no nicks are affected on the channel.
|
|
|
|
if not nicklist:
|
|
|
|
continue
|
|
|
|
|
2016-08-09 05:59:15 +02:00
|
|
|
colored_nicks = [color_text(nick) for nick in nicklist]
|
|
|
|
|
|
|
|
# Join both the nicks and colored_nicks fields into a comma separated string.
|
|
|
|
cargs['nicks'] = ', '.join(nicklist)
|
|
|
|
cargs['colored_nicks'] = ', '.join(colored_nicks)
|
|
|
|
|
|
|
|
text = text_template.safe_substitute(cargs)
|
2016-09-01 03:30:51 +02:00
|
|
|
# PMs are always sent as notice - this prevents unknown command loops with bots.
|
|
|
|
irc.msg(target, text, loopback=False, notice=private)
|
2016-07-23 21:05:22 +02:00
|
|
|
|
|
|
|
utils.add_hook(cb_relay_core, 'CLIENTBOT_MESSAGE')
|
2016-07-23 21:25:52 +02:00
|
|
|
utils.add_hook(cb_relay_core, 'CLIENTBOT_KICK')
|
|
|
|
utils.add_hook(cb_relay_core, 'CLIENTBOT_PART')
|
|
|
|
utils.add_hook(cb_relay_core, 'CLIENTBOT_JOIN')
|
2016-07-24 03:48:27 +02:00
|
|
|
utils.add_hook(cb_relay_core, 'CLIENTBOT_QUIT')
|
|
|
|
utils.add_hook(cb_relay_core, 'CLIENTBOT_NICK')
|
2016-08-04 21:50:38 +02:00
|
|
|
utils.add_hook(cb_relay_core, 'CLIENTBOT_SJOIN')
|
|
|
|
utils.add_hook(cb_relay_core, 'CLIENTBOT_SQUIT')
|
2017-08-12 04:22:14 +02:00
|
|
|
utils.add_hook(cb_relay_core, 'RELAY_RAW_MODE')
|
2016-09-01 04:48:17 +02:00
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def rpm(irc, source, args):
|
|
|
|
"""<target> <text>
|
|
|
|
|
|
|
|
Sends PMs to users over the relay, if Clientbot PMs are enabled.
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
target = args[0]
|
|
|
|
text = ' '.join(args[1:])
|
|
|
|
except IndexError:
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('Not enough arguments. Needs 2: target nick and text.')
|
2016-09-01 04:48:17 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
relay = world.plugins.get('relay')
|
2017-06-30 08:01:39 +02:00
|
|
|
if irc.has_cap('can-spawn-clients'):
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('This command is only supported on Clientbot networks. Try /msg %s <text>' % target)
|
2016-09-01 04:48:17 +02:00
|
|
|
return
|
|
|
|
elif relay is None:
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('PyLink Relay is not loaded.')
|
2016-09-01 04:48:17 +02:00
|
|
|
return
|
|
|
|
elif not text:
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('No text given.')
|
2016-09-01 04:48:17 +02:00
|
|
|
return
|
2016-09-01 04:51:52 +02:00
|
|
|
elif not conf.conf.get('relay').get('allow_clientbot_pms'):
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('Private messages with users connected via Clientbot have been '
|
2016-09-01 04:51:52 +02:00
|
|
|
'administratively disabled.')
|
|
|
|
return
|
2016-09-01 04:48:17 +02:00
|
|
|
|
2017-06-30 08:01:39 +02:00
|
|
|
uid = irc.nick_to_uid(target)
|
2016-09-01 04:48:17 +02:00
|
|
|
if not uid:
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('Unknown user %s.' % target)
|
2016-09-01 04:48:17 +02:00
|
|
|
return
|
2017-12-18 22:23:53 +01:00
|
|
|
elif not relay.is_relay_client(irc, uid):
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('%s is not a relay user.' % target)
|
2016-09-01 04:48:17 +02:00
|
|
|
return
|
|
|
|
else:
|
2017-06-30 08:01:39 +02:00
|
|
|
assert not irc.is_internal_client(source), "rpm is not allowed from PyLink bots"
|
2016-09-01 04:48:17 +02:00
|
|
|
# Send the message through relay by faking a hook for its handler.
|
|
|
|
relay.handle_messages(irc, source, 'RELAY_CLIENTBOT_PRIVMSG', {'target': uid, 'text': text})
|
|
|
|
irc.reply('Message sent.')
|