87 lines
2.6 KiB
Python
87 lines
2.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
import irc3
|
|
import time
|
|
|
|
from docopt import Dict as DocOptDict
|
|
from irc3.plugins.command import command
|
|
from irc3.utils import IrcString
|
|
|
|
from . import Plugin
|
|
from ..utils import fmt
|
|
|
|
|
|
# noinspection PyUnusedLocal
|
|
@irc3.plugin
|
|
class CTCP(Plugin):
|
|
TIMEOUT = 10
|
|
|
|
requires = [
|
|
'irc3.plugins.async',
|
|
'irc3.plugins.command',
|
|
]
|
|
|
|
async def ctcp(self, ctcp: str, mask: IrcString, args: DocOptDict):
|
|
nick = args.get('<nick>') or mask.nick
|
|
name = ctcp.upper()
|
|
ctcp = await self.bot.ctcp_async(nick, name, timeout=self.TIMEOUT)
|
|
if not ctcp or ctcp['timeout']:
|
|
reply = 'timeout'
|
|
elif not ctcp['success']:
|
|
reply = 'Error: {reply}'.format(reply=ctcp['reply'])
|
|
else:
|
|
reply = ctcp['reply']
|
|
return fmt('{bold}[{ctcp}]{reset} {nick}: {reply}',
|
|
ctcp=name,
|
|
nick=nick,
|
|
reply=reply)
|
|
|
|
@command
|
|
async def ping(self, mask: IrcString, channel: IrcString,
|
|
args: DocOptDict):
|
|
"""CTCP ping command.
|
|
%%ping [<nick>]
|
|
"""
|
|
nick = args.get('<nick>') or mask.nick
|
|
ctcp = await self.bot.ctcp_async(nick, 'PING {0}'.format(time.time()),
|
|
timeout=self.TIMEOUT)
|
|
if not ctcp or ctcp['timeout']:
|
|
reply = 'timeout'
|
|
elif not ctcp['success']:
|
|
reply = 'Error: {reply}'.format(reply=ctcp['reply'])
|
|
else:
|
|
delta = time.time() - float(ctcp['reply'])
|
|
if delta < 1.0:
|
|
delta *= 1000
|
|
unit = 'ms'
|
|
else:
|
|
unit = 's'
|
|
reply = '{delta:.3f} {unit}'.format(
|
|
unit=unit, # 'ms' if delta < 0 else 's',
|
|
delta=delta)
|
|
return fmt('{bold}[PING]{reset} {nick}: {text}',
|
|
nick=nick,
|
|
text=reply)
|
|
|
|
@command
|
|
async def finger(self, mask: IrcString, channel: IrcString,
|
|
args: DocOptDict):
|
|
"""CTCP finger command.
|
|
%%finger [<nick>]
|
|
"""
|
|
return await self.ctcp('FINGER', mask, args)
|
|
|
|
@command
|
|
async def time(self, mask: IrcString, channel: IrcString,
|
|
args: DocOptDict):
|
|
"""CTCP time command.
|
|
%%time [<nick>]
|
|
"""
|
|
return await self.ctcp('TIME', mask, args)
|
|
|
|
@command
|
|
async def ver(self, mask: IrcString, channel: IrcString, args: DocOptDict):
|
|
"""CTCP version command.
|
|
%%ver [<nick>]
|
|
"""
|
|
return await self.ctcp('VERSION', mask, args)
|