2016-06-15 20:31:40 +02:00
|
|
|
# ctcp.py: Handles basic CTCP requests.
|
2016-06-25 21:11:04 +02:00
|
|
|
import datetime
|
2019-07-15 00:12:29 +02:00
|
|
|
import random
|
2016-06-25 21:11:04 +02:00
|
|
|
|
2016-06-21 03:18:54 +02:00
|
|
|
from pylinkirc import utils
|
|
|
|
from pylinkirc.log import log
|
2016-06-15 20:31:40 +02:00
|
|
|
|
2019-07-15 00:12:29 +02:00
|
|
|
|
2018-02-19 08:10:08 +01:00
|
|
|
def handle_ctcp(irc, source, command, args):
|
2016-06-15 20:31:40 +02:00
|
|
|
"""
|
2018-02-19 08:10:08 +01:00
|
|
|
CTCP event handler.
|
2016-06-15 20:31:40 +02:00
|
|
|
"""
|
2018-02-19 08:10:08 +01:00
|
|
|
text = args['text']
|
|
|
|
if not (text.startswith('\x01') and text.endswith('\x01')):
|
|
|
|
return None # Pass through to other plugins
|
|
|
|
|
|
|
|
target = args['target']
|
|
|
|
if not irc.get_service_bot(target):
|
|
|
|
# Ignore this message if the target isn't a service bot
|
|
|
|
return None
|
|
|
|
|
|
|
|
text = text.strip('\x01')
|
|
|
|
try:
|
|
|
|
ctcp_command, data = text.split(" ", 1)
|
|
|
|
except ValueError:
|
|
|
|
ctcp_command = text
|
|
|
|
data = ''
|
|
|
|
|
|
|
|
ctcp_command = ctcp_command.upper()
|
|
|
|
log.debug('(%s) ctcp: got CTCP command %r, data %r',
|
|
|
|
irc.name, ctcp_command, data)
|
2016-06-15 20:31:40 +02:00
|
|
|
|
2018-02-19 08:10:08 +01:00
|
|
|
if ctcp_command in SUPPORTED_COMMANDS:
|
2018-02-19 08:13:44 +01:00
|
|
|
log.info('(%s) Received CTCP %s from %s to %s',
|
|
|
|
irc.name, ctcp_command, irc.get_hostmask(source),
|
|
|
|
irc.get_friendly_name(target))
|
2016-06-25 21:11:04 +02:00
|
|
|
|
2018-02-19 08:10:08 +01:00
|
|
|
# Call the helper function and display its result.
|
|
|
|
result = SUPPORTED_COMMANDS[ctcp_command](irc, source, ctcp_command, data)
|
2018-02-24 20:18:32 +01:00
|
|
|
if result and source in irc.users:
|
|
|
|
# Note, do NOT use irc.reply() in hook handlers because nothing except the
|
|
|
|
# command handler system actually updates the last caller.
|
|
|
|
irc.msg(source, '\x01%s %s\x01' % (ctcp_command, result),
|
2018-02-25 20:29:19 +01:00
|
|
|
notice=True, source=target)
|
2018-02-19 08:10:08 +01:00
|
|
|
|
|
|
|
return False # Block this message from reaching the general command handler
|
|
|
|
else:
|
2018-03-01 08:48:04 +01:00
|
|
|
log.info('(%s) Received unknown CTCP %s from %s to %s',
|
2018-02-19 08:13:44 +01:00
|
|
|
irc.name, ctcp_command, irc.get_hostmask(source),
|
|
|
|
irc.get_friendly_name(target))
|
2018-02-19 08:10:08 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
utils.add_hook(handle_ctcp, 'PRIVMSG', priority=200)
|
|
|
|
|
|
|
|
def handle_ctcpversion(irc, source, ctcp, data):
|
2016-06-25 21:11:04 +02:00
|
|
|
"""
|
2018-02-19 08:10:08 +01:00
|
|
|
Handles CTCP version requests.
|
2016-06-25 21:11:04 +02:00
|
|
|
"""
|
2018-02-19 08:10:08 +01:00
|
|
|
return irc.version()
|
2016-06-25 21:11:04 +02:00
|
|
|
|
2018-02-19 08:10:08 +01:00
|
|
|
def handle_ctcpeaster(irc, source, ctcp, data):
|
2016-06-25 21:11:04 +02:00
|
|
|
"""
|
|
|
|
Secret easter egg.
|
|
|
|
"""
|
|
|
|
|
2017-01-21 07:32:32 +01:00
|
|
|
responses = ["Legends say the cord monster was born only %s years ago..." % \
|
2016-06-25 21:11:04 +02:00
|
|
|
(datetime.datetime.now().year - 2014),
|
|
|
|
"Hiss%s" % ('...' * random.randint(1, 5)),
|
|
|
|
"His%s%s" % ('s' * random.randint(1, 4), '...' * random.randint(1, 5)),
|
|
|
|
"It's Easter already? Where are the eggs?",
|
|
|
|
"Maybe later.",
|
2017-01-21 07:31:46 +01:00
|
|
|
"Janus? Never heard of it.",
|
|
|
|
irc.version(),
|
2016-06-25 21:11:04 +02:00
|
|
|
"Let me out of here, I'll give you cookies!",
|
2017-01-21 07:31:46 +01:00
|
|
|
"About as likely as pigs flying.",
|
|
|
|
"Request timed out.",
|
|
|
|
"No actual pie here, sorry.",
|
|
|
|
"Hey, no loitering!",
|
|
|
|
"Hey, can you keep a secret? \x031,1 %s" % " " * random.randint(1,20),
|
2016-06-25 21:11:04 +02:00
|
|
|
]
|
|
|
|
|
2018-02-19 08:10:08 +01:00
|
|
|
return random.choice(responses)
|
2016-06-25 21:11:04 +02:00
|
|
|
|
2018-02-19 08:10:08 +01:00
|
|
|
# Map CTCP commands to functions generating an appropriate text response.
|
|
|
|
SUPPORTED_COMMANDS = {'VERSION': handle_ctcpversion,
|
|
|
|
'PING': lambda irc, source, ctcp, data: data,
|
|
|
|
'ABOUT': handle_ctcpeaster,
|
|
|
|
'EASTER': handle_ctcpeaster}
|