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-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
|
|
|
|
|
2016-07-23 21:05:22 +02:00
|
|
|
default_styles = {'MESSAGE': '\x02[$colored_netname]\x02 <$colored_nick> $text',
|
2016-07-23 21:25:52 +02:00
|
|
|
'KICK': '\x02[$colored_netname]\x02 -$colored_nick$identhost has kicked $target_nick from $channel ($text)',
|
|
|
|
'PART': '\x02[$colored_netname]\x02 -$colored_nick$identhost has left $channel ($text)',
|
|
|
|
'JOIN': '\x02[$colored_netname]\x02 -$colored_nick$identhost has joined $channel',
|
2016-07-24 03:48:27 +02:00
|
|
|
'NICK': '\x02[$colored_netname]\x02 -$colored_nick$identhost is now known as $newnick',
|
|
|
|
'QUIT': '\x02[$colored_netname]\x02 -$colored_nick$identhost has quit ($text)',
|
|
|
|
'ACTION': '\x02[$colored_netname]\x02 * $colored_nick $text',
|
|
|
|
'NOTICE': '\x02[$colored_netname]\x02 * Notice from $colored_nick: $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):
|
|
|
|
"""
|
|
|
|
Returns a colorized version of the given text based on a simple hash algorithm
|
|
|
|
(sum of all characters).
|
|
|
|
"""
|
|
|
|
colors = ('02', '03', '04', '05', '06', '07', '08', '09', '10', '11',
|
|
|
|
'12', '13')
|
|
|
|
num = sum([ord(char) for char in s])
|
|
|
|
num = num % len(colors)
|
|
|
|
return "\x03%s%s\x03" % (colors[num], s)
|
|
|
|
|
|
|
|
def cb_relay_core(irc, source, command, args):
|
|
|
|
"""
|
|
|
|
This function takes Clientbot actions and outputs them to a channel as regular text.
|
|
|
|
"""
|
|
|
|
real_command = command.split('_')[-1]
|
|
|
|
|
|
|
|
relay = world.plugins.get('relay')
|
|
|
|
if irc.pseudoclient and relay:
|
2016-07-24 03:48:27 +02:00
|
|
|
try:
|
|
|
|
sourcename = irc.getFriendlyName(source)
|
|
|
|
except KeyError: # User has left due to /quit
|
|
|
|
sourcename = args['userdata'].nick
|
|
|
|
|
|
|
|
# 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'
|
|
|
|
|
|
|
|
# Other CTCPs are ignored
|
|
|
|
elif args['text'].startswith('\x01'):
|
|
|
|
return
|
|
|
|
elif args.get('is_notice'): # Different syntax for notices
|
|
|
|
real_command = 'NOTICE'
|
2016-07-23 21:05:22 +02:00
|
|
|
|
|
|
|
# .get() chains are lovely. Try to fetch the format for the given command from the
|
|
|
|
# relay:clientbot_format:$command key, falling back to one defined in default_styles
|
|
|
|
# above, and then nothing if not found.
|
|
|
|
text_template = conf.conf.get('relay', {}).get('clientbot_format', {}).get(real_command,
|
|
|
|
default_styles.get(real_command, ''))
|
|
|
|
text_template = string.Template(text_template)
|
|
|
|
|
|
|
|
if text_template:
|
2016-07-24 03:48:27 +02:00
|
|
|
# Get the original client that the relay client source was meant for.
|
|
|
|
try:
|
|
|
|
origuser = relay.getOrigUser(irc, source) or args['userdata'].remote
|
|
|
|
except (AttributeError, KeyError):
|
2016-07-24 20:54:14 +02:00
|
|
|
try:
|
|
|
|
origuser = (irc.servers[source].remote, '')
|
|
|
|
except (AttributeError, KeyError):
|
|
|
|
return
|
2016-07-23 21:05:22 +02:00
|
|
|
netname = origuser[0]
|
2016-07-24 07:30:25 +02:00
|
|
|
try: # Try to get the full network name
|
|
|
|
netname = conf.conf['servers'][netname]['netname']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2016-07-23 21:05:22 +02:00
|
|
|
|
|
|
|
# Figure out where the message is destined to.
|
|
|
|
target = args.get('channel') or args.get('target')
|
|
|
|
if target is None or not utils.isChannel(target):
|
2016-07-24 03:48:27 +02:00
|
|
|
# Quit and nick messages are not channel specific. Figure out all channels that the
|
|
|
|
# sender shares over the relay, and relay them that way.
|
|
|
|
userdata = args.get('userdata') or irc.users.get(source)
|
|
|
|
assert userdata, "Got a channel-less message from bad UID %s" % source
|
|
|
|
channels = [channel for channel in userdata.channels if relay.getRelay((irc.name, channel))]
|
|
|
|
else:
|
|
|
|
# Pluralize the channel so that we can iterate over it.
|
|
|
|
channels = [target]
|
2016-07-23 21:05:22 +02:00
|
|
|
|
|
|
|
if source in irc.users:
|
2016-07-23 21:25:52 +02:00
|
|
|
try:
|
|
|
|
identhost = irc.getHostmask(source).split('!')[-1]
|
|
|
|
except KeyError: # User got removed due to quit
|
|
|
|
identhost = '%s@%s' % (args['olduser'].ident, args['olduser'].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
|
|
|
|
else:
|
|
|
|
identhost = ''
|
|
|
|
|
2016-07-24 03:48:27 +02:00
|
|
|
# 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:
|
|
|
|
target_nick = irc.getFriendlyName(args['target'])
|
|
|
|
else:
|
|
|
|
target_nick = ''
|
2016-07-24 03:48:27 +02:00
|
|
|
|
2016-07-23 21:05:22 +02:00
|
|
|
args.update({'netname': netname, 'nick': sourcename, 'identhost': identhost,
|
2016-07-23 21:25:52 +02:00
|
|
|
'colored_nick': color_text(sourcename), 'colored_netname': color_text(netname),
|
|
|
|
'target_nick': target_nick})
|
2016-07-23 21:05:22 +02:00
|
|
|
|
|
|
|
text = text_template.substitute(args)
|
|
|
|
|
2016-07-24 03:48:27 +02:00
|
|
|
for channel in channels:
|
|
|
|
irc.proto.message(irc.pseudoclient.uid, channel, text)
|
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')
|
|
|
|
|