2017-05-15 22:26:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-05-16 10:06:29 +00:00
|
|
|
import time
|
|
|
|
|
2017-08-22 13:57:57 +00:00
|
|
|
from docopt import Dict
|
2017-05-15 22:26:10 +00:00
|
|
|
from irc3.plugins.command import command
|
|
|
|
from irc3.utils import IrcString
|
|
|
|
|
|
|
|
from . import Plugin
|
|
|
|
|
|
|
|
|
|
|
|
class CTCP(Plugin):
|
2017-08-22 15:43:48 +00:00
|
|
|
requires = Plugin.requires + ['irc3.plugins.async']
|
2017-05-15 22:26:10 +00:00
|
|
|
|
2017-08-22 12:56:29 +00:00
|
|
|
TIMEOUT = 5
|
|
|
|
|
2017-05-15 22:26:10 +00:00
|
|
|
@command
|
2017-08-22 13:57:57 +00:00
|
|
|
async def ping(self, mask: IrcString, target: IrcString, args: Dict):
|
2017-05-16 12:27:34 +00:00
|
|
|
"""Sends ping via CTCP to user and sends the time needed
|
|
|
|
|
2017-05-15 22:26:10 +00:00
|
|
|
%%ping [<nick>]
|
|
|
|
"""
|
2017-08-21 14:59:27 +00:00
|
|
|
nick = args.get('<nick>', mask.nick)
|
2017-07-07 00:11:20 +00:00
|
|
|
data = await self.bot.ctcp_async(nick, 'PING {}'.format(time.time()))
|
2017-06-01 15:38:34 +00:00
|
|
|
|
|
|
|
if not data or data['timeout']:
|
2017-05-15 22:26:10 +00:00
|
|
|
reply = 'timeout'
|
2017-06-01 15:38:34 +00:00
|
|
|
elif not data['success']:
|
2017-07-07 00:11:20 +00:00
|
|
|
reply = 'Error: {}'.format(data['reply'])
|
2017-05-15 22:26:10 +00:00
|
|
|
else:
|
2017-06-01 15:38:34 +00:00
|
|
|
delta = time.time() - float(data['reply'])
|
2017-05-16 10:49:06 +00:00
|
|
|
if delta < 1.0:
|
|
|
|
delta *= 1000
|
|
|
|
unit = 'ms'
|
|
|
|
else:
|
|
|
|
unit = 's'
|
2017-07-07 00:11:20 +00:00
|
|
|
reply = '{0:.3f} {1}'.format(delta, unit)
|
2017-06-01 15:38:34 +00:00
|
|
|
|
2017-08-22 12:56:29 +00:00
|
|
|
return self._ctcp('ping', nick, reply)
|
2017-05-15 22:26:10 +00:00
|
|
|
|
|
|
|
@command
|
2017-08-22 13:57:57 +00:00
|
|
|
async def time(self, mask: IrcString, target: IrcString, args: Dict):
|
2017-05-16 12:27:34 +00:00
|
|
|
"""Gets the client time from nick via CTCP
|
|
|
|
|
2017-05-15 22:26:10 +00:00
|
|
|
%%time [<nick>]
|
|
|
|
"""
|
2017-08-22 12:56:29 +00:00
|
|
|
return await self.ctcp('time', mask, args)
|
2017-05-15 22:26:10 +00:00
|
|
|
|
|
|
|
@command
|
2017-08-22 13:57:57 +00:00
|
|
|
async def ver(self, mask: IrcString, target: IrcString, args: Dict):
|
2017-05-16 12:27:34 +00:00
|
|
|
"""Gets the client version from nick via CTCP
|
|
|
|
|
2017-05-15 22:26:10 +00:00
|
|
|
%%ver [<nick>]
|
|
|
|
"""
|
2017-08-22 12:56:29 +00:00
|
|
|
return await self.ctcp('version', mask, args)
|
2017-08-22 15:43:48 +00:00
|
|
|
|
|
|
|
async def ctcp(self, name: str, mask: IrcString, args: Dict):
|
|
|
|
nick = args.get('<nick>', mask.nick)
|
|
|
|
data = await self.bot.ctcp_async(nick, name.upper(), self.TIMEOUT)
|
|
|
|
|
|
|
|
if not data or data['timeout']:
|
|
|
|
reply = 'timeout'
|
|
|
|
elif not data['success']:
|
|
|
|
reply = 'Error: {}'.format(data['reply'])
|
|
|
|
else:
|
|
|
|
reply = data['reply']
|
|
|
|
|
|
|
|
return self._ctcp(name, nick, reply)
|
|
|
|
|
|
|
|
# noinspection PyMethodMayBeStatic
|
|
|
|
def _ctcp(self, name: str, nick: str, reply: str):
|
|
|
|
return '\x02[{}]\x02 {}: {}'.format(name.upper(), nick, reply)
|