89 lines
2.5 KiB
Python
89 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
import time
|
|
|
|
import irc3
|
|
from docopt import Dict as DocOptDict
|
|
from irc3.plugins.command import command
|
|
from irc3.utils import IrcString
|
|
|
|
from . import Plugin
|
|
|
|
|
|
@irc3.plugin
|
|
class CTCP(Plugin):
|
|
requires = ['irc3.plugins.async',
|
|
'irc3.plugins.command']
|
|
|
|
@staticmethod
|
|
def _ctcp(name: str, nick: str, reply: str):
|
|
return '\x02[{name}]\x02 {nick}: {reply}'.format(
|
|
name=name.upper(),
|
|
nick=nick,
|
|
reply=reply,
|
|
)
|
|
|
|
async def ctcp(self, name: str, mask: IrcString, args: DocOptDict):
|
|
nick = args.get('<nick>') or mask.nick
|
|
name = name.upper()
|
|
data = await self.bot.ctcp_async(nick, name)
|
|
|
|
if not data or data['timeout']:
|
|
reply = 'timeout'
|
|
elif not data['success']:
|
|
reply = 'Error: {reply}'.format(reply=data['reply'])
|
|
else:
|
|
reply = data['reply']
|
|
|
|
return self._ctcp(name, nick, reply)
|
|
|
|
@command
|
|
async def ping(self, mask: IrcString, target: IrcString,
|
|
args: DocOptDict):
|
|
"""Sends ping via CTCP to user and sends the time needed
|
|
|
|
%%ping [<nick>]
|
|
"""
|
|
nick = args.get('<nick>') or mask.nick
|
|
data = await self.bot.ctcp_async(nick, 'PING {0}'.format(time.time()))
|
|
|
|
if not data or data['timeout']:
|
|
reply = 'timeout'
|
|
elif not data['success']:
|
|
reply = 'Error: {reply}'.format(reply=data['reply'])
|
|
else:
|
|
delta = time.time() - float(data['reply'])
|
|
if delta < 1.0:
|
|
delta *= 1000
|
|
unit = 'ms'
|
|
else:
|
|
unit = 's'
|
|
reply = '{delta:.3f} {unit}'.format(unit=unit, delta=delta)
|
|
|
|
return self._ctcp('PING', nick, reply)
|
|
|
|
@command
|
|
async def finger(self, mask: IrcString, target: IrcString,
|
|
args: DocOptDict):
|
|
"""Gets the client response for finger nick user via CTCP
|
|
|
|
%%finger [<nick>]
|
|
"""
|
|
return await self.ctcp('FINGER', mask, args)
|
|
|
|
@command
|
|
async def time(self, mask: IrcString, target: IrcString,
|
|
args: DocOptDict):
|
|
"""Gets the client time from nick via CTCP
|
|
|
|
%%time [<nick>]
|
|
"""
|
|
return await self.ctcp('TIME', mask, args)
|
|
|
|
@command
|
|
async def ver(self, mask: IrcString, target: IrcString, args: DocOptDict):
|
|
"""Gets the client version from nick via CTCP
|
|
|
|
%%ver [<nick>]
|
|
"""
|
|
return await self.ctcp('VERSION', mask, args)
|